【问题标题】:java Generalized Hypergeometric functionjava广义超几何函数
【发布时间】:2014-05-21 20:55:41
【问题描述】:

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

谢谢,

【问题讨论】:

    标签: java math statistics


    【解决方案1】:

    你可以使用这个org.apache.commons.math3.distribution.HypergeometricDistribution 来自here

    Download link.

    【讨论】:

    • 感谢您的回答。我在主题上犯了一个错误。它应该是广义的超几何函数,而不是广义的超几何分布。很抱歉。
    【解决方案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 文章中提到的那些,为了使用此代码,您必须向构造函数提供这些多项式的系数数组,以及所需的近似度。

    希望对你有所帮助。

    【讨论】:

    • 这种“幼稚”近似的问题在于它们没有经过测试,因此错误范围是未知的。这些通常取决于评估函数的特定值,因此需要一些内部“智能”来获取足够的项来收敛。此外,这种评估大型多项式的方式往往在数值上不稳定......这就是为什么,我也在寻找一个可以做到这一点的包......谢谢!
    • 你在处理复数系数吗?如果是这样,那么它应该是另一个问题,因为这个问题是关于 beta-binomial 的。在实际情况下,函数在某个收敛半径上是连续可微的(参见 wiki)。因此,该函数有一个泰勒级数,与您要近似的函数一致,泰勒剩余定理实际上确实给出了误差的界限。您在哪里读到截断收敛幂级数称为朴素近似?这不是一个真正的术语。
    【解决方案3】:

    有一个 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 Central 上)时,它可以完美运行。

    我的 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>
    

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

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多