Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static long inverseCdfFast(final double p, final double s, final double N) {
- if (p > 1d || p < 0d)
- throw new IllegalArgumentException("p must be between 0 and 1");
- final double tolerance = 0.01d;
- double x = N / 2;
- final double D = p * (12 * (Math.pow(N, 1 - s) - 1) / (1 - s) + 6 - 6 * Math.pow(N, -s) + s - Math.pow(N, -1 - s) * s);
- while (true) {
- final double m = Math.pow(x, -2 - s);
- final double mx = m * x;
- final double mxx = mx * x;
- final double mxxx = mxx * x;
- final double a = 12 * (mxxx - 1) / (1 - s) + 6 * (1 - mxx) + (s - (mx * s)) - D;
- final double b = 12 * mxx + 6 * (s * mx) + (m * s * (s + 1));
- final double newx = Math.max(1, x - a / b);
- if (Math.abs(newx - x) <= tolerance)
- return (long) newx;
- x = newx;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement