Created
February 13, 2011 00:37
-
-
Save mfdela/824281 to your computer and use it in GitHub Desktop.
Pearson produt-moment correlation coefficient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
class Array | |
def sum | |
inject( 0 ) { |sum,x| sum+x } | |
end | |
def sum_square | |
inject( 0 ) { |sum,x| sum+x*x } | |
end | |
def mean | |
sum / size | |
end | |
def *(other) # dot product | |
ret = [] | |
return nil if !other.is_a? Array || size != other.size | |
self.each_with_index {|x, i| ret << x * other[i]} | |
ret.sum | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment