SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
LognormalDist.java
1/*
2 * Class: LognormalDist
3 * Description: lognormal 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.*;
28
56 private double mu;
57 private double sigma;
58
63 public LognormalDist() {
64 setParams(0.0, 1.0);
65 }
66
71 public LognormalDist(double mu, double sigma) {
72 setParams(mu, sigma);
73 }
74
75 public double density(double x) {
76 return density(mu, sigma, x);
77 }
78
79 public double cdf(double x) {
80 return cdf(mu, sigma, x);
81 }
82
83 public double barF(double x) {
84 return barF(mu, sigma, x);
85 }
86
87 public double inverseF(double u) {
88 return inverseF(mu, sigma, u);
89 }
90
91 public double getMean() {
92 return LognormalDist.getMean(mu, sigma);
93 }
94
95 public double getVariance() {
96 return LognormalDist.getVariance(mu, sigma);
97 }
98
99 public double getStandardDeviation() {
100 return LognormalDist.getStandardDeviation(mu, sigma);
101 }
102
107 public static double density(double mu, double sigma, double x) {
108 if (sigma <= 0)
109 throw new IllegalArgumentException("sigma <= 0");
110 if (x <= 0)
111 return 0;
112 double diff = Math.log(x) - mu;
113 return Math.exp(-diff * diff / (2 * sigma * sigma)) / (Math.sqrt(2 * Math.PI) * sigma * x);
114 }
115
119 public static double cdf(double mu, double sigma, double x) {
120 if (sigma <= 0.0)
121 throw new IllegalArgumentException("sigma <= 0");
122 if (x <= 0.0)
123 return 0.0;
124 return NormalDist.cdf01((Math.log(x) - mu) / sigma);
125 }
126
132 public static double barF(double mu, double sigma, double x) {
133 if (sigma <= 0.0)
134 throw new IllegalArgumentException("sigma <= 0");
135 if (x <= 0.0)
136 return 1.0;
137 return NormalDist.barF01((Math.log(x) - mu) / sigma);
138 }
139
144 public static double inverseF(double mu, double sigma, double u) {
145 double t, v;
146
147 if (sigma <= 0.0)
148 throw new IllegalArgumentException("sigma <= 0");
149
150 if (u > 1.0 || u < 0.0)
151 throw new IllegalArgumentException("u not in [0,1]");
152
153 if (Num.DBL_EPSILON >= 1.0 - u)
154 return Double.POSITIVE_INFINITY;
155
156 if (u <= 0.0)
157 return 0.0;
158
159 t = NormalDist.inverseF01(u);
160 v = mu + sigma * t;
161
162 if ((t >= XBIG) || (v >= Num.DBL_MAX_EXP * Num.LN2))
163 return Double.POSITIVE_INFINITY;
164 if ((t <= -XBIG) || (v <= -Num.DBL_MAX_EXP * Num.LN2))
165 return 0.0;
166
167 return Math.exp(v);
168 }
169
186 public static double[] getMLE(double[] x, int n) {
187 if (n <= 0)
188 throw new IllegalArgumentException("n <= 0");
189
190 final double LN_EPS = Num.LN_DBL_MIN - Num.LN2;
191 double parameters[];
192 parameters = new double[2];
193 double sum = 0.0;
194 for (int i = 0; i < n; i++) {
195 if (x[i] > 0.0)
196 sum += Math.log(x[i]);
197 else
198 sum += LN_EPS; // log(DBL_MIN / 2)
199 }
200 parameters[0] = sum / n;
201
202 double temp;
203 sum = 0.0;
204 for (int i = 0; i < n; i++) {
205 if (x[i] > 0.0)
206 temp = Math.log(x[i]) - parameters[0];
207 else
208 temp = LN_EPS - parameters[0];
209 sum += temp * temp;
210 }
211 parameters[1] = Math.sqrt(sum / n);
212
213 return parameters;
214 }
215
225 public static LognormalDist getInstanceFromMLE(double[] x, int n) {
226 double parameters[] = getMLE(x, n);
227 return new LognormalDist(parameters[0], parameters[1]);
228 }
229
236 public static double getMean(double mu, double sigma) {
237 if (sigma <= 0.0)
238 throw new IllegalArgumentException("sigma <= 0");
239
240 return (Math.exp(mu + (sigma * sigma) / 2.0));
241 }
242
250 public static double getVariance(double mu, double sigma) {
251 if (sigma <= 0.0)
252 throw new IllegalArgumentException("sigma <= 0");
253
254 return (Math.exp(2.0 * mu + sigma * sigma) * (Math.exp(sigma * sigma) - 1.0));
255 }
256
263 public static double getStandardDeviation(double mu, double sigma) {
264 return Math.sqrt(LognormalDist.getVariance(mu, sigma));
265 }
266
270 public double getMu() {
271 return mu;
272 }
273
277 public double getSigma() {
278 return sigma;
279 }
280
284 public void setParams(double mu, double sigma) {
285 if (sigma <= 0)
286 throw new IllegalArgumentException("sigma <= 0");
287 this.mu = mu;
288 this.sigma = sigma;
289 supportA = 0.0;
290 }
291
296 public double[] getParams() {
297 double[] retour = { mu, sigma };
298 return retour;
299 }
300
304 public String toString() {
305 return getClass().getSimpleName() + " : mu = " + mu + ", sigma = " + sigma;
306 }
307
308}
Classes implementing continuous distributions should inherit from this base class.
double barF(double x)
Returns the complementary distribution function.
double density(double x)
Returns , the density evaluated at .
double cdf(double x)
Returns the distribution function .
double[] getParams()
Returns a table containing the parameters of the current distribution, in the order: [ ,...
static double inverseF(double mu, double sigma, double u)
Computes the inverse of the lognormal distribution function, using NormalDist.inverseF01.
double getStandardDeviation()
Returns the standard deviation.
static double getMean(double mu, double sigma)
Computes and returns the mean of the lognormal distribution with parameters and.
static double barF(double mu, double sigma, double x)
Computes the lognormal complementary distribution function.
static double[] getMLE(double[] x, int n)
Estimates the parameters of the lognormal distribution using the maximum likelihood method,...
LognormalDist(double mu, double sigma)
Constructs a LognormalDist object with parameters = mu and = sigma.
static double getVariance(double mu, double sigma)
Computes and returns the variance of the lognormal distribution with parameters and .
static double cdf(double mu, double sigma, double x)
Computes the lognormal distribution function, using NormalDist.cdf01.
void setParams(double mu, double sigma)
Sets the parameters and of this object.
double getMu()
Returns the parameter of this object.
double getMean()
Returns the mean.
double inverseF(double u)
Returns the inverse distribution function .
static LognormalDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a lognormal distribution with parameters.
double getSigma()
Returns the parameter of this object.
LognormalDist()
Constructs a LognormalDist object with default parameters and .
static double density(double mu, double sigma, double x)
Computes the lognormal density function in ( flognormal ).
double getVariance()
Returns the variance.
static double getStandardDeviation(double mu, double sigma)
Computes and returns the standard deviation of the lognormal distribution with parameters and .
String toString()
Returns a String containing information about the current distribution.
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
static double inverseF01(double u)
Same as inverseF(0, 1, u).
static double cdf01(double x)
Same as cdf(0, 1, x).
static double barF01(double x)
Same as barF(0, 1, x).
This class provides various constants and methods to compute numerical quantities such as factorials,...
Definition Num.java:35
static final double LN_DBL_MIN
Natural logarithm of DBL_MIN.
Definition Num.java:118
static final double DBL_EPSILON
Difference between 1.0 and the smallest double greater than 1.0.
Definition Num.java:90
static final int DBL_MAX_EXP
Largest int such that is representable (approximately) as a double.
Definition Num.java:96
static final double LN2
The values of .
Definition Num.java:148