Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/rstats.rb
Instance Method Summary collapse
- #deviations ⇒ Object
-
#mean ⇒ Object
end.
- #median ⇒ Object
- #mode ⇒ Object
-
#rsum ⇒ Object
if ! self.methods.include?(“sum”).
- #skewness ⇒ Object
- #squares_of_deviations ⇒ Object
- #standard_deviation ⇒ Object
- #variance ⇒ Object
Instance Method Details
#deviations ⇒ Object
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 |
#mean ⇒ Object
end
19 20 21 |
# File 'lib/rstats.rb', line 19 def mean rsum.to_f/size end |
#median ⇒ Object
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 |
#mode ⇒ Object
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 |
#rsum ⇒ Object
if ! self.methods.include?(“sum”)
14 15 16 |
# File 'lib/rstats.rb', line 14 def rsum inject(0){ |a, x| a + x } end |
#skewness ⇒ Object
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_deviations ⇒ Object
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_deviation ⇒ Object
40 41 42 |
# File 'lib/rstats.rb', line 40 def standard_deviation Math::sqrt(variance) end |
#variance ⇒ Object
36 37 38 |
# File 'lib/rstats.rb', line 36 def variance squares_of_deviations.rsum.to_f/(size - 1) end |