MovCoef

Description

This function takes two vectors and returns a vector of moving correlation coefficients of adjacent ranges [i-back, i+forward], for a point i. The part of range beyond the input vector is dropped.

\frac{\overline{v_1v_2}-\bar{v_1}\bar{v_2}}{\sqrt{(\bar{v_1^2}-\bar{v_1}\bar{v_1})(\bar{v_2^2}-\bar{v_2}\bar{v_2})}}

within the window [i-back, i+forward]

Syntax

vector MovCoef(vector v1, vector v2, int back, int forward[, int missing])

Parameters

v1

A data vector

v2

A second data vector

back

Backward window. At any given i, backward window is defined by i - back.

forward

Forward window. At any given i, forward window is defined by i + forward.

missing

Optional. Determine how to deal with the missing value in the adjacent range.
  • 0 = omits missing values from the calculation. This is the default value.
  • 1 = includes missing values in the calculation. That means, if an adjacent range includes missing its output will be missing;
  • 2 = omits missing values from the calculation but keeps missing in the ouput. That means, if a row is missing, its output will be missing.
Please refer to example 2 to see the difference.

Return

Return the moving correlation vector.

Example

Example 1

MovCoef(col(2), col(3), 20, 0);

Example 2

newbook;
col(A) = {4,8,6,-1,NAN,-3,-1, 3, 4, 5};
col(B) = {1,2,3,4,5,6,7, 8, 9, 10};
col(C) = MovCoef(col(A),col(B),3,0,0); //returns [-- 1        0.5 -0.56825 -1.19763 -1.00447 -0.47434 0.65738 0.97683 0.93267]
col(D) = MovCoef(col(A),col(B),3,0,1); //returns [-- 1 0.5 -0.56825 -- -- -- -- 0.97683 0.93267]
col(E) = MovCoef(col(A),col(B),3,0,2); //returns [-- 1 0.5 -0.56825 -- -1.00447 -0.47434 0.65738 0.97683 0.93267]