Module: Enumerable

Defined in:
lib/floatstats.rb

Overview

expects floating point arrays

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.distance(x, y) ⇒ Object



35
36
37
# File 'lib/floatstats.rb', line 35

def self.distance(x,y)
  x.zip(y).map{|i,j| i-j}
end

.pearson_correlation(x, y) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/floatstats.rb', line 41

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



32
33
34
# File 'lib/floatstats.rb', line 32

def self.product(x,y)
  x.zip(y).map{|i,j| i*j}
end

.ratio(x, y) ⇒ Object



38
39
40
# File 'lib/floatstats.rb', line 38

def self.ratio(x,y)
  x.zip(y).map{|i,j| i.to_f / j.to_f}
end

.spearman_rank(x, y) ⇒ Object



48
49
50
51
52
53
# File 'lib/floatstats.rb', line 48

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



54
55
56
# File 'lib/floatstats.rb', line 54

def self.spearman_rank2(x, y)
  self.pearson_correlation(x.rank, y.rank)
end

Instance Method Details

#averageObject



18
19
20
# File 'lib/floatstats.rb', line 18

def average
  self.sum / self.length.to_f
end

#diffavgObject



21
22
23
24
# File 'lib/floatstats.rb', line 21

def diffavg
  avg = self.average
  self.map{|v| v - avg}
end

#sample_varianceObject



25
26
27
# File 'lib/floatstats.rb', line 25

def sample_variance
  self.diffavg.sum_of_squares / self.length.to_f
end

#standard_deviationObject



28
29
30
# File 'lib/floatstats.rb', line 28

def standard_deviation
  Math.sqrt(self.sample_variance)
end

#sumObject



12
13
14
# File 'lib/floatstats.rb', line 12

def sum
  self.inject(0){|acc,i|acc + i}
end

#sum_of_squaresObject



15
16
17
# File 'lib/floatstats.rb', line 15

def sum_of_squares
  self.inject(0){|acc,i| acc + i**2}
end