Module: Enumerable
- Defined in:
- lib/floatstats.rb
Overview
expects floating point arrays
Class Method Summary collapse
- .distance(x, y) ⇒ Object
- .pearson_correlation(x, y) ⇒ Object
-
.product(x, y) ⇒ Object
operations with 2 enumerable vectors.
- .ratio(x, y) ⇒ Object
- .spearman_rank(x, y) ⇒ Object
- .spearman_rank2(x, y) ⇒ Object
Instance Method Summary collapse
- #average ⇒ Object
- #diffavg ⇒ Object
-
#mad ⇒ Object
median absolute deviation.
- #median ⇒ Object
- #rank ⇒ Object
- #sample_variance ⇒ Object
- #standard_deviation ⇒ Object
- #sum ⇒ Object
- #sum_of_squares ⇒ Object
Class Method Details
.distance(x, y) ⇒ Object
59 60 61 |
# File 'lib/floatstats.rb', line 59 def self.distance(x,y) x.zip(y).map{|i,j| i-j} end |
.pearson_correlation(x, y) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/floatstats.rb', line 65 def self.pearson_correlation(x, y) xda = x.diffavg yda = y.diffavg num = self.product(xda, yda).sum den = Math.sqrt(xda.sum_of_squares * yda.sum_of_squares) num / den end |
.product(x, y) ⇒ Object
operations with 2 enumerable vectors
56 57 58 |
# File 'lib/floatstats.rb', line 56 def self.product(x,y) x.zip(y).map{|i,j| i*j} end |
.ratio(x, y) ⇒ Object
62 63 64 |
# File 'lib/floatstats.rb', line 62 def self.ratio(x,y) x.zip(y).map{|i,j| i.to_f / j.to_f} end |
.spearman_rank(x, y) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/floatstats.rb', line 72 def self.spearman_rank(x, y) n = x.size num = 6 * self.distance(x.rank, y.rank).sum_of_squares den = n * (n**2 - 1) 1.0 - num.to_f/den.to_f end |
.spearman_rank2(x, y) ⇒ Object
78 79 80 |
# File 'lib/floatstats.rb', line 78 def self.spearman_rank2(x, y) self.pearson_correlation(x.rank, y.rank) end |
Instance Method Details
#average ⇒ Object
18 19 20 |
# File 'lib/floatstats.rb', line 18 def average self.sum / self.length.to_f end |
#diffavg ⇒ Object
21 22 23 24 |
# File 'lib/floatstats.rb', line 21 def diffavg avg = self.average self.map{|v| v - avg} end |
#mad ⇒ Object
median absolute deviation
40 41 42 43 44 |
# File 'lib/floatstats.rb', line 40 def mad # median absolute deviation med = self.median deviation_set = (self.map{|n| (n-med).abs }).sort.delete_if{|x| x == 0.0 } 1.4826 * deviation_set.median # scale for consistency with std dev end |
#median ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/floatstats.rb', line 31 def median n = (self.length - 1) / 2 n2 = (self.length) / 2 if self.length % 2 == 0 # even case (self[n] + self[n2]) / 2 else self[n] end end |
#rank ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/floatstats.rb', line 45 def rank ranked = [] order = (0...self.size).to_a self.zip(order).sort{|a,b| b[0]<=>a[0]}.each_with_index do |elem, i| ranked[elem[1]] = i + 1 end ranked end |
#sample_variance ⇒ Object
25 26 27 |
# File 'lib/floatstats.rb', line 25 def sample_variance self.diffavg.sum_of_squares / self.length.to_f end |
#standard_deviation ⇒ Object
28 29 30 |
# File 'lib/floatstats.rb', line 28 def standard_deviation Math.sqrt(self.sample_variance) end |
#sum ⇒ Object
12 13 14 |
# File 'lib/floatstats.rb', line 12 def sum self.inject(0){|acc,i|acc + i} end |
#sum_of_squares ⇒ Object
15 16 17 |
# File 'lib/floatstats.rb', line 15 def sum_of_squares self.inject(0){|acc,i| acc + i**2} end |