Re: Different result between Octave and GSL (Gnu Scientific Library) C function call

Markus Mützel <[email protected]>
Newsgroups gmane.comp.gnu.octave.general
Message-ID <trinity-862bc4f9-b05d-4ba2-b4d4-1e0e77e22d2e-1609496582184@3c-app-gmx-bs18>
Am 01. Januar 2021 um 07:24 Uhr schrieb "Tallis Huther da Costa":
> >> gsl_stats_wvariance_test_c
> Hello World has 0 input arguments and 0 output arguments.
> n: 11
> ans =  1.7643
> 
> As you can see, the answer is 1.7643.
> 
> Now, from the function's documentation:
> 
> So, I wrote this little Octave script to confirm that the call was correct:
> data = [0 2 2 3 1 5 2.5 7 3 3.9 1.1];
> _mean = mean (data);
> stride = 1;
> w = [2 20 1.8 3 4 5.4 4.5 1.2 30 1.9 10.1];
> wstride = 1;
> n = numel (data);
> expected = (sum (w) / ((sum (w))^2 - sum (w .^ 2))) * sum (w .* ((data - _mean) .^ 2));
> 
> The result was:
> expected =  1.8427
 
Looking at the definition of the weighted variance as used by gsl, the *weighted* mean has to be used in its calculation.
Correcting for that gives the same result in Octave as the one you observed when using the gsl function:
data = [0 2 2 3 1 5 2.5 7 3 3.9 1.1];
w = [2 20 1.8 3 4 5.4 4.5 1.2 30 1.9 10.1];
weighted_mean = sum (w .* data) / sum (w);
expected = ((sum (w) / ((sum (w))^2 - sum (w .^ 2))) * sum (w .* ((data - weighted_mean) .^ 2));

>> expected
expected =  1.7643

HTH,
Markus
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.