Skip to content

Instantly share code, notes, and snippets.

@mjbommar
Created October 3, 2010 15:37
Show Gist options
  • Save mjbommar/608672 to your computer and use it in GitHub Desktop.
Save mjbommar/608672 to your computer and use it in GitHub Desktop.
% Author: Michael J. Bommarito II
% Contact: [email protected]
% Date: Oct 3, 2010
% Provided as-is, informational purposes only, public domain.
%
% Inputs:
% 1. dataMatrix: variables (X1,X2,...,X_M) in the columns
% 2. windowSize: number of samples to include in the moving window
% 3. indexColumn: the variable X_i against which correlations should be
% returned
%
% Output:
% 1. correlationTS: correlation between X_{indexColumn} and X_j for j !=
% indexColumn from windowSize+1 to the number of observations. The first
% windowSize rows are NaN.
function correlationTS = movingCorrelation(dataMatrix, windowSize, indexColumn)
[N,M] = size(dataMatrix);
correlationTS = nan(N, M-1);
for t = windowSize+1:N
C = corrcoef(dataMatrix(t-windowSize:t, :));
idx = setdiff(1:M, [indexColumn]);
correlationTS(t, :) = C(indexColumn, idx);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment