Apache Math从数据集生成分布

时间:2019-03-27 18:36:01

标签: java statistics apache-commons-math

在Java的Apache Math库中,是否有任何方法可以获取一组数据点并从中生成分布对象?更具体地说,我试图从一组数据中创建一个BetaDistribution对象,但是创建一个对象的唯一方法是传入一个alpha和beta作为参数。我是否必须从数据中手动找出这些值,或者在Apache数学中是否有适合我这些值的东西?

1 个答案:

答案 0 :(得分:0)

smile project

    public BetaDistribution(double[] data) {
        for (int i = 0; i < data.length; i++) {
            if (data[i] < 0 || data[i] > 1) {
                throw new IllegalArgumentException("Samples are not in range [0, 1].");
            }
        }

        mean = Math.mean(data);
        var = Math.var(data);

        alpha = mean * (mean * (1 - mean) / var - 1);
        beta = (1 - mean) * (mean * (1 - mean) / var - 1);
        if (alpha <= 0 || beta <= 0) {
            throw new IllegalArgumentException("Samples don't follow Beta Distribution.");
        }

        mean = alpha / (alpha + beta);
        var = alpha * beta / ((alpha + beta) * (alpha + beta) * (alpha + beta + 1));
        entropy = Math.log(Beta.beta(alpha, beta)) - (alpha - 1) * Gamma.digamma(alpha) - (beta - 1) * Gamma.digamma(beta) + (alpha + beta - 2) * Gamma.digamma(alpha + beta);
    }