필터 지우기
필터 지우기

covariance of weighted sample set

조회 수: 25 (최근 30일)
Praveen Babu
Praveen Babu 2016년 6월 23일
댓글: Garrison Gross 2022년 8월 31일
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.

채택된 답변

Paul O'Brien
Paul O'Brien 2021년 5월 3일
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
Praveen Babu
Praveen Babu 2021년 5월 9일
Charming. Thank you.
Garrison Gross
Garrison Gross 2022년 8월 31일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by