View Javadoc

1   /*
2    * @(#)DistBernoulli.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 Bernouilli distribution. For more information on this distribution see <a
16   * href="http://mathworld.wolfram.com/BernouilliDistribution.html">
17   * http://mathworld.wolfram.com/BernouilliDistribution.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 DistBernoulli extends DistDiscrete
34  {
35  	/*** p is the p-value of the Bernouilli distribution */
36  	private double p;
37  
38  	/***
39  	 * constructs a new Bernoulli distribution. Random occurence with two
40  	 * possible outcomes; used to generate other discrete random variates.
41  	 * 
42  	 * @param stream is the stream
43  	 * @param p the p-value of a Bernoulli distribution
44  	 */
45  	public DistBernoulli(final StreamInterface stream, final double p)
46  	{
47  		super(stream);
48  		if ((p >= 0.0) && (p <= 1.0))
49  		{
50  			this.p = p;
51  		} else
52  		{
53  			throw new IllegalArgumentException(
54  					"Error Exponential - p<0 or p>1 (p=" + p + ")");
55  		}
56  	}
57  
58  	/***
59  	 * draws the next value from the Bernoulli distribution. More information on
60  	 * this distribution can be found at < a
61  	 * href="http://mathworld.wolfram.com/BernoulliDistribution.html">
62  	 * http://mathworld.wolfram.com/BernoulliDistribution.html </a>.
63  	 * 
64  	 * @return the next value {0,1}.
65  	 */
66  	public long draw()
67  	{
68  		if (this.stream.nextDouble() <= this.p)
69  		{
70  			return 1L;
71  		}
72  		return 0L;
73  	}
74  
75  	/***
76  	 * @see nl.tudelft.simulation.jstats.distributions.DistDiscrete
77  	 *      #probability(int)
78  	 */
79  	public double probability(final int observation)
80  	{
81  		if (observation == 0)
82  		{
83  			return 1 - this.p;
84  		}
85  		if (observation == 1)
86  		{
87  			return this.p;
88  		}
89  		return 0;
90  	}
91  
92  	/***
93  	 * @see java.lang.Object#toString()
94  	 */
95  	public String toString()
96  	{
97  		return "Bernoulli(" + this.p + ")";
98  	}
99  }