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


63
64
65
# File 'lib/statsample.rb', line 63

def mean
  sum.fdiv(size)
end

#sdObject

Calculate sample standard deviation


80
81
82
83
# File 'lib/statsample.rb', line 80

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

#sumObject


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

def sum
  inject(:+)
end

#sum_of_squares(m = nil) ⇒ Object

Calcualte sum of squares


68
69
70
71
# File 'lib/statsample.rb', line 68

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


74
75
76
77
# File 'lib/statsample.rb', line 74

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