java 广义超几何函数

2022-09-03 08:10:22

我正在寻找一个可以计算广义超几何函数(http://en.wikipedia.org/wiki/Generalized_hypergeometric_series)的java库。我看了一下Apach Common Math,但没有找到这个函数。实际上,我需要该函数来计算β-二项分布的累积概率函数(http://en.wikipedia.org/wiki/Beta-binomial_distribution)。如果有人知道一个java包,包括发行版,那对我来说是件好事。

谢谢


答案 1

您可以从此处使用它org.apache.commons.math3.distribution.HypergeometricDistribution

下载链接


答案 2

根据您发布的wiki文章,我认为您可以使用我编写的以下代码近似超几何函数的值:

作为下一步,可以估计近似值的误差。

/**
 * The generalized hypergeometric function is a convergent power series \sum_{i=0}^{\infty} c_i x^i
 * where the coefficients satisfy c_{n+1}/c_n = A(n)/B(n) for some polynomials A and B in n.
 * It is customary to factor out the leading term, so c_0 is assumed to be 1
 */

public class HypergeometricFunction {
    private final int degreeOfApproximation;
    private final double[] coefficientsOfA;
    private final double[] coefficientsOfB;
    private final double[] coefficientsOfHypergeometricFunction;

    public HypergeometricFunction(int degreeOfApproximation, double[] coefficientsOfA, double[] coefficientsOfB) {
        this.degreeOfApproximation = degreeOfApproximation;
        this.coefficientsOfA = coefficientsOfA;
        this.coefficientsOfB = coefficientsOfB;
        this.coefficientsOfHypergeometricFunction = generateCoefficients();
    }

    /**
     * @param x input
     * @return Approximation to the hypergeometric function by taking the first
     * {@code degreeOfApproximation} terms from the series.
     */
    public double approximate(double x){
        return evaluatePolynomial(x, coefficientsOfHypergeometricFunction);
    }


    private double[] generateCoefficients() {
        double[] coefficients = new double[degreeOfApproximation];
        coefficients[0] = 1;
        for (int i = 1; i < degreeOfApproximation; i++)
            coefficients[i] = (evaluatePolynomial(i, coefficientsOfA) / evaluatePolynomial(i, coefficientsOfB)) * coefficients[i - 1];
        return coefficients;
    }

    private double evaluatePolynomial(double n, double[] coefficients) {
        int length = coefficients.length;
        double out = 0.0D;
        for (int i = 0; i < length; i++) {
            out += coefficients[i] * pow(n, i);
        }
        return out;
    }

    private double pow(double a, int b) {
        double out = 1;
        for (int i = 0; i < b; i++) out *= a;
        return out;
    }

}

如果序列收敛(因此提供了适当的超几何函数),则必须为零,因此,如果您认为足够大,这应该提供合理的近似值。lim[c_i*x^i]degreeOfApproximation

多项式 A 和 B 是 wiki 文章中提到的多项式,为了使用此代码,您必须向构造函数提供这些多项式的系数数组,以及所需的近似程度。

希望这对您有所帮助。