Created
December 18, 2014 12:02
-
-
Save gyaikhom/1349d1c92f095031fe00 to your computer and use it in GitHub Desktop.
Implements Pearson's product-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
/* Copyright 2014 Gagarine Yaikhom MIT License | |
* | |
* Implements Pearson's product-moment correlation coefficient. | |
* (see http://stattrek.com/statistics/correlation.aspx?tutorial=ap) | |
*/ | |
function correlation_coefficient(datapoints) { | |
var sum_x, sum_y, mean_x, mean_y, | |
dx, dy, sum_dx_dy, sum_dx_2, sum_dy_2, | |
n, i, d, result; | |
result = undefined; | |
if (datapoints instanceof Array) { | |
n = datapoints.length; | |
if (n < 1) | |
throw RangeError("Not enough data points"); | |
/* calculate mean */ | |
sum_x = sum_y = 0; | |
for (i = 0; i < n; ++i) { | |
d = datapoints[i]; | |
if (typeof d.x !== "number") | |
throw TypeError("Supplied x-axis value '" + d.x | |
+ "' is not a number"); | |
if (typeof d.y !== "number") | |
throw TypeError("Supplied y-axis value '" + d.y | |
+ "' is not a number"); | |
sum_x += d.x; | |
sum_y += d.y; | |
} | |
mean_x = sum_x / n; | |
mean_y = sum_y / n; | |
/* calculate correlation coefficient */ | |
sum_dx_dy = sum_dx_2 = sum_dy_2 = 0; | |
for (i = 0; i < n; ++i) { | |
d = datapoints[i]; | |
dx = d.x - mean_x; | |
dy = d.y - mean_y; | |
sum_dx_dy += dx * dy; | |
sum_dx_2 += dx * dx; | |
sum_dy_2 += dy * dy; | |
} | |
d = Math.sqrt(sum_dx_2 * sum_dy_2); | |
if (d === 0) | |
throw EvalError("Correlation coefficient is undefined"); | |
result = sum_dx_dy / d; | |
} else | |
throw TypeError("Supplied input is not an array"); | |
return result; | |
} | |
function test() { | |
var datapoints = [ | |
[ | |
{ 'x': 1, 'y': 1}, | |
{ 'x': 2, 'y': 2}, | |
{ 'x': 3, 'y': 3}, | |
{ 'x': 4, 'y': 4}, | |
{ 'x': 5, 'y': 5}, | |
{ 'x': 6, 'y': 6}, | |
{ 'x': 7, 'y': 7}, | |
{ 'x': 8, 'y': 8}, | |
{ 'x': 9, 'y': 9}, | |
{ 'x':10, 'y':10} | |
], | |
[ | |
{ 'x': 1, 'y':20}, | |
{ 'x': 2, 'y':18}, | |
{ 'x': 3, 'y':16}, | |
{ 'x': 4, 'y':14}, | |
{ 'x': 5, 'y':12}, | |
{ 'x': 6, 'y':10}, | |
{ 'x': 7, 'y': 8}, | |
{ 'x': 8, 'y': 6}, | |
{ 'x': 9, 'y': 4}, | |
{ 'x':10, 'y': 2} | |
], | |
[ | |
{ 'x': 1, 'y': 3.5}, | |
{ 'x': 2, 'y': 5.2}, | |
{ 'x': 3, 'y': 4.7}, | |
{ 'x': 4, 'y': 8}, | |
{ 'x': 5, 'y':10}, | |
{ 'x': 6, 'y':15.25}, | |
{ 'x': 7, 'y':13.75}, | |
{ 'x': 8, 'y':12.5}, | |
{ 'x': 9, 'y':20.25}, | |
{ 'x':10, 'y':23} | |
], | |
[ | |
{ 'x': "Apple", 'y': "Orange"}, | |
{ 'x': 0, 'y': 4}, | |
{ 'x': 0, 'y': 6}, | |
{ 'x': 0, 'y': 8} | |
], | |
[ | |
{ 'x': 0, 'y': 2}, | |
{ 'x': 0, 'y': 4}, | |
{ 'x': 0, 'y': 6}, | |
{ 'x': 0, 'y': 8}, | |
{ 'x': 0, 'y':10} | |
]]; | |
for (var i = 0, n = datapoints.length; i < n; ++i) { | |
try { | |
console.log(correlation_coefficient(datapoints[i])); | |
} catch (x) { | |
console.error(x.message); | |
} | |
} | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment