Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public static long inverseCdfFast(final double p, final double s, final double N) {
  2. if (p > 1d || p < 0d)
  3. throw new IllegalArgumentException("p must be between 0 and 1");
  4.  
  5. final double tolerance = 0.01d;
  6. double x = N / 2;
  7.  
  8. 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);
  9.  
  10. while (true) {
  11. final double m = Math.pow(x, -2 - s);
  12. final double mx = m * x;
  13. final double mxx = mx * x;
  14. final double mxxx = mxx * x;
  15.  
  16. final double a = 12 * (mxxx - 1) / (1 - s) + 6 * (1 - mxx) + (s - (mx * s)) - D;
  17. final double b = 12 * mxx + 6 * (s * mx) + (m * s * (s + 1));
  18. final double newx = Math.max(1, x - a / b);
  19. if (Math.abs(newx - x) <= tolerance)
  20. return (long) newx;
  21. x = newx;
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement