SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
LogarithmicDist.java
1/*
2 * Class: LogarithmicDist
3 * Description: logarithmic 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.Num;
28import umontreal.ssj.util.RootFinder;
29import umontreal.ssj.functions.MathFunction;
30
47
48 private double theta;
49 private double t;
50
51 private static class Function implements MathFunction {
52 protected double mean;
53
54 public Function(double mean) {
55 this.mean = mean;
56 }
57
58 public double evaluate(double x) {
59 if (x <= 0.0 || x >= 1.0)
60 return 1.0e200;
61 return (x + mean * (1.0 - x) * Math.log1p(-x));
62 }
63 }
64
68 public LogarithmicDist(double theta) {
69 setTheta(theta);
70 }
71
72 public double prob(int x) {
73 if (x < 1)
74 return 0;
75 return t * Math.pow(theta, x) / x;
76 }
77
78 public double cdf(int x) {
79 if (x < 1)
80 return 0;
81 double res = prob(1);
82 double term = res;
83 for (int i = 2; i <= x; i++) {
84 term *= theta;
85 res += term / i;
86 }
87 return res;
88 }
89
90 public double barF(int x) {
91 if (x <= 1)
92 return 1.0;
93 double res = prob(x);
94 double term = res;
95 int i = x + 1;
96 while (term > EPSILON) {
97 term *= theta * (i - 1) / i;
98 res += term;
99 }
100 return res;
101 }
102
103 public int inverseFInt(double u) {
104 return inverseF(theta, u);
105 }
106
107 public double getMean() {
108 return LogarithmicDist.getMean(theta);
109 }
110
111 public double getVariance() {
112 return LogarithmicDist.getVariance(theta);
113 }
114
115 public double getStandardDeviation() {
116 return LogarithmicDist.getStandardDeviation(theta);
117 }
118
123 public static double prob(double theta, int x) {
124 if (theta <= 0 || theta >= 1)
125 throw new IllegalArgumentException("theta not in range (0,1)");
126 if (x < 1)
127 return 0;
128 return -1.0 / Math.log1p(-theta) * Math.pow(theta, x) / x;
129 }
130
134 public static double cdf(double theta, int x) {
135 if (theta <= 0 || theta >= 1)
136 throw new IllegalArgumentException("theta not in range (0,1)");
137 if (x < 1)
138 return 0;
139 double res = prob(theta, 1);
140 double term = res;
141 for (int i = 2; i <= x; i++) {
142 term *= theta;
143 res += term / i;
144 }
145 return res;
146 }
147
153 public static double barF(double theta, int x) {
154 if (theta <= 0 || theta >= 1)
155 throw new IllegalArgumentException("theta not in range (0,1)");
156 if (x <= 1)
157 return 1.0;
158 double res = prob(theta, x);
159 double term = res;
160 int i = x + 1;
161 while (term > EPSILON) {
162 term *= theta * (i - 1) / i;
163 res += term;
164 }
165 return res;
166 }
167
168 public static int inverseF(double theta, double u) {
169 throw new UnsupportedOperationException();
170 }
171
187 public static double[] getMLE(int[] x, int n) {
188 if (n <= 0)
189 throw new IllegalArgumentException("n <= 0");
190
191 double parameters[];
192 parameters = new double[1];
193 double sum = 0.0;
194 for (int i = 0; i < n; i++) {
195 sum += x[i];
196 }
197
198 double mean = (double) sum / (double) n;
199
200 Function f = new Function(mean);
201 parameters[0] = RootFinder.brentDekker(1e-15, 1.0 - 1e-15, f, 1e-7);
202
203 return parameters;
204 }
205
214 public static LogarithmicDist getInstanceFromMLE(int[] x, int n) {
215 double parameters[] = getMLE(x, n);
216 return new LogarithmicDist(parameters[0]);
217 }
218
227 public static double getMean(double theta) {
228 if (theta <= 0.0 || theta >= 1.0)
229 throw new IllegalArgumentException("theta not in range (0,1)");
230
231 return ((-1 / Math.log1p(-theta)) * (theta / (1 - theta)));
232 }
233
243 public static double getVariance(double theta) {
244 if (theta <= 0.0 || theta >= 1.0)
245 throw new IllegalArgumentException("theta not in range (0,1)");
246
247 double v = Math.log1p(-theta);
248 return ((-theta * (theta + v)) / ((1 - theta) * (1 - theta) * v * v));
249 }
250
257 public static double getStandardDeviation(double theta) {
258 return Math.sqrt(LogarithmicDist.getVariance(theta));
259 }
260
264 public double getTheta() {
265 return theta;
266 }
267
271 public void setTheta(double theta) {
272 if (theta <= 0 || theta >= 1)
273 throw new IllegalArgumentException("theta not in range (0,1)");
274 this.theta = theta;
275 t = -1.0 / Math.log1p(-theta);
276 supportA = 1;
277 }
278
282 public double[] getParams() {
283 double[] retour = { theta };
284 return retour;
285 }
286
290 public String toString() {
291 return getClass().getSimpleName() + " : theta = " + theta;
292 }
293
294}
Classes implementing discrete distributions over the integers should inherit from this class.
static double EPSILON
Environment variable that determines what probability terms can be considered as negligible when buil...
double prob(int x)
Returns , the probability of .
double getMean()
Returns the mean of the distribution function.
double getVariance()
Returns the variance of the distribution function.
int inverseFInt(double u)
Returns the inverse distribution function , where.
static double[] getMLE(int[] x, int n)
Estimates the parameter of the logarithmic distribution using the maximum likelihood method,...
static double cdf(double theta, int x)
Computes the distribution function .
static LogarithmicDist getInstanceFromMLE(int[] x, int n)
Creates a new instance of a logarithmic distribution with parameter.
static double getVariance(double theta)
Computes and returns the variance.
static double prob(double theta, int x)
Computes the logarithmic probability given in ( flogar ) .
void setTheta(double theta)
Sets the associated with this object.
double getTheta()
Returns the associated with this object.
String toString()
Returns a String containing information about the current distribution.
double[] getParams()
Return a table containing the parameters of the current distribution.
double getStandardDeviation()
Returns the standard deviation of the distribution function.
static double getMean(double theta)
Computes and returns the mean.
double barF(int x)
Returns , the complementary distribution function.
LogarithmicDist(double theta)
Constructs a logarithmic distribution with parameter theta.
static double getStandardDeviation(double theta)
Computes and returns the standard deviation of the logarithmic distribution with parameter theta.
static double barF(double theta, int x)
Computes the complementary distribution function.
double cdf(int x)
Returns the distribution function evaluated at (see ( FDistDisc )).
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.