如何在MQL4中计算Pearson相关性?

时间:2016-10-09 00:49:26

标签: mql4

使用简单的交易相关对(对冲),我需要编码一个相关矩阵,就像myfxbook或Oanda上的那样。

重点是我希望能够循环遍历矩阵中的每个值并检查它是否大于85.0左右。

1 个答案:

答案 0 :(得分:0)

Q:如何用MQL4计算Pearson的相关性?

方法A:使用MQL4直接计算PearsonCorr_r

如果足以使用 double 的精确度,MQL4代码可以实现合理大小的值向量 {{1}的过程}

( X[], Y[] )

方法B:在R,MATLAB等人中重复使用具有Pearson Correlation的外部Libs:

可以使用例如ZeroMQ消息传递基础结构的分布式处理来请求在MQL4之外执行微积分/独立于本地主机处理。

如果有兴趣,请在 #define RET_OK 0 #define RET_ERROR EMPTY #define VAL_ERROR EMPTY_VALUE int PearsonCorr_r( double const &vectorX[], // |-> INPUT X[] = { 1, 3, 5, 5, 6 } double const &vectorY[], // |-> INPUT Y[] = { 5, 6, 10, 12, 13 } double &pearson_r // <=| returns RESULT = 0.968 ){ double sumX = 0, meanX = 0, meanY = 0, sumY = 0, sumXY = 0, sumX2 = 0, sumY2 = 0; // deviation_score_x[], // may be re-used for _x^2 // deviation_score_y[], // may be re-used for _y^2 // deviation_score_xy[]; /* ===================================================================== DEVIATION SCORES >>> http://onlinestatbook.com/2/describing_bivariate_data/calculation.html X[] Y[] x y xy x^2 y^2 1 4 -3 -5 15 9 25 3 6 -1 -3 3 1 9 5 10 1 1 1 1 1 5 12 1 3 3 1 9 6 13 2 4 8 4 16 _______________________________________ SUM 20 45 0 0 30 16 60 MEAN 4 9 0 0 6 r = SUM(xy) / SQRT( SUM( x^2 ) * SUM( y^2 ) ) r = 30 / SQRT( 960 ) r = 0.968 ===================================================================== */ int vector_maxLEN = MathMin( ArrayRange( vectorX, 0 ), ArrayRange( vectorY, 0 ) ); if ( vector_maxLEN == 0 ){ pearson_r = VAL_ERROR; // STOR VAL ERROR IN RESULT return( RET_ERROR ); // FLAG RET_ERROR in JIT/RET } for ( int jj = 0; jj < vector_maxLEN; jj++ ){ sumX += vectorX[jj]; sumY += vectorY[jj]; } meanX = sumX / vector_maxLEN; // DIV!0 FUSED meanY = sumY / vector_maxLEN; // DIV!0 FUSED for ( int jj = 0; jj < vector_maxLEN; jj++ ){ // deviation_score_x[ jj] = meanX - vectorX[jj]; // // deviation_score_y[ jj] = meanY - vectorY[jj]; // deviation_score_xy[jj] = deviation_score_x[jj] // * deviation_score_y[jj]; // sumXY += deviation_score_x[jj] // * deviation_score_y[jj]; sumXY += ( meanX - vectorX[jj] ) // PSPACE MOTIVATED MINIMALISTIC WITH CACHE-BENEFITS IN PROCESSING * ( meanY - vectorY[jj] ); // deviation_score_x[jj] *= deviation_score_x[jj]; // PSPACE MOTIVATED RE-USE, ROW-WISE DESTRUCTIVE, BUT VALUE WAS NEVER USED AGAIN // sumX2 += deviation_score_x[jj] // * deviation_score_x[jj]; sumX2 += ( meanX - vectorX[jj] ) // PSPACE MOTIVATED MINIMALISTIC WITH CACHE-BENEFITS IN PROCESSING * ( meanX - vectorX[jj] ); // deviation_score_y[jj] *= deviation_score_y[jj]; // PSPACE MOTIVATED RE-USE, ROW-WISE DESTRUCTIVE, BUT VALUE WAS NEVER USED AGAIN // sumY2 += deviation_score_y[jj] // * deviation_score_y[jj]; sumY2 += ( meanY - vectorY[jj] ) // PSPACE MOTIVATED MINIMALISTIC WITH CACHE-BENEFITS IN PROCESSING * ( meanY - vectorY[jj] ); } pearson_r = sumXY / MathSqrt( sumX2 * sumY2 ); // STOR RET VALUE IN RESULT return( RET_OK ); // FLAG RET_OK in JIT/RET 中阅读关于分布式流程的other posts(代码示例 - 只是为了了解MQL4方面的情况得到设置 - 可能是found here) 和 MQL4 (ZeroMQ基础架构设置的代码示例可以是found here

因此允许使用MATLAB内置的Pearson相关实现(记得正确地将数据预先格式化为列,并且如果还添加 MATLAB -fusing,则最好) :

DIV!0

同样, [ RHO, PVAL ] = corr( vectorX, vectorY, 'type', 'Pearson' ); % note: double-r in corr() % # 'Pearson' is default method 语言有一个内置工具:

R

最后但并非最不重要的是 corr_r <- cor( vecORmatX, vecORmatY, use = "everything", method = "pearson" ) # "Pearson" is default method - 作为工具实施,同时 python scipy.stats.stats pearsonr float32 精度:

float64

结语:
方法A产生结果== >>> from scipy.stats.stats import pearsonr as pearson_r >>> >>> X = np.zeros( (5,), dtype = np.float32 ) >>> Y = np.zeros( (5,), dtype = np.float32 ) >>> >>> X[0] = 1; X[1] = 3; X[2] = 5; X[3] = 5; X[4] = 6 >>> Y[0] = 5; Y[1] = 6; Y[2] = 10; Y[3] = 12; Y[4] = 13 >>> >>> pearson_r( X, Y) (0.94704783, 0.01451040731338055) >>> >>> X = np.zeros( (5,), dtype = np.float64 ) >>> Y = np.zeros( (5,), dtype = np.float64 ) >>> >>> X[0] = 1; X[1] = 3; X[2] = 5; X[3] = 5; X[4] = 6 >>> Y[0] = 5; Y[1] = 6; Y[2] = 10; Y[3] = 12; Y[4] = 13 >>> >>> pearson_r( X, Y) (0.94704783738690446, 0.014510403904375592) >>>
(即引用的onlinestatbook.com结果不准确) < / H2>
python.scipy.stats.stats.pearsonr(X,Y)