我正在寻找一个可以计算广义超几何函数(http://en.wikipedia.org/wiki/Generalized_hypergeometric_series)的java库。我查看了Apach Common Math,但没有找到函数。实际上,我需要这个函数来计算beta-二项分布(http://en.wikipedia.org/wiki/Beta-binomial_distribution)的累积概率函数。如果有人知道包含发行版的java包,那对我来说是件好事。
谢谢,
发布于 2014-04-09 06:22:03
您可以从here使用此org.apache.commons.math3.distribution.HypergeometricDistribution
。
发布于 2017-10-28 01:19:21
根据您发布的维基文章,我认为您可以使用我编写的以下代码近似超几何函数的值:
下一步,估计近似的误差是可能的。
/**
* 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文章中提到的两个多项式,为了使用此代码,您必须向构造器提供这些多项式的系数数组,以及所需的近似程度。
希望这能帮到你。
发布于 2017-10-29 17:44:46
有一个实现hypergeometric functions和many random number distributions的GNU Scientific Library --不幸的是,它是一个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>
免责声明:我不是数学家,所以请证实我的想法。
祝好运!
https://stackoverflow.com/questions/22949281
复制相似问题