java广义超几何函数

时间:2014-04-08 22:17:47

标签: java math statistics

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

谢谢,

3 个答案:

答案 0 :(得分:1)

您可以使用此org.apache.commons.math3.distribution.HypergeometricDistribution 来自here

Download link

答案 1 :(得分:0)

根据您发布的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文章中提到的那些,为了使用这个代码,你必须为构造函数提供那些多项式的系数数组,以及你想要的近似程度。

希望这可以帮助你。

答案 2 :(得分:0)

GNU Scientific Library实施hypergeometric functionsmany random number distributions - 很遗憾,它是一个C库。

幸运的是有JavaCPP 预设available这意味着你可以轻松地从java中使用它(它包含了用于windows / linux / android的原生gcl库)。

example对我不起作用(它使用了库的2.4-1.3.4-SNAPSHOT版本),但是当修改为使用版本2.2.1-1.3(位于maven中心)时,它可以完美地运行。

我的pom.xml是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test-gsl-java</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <exec.mainClass>Example</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>gsl-platform</artifactId>
            <version>2.2.1-1.3</version>
        </dependency>
    </dependencies>
</project>

免责声明:我不是数学家,所以请确认我的想法。

祝你好运!