File Exchange

Weighted Covariance Matrix

version 1.2 (2.21 KB) by

Calculates a Weighted Covariance Matrix

Updated

WEIGHTEDCOV returns a symmetric matrix C of weighted covariances calculated from an input T-by-N matrix Y whose rows are observations and whose columns are variables and an input T-by-1 vector w of weights for the observations. This function may be a valid alternative to COV if observations are not all equally relevant and need to be weighted according to some theoretical hypothesis or knowledge.
C = WEIGHTEDCOV(Y, w) returns a positive semidefinite matrix C, i.e. all its eigenvalues are non-negative.

If w = ones(size(Y, 1), 1), no difference exists between WEIGHTEDCOV(Y, w) and COV(Y, 1).

REFERENCE: mathematical formulas in matrix notation are available in F. Pozzi, T. Di Matteo, T. Aste, "Exponential smoothing weighted correlations", The European Physical Journal B, Volume 85, Issue 6, 2012. DOI:10.1140/epjb/e2012-20697-x.

EXAMPLE:
% GENERATE CORRELATED STOCHASTIC PROCESSES
T = 100; % number of observations
N = 500; % number of variables
Y = randn(T, N); % shocks from standardized normal distribution
Y = cumsum(Y); % correlated stochastic processes
% CHOOSE EXPONENTIAL WEIGHTS
alpha = 2 / T;
w0 = 1 / sum(exp(((1:T) - T) * alpha));
w = w0 * exp(((1:T) - T) * alpha); % weights: exponential decay
% COMPUTE WEIGHTED COVARIANCE MATRIX
c = weightedcov(Y, w); % Weighted Covariance Matrix

Check also WEIGHTEDCORRS (FE 20846) and KENDALLTAU (FE 27361)

David Chorlian

Xiaoshan Yang

Xiaoshan Yang (view profile)

 12 Jan 2015 1.2 minor edits 25 Jun 2012 1.1 Corrected minor issue
MATLAB 7 (R14)