View Javadoc
1   package nl.tudelft.simulation.jstats.distributions;
2   
3   import org.djutils.exceptions.Throw;
4   
5   import nl.tudelft.simulation.jstats.math.ProbMath;
6   import nl.tudelft.simulation.jstats.streams.StreamInterface;
7   
8   /**
9    * The Poisson distribution. For more information on this distribution see
10   * <a href="https://mathworld.wolfram.com/PoissonDistribution.html"> https://mathworld.wolfram.com/PoissonDistribution.html </a>
11   * <p>
12   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
13   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
14   * project is distributed under a three-clause BSD-style license, which can be found at
15   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
16   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
17   * </p>
18   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
19   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   */
21  public class DistPoisson extends DistDiscrete
22  {
23      /** */
24      private static final long serialVersionUID = 1L;
25  
26      /** lambda is the lambda parameter. */
27      private final double lambda;
28  
29      /** expl is a helper variable. */
30      private final double expl;
31  
32      /**
33       * constructs a new Poisson distribution.
34       * @param stream StreamInterface; the random number stream
35       * @param lambda double; the lambda parameter
36       * @throws IllegalArgumentException when lambda &lt;= 0
37       */
38      public DistPoisson(final StreamInterface stream, final double lambda)
39      {
40          super(stream);
41          Throw.when(lambda <= 0.0, IllegalArgumentException.class, "Error Poisson - lambda<=0");
42          this.lambda = lambda;
43          this.expl = Math.exp(-this.lambda);
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public long draw()
49      {
50          // Adapted from Fortran program in Shannon, Systems Simulation, 1975, p. 359
51          double s = 1.0;
52          long x = -1;
53          do
54          {
55              s = s * this.stream.nextDouble();
56              x++;
57          }
58          while (s > this.expl);
59          return x;
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public double probability(final long observation)
65      {
66          if (observation >= 0)
67          {
68              return (Math.exp(-this.lambda) * Math.pow(this.lambda, observation)) / ProbMath.factorial(observation);
69          }
70          return 0;
71      }
72  
73      /**
74       * @return lambda
75       */
76      public double getLambda()
77      {
78          return this.lambda;
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public String toString()
84      {
85          return "Poisson(" + this.lambda + ")";
86      }
87  }