View Javadoc

1   /*
2    * @(#)DistPoisson.java Apr 3, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.jstats.distributions;
11  
12  import nl.tudelft.simulation.jstats.math.ProbMath;
13  import nl.tudelft.simulation.jstats.streams.StreamInterface;
14  
15  /***
16   * The Poisson distribution. For more information on this distribution see <a
17   * href="http://mathworld.wolfram.com/PoissonDistribution.html">
18   * http://mathworld.wolfram.com/PoissonDistribution.html </a>
19   * <p>
20   * (c) copyright 2002-2004 <a href="http://www.simulation.tudelft.nl">Delft
21   * University of Technology </a>, the Netherlands. <br>
22   * See for project information <a href="http://www.simulation.tudelft.nl">
23   * www.simulation.tudelft.nl </a> <br>
24   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
25   * License (GPL) </a>, no warranty <br>
26   * 
27   * @author <a href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">
28   *         Alexander Verbraeck </a> <br>
29   *         <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm"> Peter
30   *         Jacobs </a>
31   * @version 1.8 2004-03-22
32   * @since 1.2
33   */
34  public class DistPoisson extends DistDiscrete
35  {
36  	/*** lambda is the lambda parameter */
37  	private double lambda;
38  
39  	/*** expl is a helper variable */
40  	private double expl;
41  
42  	/***
43  	 * constructs a new poisson distribution
44  	 * 
45  	 * @param stream the numberstream
46  	 * @param lambda the lambda parameter
47  	 */
48  	public DistPoisson(final StreamInterface stream, final double lambda)
49  	{
50  		super(stream);
51  		if (lambda > 0.0)
52  		{
53  			this.lambda = lambda;
54  		} else
55  		{
56  			throw new IllegalArgumentException("Error Poisson - lambda<=0");
57  		}
58  		this.expl = Math.exp(-this.lambda);
59  	}
60  
61  	/***
62  	 * @see DistDiscrete#draw()
63  	 */
64  	public long draw()
65  	{
66  		// Adapted from Fortran program in Shannon, Systems Simulation, 1975,
67  		// p. 359
68  		double s = 1.0;
69  		long x = -1;
70  		do
71  		{
72  			s = s * this.stream.nextDouble();
73  			x++;
74  		} while (s > this.expl);
75  		return x;
76  	}
77  
78  	/***
79  	 * @see nl.tudelft.simulation.jstats.distributions.DistDiscrete
80  	 *      #probability(int)
81  	 */
82  	public double probability(final int observation)
83  	{
84  		if (observation >= 0)
85  		{
86  			return (Math.exp(-this.lambda) * Math.pow(this.lambda, observation))
87  					/ ProbMath.faculty(observation);
88  		}
89  		return 0;
90  	}
91  
92  	/***
93  	 * @see java.lang.Object#toString()
94  	 */
95  	public String toString()
96  	{
97  		return "Poisson(" + this.lambda + ")";
98  	}
99  }