I have this code:
DescriptiveStatistics stats = new DescriptiveStatistics( new double[] {2,4,4,4,5,5,7,9} );
System.out.println("var="+stats.getVariance());
System.out.println("sd="+stats.getStandardDeviation());
I took the example from Wikipedia https://en.wikipedia.org/wiki/Standard_deviation. The answer should be var=4.0, sd=2.0, but what I get is:
4.571428571428571
2.138089935299395
What am I missing?
getVariance()
divides by n-1
, here from the docs:
Returns the (sample) variance of the available values.
This method returns the bias-corrected sample variance (using
n - 1
in the denominator). UsegetPopulationVariance()
for the non-bias-corrected population variance.
Background: typically, one has a sample from the population, and by calculating the bias-corrected (or unbiased) sample variance, the expected value of the calculated estimate equals the population variance. I wrote sample code that demonstrates this for this answer. And Wikipedia has background on population variance vs. sample variance.