1

If you are unsure of what "Poisson Distrubtion using Normal Approximation" means, follow this link and check the texts inside the yellow box. https://onlinecourses.science.psu.edu/stat414/node/180

Here, is the simple snapshot of the math from the link.

P(Y≥9) = P(Y>8.5) = P(Z>(8.5−6.5)/√6.5) = P(Z>0.78)= 0.218

So to get the value in .218, we use Simpson's integration rule which integrates the function(Implemented in method named "f" from code below) from "negative infinity" to the value that equals to this >> "((8.5−6.5)/√6.5))"

R successfully gives the correct output. But in Java when i implemented the code below copied from "http://introcs.cs.princeton.edu/java/93integration/SimpsonsRule.java.html" I get "0.28360853976343986" which should have been ".218" Is it any how because of the negative infinity value I am using, which is "Double.MIN_VALUE"

This is the code in Java. See at the very end for my INPUTS in the main method.

       * Standard normal distribution density function.
       * Replace with any sufficiently smooth function.
       **********************************************************************/
       public static double f(double x) {
          return Math.exp(- x * x / 2) / Math.sqrt(2 * Math.PI);
       }

      /**********************************************************************
       * Integrate f from a to b using Simpson's rule.
       * Increase N for more precision.
       **********************************************************************/
       public static double integrate(double a, double b) {
          int N = 10000;                    // precision parameter
          double h = (b - a) / (N - 1);     // step size

          // 1/3 terms
          double sum = 1.0 / 3.0 * (f(a) + f(b));

          // 4/3 terms
          for (int i = 1; i < N - 1; i += 2) {
             double x = a + h * i;
             sum += 4.0 / 3.0 * f(x);
          }

          // 2/3 terms
          for (int i = 2; i < N - 1; i += 2) {
             double x = a + h * i;
             sum += 2.0 / 3.0 * f(x);
          }

          return sum * h;
       }



       // sample client program
       public static void main(String[] args) { 
           double z = (8.5-6.5)/Math.sqrt(6.5);
           double a = Double.MIN_VALUE;
           double b =  z;
           System.out.println(integrate(a, b));
      }

Anybody has any ideas? I tried using Apache math's "PoissonDistribution" class's method "normalApproximateProbability(int x)". But the problem is this method takes an "int".

Anyone has any better ideas on how do I get the correct output or any other code. I have used another library for simpson too but I get the same output.

I need this to be done in Java.

xem
  • 125
  • 5
  • 17
  • Take a look to [this question](http://stackoverflow.com/questions/3884793/minimum-values-and-double-min-value-in-java). `Double.MIN_VALUE` might not be what you think... – lrnzcig Oct 12 '15 at 09:50
  • Tried all of them. "-Double.MIN_VALUE", "-Double.MAX_VALUE", "Double.NEGATIVE_INFINITY", no hope. – xem Oct 12 '15 at 15:42
  • Run out of time today. I think you should use `-Double.MAX_VALUE`. I had time to debug your code a bit and I think you might be facing the "typical" floating point rounding errors (see [this](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)). Maybe could try rewriting your function so that instead of going from `a` to `b` with step `h`, you go from `b` to `a`. I think this way you could avoid the errors, but please take into account it is a wild guess. – lrnzcig Oct 12 '15 at 18:06
  • Thanks for your time. -Double.MAX_VALUE gives me a number greater than 1. You mean b to a as in "h = (a - b) / (N - 1);" . If I do it or not, for the "Double.MAX_VALUE" i am getting value greater than 1. Which it shouldn't be. – xem Oct 12 '15 at 18:17

1 Answers1

0

I tried to test the code by writing another method that implements Simpson's 3/8 rule instead of your integrate function. It gave the same result as the one you obtained at first time. So i think the difference arises most probably from rounding errors.

  • You know instead of "Double.MIN_VALUE", I used "-150"..It gave me the correct output. So weird. I started playing with negative number as great as -10000 and then lowering it down. . But yeah "-150" is not the answer to this question. @lrnzcig – xem Oct 13 '15 at 03:29