Correlate

Compute the cross-correlation of two vectors.

vector vector::Correlate(
  const vector&          v,        // vector
  ENUM_VECTOR_CONVOLVE   mode      // mode
   );

Parameters

v

[in]  Second vector.

mode

[in]  The 'mode' parameter determines the linear convolution calculation mode. Value from the ENUM_VECTOR_CONVOLVE enumeration.

Return Value

Cross-correlation of two vectors.

Note

The 'mode' parameter determines the linear convolution calculation mode.

A simple algorithm for calculating the correlation coefficient of two vectors using MQL5:

vector VectorCrossCorrelationFull(const vectora,const vectorb)
  {
   int    m=(int)a.Size();
   int    n=(int)b.Size();
   int    size=m+n-1;
   vector c=vector::Zeros(size);
 
   for(int i=0i<ni++)
      for(int i_=ii_<i+mi_++)
         c[i_]+=b[n-i-1]*a[i_-i];
 
   return(c);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
vector VectorCrossCorrelationSame(const vectora,const vectorb)
  {
   int    m=(int)a.Size();
   int    n=(int)b.Size();
   int    size=MathMax(m,n);
   vector c=vector::Zeros(size);
 
   for(int i=0i<ni++)
     {
      for(int i_=ii_<i+mi_++)
        {
         int k=i_-size/2+1;
         if(k>=0 && k<size)
            c[k]+=b[n-i-1]*a[i_-i];
        }
     }
 
   return(c);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
vector VectorCrossCorrelationValid(const vectora,const vectorb)
  {
   int    m=(int)a.Size();
   int    n=(int)b.Size();
   int    size=MathMax(m,n)-MathMin(m,n)+1;
   vector c=vector::Zeros(size);
 
   for(int i=0i<ni++)
     {
      for(int i_=ii_<i+mi_++)
        {
         int k=i_-n+1;
         if(k>=0 && k<size)
            c[k]+=b[n-i-1]*a[i_-i];
        }
     }
 
   return(c);
  }

 

MQL5 example:

   vector a={1,2,3,4,5};
   vector b={0,1,0.5};
 
   Print("full\n",a.Correlate(b,VECTOR_CONVOLVE_FULL));
   Print("same\n",a.Correlate(b,VECTOR_CONVOLVE_SAME));
   Print("valid\n",a.Correlate(b,VECTOR_CONVOLVE_VALID));
   Print("full\n",b.Correlate(a,VECTOR_CONVOLVE_FULL));
 
  /*
   full
   [0.5,2,3.5,5,6.5,5,0]
   same
   [2,3.5,5,6.5,5]
   valid
   [3.5,5,6.5]
   full
   [0,5,6.5,5,3.5,2,0.5]
  */

 

Python example:

import numpy as np
a=[1,2,3,4,5]
b=[0,1,0.5]
 
print("full\n",np.correlate(a,b,'full'))
print("same\n",np.correlate(a,b,'same'));
print("valid\n",np.correlate(a,b,'valid'));
print("full\n",np.correlate(b,a,'full'))
 
full
 [0.5 2.  3.5 5.  6.5 5.  0. ]
same
 [2.  3.5 5.  6.5 5. ]
valid
 [3.5 5.  6.5]
full
 [0.  5.  6.5 5.  3.5 2.  0.5]