module Enumerable
statistics expects floating point arrays
Public Class Methods
distance(x,y)
click to toggle source
# File lib/floatstats.rb, line 59 def self.distance(x,y) x.zip(y).map{|i,j| i-j} end
pearson_correlation(x, y)
click to toggle source
# 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)
click to toggle source
operations with 2 enumerable vectors
# File lib/floatstats.rb, line 56 def self.product(x,y) x.zip(y).map{|i,j| i*j} end
ratio(x,y)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/floatstats.rb, line 78 def self.spearman_rank2(x, y) self.pearson_correlation(x.rank, y.rank) end
Public Instance Methods
average()
click to toggle source
# File lib/floatstats.rb, line 18 def average self.sum / self.length.to_f end
diffavg()
click to toggle source
# File lib/floatstats.rb, line 21 def diffavg avg = self.average self.map{|v| v - avg} end
mad()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/floatstats.rb, line 25 def sample_variance self.diffavg.sum_of_squares / self.length.to_f end
standard_deviation()
click to toggle source
# File lib/floatstats.rb, line 28 def standard_deviation Math.sqrt(self.sample_variance) end
sum()
click to toggle source
# File lib/floatstats.rb, line 12 def sum self.inject(0){|acc,i|acc + i} end
sum_of_squares()
click to toggle source
# File lib/floatstats.rb, line 15 def sum_of_squares self.inject(0){|acc,i| acc + i**2} end