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.math.ProbMath;
13  import nl.tudelft.simulation.jstats.streams.StreamInterface;
14  
15  /***
16   * The Binomial distribution. For more information on this distribution see <a
17   * href="http://mathworld.wolfram.com/BinomialDistribution.html">
18   * http://mathworld.wolfram.com/BinomialDistribution.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 DistBinomial extends DistDiscrete
35  {
36  	/*** n is the n-parameter of the Binomial distribution */
37  	private long n;
38  
39  	/*** p is the p-value of the binomial distribution */
40  	private double p;
41  
42  	/***
43  	 * constructs a Binomial distribution. Number of successes in t independent
44  	 * Bernoulli trials with probability p of sucess on each trial.
45  	 * 
46  	 * @param stream the numberstream
47  	 * @param n is the n-parameter of the Binomial distribution
48  	 * @param p is the p-parameter of the Binomial distribution
49  	 */
50  	public DistBinomial(final StreamInterface stream, final long n,
51  			final double p)
52  	{
53  		super(stream);
54  		if ((n > 0) && (p > 0) && (p < 1))
55  		{
56  			this.n = n;
57  			this.p = p;
58  		} else
59  		{
60  			throw new IllegalArgumentException(
61  					"Error Binomial - n<=0 or p<=0.0 or p>=1.0");
62  		}
63  	}
64  
65  	/***
66  	 * @see DistDiscrete#draw()
67  	 */
68  	public long draw()
69  	{
70  		long x = 0;
71  		for (long i = 0; i < this.n; i++)
72  		{
73  			if (this.stream.nextDouble() <= this.p)
74  			{
75  				x++;
76  			}
77  		}
78  		return x;
79  	}
80  
81  	/***
82  	 * @see nl.tudelft.simulation.jstats.distributions.DistDiscrete
83  	 *      #probability(int)
84  	 */
85  	public double probability(final int observation)
86  	{
87  		if (observation <= this.n && observation >= 0)
88  		{
89  			return ProbMath.permutations((int) this.n, observation)
90  					* Math.pow(this.p, observation)
91  					* Math.pow(1 - this.p, this.n - observation);
92  		}
93  		return 0.0;
94  	}
95  
96  	/***
97  	 * @see java.lang.Object#toString()
98  	 */
99  	public String toString()
100 	{
101 		return "Binomial(" + this.n + "," + this.p + ")";
102 	}
103 }