1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.jstats.distributions;
11
12 import nl.tudelft.simulation.jstats.streams.StreamInterface;
13
14 /***
15 * The Triangular distribution. For more information on this distribution see <a
16 * href="http://mathworld.wolfram.com/TriangularDistribution.html">
17 * http://mathworld.wolfram.com/TriangularDistribution.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.11 2004-03-22
31 * @since 1.2
32 */
33 public class DistTriangular extends DistContinuous
34 {
35 /*** a is the minimum */
36 private double a;
37
38 /*** b is the mode */
39 private double b;
40
41 /*** c is the maximum */
42 private double c;
43
44 /***
45 * constructs a new triangular distribution
46 *
47 * @param stream the numberstream
48 * @param a the minimum
49 * @param b the mode
50 * @param c the maximum
51 */
52 public DistTriangular(final StreamInterface stream, final double a,
53 final double b, final double c)
54 {
55 super(stream);
56 if ((a < b) && (b < c))
57 {
58 this.a = a;
59 this.b = b;
60 this.c = c;
61 } else
62 {
63 throw new IllegalArgumentException(
64 "Error condition for tria: a<b<c");
65 }
66 }
67
68 /***
69 * @see DistContinuous#draw()
70 */
71 public double draw()
72 {
73 double u = this.stream.nextDouble();
74 if (u <= ((this.b - this.a) / (this.c - this.a)))
75 {
76 return this.a
77 + Math.sqrt((this.b - this.a) * (this.c - this.a) * u);
78 }
79 return this.c
80 - Math.sqrt((this.c - this.a) * (this.c - this.b) * (1.0d - u));
81 }
82
83 /***
84 * @see nl.tudelft.simulation.jstats.distributions.DistContinuous
85 * #probDensity(double)
86 */
87 public double probDensity(final double observation)
88 {
89 if (observation >= this.a && observation <= this.b)
90 {
91 return 2 * (observation - this.a)
92 / ((this.c - this.a) * (this.b - this.a));
93 }
94 if (observation >= this.b && observation <= this.c)
95 {
96 return 2 * (this.c - observation)
97 / ((this.c - this.a) * (this.c - this.b));
98 }
99 return 0.0;
100 }
101
102 /***
103 * @see java.lang.Object#toString()
104 */
105 public String toString()
106 {
107 return "Triangular(" + this.a + "," + this.b + "," + this.c + ")";
108 }
109 }