ManageIQ / more_core_extensions

MoreCoreExtensions are a set of core extensions beyond those provided by ActiveSupport.
MIT License
5 stars 23 forks source link

Array math fixes for small arrays #28

Closed bdunne closed 8 years ago

bdunne commented 8 years ago

[].mean should be 0.0 [1].variance should be 0.0

miq-bot commented 8 years ago

Checked commits https://github.com/bdunne/more_core_extensions/compare/c3b0e02eabbbd44710839c2508555d13dd119393~...21724928a67b198246e4b59d258b9c4a2b0e2459 with ruby 2.2.5, rubocop 0.37.2, and haml-lint 0.16.1 2 files checked, 0 offenses detected Everything looks good. :+1:

bdunne commented 8 years ago

See conversation here

jrafanie commented 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
Fryguy commented 8 years ago

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