SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BernoulliDist.java
1/*
2 * Class: BernoulliDist
3 * Description: Bernoulli distribution
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author Richard Simard
9 * @since August 2010
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.probdist;
26
44 private double p;
45 private double q;
46
50 public BernoulliDist(double p) {
51 if (p < 0.0 || p > 1.0)
52 throw new IllegalArgumentException("p not in [0,1]");
53 this.p = p;
54 q = 1 - p;
55 supportA = 0;
56 supportB = 1;
57 }
58
59 public double prob(int x) {
60 if (1 == x)
61 return p;
62 if (0 == x)
63 return q;
64 return 0.0;
65 }
66
67 public double cdf(int x) {
68 if (x < 0)
69 return 0.0;
70 if (x < 1)
71 return q;
72 return 1.0;
73 }
74
75 public double barF(int x) {
76 if (x > 1)
77 return 0.0;
78 if (x > 0)
79 return p;
80 return 1.0;
81 }
82
83 public int inverseFInt(double u) {
84 if (u < 0.0 || u > 1.0)
85 throw new IllegalArgumentException("u not in [0,1]");
86 if (u > q)
87 return 1;
88 return 0;
89 }
90
91 public double getMean() {
92 return BernoulliDist.getMean(p);
93 }
94
95 public double getVariance() {
96 return BernoulliDist.getVariance(p);
97 }
98
99 public double getStandardDeviation() {
100 return BernoulliDist.getStandardDeviation(p);
101 }
102
107 public static double prob(double p, int x) {
108 if (p < 0.0 || p > 1.0)
109 throw new IllegalArgumentException("p not in [0,1]");
110 if (1 == x)
111 return p;
112 if (0 == x)
113 return 1.0 - p;
114 return 0.0;
115 }
116
122 public static double cdf(double p, int x) {
123 if (p < 0.0 | p > 1.0)
124 throw new IllegalArgumentException("p not in [0,1]");
125 if (x < 0)
126 return 0.0;
127 if (x < 1)
128 return 1.0 - p;
129 return 1.0;
130 }
131
137 public static double barF(double p, int x) {
138 if (p < 0.0 | p > 1.0)
139 throw new IllegalArgumentException("p not in [0,1]");
140 if (x > 1)
141 return 0.0;
142 if (x > 0)
143 return p;
144 return 1.0;
145 }
146
151 public static int inverseF(double p, double u) {
152 if (p < 0.0 | p > 1.0)
153 throw new IllegalArgumentException("p not in [0,1]");
154 if (u < 0.0 || u > 1.0)
155 throw new IllegalArgumentException("u not in [0,1]");
156 if (u > 1.0 - p)
157 return 1;
158 return 0;
159 }
160
171 public static double[] getMLE(int[] x, int m) {
172 if (m < 2)
173 throw new UnsupportedOperationException(" m < 2");
174
175 // compute the empirical mean
176 double sum = 0.0;
177 for (int i = 0; i < m; i++)
178 sum += x[i];
179 sum /= (double) m;
180
181 double param[] = new double[1];
182 param[0] = sum;
183 return param;
184 }
185
194 public static BernoulliDist getInstanceFromMLE(int[] x, int m) {
195 double param[] = new double[1];
196 param = getMLE(x, m);
197 return new BernoulliDist(param[0]);
198 }
199
206 public static double getMean(double p) {
207 if (p < 0.0 || p > 1.0)
208 throw new IllegalArgumentException("p not in [0, 1]");
209
210 return (p);
211 }
212
219 public static double getVariance(double p) {
220 if (p < 0.0 || p > 1.0)
221 throw new IllegalArgumentException("p not in [0, 1]");
222 return (p * (1.0 - p));
223 }
224
231 public static double getStandardDeviation(double p) {
232 return Math.sqrt(BernoulliDist.getVariance(p));
233 }
234
238 public double getP() {
239 return p;
240 }
241
246 public double[] getParams() {
247 double[] retour = { p };
248 return retour;
249 }
250
254 public void setParams(double p) {
255 this.p = p;
256 q = 1 - p;
257 }
258
262 public String toString() {
263 return getClass().getSimpleName() + " : p = " + p;
264 }
265
266}
static double cdf(double p, int x)
Returns the Bernoulli distribution function with parameter (see eq.
static double prob(double p, int x)
Returns the Bernoulli probability with parameter (see eq.
static double[] getMLE(int[] x, int m)
Estimates the parameters of the Bernoulli distribution using the maximum likelihood method,...
int inverseFInt(double u)
Returns the inverse distribution function , where.
static double barF(double p, int x)
Returns the complementary Bernoulli distribution function.
void setParams(double p)
Resets the parameter to this new value.
String toString()
Returns a String containing information about the current distribution.
double getMean()
Returns the mean of the distribution function.
static double getMean(double p)
Returns the mean of the Bernoulli distribution with parameter .
static BernoulliDist getInstanceFromMLE(int[] x, int m)
Creates a new instance of a Bernoulli distribution with parameter.
static double getVariance(double p)
Computes the variance of the Bernoulli distribution with parameter .
static double getStandardDeviation(double p)
Computes the standard deviation of the Bernoulli distribution with parameter .
static int inverseF(double p, double u)
Returns the inverse of the Bernoulli distribution function with parameter at .
double getStandardDeviation()
Returns the standard deviation of the distribution function.
double getP()
Returns the parameter of this object.
double[] getParams()
Returns an array that contains the parameter of the current distribution: [ ].
BernoulliDist(double p)
Creates a Bernoulli distribution object.
double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
double prob(int x)
Returns , the probability of .
double getVariance()
Returns the variance of the distribution function.
double barF(int x)
Returns , the complementary distribution function.
Classes implementing discrete distributions over the integers should inherit from this class.