Class: Array

Inherits:
Object show all
Includes:
Statsample::VectorShorthands
Defined in:
lib/statsample.rb,
lib/statsample/vector.rb

Instance Method Summary collapse

Methods included from Statsample::VectorShorthands

#to_vector

Instance Method Details

#meanObject


61
62
63
# File 'lib/statsample.rb', line 61

def mean
  sum.fdiv(size)
end

#sdObject

Calculate sample standard deviation


78
79
80
81
# File 'lib/statsample.rb', line 78

def sd
  m ||= mean
  Math::sqrt(variance_sample(m))
end

#sumObject


57
58
59
# File 'lib/statsample.rb', line 57

def sum
  inject(:+)
end

#sum_of_squares(m = nil) ⇒ Object

Calcualte sum of squares


66
67
68
69
# File 'lib/statsample.rb', line 66

def sum_of_squares(m=nil)
  m ||= mean
  inject(0) {|a,x| a + (x-m).square }
end

#variance_sample(m = nil) ⇒ Object

Calculate sample variance


72
73
74
75
# File 'lib/statsample.rb', line 72

def variance_sample(m=nil)
  m ||= mean
  sum_of_squares(m).quo(size - 1)
end