Standard deviation with Apache Commons Math

一曲冷凌霜 提交于 2019-12-07 03:52:25

问题


I am computing the SD of a vector using Apache Commons Math. The problem: I get different values than by hand

DescriptiveStatistics stats = new DescriptiveStatistics();
stats.addValue(value1);
...
stats.addValue(value8);
stats.getStandardDeviation();

E.g., take the values [1699.0, 1819.0, 1699.0, 1719.0, 1689.0, 1709.0, 1819.0, 1689.0]. SD should be 52.067 but Commons Math = 55.662.

What am I doing wrong?


回答1:


The Apache StandardDeviation class can be used for computing both values: "Standard Deviation" and "Population Standard deviation".

For computing the second value initialize it with

    StandardDeviation sd = new StandardDeviation(false);

Example:

    double[] v = {1.0, 2.0, 3.0, 4.0, 5.0};
    StandardDeviation sd = new StandardDeviation(false);
    sd.evaluate(v);
    // returns 1.414

    StandardDeviation sd2 = new StandardDeviation();
    sd2.evaluate(v);
    // returns 1.581



回答2:


Apache is giving you the "Standard Deviation" but you are looking for the "Population Standard Deviation"

Maybe you could use getPopulationVariance() and then take the square root yourself? I don't see a function for this in the DS library.



来源:https://stackoverflow.com/questions/15306207/standard-deviation-with-apache-commons-math

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!