messed StdDev in Array.getStatistics?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

messed StdDev in Array.getStatistics?

Schebique
Hello all.

I would like to know why I get a different number with custom written
functions to get standard deviation (according to
http://www.investopedia.com/terms/s/standarddeviation.asp) compare to
Array.getStatistics build-in macro function? Is there a bug or am I missing
something?

Thank you very much.
Ondrej

minimal macro:

volumeArray=newArray(3, 5, 6, 4, 4, 1, 2, 7);
volume=Mean(volumeArray);
volStDev=StDev(volumeArray);
print("custom functions send "+volume+"+-"+volStDev);
Array.getStatistics(volumeArray, min, max, volume, volStDev);
print("Array.getStatistics send "+volume+"+-"+volStDev);

//******************** functions **********************
function StDev(pole) {
    SUM=0;
    n=pole.length;
    mean=Mean(pole);
    for (x=0; x<n; x++) {
         SUM = SUM+square(pole[x]-mean);
    }
    return sqrt(SUM/n);
}

function Mean(pole) {
    mean=0;
    n=pole.length;
    for (x=0; x<n; x++) {
        mean+=pole[x];
    }
    return mean/n;
}
function square(a) {
    return a*a;
}

--
Mgr. Ondřej Šebesta
Laboratory of Confocal and Fluorescence Microscopy
Faculty of Science, Charles University in Prague
Vinicna 7
128 44 Prague
Czech Republic

Phone: +420 2 2195 1061
e-mail: [hidden email]

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: messed StdDev in Array.getStatistics?

Michael Schmid
Hi Ondřej,

if you want to calculate the stddev of a sample of values where the
exact mean is not known, but only the mean of the values in the sample,
you have to divide by (n-1) instead of n.

https://en.wikipedia.org/wiki/Standard_deviation#Corrected_sample_standard_deviation

So you should have
...
   return sqrt(SUM/(n-1));

A hand-waving argument for this:
The denominator is zero if you have only one value (n=1): For a sample
with only one value you cannot determine the standard deviation. For
very few values, the mean value of these values will on average still be
quite off from the mean value that you would get from an infinitely
large sample. So the deviations (x-mean) from this mean will be too
small and the equation with dividing by n will give you a stddev that is
too small. The (n-1) corrects for this fact.

Michael
________________________________________________________________
On 2016-06-30 15:30, Ondřej Šebesta wrote:

> Hello all.
>
> I would like to know why I get a different number with custom written
> functions to get standard deviation (according to
> http://www.investopedia.com/terms/s/standarddeviation.asp) compare to
> Array.getStatistics build-in macro function? Is there a bug or am I missing
> something?
>
> Thank you very much.
> Ondrej
>
> minimal macro:
>
> volumeArray=newArray(3, 5, 6, 4, 4, 1, 2, 7);
> volume=Mean(volumeArray);
> volStDev=StDev(volumeArray);
> print("custom functions send "+volume+"+-"+volStDev);
> Array.getStatistics(volumeArray, min, max, volume, volStDev);
> print("Array.getStatistics send "+volume+"+-"+volStDev);
>
> //******************** functions **********************
> function StDev(pole) {
>     SUM=0;
>     n=pole.length;
>     mean=Mean(pole);
>     for (x=0; x<n; x++) {
>          SUM = SUM+square(pole[x]-mean);
>     }
>     return sqrt(SUM/n);
> }
>
> function Mean(pole) {
>     mean=0;
>     n=pole.length;
>     for (x=0; x<n; x++) {
>         mean+=pole[x];
>     }
>     return mean/n;
> }
> function square(a) {
>     return a*a;
> }
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html