Pearson Correlation Coefficient calculation MQL4
Pearson Correlation Coefficient calculation MQL4
Loading saved threads...
Qan N · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Qan N
Original post: https://stackoverflow.com/questions/73565317
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Hi i am trying to calculate the Pearson Correlation Coefficient across nbars.
I am trying to do so with the for loop function. x
2
y
2
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));
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: PaulB
Original post: https://stackoverflow.com/a/73568247
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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));
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: PaulB Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/73568247 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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));
Checking account access…