Module: ArrayMetrics
- Defined in:
- lib/array_metrics/pearson_correlation.rb
Class Method Summary collapse
-
.constraint_pearson_correlation(x, y, m) ⇒ Object
Calculates the Constraint Pearson Correlation between Arrays x and y.
-
.pearson_correlation(x, y) ⇒ Object
Calculates the Pearson Correlation between the Arrays x and x.
Class Method Details
.constraint_pearson_correlation(x, y, m) ⇒ Object
Calculates the Constraint Pearson Correlation between Arrays x and y. See Shardanand and Maes 1995 for more details.
Shardanand, U. and Maes, P. Social information filtering: algorithms for automating “word of mouth”. In Proceedings of the SIGCHI conference on Human factors in computing systems (CHI ‘95). ACM Press/Addison-Wesley Publishing Co., New York, NY, USA, 210-217. DOI=10.1145/223904.223931 dx.doi.org/10.1145/223904.223931
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/array_metrics/pearson_correlation.rb', line 10 def self.constraint_pearson_correlation(x, y, m) raise "array sizes don't match" if x.size != y.size cx, cy = [], [] x.each_with_index do |xi, i| next if xi.nil? or y[i].nil? cx << xi cy << y[i] end return NaN if cx.size == 0 s = sx = sy = 0.0 if m.nil? cx_ = cx.mean cy_ = cy.mean else cx_ = cy_ = m end cx.each_with_index do |cxi,i| px = cxi-cx_ py = cy[i]-cy_ s += px*py sx += px*px sy += py*py end s/Math.sqrt(sx*sy) end |
.pearson_correlation(x, y) ⇒ Object
Calculates the Pearson Correlation between the Arrays x and x. See secure.wikimedia.org/wikipedia/en/wiki/Pearson_correlation for more details.
44 45 46 |
# File 'lib/array_metrics/pearson_correlation.rb', line 44 def self.pearson_correlation(x,y) self.constraint_pearson_correlation(x,y,nil) end |