对我来说,一个挑战是使用Java来获得泊松分布分位数(整数)。
由于我是Java新手,我搜索了很多并编写了一些代码,不确定这是否正确,有人可以帮助我吗?
根据泊松分布pdf函数Poisson Distribution
(pr = k)= pow((lambda),k)* pow(e,(lambda)/ k!
其中k是出现次数,λ是分布均值(预期出现次数)。
我的代码:
// quantile function calculate pdf for each i, sum it until it hits probability threshold pr; finally output i as distribution quantile; it calls the second function which aims to obtain i!;
private int quantile(double pr, double mean){
int n = (long) 3 * mean;
double prev = 0;
for (int i = 0; i < n; i++) {
double curr = math.pow(mean, i) * math.exp(-mean) / factorialLoop(i);
prev = curr;
curr = prev + curr;
if (curr < pr) {
continue;
}
else {
break;
}
}
return i;
}
// The following function will return result for i!;
private static long factorialLoop(int n) {
if (n < 0) {
return -1;
}
if (n == 0) {
return 1;
}
long result = 1;
for (int i = n; i > 0; i--) {
result *= i;
}
return result;
}
代码是否会返回i(泊松分布的分位数输出)?谢谢!
// Poisson Distribution Quantile function: pr and mean are two parameters. Aims to return i for the quantile function as output。
// pr and mean are two parameters; pr is probability threshold and mean is expected occurence.
private int quantile(double pr, double mean){
int n = (long) 3 * mean;
double prev = 0;
for (int i = 0; i < n; i++) {
double curr = math.pow(mean, i) * math.exp(-mean) / factorialRecursive(i);
double summ = curr + prev;
if (summ >= pr) {
break;
}
prev = summ;
}
return i;
}
private static long factorialRecursive(int n) {
if (n < 0) {
return -1;
}
if (n == 0) {
return 1;
}
if (n < 2)
return n * 1;
return n * factorialRecursive(n - 1);
}
k是出现,lambda是分布均值。