Class: Array

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

Instance Method Summary collapse

Instance Method Details

#deviationsObject



44
45
46
47
48
49
50
# File 'lib/rstats.rb', line 44

def deviations
  tmp = []
  each do |val|
    tmp << val - mean
  end
  tmp
end

#meanObject

end



19
20
21
# File 'lib/rstats.rb', line 19

def mean
  rsum.to_f/size
end

#medianObject



23
24
25
26
27
28
# File 'lib/rstats.rb', line 23

def median
  case size % 2
    when 0 then sort[size/2-1, 2].mean
    when 1 then sort[size/2].to_f
  end if size > 0
end

#modeObject



30
31
32
33
34
# File 'lib/rstats.rb', line 30

def mode
  tmp = sort.inject({}){|a,x| a[x]=a[x].to_i+1; a}
  max = tmp.values.max
  tmp.keys.select{|x|tmp[x]==max}
end

#rsumObject

if ! self.methods.include?(“sum”)



14
15
16
# File 'lib/rstats.rb', line 14

def rsum
  inject(0){ |a, x| a + x  }
end

#skewnessObject



60
61
62
63
64
65
66
# File 'lib/rstats.rb', line 60

def skewness
  cubed_deviations = []
  each do |val| 
    cubed_deviations << ((val-mean)/standard_deviation)**3
  end
  (size.to_f/(size.to_f-1)/(size.to_f-2)*cubed_deviations.rsum.to_f)
end

#squares_of_deviationsObject



52
53
54
55
56
57
58
# File 'lib/rstats.rb', line 52

def squares_of_deviations
  tmp = []
  deviations.each do |val|
    tmp << val**2
  end
  tmp
end

#standard_deviationObject



40
41
42
# File 'lib/rstats.rb', line 40

def standard_deviation
  Math::sqrt(variance)
end

#varianceObject



36
37
38
# File 'lib/rstats.rb', line 36

def variance
  squares_of_deviations.rsum.to_f/(size - 1)
end