Recent

Author Topic: Problem with statistical routines (MeanAndStdDev and MomentSkewKurtosis)  (Read 1659 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Hello,
I was playing with the statistical routines in Math and I came across a behaviour that I am not sure is ok.

The portion of code "All samples with distinct values" fails only for 0 and 1.
This is senseful because for the requested calculations at least 2 samples are requested.
Maybe putting a check in the method could slow down excessively on frequent repeated calls .

The portion of code "All samples with same value" fails periodically (btw in correspondence of powers of 2).
This probably due to the fact that the samples are a fake distribution with no spreadness.

Is that the caller obeyed to make a preverification that the sample set is somehow spread to a minimum?


Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   SysUtils, Math;
  4. var
  5.   a: array of float;
  6.   Mean, StdDev: float;
  7.   m1, m2, m3, m4, Skewness, Kurtosis: float;
  8.   n, i: Integer;
  9. begin
  10.   // All samples with same value
  11.   for n := 0 to 99 do
  12.     try
  13.       SetLength(a, n);
  14.       for i := 0 to n - 1 do a[i] := 9;
  15.       MeanAndStdDev(a, Mean, StdDev);
  16.       MomentSkewKurtosis(a, m1, m2, m3, m4, Skewness, Kurtosis);
  17.       Writeln(Format('%d done', [n]));
  18.     except
  19.       Writeln(Format('%d failed', [n]));
  20.     end;
  21.  
  22.   // All samples with distinct values
  23.   for n := 0 to 99 do
  24.     try
  25.       SetLength(a, n);
  26.       for i := 0 to n - 1 do a[i] := 9 + i;
  27.       MeanAndStdDev(a, Mean, StdDev);
  28.       MomentSkewKurtosis(a, m1, m2, m3, m4, Skewness, Kurtosis);
  29.       Writeln(Format('%d done', [n]));
  30.     except
  31.       Writeln(Format('%d failed', [n]));
  32.     end;
  33.  
  34. end.
  35.  
« Last Edit: February 04, 2022, 02:02:41 pm by tt »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Problem with statistical routines (MeanAndStdDev and MomentSkewKurtosis)
« Reply #1 on: February 07, 2022, 12:11:27 pm »
Is that the caller obeyed to make a preverification that the sample set is somehow spread to a minimum?

Hi, does anybody have a clue on the question posed?
Thank you vm
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

wp

  • Hero Member
  • *****
  • Posts: 13504
Re: Problem with statistical routines (MeanAndStdDev and MomentSkewKurtosis)
« Reply #2 on: February 07, 2022, 12:39:52 pm »
Look at the definition of skewness and kurtosis in wikipedia: https://en.wikipedia.org/wiki/Skewness#Fisher's_moment_coefficient_of_skewness and https://en.wikipedia.org/wiki/Kurtosis#Pearson_moments: All of them divide some number by the standard deviation of the data set. But when all values are equal the standard deviation is zero, and this results in the error observed.

The second part of your demo ("All samples with distinct values") crashes for the same reason because you start your n loop with 0. With n=0 your data array is empty - having no data, the demo crashes already in the calculation of the mean. With n=1, the standard deviation cannot be calculated since it requires a division by n-1 (=0) --> crash. Only when the loop starts with n=2 the program runs successfully.

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Problem with statistical routines (MeanAndStdDev and MomentSkewKurtosis)
« Reply #3 on: February 07, 2022, 01:44:50 pm »
Ok.

So I assume that before calling MomentSkewKurtosis() it is necessary to make sure (StdDev > 0).
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018