SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
FoldedNormalDist.java
1/*
2 * Class: FoldedNormalDist
3 * Description: folded normal 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.*;
28import umontreal.ssj.functions.MathFunction;
29import optimization.*;
30
48 protected double mu;
49 protected double sigma;
50 private static final double RACPI = 1.7724538509055160273; // Sqrt[PI]
51
52 private static class FunctionInverse implements MathFunction {
53 private double u, mu, sigma;
54
55 public FunctionInverse(double mu, double sigma, double u) {
56 this.u = u;
57 this.mu = mu;
58 this.sigma = sigma;
59 }
60
61 public double evaluate(double x) {
62 return u - cdf(mu, sigma, x);
63 }
64 }
65
70 public FoldedNormalDist(double mu, double sigma) {
71 setParams(mu, sigma);
72 }
73
74 public double density(double x) {
75 return density(mu, sigma, x);
76 }
77
78 public double cdf(double x) {
79 return cdf(mu, sigma, x);
80 }
81
82 public double barF(double x) {
83 return barF(mu, sigma, x);
84 }
85
86 public double inverseF(double u) {
87 return inverseF(mu, sigma, u);
88 }
89
90 public double getMean() {
91 return FoldedNormalDist.getMean(mu, sigma);
92 }
93
94 public double getVariance() {
95 return FoldedNormalDist.getVariance(mu, sigma);
96 }
97
98 public double getStandardDeviation() {
99 return FoldedNormalDist.getStandardDeviation(mu, sigma);
100 }
101
110 public static double density(double mu, double sigma, double x) {
111 if (sigma <= 0.0)
112 throw new IllegalArgumentException("sigma <= 0");
113 if (mu < 0.0)
114 throw new IllegalArgumentException("mu < 0");
115 if (x < 0.0)
116 return 0.0;
117 return NormalDist.density(mu, sigma, x) + NormalDist.density(mu, sigma, -x);
118 }
119
128 public static double cdf(double mu, double sigma, double x) {
129 if (sigma <= 0.0)
130 throw new IllegalArgumentException("sigma <= 0");
131 if (mu < 0.0)
132 throw new IllegalArgumentException("mu < 0");
133 if (x <= 0.0)
134 return 0.0;
135 return NormalDist.cdf01((x - mu) / sigma) - NormalDist.cdf01((-x - mu) / sigma);
136 }
137
146 public static double barF(double mu, double sigma, double x) {
147 if (sigma <= 0.0)
148 throw new IllegalArgumentException("sigma <= 0");
149 if (mu < 0.0)
150 throw new IllegalArgumentException("mu < 0");
151 if (x <= 0.0)
152 return 1.0;
153 return NormalDist.barF01((x - mu) / sigma) - NormalDist.barF01((-x - mu) / sigma);
154 }
155
164 public static double inverseF(double mu, double sigma, double u) {
165 if (sigma <= 0.0)
166 throw new IllegalArgumentException("sigma <= 0");
167 if (mu < 0.0)
168 throw new IllegalArgumentException("mu < 0");
169 if (u > 1.0 || u < 0.0)
170 throw new IllegalArgumentException("u not in [0,1]");
171 if (u <= 0.0)
172 return 0.0;
173 if (u >= 1.0)
174 return Double.POSITIVE_INFINITY;
175
176 MathFunction f = new FunctionInverse(mu, sigma, u);
177 return RootFinder.brentDekker(0.0, mu + 10.0 * sigma, f, 1.0e-14);
178 }
179
190 public static double getMean(double mu, double sigma) {
191 if (sigma <= 0.0)
192 throw new IllegalArgumentException("sigma <= 0");
193 if (mu < 0.0)
194 throw new IllegalArgumentException("mu < 0");
195
196 return sigma * Num.RAC2 / RACPI * Math.exp(-mu * mu / (2.0 * sigma * sigma))
197 + mu * Num.erf(mu / (sigma * Num.RAC2));
198 }
199
208 public static double getVariance(double mu, double sigma) {
209 if (sigma <= 0.0)
210 throw new IllegalArgumentException("sigma <= 0");
211 if (mu < 0.0)
212 throw new IllegalArgumentException("mu < 0");
213 double mean = sigma * Num.RAC2 / RACPI * Math.exp(-mu * mu / (2.0 * sigma * sigma))
214 + mu * Num.erf(mu / (sigma * Num.RAC2));
215 return mu * mu + sigma * sigma - mean * mean;
216 }
217
226 public static double getStandardDeviation(double mu, double sigma) {
227 return Math.sqrt(FoldedNormalDist.getVariance(mu, sigma));
228 }
229
239 public static double[] getMLE(double[] x, int n) {
240 if (n <= 0)
241 throw new IllegalArgumentException("n <= 0");
242 throw new UnsupportedOperationException("getMLE is not implemented ");
243 }
244
250 public double getMu() {
251 return mu;
252 }
253
259 public double getSigma() {
260 return sigma;
261 }
262
269 public void setParams(double mu, double sigma) {
270 if (sigma <= 0.0)
271 throw new IllegalArgumentException("sigma <= 0");
272 if (mu < 0.0)
273 throw new IllegalArgumentException("mu < 0");
274 this.mu = mu;
275 this.sigma = sigma;
276 }
277
284 public double[] getParams() {
285 double[] retour = { mu, sigma };
286 return retour;
287 }
288
295 public String toString() {
296 return getClass().getSimpleName() + " : mu = " + mu + ", sigma = " + sigma;
297 }
298
299}
Classes implementing continuous distributions should inherit from this base class.
String toString()
Returns a String containing information about the current distribution.
static double getMean(double mu, double sigma)
Computes and returns the mean.
static double cdf(double mu, double sigma, double x)
Computes the distribution function.
double density(double x)
Returns , the density evaluated at .
static double[] getMLE(double[] x, int n)
NOT IMPLEMENTED.
static double getVariance(double mu, double sigma)
Computes and returns the variance.
double getStandardDeviation()
Returns the standard deviation.
static double density(double mu, double sigma, double x)
Computes the density function of the folded normal distribution.
double inverseF(double u)
Returns the inverse distribution function .
double cdf(double x)
Returns the distribution function .
double getSigma()
Returns the parameter of this object.
void setParams(double mu, double sigma)
Sets the parameters and for this object.
static double getStandardDeviation(double mu, double sigma)
Computes the standard deviation of the folded normal distribution with parameters and .
double[] getParams()
Return a table containing the parameters of the current distribution.
double getMu()
Returns the parameter of this object.
double barF(double x)
Returns the complementary distribution function.
double getVariance()
Returns the variance.
static double barF(double mu, double sigma, double x)
Computes the complementary distribution function.
static double inverseF(double mu, double sigma, double u)
Computes the inverse of the distribution function.
FoldedNormalDist(double mu, double sigma)
Constructs a FoldedNormalDist object with parameters mu and sigma.
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
double density(double x)
Returns , the density evaluated at .
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 RAC2
The value of .
Definition Num.java:138
static double erf(double x)
Returns the value of erf( ), the error function.
Definition Num.java:930
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.