Class: MadMath::Stats
- Inherits:
-
Object
- Object
- MadMath::Stats
- Defined in:
- lib/mad_math.rb
Instance Method Summary collapse
- #average ⇒ Object
- #count ⇒ Object
-
#initialize(data) ⇒ Stats
constructor
A new instance of Stats.
- #load ⇒ Object
- #max ⇒ Object
- #min ⇒ Object
- #stdev ⇒ Object
- #stdevp ⇒ Object
-
#sum ⇒ Object
#reduce with the symbol (non-block form) seems most performant.
- #sum_of_squares ⇒ Object
Constructor Details
#initialize(data) ⇒ Stats
Returns a new instance of Stats.
3 4 5 |
# File 'lib/mad_math.rb', line 3 def initialize(data) @data = data end |
Instance Method Details
#average ⇒ Object
12 13 14 |
# File 'lib/mad_math.rb', line 12 def average @average ||= sum / count.to_f end |
#count ⇒ Object
21 22 23 |
# File 'lib/mad_math.rb', line 21 def count @count ||= @data.size end |
#load ⇒ Object
7 8 9 10 |
# File 'lib/mad_math.rb', line 7 def load stdev self end |
#max ⇒ Object
37 38 39 |
# File 'lib/mad_math.rb', line 37 def max @max ||= @data.max end |
#min ⇒ Object
41 42 43 |
# File 'lib/mad_math.rb', line 41 def min @min ||= @data.min end |
#stdev ⇒ Object
29 30 31 |
# File 'lib/mad_math.rb', line 29 def stdev @stdev ||= Math.sqrt(sum_of_squares / (count - 1)) end |
#stdevp ⇒ Object
33 34 35 |
# File 'lib/mad_math.rb', line 33 def stdevp @stdevp ||= Math.sqrt(sum_of_squares / count) end |
#sum ⇒ Object
#reduce with the symbol (non-block form) seems most performant
17 18 19 |
# File 'lib/mad_math.rb', line 17 def sum @sum ||= @data.reduce(:+) end |
#sum_of_squares ⇒ Object
25 26 27 |
# File 'lib/mad_math.rb', line 25 def sum_of_squares @sum_of_squares ||= get_sum_of_squares end |