View Javadoc

1   /*
2    * @(#)DistErlang.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 Erlang distribution. For more information on this distribution see <a
17   * href="http://mathworld.wolfram.com/ErlangDistribution.html">
18   * http://mathworld.wolfram.com/ErlangDistribution.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.10 2004-03-22
32   * @since 1.2
33   */
34  public class DistErlang extends DistContinuous
35  {
36  	/*** k is the k-value of the erlang distribution */
37  	private int k;
38  
39  	/*** beta is the beta value of the erlang distribution */
40  	private double beta;
41  
42  	/*** betak is the mean value of the erlang distribution */
43  	private double betak;
44  
45  	/*** distGamma is the underlying gamma distribution */
46  	private DistGamma distGamma;
47  
48  	/*** GAMMABORDER is the gammaBorder */
49  	private static final short GAMMABORDER = 10;
50  
51  	/***
52  	 * constructs a new Erlang distribution
53  	 * 
54  	 * @param stream the numberstream
55  	 * @param k is the k-parameter
56  	 * @param beta is the beta-parameter
57  	 */
58  	public DistErlang(final StreamInterface stream, final int k,
59  			final double beta)
60  	{
61  		super(stream);
62  		if ((k > 0) && (beta > 0.0))
63  		{
64  			this.k = k;
65  			this.beta = beta;
66  		} else
67  		{
68  			throw new IllegalArgumentException(
69  					"Error Erlang - k <= 0 or beta < 0");
70  		}
71  		if (this.k <= DistErlang.GAMMABORDER)
72  		{
73  			this.betak = -this.beta / this.k;
74  		} else
75  		{
76  			this.distGamma = new DistGamma(stream, this.k, this.beta);
77  		}
78  	}
79  
80  	/***
81  	 * @see DistContinuous#draw()
82  	 */
83  	public double draw()
84  	{
85  		if (this.k <= DistErlang.GAMMABORDER)
86  		{
87  			// according to Law and Kelton, Simulation Modeling and Analysis
88  			// repeated drawing and composition is usually faster for k<=10
89  			double product = 1.0;
90  			for (int i = 1; i <= this.k; i++)
91  			{
92  				product = product * this.stream.nextDouble();
93  			}
94  			return this.betak * Math.log(product);
95  		}
96  		// and using the gamma distribution is faster for k>10
97  		return this.distGamma.draw();
98  	}
99  
100 	/***
101 	 * @see nl.tudelft.simulation.jstats.distributions.DistContinuous
102 	 *      #probDensity(double)
103 	 */
104 	public double probDensity(final double observation)
105 	{
106 		if (observation < 0)
107 		{
108 			return 0;
109 		}
110 		return this.beta
111 				* Math
112 						.exp(-this.beta
113 								* observation
114 								* (Math
115 										.pow(this.beta * observation,
116 												this.k - 1) / ProbMath
117 										.faculty(this.k - 1)));
118 	}
119 
120 	/***
121 	 * @see java.lang.Object#toString()
122 	 */
123 	public String toString()
124 	{
125 		return "Erlang(" + this.k + "," + this.beta + ")";
126 	}
127 }