Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
var with weights

Subject: var with weights

From: spasmous

Date: 23 Sep, 2009 23:42:07

Message: 1 of 4

y = -5:5;
w = 1:11;
w = w/sum(w); % normallize

% these agree
var(y)
sum((y-mean(y)).^2)/(numel(y)-1) % sample variance

ans = 11
ans = 11


% these do not agree
var(y,w)
(1/(1-sum(w.^2)))*sum(w.*(y-mean(y)).^2) % from Wikipedia

ans = 7.222
ans = 11.3143

(ref http://en.wikipedia.org/wiki/Weighted_mean#Weighted_sample_variance)

Does anyone know the formula MATLAB uses to calculate the weighted
variance? I'm sure it's more reliable than Wikipedia.

Subject: var with weights

From: Sprinceana

Date: 24 Sep, 2009 06:49:02

Message: 2 of 4

You have in EQ2-24 the formula for weighted variance from this link:

http://www.itl.nist.gov/div898/software/dataplot/refman2/ch2/weighvar.pdf

Subject: var with weights

From: Tom Lane

Date: 24 Sep, 2009 15:00:07

Message: 3 of 4

> % these do not agree
> var(y,w)
> (1/(1-sum(w.^2)))*sum(w.*(y-mean(y)).^2) % from Wikipedia
>
> ans = 7.222
> ans = 11.3143

You need to use the weights in computing the mean as well.

>> wmean = sum(w.*y)
wmean =
    1.6667
>> sum(w.*(y-wmean).^2)
ans =
    7.2222

Also, MATLAB's var function doesn't use a bias correction when weights are
given. This matches the weighted sigma^2 formula in the Wikipedia article,
rather than the s^2 formula given in the same section.

-- Tom

Subject: var with weights

From: spasmous2

Date: 24 Sep, 2009 19:50:06

Message: 4 of 4

On Sep 24, 8:00 am, "Tom Lane" <tl...@mathworks.com> wrote:
> > % these do not agree
> > var(y,w)
> > (1/(1-sum(w.^2)))*sum(w.*(y-mean(y)).^2) % from Wikipedia
>
> > ans = 7.222
> > ans = 11.3143
>
> You need to use the weights in computing the mean as well.
>
> >> wmean = sum(w.*y)
>
> wmean =
>     1.6667>> sum(w.*(y-wmean).^2)
>
> ans =
>     7.2222
>
> Also, MATLAB's var function doesn't use a bias correction when weights are
> given. This matches the weighted sigma^2 formula in the Wikipedia article,
> rather than the s^2 formula given in the same section.
>
> -- Tom

Thanks Tom, fixed my mistake :)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us