简体   繁体   English

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

[英]How is the Pearson's Correlation calculated in MQL4?

Working on an easy that trades correlating pair( hedge ), I need to code a correlation matrix, like the ones on myfxbook, or Oanda. 在交易相关对(套期保值)的简单交易上,我需要编写一个相关矩阵,例如myfxbook或Oanda上的相关矩阵。

The main point is I want to be able to loop through each value in the matrix and check if its greater than 85.0 or so. 要点是我希望能够遍历矩阵中的每个值并检查其是否大于85.0左右。

Q: How is the Pearson's Correlation calculated in MQL4? Q:在MQL4中如何计算Pearson的相关性?

Method A: 方法A:
use the MQL4 to compute the PearsonCorr_r directly: 使用MQL4直接计算PearsonCorr_r

If it is enough to work with the precision of double , MQL4 code can implement the process for reasonable-sized vectors of values ( X[], Y[] ) 如果足以使用double的精度进行工作,那么MQL4代码可以对大小合理的值向量( X[], Y[] )实施该过程。

#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

Method B: 方法B:
re-use external Libs having Pearson Correlation in R, MATLAB et al: 重复使用R,MATLAB等人中具有Pearson相关性的外部Lib:

One may use a distributed processing using a for example a ZeroMQ messaging infrastructure to request the calculus to be performed outside of the MQL4 / independently from the localhost processing. 可以使用分布式处理,例如使用ZeroMQ消息传递基础结构,以要求在MQL4外部/独立于本地处理执行演算。

If interested, read my other posts on distributed processes in MQL4 ( a code-example -- just to have some feeling of how the MQL4 side gets setup -- could be found here ) and MATLAB ( a code-example of the ZeroMQ-infrastructure setup could be found here 如果有兴趣,请阅读我在MQL4分布式过程的其他文章 (一个代码示例-只是对MQL4方面的设置有一些了解-可以在这里找到 )和MATLAB (ZeroMQ基础结构的一个代码示例设置可以在这里找到

thus allowing to use the MATLAB built-in implementation of Pearson correlation ( remember to properly pre-format data into columns and best if added also a DIV!0 -fusing ), to compute: 因此,可以使用MATLAB内置的Pearson相关性实现(记住将数据正确地预先格式化为列,最好还添加DIV!0 -fusing)来计算:

[ RHO, PVAL ] = corr( vectorX, vectorY, 'type', 'Pearson' );
                                               % note: double-r in corr() 
                                               %            # 'Pearson' is default method

Similarly an R -language has a built-in tool: 同样, R语言具有内置工具:

corr_r <- cor( vecORmatX, vecORmatY, use = "everything", method = "pearson" )
                                                            # "Pearson" is default method

Last but not least is a python scipy.stats.stats pearsonr -implementation as a tool, with both float32 and float64 precisions: 最后但并非最不重要的是python scipy.stats.stats pearsonr作为工具的实现,具有float32float64精度:

>>> 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)
>>>

Epilogue: 结语:
Method A yields results == python.scipy.stats.stats.pearsonr(X,Y) 方法A产生结果== python.scipy.stats.stats.pearsonr(X,Y)
( ie the cited onlinestatbook.com result is inaccurate ) (即,所引用的onlinestatbook.com结果不准确)

2016.10.13 11:31:55.421 ___StackOverflow_Pearson_r_DEMO XAUUSD,H1:
                           PearsonCorr_r( testX, testY, Pearson_r ):= 0.968
                           The actual call returned    aReturnCODE == 0,
                                  whereas the          Pearson_r   == 0.9470

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM