1 package nl.tudelft.simulation.jstats.distributions;
2
3 import org.djutils.exceptions.Throw;
4
5 import nl.tudelft.simulation.jstats.streams.StreamInterface;
6
7 /**
8 * The Exponential distribution. For more information on this distribution see
9 * <a href="https://mathworld.wolfram.com/ExponentialDistribution.html">
10 * https://mathworld.wolfram.com/ExponentialDistribution.html </a><br>
11 * The exponential distribution describes the interarrival times of entities to a system that occur randomly at a constant rate.
12 * The exponential distribution here is characterized by the mean interarrival time, but can also be characterized by this rate
13 * parameter λ; mean = 1 / λ.
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 DistExponential extends DistContinuous
24 {
25 /** mean is the mean value of the exponential distribution. */
26 private final double mean;
27
28 /**
29 * constructs a new exponential function. The exponential distribution describes the interarrival times of entities to a
30 * system that occur randomly at a constant rate. The exponential distribution can also be characterized by this rate
31 * parameter λ; mean = 1 / λ.
32 * @param stream the random number stream
33 * @param mean the mean (mean > 0) value of the exponential distribution. The exponential distribution can also be
34 * characterized by the rate parameter λ; mean = 1 / λ
35 * @throws IllegalArgumentException in case mean <= 0
36 */
37 public DistExponential(final StreamInterface stream, final double mean)
38 {
39 super(stream);
40 Throw.when(mean <= 0.0, IllegalArgumentException.class, "Error Exponential - mean<=0");
41 this.mean = mean;
42 }
43
44 @Override
45 public double draw()
46 {
47 return -this.mean * Math.log(this.stream.nextDouble());
48 }
49
50 @Override
51 public double getProbabilityDensity(final double x)
52 {
53 if (x >= 0)
54 {
55 return (1 / this.mean) * Math.exp(-x / this.mean);
56 }
57 return 0.0;
58 }
59
60 /**
61 * @return mean
62 */
63 public double getMean()
64 {
65 return this.mean;
66 }
67
68 @Override
69 public String toString()
70 {
71 return "Exponential(" + this.mean + ")";
72 }
73 }