View Javadoc

1   /*
2    * @(#)DistNegBinomial.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 NegBinomial distribution. For more information on this distribution see
17   * <a href="http://mathworld.wolfram.com/NegativeBinomialDistribution.html">
18   * http://mathworld.wolfram.com/NegativeBinomialDistribution.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.9 2004-03-22
32   * @since 1.2
33   */
34  public class DistNegBinomial extends DistDiscrete
35  {
36  	/*** n independent geometric trials with probability p */
37  	private long n;
38  
39  	/*** p is the propbability */
40  	private double p;
41  
42  	/*** lnp is a helper variable to avoid repetitive calculation */
43  	private double lnp;
44  
45  	/***
46  	 * constructs a new negative benomial distribution
47  	 * 
48  	 * @param stream the numberstream
49  	 * @param n reflect the independent geometric trials with probability p
50  	 * @param p is the propbability
51  	 */
52  	public DistNegBinomial(final StreamInterface stream, final long n,
53  			final double p)
54  	{
55  		super(stream);
56  		if ((n > 0) && (p > 0) && (p < 1))
57  		{
58  			this.n = n;
59  			this.p = p;
60  		} else
61  		{
62  			throw new IllegalArgumentException(
63  					"Error NegBinomial - n<=0 or p<=0.0 or p>=1.0");
64  		}
65  		this.lnp = Math.log(1.0 - this.p);
66  	}
67  
68  	/***
69  	 * @see DistDiscrete#draw()
70  	 */
71  	public long draw()
72  	{
73  		long x = 0;
74  		for (long i = 0; i < this.n; i++)
75  		{
76  			double u = this.stream.nextDouble();
77  			x = x + (long) (Math.floor(Math.log(u) / this.lnp));
78  		}
79  		return x;
80  	}
81  
82  	/***
83  	 * @see nl.tudelft.simulation.jstats.distributions.DistDiscrete
84  	 *      #probability(int)
85  	 */
86  	public double probability(final int observation)
87  	{
88  		if (observation >= 0)
89  		{
90  			return ProbMath.permutations((int) this.n + observation - 1,
91  					observation)
92  					* Math.pow(this.p, this.n)
93  					* Math.pow(1 - this.p, observation);
94  		}
95  		return 0.0;
96  	}
97  
98  	/***
99  	 * @see java.lang.Object#toString()
100 	 */
101 	public String toString()
102 	{
103 		return "NegBinomial(" + this.n + "," + this.p + ")";
104 	}
105 }