Filter löschen
Filter löschen

covariance of weighted sample set

23 Ansichten (letzte 30 Tage)
Praveen Babu
Praveen Babu am 23 Jun. 2016
Kommentiert: Garrison Gross am 31 Aug. 2022
I have a weighted multidimensional sample set, e.g.
D=2; % dimension
N=100; % no of samples
x=randn(D,N); % samples
w=ones(1,N)/N; % corresponding weights
I would like to find the weighted covariance of this sample set. To obtain this, I first computed the weighted mean using formula: mu = \sum_{i=1}^{N} w_{i} x_{i} as
mu = sum(bsxfun(@times,w,x),2);
I now want to find the covariance using the formula: Sigma = \sum_{i=1}^{N} w_{i} (x_{i}-\mu) (x_{i} - \mu)'. I would like to know if there is a built in function to calculate this or if not the computationally best possible approach. Any help is appreciated.

Akzeptierte Antwort

Paul O'Brien
Paul O'Brien am 3 Mai 2021
Having been looking into something similar, I found that MatLab has no built-in way to do this for you. The cleanest and most concise form I came up with was:
D = 2; % dimension
N = 100; % no of samples
X = randn(N,D); % samples, each column is a random variable
w = exp(-([1:N]'/N-0.5).^2); % arbitrary weights, e.g. Gaussian, unnormalised
mu = sum(w.*X) / sum(w); % row containing mean of each column
sigma_sq = N/(N-1) * (w.*(X-mu))'*(X-mu) / sum(w); % Covariance matrix
Note that using this implementation with your choice of normalising w to add up to 1 would allow you to omit the "/sum(w)".
As for the computation time, for small to moderate N (<= 1e4), this calculation actually took less time than computing "cov(X)", and for larger N (1e5 and above) it was somewhat slower than the cov function.
  2 Kommentare
Praveen Babu
Praveen Babu am 9 Mai 2021
Charming. Thank you.
Garrison Gross
Garrison Gross am 31 Aug. 2022
Can you explain what you're doing here? You multiply the transposed matrix by the weight and then divide by the sum of the weight, and then multiply the whole thing by N/(N-1), why is that?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by