covariance of weighted sample set
25 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!