[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: var(x) and std(x)
From: |
Dave Cottingham |
Subject: |
Re: var(x) and std(x) |
Date: |
Tue, 12 Nov 2002 12:27:49 -0500 |
I see several people have already explained the n - 1, but I have
another concern. The method cited here is prone to bad roundoff error
in cases where sqrt(variance) is much smaller than the mean. A better
method would be
y = sumsq(x - sum(x) / n) / (n - 1);
- Dave Cottingham
> Hi,
> I have a doubt regarding the implementation of variance function var(x) in
> octave.It finds the variance as follows
>
> function y = var(x)
>
> if (nargin != 1)
> usage ("var (x)");
> endif
>
> [nr, nc] = size (x);
> if (nr == 0 || nc == 0)
> error ("var: x must not be empty");
> elseif ((nr == 1) && (nc == 1))
> y = 0;
> elseif ((nr == 1) || (nc == 1))
> n = length (x);
> y = (sumsq (x) - sum(x)^2 / n) / (n-1);
> else
> y = (sumsq (x) - sum(x).^2 / nr) / (nr-1);
> endif
>
> endfunction
>
> It gives variance after averaging over n-1 samples, rather than averaging
> over n samples. for example, if we calculate the variance of
> x = [1,2,3] , it returns the value 1, instead of 2/3..This is because it
> finds 2/2 and not 2/3..The same is the case with std(x) also.
> Is it an error or is there any explanation for that?
> Please correct me if I am wrong.
>
> Thanks and Regards
> Anand
>
>
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------