SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
PascalDist.java
1/*
2 * Class: PascalDist
3 * Description: Pascal 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.probdist;
26
27import umontreal.ssj.util.RootFinder;
28import umontreal.ssj.functions.MathFunction;
29
48public class PascalDist extends NegativeBinomialDist {
49 private static final double EPSI = 1.0E-10;
50
51 private static class Function implements MathFunction {
52 protected int m;
53 protected int max;
54 protected double mean;
55 protected int[] Fj;
56
57 public Function(int m, int max, double mean, int[] Fj) {
58 this.m = m;
59 this.max = max;
60 this.mean = mean;
61 this.Fj = new int[Fj.length];
62 System.arraycopy(Fj, 0, this.Fj, 0, Fj.length);
63 }
64
65 public double evaluate(double p) {
66 double sum = 0.0;
67 double s = (p * mean) / (1.0 - p);
68
69 for (int j = 0; j < max; j++)
70 sum += Fj[j] / (s + (double) j);
71
72 return sum + m * Math.log(p);
73 }
74
75 public double evaluateN(int n, double p) {
76 double sum = 0.0;
77
78 for (int j = 0; j < max; j++)
79 sum += Fj[j] / (n + j);
80
81 return sum + m * Math.log(p);
82 }
83 }
84
91 public PascalDist(int n, double p) {
92 setParams(n, p);
93 }
94
112 public static double[] getMLE(int[] x, int m) {
113 if (m <= 0)
114 throw new IllegalArgumentException("m <= 0");
115
116 double sum = 0.0;
117 int max = Integer.MIN_VALUE;
118 for (int i = 0; i < m; i++) {
119 sum += x[i];
120 if (x[i] > max)
121 max = x[i];
122 }
123 double mean = (double) sum / (double) m;
124
125 double var = 0.0;
126 for (int i = 0; i < m; i++)
127 var += (x[i] - mean) * (x[i] - mean);
128 var /= (double) m;
129
130 if (mean >= var)
131 throw new UnsupportedOperationException("mean >= variance");
132
133 int[] Fj = new int[max];
134 for (int j = 0; j < max; j++) {
135 int prop = 0;
136 for (int i = 0; i < m; i++)
137 if (x[i] > j)
138 prop++;
139
140 Fj[j] = prop;
141 }
142
143 double[] parameters = new double[2];
144 Function f = new Function(m, max, mean, Fj);
145
146 parameters[1] = RootFinder.brentDekker(EPSI, 1 - EPSI, f, 1e-5);
147 if (parameters[1] >= 1.0)
148 parameters[1] = 1.0 - 1e-15;
149
150 parameters[0] = Math.round((parameters[1] * mean) / (1.0 - parameters[1]));
151 if (parameters[0] == 0)
152 parameters[0] = 1;
153
154 return parameters;
155 }
156
165 public static PascalDist getInstanceFromMLE(int[] x, int m) {
166 double parameters[] = getMLE(x, m);
167 return new PascalDist((int) parameters[0], parameters[1]);
168 }
169
173 public int getN1() {
174 return (int) (n + 0.5);
175 }
176
180 public void setParams(int n, double p) {
181 super.setParams((double) n, p);
182 }
183
187 public String toString() {
188 return getClass().getSimpleName() + " : n = " + getN1() + ", p = " + getP();
189 }
190
191}
double getP()
Returns the parameter of this object.
String toString()
Returns a String containing information about the current distribution.
void setParams(int n, double p)
Sets the parameter and of this object.
static double[] getMLE(int[] x, int m)
Estimates the parameter of the Pascal distribution using the maximum likelihood method,...
PascalDist(int n, double p)
Creates an object that contains the probability terms ( fmass-pascal ) and the distribution function ...
static PascalDist getInstanceFromMLE(int[] x, int m)
Creates a new instance of a Pascal distribution with parameters.
int getN1()
Returns the parameter of this object.
This class provides numerical methods to solve non-linear equations.
static double brentDekker(double a, double b, MathFunction f, double tol)
Computes a root of the function in f using the Brent-Dekker method.
This interface should be implemented by classes which represent univariate mathematical functions.