1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.jstats.distributions;
11
12 import cern.jet.stat.Gamma;
13 import nl.tudelft.simulation.jstats.streams.StreamInterface;
14
15 /***
16 * The Beta distribution. For more information on this distribution see <a
17 * href="http://mathworld.wolfram.com/BetaDistribution.html">
18 * http://mathworld.wolfram.com/BetaDistribution.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 DistBeta extends DistContinuous
35 {
36 /*** dist1 refers to the first Gamma distribution */
37 private DistGamma dist1;
38
39 /*** dist2 refers to the second Gamma distribution */
40 private DistGamma dist2;
41
42 /*** alpha1 is the first parameter for the Beta distribution */
43 private double alpha1;
44
45 /*** alpha2 is the second parameter for the Beta distribution */
46 private double alpha2;
47
48 /***
49 * constructs a new beta distribution.
50 *
51 * @param stream the stream.
52 * @param alpha1 the first alpha parameter for the distribution.
53 * @param alpha2 the second alpha parameter for the distribution.
54 */
55 public DistBeta(final StreamInterface stream, final double alpha1,
56 final double alpha2)
57 {
58 super(stream);
59 if ((alpha1 > 0.0) && (alpha2 > 0.0))
60 {
61 this.alpha1 = alpha1;
62 this.alpha2 = alpha2;
63 } else
64 {
65 throw new IllegalArgumentException(
66 "Error alpha1 <= 0.0 or alpha2 <= 0.0");
67 }
68 this.dist1 = new DistGamma(stream, this.alpha1, 1.0);
69 this.dist2 = new DistGamma(stream, this.alpha2, 1.0);
70 }
71
72 /***
73 * @see DistContinuous#draw()
74 */
75 public double draw()
76 {
77
78
79 double y1 = this.dist1.draw();
80 double y2 = this.dist2.draw();
81 return y1 / (y1 + y2);
82 }
83
84 /***
85 * @see nl.tudelft.simulation.jstats.distributions.DistContinuous
86 * #probDensity(double)
87 */
88 public double probDensity(final double observation)
89 {
90 if (observation > 0 && observation < 1)
91 {
92 return (Math.pow(observation, this.alpha1 - 1) * Math.pow(
93 1 - observation, this.alpha2 - 1))
94 / Gamma.beta(this.alpha1, this.alpha2);
95 }
96 return 0;
97 }
98
99 /***
100 * @see java.lang.Object#toString()
101 */
102 public String toString()
103 {
104 return "Beta(" + this.alpha1 + "," + this.alpha2 + ")";
105 }
106 }