Closed bdunne closed 8 years ago
Copying comment from here
I'm leaning towards raising an exception vs returning 0 or 0.0 for empty or single item sets. I don't know how this data is being used, but it feels like 0/0.0 is incorrect. Even python raises an exception on stdev of empty or single item sets. Our code consumers of the stdev value in this case should handle the lack of a stddev
. Maybe?
>>> import statistics
>>> statistics.stdev([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/statistics.py", line 575, in stdev
var = variance(data, xbar)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/statistics.py", line 513, in variance
raise StatisticsError('variance requires at least two data points')
statistics.StatisticsError: variance requires at least two data points
>>> statistics.stdev([1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/statistics.py", line 575, in stdev
var = variance(data, xbar)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/statistics.py", line 513, in variance
raise StatisticsError('variance requires at least two data points')
statistics.StatisticsError: variance requires at least two data points
I agree with @jrafanie . Also note that / 0.0
returning NaN is an IEEE standard for Floats...see IEEE-754 and http://stackoverflow.com/questions/14682005/why-does-division-by-zero-in-ieee754-standard-results-in-infinite-value
[].mean
should be 0.0[1].variance
should be 0.0