Hi i am trying to calculate the Pearson Correlation Coefficient across nbars.
I am trying to do so with the for loop function. x2 y2 total is collected by the for loop but the MathPow(f,2) function doesn't work. Secondly, i don't know how to calculate the xy calculation for each bar to find a total of xy calculation. ideas? I would also like to know if there is a better way to calculate Pearson Correlation Coefficient (r).
//time
double y1=0.0;
int counttime=2000;
for(int c=counttime-1; c>=0; c--) y1+=Time[c];
double Meantime = totaltime/counttime;
double y2=0.0;
int counttime2=2000;
for(int f=counttime2-1; f>=0; f--) y2+=Time[MathPow(f,2)];
double Meantime2 = totaltime2/counttime2;
// Price
double x1=0.0;
int count1=2000;
for(int g=count1-1; g>=0; g--) x1+=High[g];
double x2=0.0;
int countprice=2000;
for(int e=countprice-1; e>=0; e--) x2+=High[MathPow(e,2)];
double x22 = MathPow(x1,2);
double y22 = MathPow(y1,2);
double r = (count1*(x1*y1)-(x1*y1))/sqrt(count1*(x2-x22)*(count1*(y2-y22));
Your actual formula interpretation appears incorrect: Try this:
int count=2000;
double time=0.0, high=0.0;
for(int i=count-1; i>=0; i--) {time+=double(Time[i]); high+=High[i];}
double timeAVG=time/count; double highAVG=high/count;
double timeDiff=0.0, highDiff=0.0, ProductDiff=0.0;
for(int i=count-1; i>=0; i--) {timeDiff+=MathPow(double(Time[i])-timeAVG,2); highDiff+=MathPow(High[i]-highAVG,2); ProductDiff+=(double(Time[i])-timeAVG)*(High[i]-highAVG);}
Print("Correlation = ", DoubleToString(ProductDiff/(MathSqrt(timeDiff)*MathSqrt(highDiff)),8));