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.
% 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
See also CORRCOEF, COV, STD, MEAN.
Check also WEIGHTEDCORRS (FE 20846) and KENDALLTAU (FE 27361)
Corrected minor issue