View Javadoc

1   /*
2    * @(#)DistGeometric.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.streams.StreamInterface;
13  
14  /***
15   * The Geometric distribution. For more information on this distribution see <a
16   * href="http://mathworld.wolfram.com/GeometricDistribution.html">
17   * http://mathworld.wolfram.com/GeometricDistribution.html </a>
18   * <p>
19   * (c) copyright 2002-2004 <a href="http://www.simulation.tudelft.nl">Delft
20   * University of Technology </a>, the Netherlands. <br>
21   * See for project information <a href="http://www.simulation.tudelft.nl">
22   * www.simulation.tudelft.nl </a> <br>
23   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
24   * License (GPL) </a>, no warranty <br>
25   * 
26   * @author <a href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">
27   *         Alexander Verbraeck </a> <br>
28   *         <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm"> Peter
29   *         Jacobs </a>
30   * @version 1.9 2004-03-22
31   * @since 1.2
32   */
33  public class DistGeometric extends DistDiscrete
34  {
35  	/*** p is the p-value of the geometric distribution */
36  	private double p;
37  
38  	/*** lnp is a helper variable to avoid repetitive calculation */
39  	private double lnp;
40  
41  	/***
42  	 * constructs a new geometric distribution
43  	 * 
44  	 * @param stream the numberstream
45  	 * @param p is the p-value
46  	 */
47  	public DistGeometric(final StreamInterface stream, final double p)
48  	{
49  		super(stream);
50  		if ((p > 0.0) && (p < 1.0))
51  		{
52  			this.p = p;
53  		} else
54  		{
55  			throw new IllegalArgumentException("Error Geometric - p<=0 or p>=1");
56  		}
57  		this.lnp = Math.log(1.0 - this.p);
58  	}
59  
60  	/***
61  	 * @see DistDiscrete#draw()
62  	 */
63  	public long draw()
64  	{
65  		double u = this.stream.nextDouble();
66  		return (long) (Math.floor(Math.log(u) / this.lnp));
67  	}
68  
69  	/***
70  	 * @see nl.tudelft.simulation.jstats.distributions.DistDiscrete
71  	 *      #probability(int)
72  	 */
73  	public double probability(final int observation)
74  	{
75  		if (observation >= 0)
76  		{
77  			return this.p * Math.pow(1 - this.p, observation);
78  		}
79  		return 0.0;
80  	}
81  
82  	/***
83  	 * @see java.lang.Object#toString()
84  	 */
85  	public String toString()
86  	{
87  		return "Geometric(" + this.p + ")";
88  	}
89  }