Class: MadMath::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/mad_math.rb

Instance Method Summary collapse

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

#averageObject



12
13
14
# File 'lib/mad_math.rb', line 12

def average
  @average ||= sum / count.to_f
end

#countObject



21
22
23
# File 'lib/mad_math.rb', line 21

def count
  @count ||= @data.size
end

#loadObject



7
8
9
10
# File 'lib/mad_math.rb', line 7

def load
  stdev
  self
end

#maxObject



37
38
39
# File 'lib/mad_math.rb', line 37

def max
  @max ||= @data.max
end

#minObject



41
42
43
# File 'lib/mad_math.rb', line 41

def min
  @min ||= @data.min
end

#stdevObject



29
30
31
# File 'lib/mad_math.rb', line 29

def stdev
  @stdev ||= Math.sqrt(sum_of_squares / (count - 1))
end

#stdevpObject



33
34
35
# File 'lib/mad_math.rb', line 33

def stdevp
  @stdevp ||= Math.sqrt(sum_of_squares / count)
end

#sumObject

#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_squaresObject



25
26
27
# File 'lib/mad_math.rb', line 25

def sum_of_squares
  @sum_of_squares ||= get_sum_of_squares
end