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 custom distribution. This distribution is based on its entry map
16 * consisting of value, probability pairs.
17 * <p>
18 * (c) copyright 2002-2004 <a href="http://www.simulation.tudelft.nl">Delft
19 * University of Technology </a>, the Netherlands. <br>
20 * See for project information <a href="http://www.simulation.tudelft.nl">
21 * www.simulation.tudelft.nl </a> <br>
22 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
23 * License (GPL) </a>, no warranty <br>
24 *
25 * @author <a href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">
26 * Alexander Verbraeck </a> <br>
27 * <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm"> Peter
28 * Jacobs </a>
29 * @version 1.4 2004-03-22
30 * @since 1.2
31 */
32 public class DistCustom extends DistDiscrete
33 {
34 /*** the values */
35 private Entry[] values = null;
36
37 /***
38 * constructs a new DistCustom
39 *
40 * @param stream the stream to use
41 * @param values the values for this custom distribution
42 */
43 public DistCustom(final StreamInterface stream, final Entry[] values)
44 {
45 super(stream);
46 this.values = values;
47 }
48
49 /***
50 * @see nl.tudelft.simulation.jstats.distributions.DistDiscrete#draw()
51 */
52 public long draw()
53 {
54 if (this.values.length == 0)
55 {
56 throw new RuntimeException(
57 "distribution not initialized! values==null");
58 }
59 double value = this.stream.nextDouble();
60 double temp = 0.0;
61 if (value < this.values[0].getProbability())
62 {
63 return this.values[0].getValue();
64 }
65 for (int i = 0; i < this.values.length - 1; i++)
66 {
67 temp = temp + this.values[i].getProbability();
68 if (value >= temp
69 && value < temp + this.values[i + 1].getProbability())
70 {
71 return this.values[i].getValue();
72 }
73 }
74 return this.values[this.values.length - 1].getValue();
75 }
76
77 /***
78 * @see nl.tudelft.simulation.jstats.distributions.DistDiscrete#probability(int)
79 */
80 public double probability(final int observation)
81 {
82 for (int i = 0; i < this.values.length; i++)
83 {
84 if (observation == this.values[i].getValue())
85 {
86 return this.values[i].getProbability();
87 }
88 }
89 return 0;
90 }
91
92 /***
93 * A Entry for the custom distribution
94 */
95 public static class Entry
96 {
97 /*** the value */
98 private long value = 0L;
99
100 /*** the probability */
101 private double probability = 0.0;
102
103 /***
104 * constructs a new Entry
105 *
106 * @param value the value
107 * @param probability the probability
108 */
109 public Entry(final long value, final double probability)
110 {
111 this.value = value;
112 this.probability = probability;
113 }
114
115 /***
116 * returns the value
117 *
118 * @return long the value
119 */
120 public long getValue()
121 {
122 return this.value;
123 }
124
125 /***
126 * returns the probability
127 *
128 * @return double the probability
129 */
130 public double getProbability()
131 {
132 return this.probability;
133 }
134 }
135 }