SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MultinomialDist.java
1/*
2 * Class: MultinomialDist
3 * Description: multinomial 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
9 * @since
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.probdistmulti;
26
27import umontreal.ssj.util.Num;
28
44 protected int n;
45 protected double p[];
46
52 public MultinomialDist(int n, double p[]) {
53 setParams(n, p);
54 }
55
56 public double prob(int x[]) {
57 return prob_(n, p, x);
58 }
59
60 public double cdf(int x[]) {
61 return cdf_(n, p, x);
62 }
63
64 public double[] getMean() {
65 return getMean_(n, p);
66 }
67
68 public double[][] getCovariance() {
69 return getCovariance_(n, p);
70 }
71
72 public double[][] getCorrelation() {
73 return getCorrelation_(n, p);
74 }
75
76 private static void verifParam(int n, double p[]) {
77 if (n <= 0)
78 throw new IllegalArgumentException("n <= 0");
79
80 double sumPi = 0.0;
81 for (int i = 0; i < p.length; i++) {
82 if ((p[i] < 0) || (p[i] > 1))
83 throw new IllegalArgumentException("p is not a probability vector");
84 sumPi += p[i];
85 }
86
87 if (Math.abs(sumPi - 1.0) > 1.0e-15)
88 throw new IllegalArgumentException("p is not a probability vector");
89 }
90
91 private static double prob_(int n, double p[], int x[]) {
92 if (x.length != p.length)
93 throw new IllegalArgumentException("x and p must have the same dimension");
94
95 double sumXFact = 0.0;
96 int sumX = 0;
97 double sumPX = 0.0;
98
99 for (int i = 0; i < p.length; i++) {
100 sumX += x[i];
101 sumXFact += Num.lnFactorial(x[i]);
102 sumPX += (x[i] * Math.log(p[i]));
103 }
104
105 if (sumX != n)
106 return 0.0;
107 else {
108 return Math.exp(Num.lnFactorial(n) - sumXFact + sumPX);
109 }
110 }
111
118 public static double prob(int n, double p[], int x[]) {
119 verifParam(n, p);
120 return prob_(n, p, x);
121 }
122
123 private static double cdf_(int n, double p[], int x[]) {
124 boolean end = false;
125 double sum = 0.0;
126 int j;
127
128 if (x.length != p.length)
129 throw new IllegalArgumentException("x and p must have the same dimension");
130
131 int is[] = new int[x.length];
132 for (int i = 0; i < is.length; i++)
133 is[i] = 0;
134
135 sum = 0.0;
136 while (!end) {
137 sum += prob(n, p, is);
138 is[0]++;
139
140 if (is[0] > x[0]) {
141 is[0] = 0;
142 j = 1;
143 while (j < x.length && is[j] == x[j])
144 is[j++] = 0;
145
146 if (j == x.length)
147 end = true;
148 else
149 is[j]++;
150 }
151 }
152
153 return sum;
154 }
155
160 public static double cdf(int n, double p[], int x[]) {
161 verifParam(n, p);
162 return cdf_(n, p, x);
163 }
164
165 private static double[] getMean_(int n, double[] p) {
166 double mean[] = new double[p.length];
167
168 for (int i = 0; i < p.length; i++)
169 mean[i] = n * p[i];
170
171 return mean;
172 }
173
178 public static double[] getMean(int n, double[] p) {
179 verifParam(n, p);
180
181 return getMean_(n, p);
182 }
183
184 private static double[][] getCovariance_(int n, double[] p) {
185 double cov[][] = new double[p.length][p.length];
186
187 for (int i = 0; i < p.length; i++) {
188 for (int j = 0; j < p.length; j++)
189 cov[i][j] = -n * p[i] * p[j];
190
191 cov[i][i] = n * p[i] * (1.0 - p[i]);
192 }
193 return cov;
194 }
195
200 public static double[][] getCovariance(int n, double[] p) {
201 verifParam(n, p);
202 return getCovariance_(n, p);
203 }
204
205 private static double[][] getCorrelation_(int n, double[] p) {
206 double corr[][] = new double[p.length][p.length];
207
208 for (int i = 0; i < p.length; i++) {
209 for (int j = 0; j < p.length; j++)
210 corr[i][j] = -Math.sqrt(p[i] * p[j] / ((1.0 - p[i]) * (1.0 - p[j])));
211 corr[i][i] = 1.0;
212 }
213 return corr;
214 }
215
220 public static double[][] getCorrelation(int n, double[] p) {
221 verifParam(n, p);
222 return getCorrelation_(n, p);
223 }
224
240 public static double[] getMLE(int x[][], int m, int d, int n) {
241 double parameters[] = new double[d];
242 double xBar[] = new double[d];
243 double N = 0.0;
244
245 if (m <= 0)
246 throw new IllegalArgumentException("m <= 0");
247 if (d <= 0)
248 throw new IllegalArgumentException("d <= 0");
249
250 for (int i = 0; i < d; i++)
251 xBar[i] = 0;
252
253 for (int v = 0; v < m; v++)
254 for (int c = 0; c < d; c++)
255 xBar[c] += x[v][c];
256
257 for (int i = 0; i < d; i++) {
258 xBar[i] = xBar[i] / (double) n;
259 N += xBar[i];
260 }
261 if (N != (double) n)
262 throw new IllegalArgumentException("n is not correct");
263
264 for (int i = 0; i < d; i++)
265 parameters[i] = xBar[i] / (double) n;
266
267 return parameters;
268 }
269
273 public int getN() {
274 return n;
275 }
276
280 public double[] getP() {
281 return p;
282 }
283
287 public void setParams(int n, double p[]) {
288 double sumP = 0.0;
289
290 if (n <= 0)
291 throw new IllegalArgumentException("n <= 0");
292 if (p.length < 2)
293 throw new IllegalArgumentException("p.length < 2");
294
295 this.n = n;
296 this.dimension = p.length;
297 this.p = new double[dimension];
298 for (int i = 0; i < dimension; i++) {
299 if ((p[i] < 0) || (p[i] > 1))
300 throw new IllegalArgumentException("p is not a probability vector");
301
302 this.p[i] = p[i];
303 sumP += p[i];
304 }
305
306 if (Math.abs(sumP - 1.0) > 1.0e-15)
307 throw new IllegalArgumentException("p is not a probability vector");
308 }
309
310}
Classes implementing multi-dimensional discrete distributions over the integers should inherit from t...
double cdf(int x[])
Computes the cumulative probability function of the distribution evaluated at x, assuming the lowest...
double[][] getCorrelation()
Returns the correlation matrix of the distribution, defined as.
static double[] getMean(int n, double[] p)
Computes the mean of the multinomial distribution with parameters and ( ,…, ).
int getN()
Returns the parameter of this object.
static double[][] getCovariance(int n, double[] p)
Computes the covariance matrix of the multinomial distribution with parameters and ( ,...
static double[] getMLE(int x[][], int m, int d, int n)
Estimates and returns the parameters [ ,…, ] of the multinomial distribution using the maximum likeli...
double[][] getCovariance()
Returns the variance-covariance matrix of the distribution, defined as .
static double prob(int n, double p[], int x[])
Computes the probability mass function ( fMultinomial ) of the multinomial distribution with paramete...
double prob(int x[])
Returns the probability mass function , which should be a real number in .
static double cdf(int n, double p[], int x[])
Computes the function of the multinomial distribution with parameters and ( ,…, ) evaluated at .
double[] getP()
Returns the parameters ( ,…, ) of this object.
double[] getMean()
Returns the mean vector of the distribution, defined as .
void setParams(int n, double p[])
Sets the parameters and ( ,…, ) of this object.
MultinomialDist(int n, double p[])
Creates a MultinomialDist object with parameters and ( ,…, ) such that .
static double[][] getCorrelation(int n, double[] p)
Computes the correlation matrix of the multinomial distribution with parameters and ( ,...