Monday, May 13, 2013
Monday, May 6, 2013
Python Snippet - Pearson Correlation function
Here's the code I wrote in Python to calculate the Pearson Correlation. It works just like the Java version.
def get_Pearson_Correlation(dataseries1, dataseries2):
result = 0.0
sum_sq_x = 0.0
sum_sq_y = 0.0
sum_coproduct = 0.0
mean_x = dataseries1[0]
mean_y = dataseries2[0]
for i in range(2,len(dataseries1)+1):
sweep = (i-1)/float(i)
delta_x = dataseries1[i-1]-mean_x
delta_y = dataseries2[i-1]-mean_y
sum_sq_x += delta_x * delta_x * sweep
sum_sq_y += delta_y * delta_y * sweep
sum_coproduct += delta_x * delta_y * sweep
mean_x += delta_x / float(i)
mean_y += delta_y / float(i)
pop_sd_x = (sum_sq_x / float(len(dataseries1)))**0.5
pop_sd_y = (sum_sq_y / float(len(dataseries1)))**0.5
cov_x_y = sum_coproduct / float(len(dataseries1))
result = cov_x_y / (pop_sd_x*pop_sd_y)
return result
def get_Pearson_Correlation(dataseries1, dataseries2):
result = 0.0
sum_sq_x = 0.0
sum_sq_y = 0.0
sum_coproduct = 0.0
mean_x = dataseries1[0]
mean_y = dataseries2[0]
for i in range(2,len(dataseries1)+1):
sweep = (i-1)/float(i)
delta_x = dataseries1[i-1]-mean_x
delta_y = dataseries2[i-1]-mean_y
sum_sq_x += delta_x * delta_x * sweep
sum_sq_y += delta_y * delta_y * sweep
sum_coproduct += delta_x * delta_y * sweep
mean_x += delta_x / float(i)
mean_y += delta_y / float(i)
pop_sd_x = (sum_sq_x / float(len(dataseries1)))**0.5
pop_sd_y = (sum_sq_y / float(len(dataseries1)))**0.5
cov_x_y = sum_coproduct / float(len(dataseries1))
result = cov_x_y / (pop_sd_x*pop_sd_y)
return result
Labels:
Code Snippet,
Correlation,
Pearson Correlation,
Python
Java Snippet - Pearson Correlation function
I am not certain the math is completely correct, regardless, this might serve as a good base to build upon or improve. If you encounter errors feel free to comment!
public class PearsonCorrelation {
public static double getPearsonCorrelation(double[] scores1, double[] scores2){
double result = 0;
double sum_sq_x = 0;
double sum_sq_y = 0;
double sum_coproduct = 0;
double mean_x = scores1[0];
double mean_y = scores2[0];
for(int i=2;i<scores1.length+1;i+=1){
double sweep = Double.valueOf(i-1)/i;
double delta_x = scores1[i-1]-mean_x;
double delta_y = scores2[i-1]-mean_y;
sum_sq_x += delta_x * delta_x * sweep;
sum_sq_y += delta_y * delta_y * sweep;
sum_coproduct += delta_x * delta_y * sweep;
mean_x += delta_x / i;
mean_y += delta_y / i;
}
double pop_sd_x = (double) Math.sqrt(sum_sq_x/scores1.length);
double pop_sd_y = (double) Math.sqrt(sum_sq_y/scores1.length);
double cov_x_y = sum_coproduct / scores1.length;
result = cov_x_y / (pop_sd_x*pop_sd_y);
return result;
}
}
public class PearsonCorrelation {
public static double getPearsonCorrelation(double[] scores1, double[] scores2){
double result = 0;
double sum_sq_x = 0;
double sum_sq_y = 0;
double sum_coproduct = 0;
double mean_x = scores1[0];
double mean_y = scores2[0];
for(int i=2;i<scores1.length+1;i+=1){
double sweep = Double.valueOf(i-1)/i;
double delta_x = scores1[i-1]-mean_x;
double delta_y = scores2[i-1]-mean_y;
sum_sq_x += delta_x * delta_x * sweep;
sum_sq_y += delta_y * delta_y * sweep;
sum_coproduct += delta_x * delta_y * sweep;
mean_x += delta_x / i;
mean_y += delta_y / i;
}
double pop_sd_x = (double) Math.sqrt(sum_sq_x/scores1.length);
double pop_sd_y = (double) Math.sqrt(sum_sq_y/scores1.length);
double cov_x_y = sum_coproduct / scores1.length;
result = cov_x_y / (pop_sd_x*pop_sd_y);
return result;
}
}
Labels:
Code Snippet,
Correlation,
Java,
Pearson Correlation
Subscribe to:
Posts (Atom)