1 package nl.tudelft.simulation.jstats.distributions;
2
3 import org.djutils.exceptions.Throw;
4
5 import nl.tudelft.simulation.jstats.math.ProbMath;
6 import nl.tudelft.simulation.jstats.streams.StreamInterface;
7
8 /**
9 * The Erlang distribution. For more information on this distribution see
10 * <a href="https://mathworld.wolfram.com/ErlangDistribution.html"> http://mathworld.wolfram.com/ErlangDistribution.html
11 * </a><br>
12 * The Erlang distribution is the distribution of a sum of k independent exponential variables with the scale parameter as the
13 * mean. The scale parameter is equal to 1/rate or 1/λ, giving the entire Erlang distribution a mean of k*scale.
14 * <p>
15 * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16 * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
17 * project is distributed under a three-clause BSD-style license, which can be found at
18 * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
19 * </p>
20 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
21 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
22 */
23 public class DistErlang extends DistContinuous
24 {
25 /**
26 * k is the shape parameter of the Erlang distribution. The shape k is the number of times a drawing is done from the
27 * exponential distribution, where the Erlang distribution is the sum of these k independent exponential variables.
28 */
29 private final int k;
30
31 /** scale is the mean of a single exponential distribution (1/rate), of which k are summed. */
32 private final double scale;
33
34 /** the rate value of the Erlang distribution (1 / scale). */
35 private final double lambda;
36
37 /** distGamma is the underlying gamma distribution. */
38 private final DistGamma distGamma;
39
40 /** GAMMATHRESHOLD is the threshold above which we use a gamma function and below repeated drawing. */
41 private static final short GAMMATHRESHOLD = 10;
42
43 /**
44 * Construct a new Erlang distribution with k and a mean (so not k and a rate) as parameters. It is the distribution of a
45 * sum of k independent exponential variables with the scale parameter as the mean. The scale parameter is equal to 1/rate
46 * or 1/λ, giving the entire Erlang distribution a mean of k*scale.
47 * @param stream the random number stream
48 * @param scale the mean of a single sample from the exponential distribution, of which k are summed. Equal to 1/rate or
49 * 1/λ.
50 * @param k the shape parameter of the Erlang distribution. The shape k is the number of times a drawing is done from the
51 * exponential distribution, where the Erlang distribution is the sum of these k independent exponential
52 * variables.
53 * @throws IllegalArgumentException when k <= 0 or scale <= 0
54 */
55 public DistErlang(final StreamInterface stream, final double scale, final int k)
56 {
57 super(stream);
58 Throw.when(k <= 0 || scale <= 0.0, IllegalArgumentException.class, "Error Erlang - k <= 0 or scale <= 0");
59 this.k = k;
60 this.scale = scale;
61 this.lambda = 1.0 / scale;
62 this.distGamma = this.k <= DistErlang.GAMMATHRESHOLD ? null : new DistGamma(stream, this.k, this.scale);
63 }
64
65 @Override
66 public double draw()
67 {
68 if (this.k <= DistErlang.GAMMATHRESHOLD)
69 {
70 // according to Law and Kelton, Simulation Modeling and Analysis
71 // repeated drawing and composition is usually faster for k<=10
72 double product = 1.0;
73 for (int i = 1; i <= this.k; i++)
74 {
75 product = product * this.stream.nextDouble();
76 }
77 return -this.scale * Math.log(product);
78 }
79 // and using the gamma distribution is faster for k>10
80 return this.distGamma.draw();
81 }
82
83 @Override
84 public double getProbabilityDensity(final double x)
85 {
86 if (x < 0)
87 {
88 return 0;
89 }
90 return this.lambda * Math.exp(-this.lambda * x) * Math.pow(this.lambda * x, this.k - 1)
91 / ProbMath.factorial(this.k - 1);
92 }
93
94 /**
95 * @return k
96 */
97 public int getK()
98 {
99 return this.k;
100 }
101
102 /**
103 * @return scale parameter
104 */
105 public double getScale()
106 {
107 return this.scale;
108 }
109
110 @Override
111 public void setStream(final StreamInterface stream)
112 {
113 super.setStream(stream);
114 if (this.distGamma != null)
115 {
116 this.distGamma.setStream(stream);
117 }
118 }
119
120 @Override
121 public String toString()
122 {
123 return "Erlang(" + this.scale + "," + this.k + ")";
124 }
125 }