对我来说,一个挑战是使用Java获取泊松分布分位数(整数)。
由于我是Java新手,所以我在Google上搜索了很多东西,并写了一些代码,不确定是否正确,有人可以帮我吗?
根据泊松分布pdf函数Poisson Distribution
(pr = k)= pow((lambda),k)* pow(e,(-lambda))/ k!
其中k是出现次数,lambda是分布平均值(预期出现次数)。
我的代码:
// 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(泊松分布的分位数输出)吗? 谢谢!
答案 0 :(得分:0)
// 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是出现次数,λ是分布平均值。