【发布时间】:2016-01-09 07:09:06
【问题描述】:
如果您不确定“使用正态近似的泊松分布”是什么意思,请点击此链接并检查黄色框内的文本。 https://onlinecourses.science.psu.edu/stat414/node/180
这里是链接中数学的简单快照。
P(Y≥9) = P(Y>8.5) = P(Z>(8.5−6.5)/√6.5) = P(Z>0.78)= 0.218
所以要获得 .218 中的值,我们使用辛普森积分规则 集成函数(在下面代码中名为“f”的方法中实现) 无穷”到等于这个的值>>“((8.5−6.5)/√6.5))”
R 成功地给出了正确的输出。但是在 Java 中,当我实现代码时 下面从“http://introcs.cs.princeton.edu/java/93integration/SimpsonsRule.java.html”复制 我得到“0.28360853976343986”,它应该是“.218”,因为我使用的是负无穷大值,它是“Double.MIN_VALUE” "
这是 Java 中的代码。 在最后查看我在 main 方法中的 INPUTS。
* Standard normal distribution density function.
* Replace with any sufficiently smooth function.
**********************************************************************/
public static double f(double x) {
return Math.exp(- x * x / 2) / Math.sqrt(2 * Math.PI);
}
/**********************************************************************
* Integrate f from a to b using Simpson's rule.
* Increase N for more precision.
**********************************************************************/
public static double integrate(double a, double b) {
int N = 10000; // precision parameter
double h = (b - a) / (N - 1); // step size
// 1/3 terms
double sum = 1.0 / 3.0 * (f(a) + f(b));
// 4/3 terms
for (int i = 1; i < N - 1; i += 2) {
double x = a + h * i;
sum += 4.0 / 3.0 * f(x);
}
// 2/3 terms
for (int i = 2; i < N - 1; i += 2) {
double x = a + h * i;
sum += 2.0 / 3.0 * f(x);
}
return sum * h;
}
// sample client program
public static void main(String[] args) {
double z = (8.5-6.5)/Math.sqrt(6.5);
double a = Double.MIN_VALUE;
double b = z;
System.out.println(integrate(a, b));
}
有人有什么想法吗?我尝试使用 Apache 数学的“PoissonDistribution”类的方法“normalApproximateProbability(int x)”。但问题是这个方法需要一个“int”。
对于如何获得正确的输出或任何其他代码,任何人都有更好的想法。我也为 simpson 使用了另一个库,但我得到了相同的输出。
我需要在 Java 中完成此操作。
【问题讨论】:
-
看看this question。
Double.MIN_VALUE可能不是你想的那样... -
都试过了。 “-Double.MIN_VALUE”、“-Double.MAX_VALUE”、“Double.NEGATIVE_INFINITY”,没有希望。
-
今天没时间了。我认为你应该使用
-Double.MAX_VALUE。我有时间稍微调试一下您的代码,我认为您可能会遇到“典型的”浮点舍入错误(请参阅this)。也许可以尝试重写你的函数,而不是从a到b步骤h,而是从b到a。我认为这样可以避免错误,但请注意这是一个疯狂的猜测。 -
感谢您的宝贵时间。 -Double.MAX_VALUE 给我一个大于 1 的数字。你的意思是 b 到 a,如“h = (a - b) / (N - 1);” .如果我这样做与否,对于“Double.MAX_VALUE”,我得到的值大于 1。它不应该是。
标签: java statistics probability normal-distribution poisson