-3

A challenge for me is to use Java to obtain Poisson Distribution quantile (an integer).

As I am new to Java, I googled a lot and wrote some code, not sure this is correct or not, could someone help me on it?

According to Poisson Distribution pdf function Poisson Distribution

(pr = k) = pow((lambda), k) * pow(e, (-lambda)) / k!

Where k is number of occurrence and lambda is distribution mean (expected number of occurrences).

My code:

// 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;
}

Will the code return i (output of quantile for Poisson Distribution)? Thank you!

Chenxi
  • 297
  • 1
  • 4
  • 15

1 Answers1

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);
}

According to Poisson Distribution pdf function(p = k) = (lambda)k * e(-lambda) / k!

k is occurence and lambda is distribution mean.

Chenxi
  • 297
  • 1
  • 4
  • 15