使用Java计算正态分布

时间:2011-06-15 06:03:25

标签: java statistics normal-distribution

编辑:

实际上我意识到我需要的是X的价值。让我说清楚一点。假设,我知道概率P = 0.95,因为我想使用两个标准偏差。我知道范围 P(-500 这意味着我知道y和z,我也知道平均值和标准偏差。如果我想知道x的值是什么,我应该使用哪种方法。我发现有一个calculator做了这样的事情,但无法理解使用哪个公式。

原始问题:

  

我想用Java计算随机变量的正态分布概率。不确定使用哪种公式来编码来解决像this这样的问题。如果我知道平均值和标准差的值,并且想要找出在两个特定值y和z之间为x的值的概率(P(-500)

有人能帮帮我吗?

3 个答案:

答案 0 :(得分:11)

您可以使用error function中提供的org.apache.commons.math.special.Erf,如[{3}}和here所述。

附录:@Brent Worden here中提出的方法大大简化了这些问题的解决方案。作为一个具体示例,下面的代码显示了如何解决您引用的answer。此外,我发现使用examples将定义herecumulativeProbability()的实施进行比较会很有帮助。还要注意Erf.erf的实现如何概括所需的迭代方法。

import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.NormalDistribution;
import org.apache.commons.math.distribution.NormalDistributionImpl;

/**
 * @see http://stattrek.com/Tables/Normal.aspx#examples
 * @see https://stackoverflow.com/questions/6353678
 */
public class CumulativeProbability {

    private static NormalDistribution d;

    public static void main(String[] args) throws MathException {
        // Problem 1; µ = 1000; σ = 100
        d = new NormalDistributionImpl(1000, 100);
        System.out.println(d.cumulativeProbability(1200));
        // Problem 2; µ = 50; σ = 10
        d = new NormalDistributionImpl(50, 10);
        System.out.println(d.inverseCumulativeProbability(0.9));
    }
}

控制台:

0.9772498680518208
62.81551565546365

讨论:

问题1.在正常分布寿命平均为1000小时且标准偏差为100小时的设备中,~97.7%将在1200小时内失效。

问题2.在具有正常分布技能的人中,平均50次重复,标准差为10次重复,个人可以超过90%的人口,重复63次。

答案 1 :(得分:8)

commons-math的另一个替代方法是使用其NormalDistributionImpl

    new org.apache.commons.math.distribution.NormalDistributionImpl(mean, std)
        .cumulativeProbability(a, b)

这给出了X~N(均值,std)的'P(a≤X≤b)。

从更新的问题看,您似乎想构建置信区间。如果是这样,请使用inverseCumulativeProbability方法。它计算概率p的值x,使得P(X≤x)= p。

答案 2 :(得分:4)

CERN开发的Colt库支持许多统计功能;也是cern.jet.random.Normal中的正态(又称高斯)分布。