Class: Array

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

Instance Method Summary collapse

Instance Method Details

#cov(other) ⇒ Object



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

def cov(other)
  zip(other).map {|a,b| a*b }.mean - (mean * other.mean)
end

#meanObject



4
5
6
# File 'lib/excite/array_helpers.rb', line 4

def mean
  (size > 0) ? sum.to_f / size : 0
end

#pearson_r(other) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/excite/array_helpers.rb', line 18

def pearson_r(other)
  unless size == other.size
    raise "Vectors must be of same length to calculate pearson_r"
  end
  devp = stddev * other.stddev
  (devp > 0) ? cov(other) / devp : 0.0
end

#stddevObject



8
9
10
11
12
# File 'lib/excite/array_helpers.rb', line 8

def stddev
  m = mean
  devsum = inject( 0 ) { |ds,x| ds += (x - m)**2 }
  (size > 0) ? (devsum.to_f / size) ** 0.5 : 0
end