SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
HalfNormalDist.java
1/*
2 * Class: HalfNormalDist
3 * Description: half-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
44 protected double mu;
45 protected double sigma;
46 protected double C1;
47
52 public HalfNormalDist(double mu, double sigma) {
53 setParams(mu, sigma);
54 }
55
56 public double density(double x) {
57 final double z = (x - mu) / sigma;
58 if (z < 0.0)
59 return 0.0;
60 return C1 * Math.exp(-z * z / 2.0);
61 }
62
63 public double cdf(double x) {
64 return cdf(mu, sigma, x);
65 }
66
67 public double barF(double x) {
68 return barF(mu, sigma, x);
69 }
70
71 public double inverseF(double u) {
72 return inverseF(mu, sigma, u);
73 }
74
75 public double getMean() {
76 return HalfNormalDist.getMean(mu, sigma);
77 }
78
79 public double getVariance() {
80 return HalfNormalDist.getVariance(mu, sigma);
81 }
82
83 public double getStandardDeviation() {
84 return HalfNormalDist.getStandardDeviation(mu, sigma);
85 }
86
95 public static double density(double mu, double sigma, double x) {
96 if (sigma <= 0.0)
97 throw new IllegalArgumentException("sigma <= 0");
98 final double Z = (x - mu) / sigma;
99 if (Z < 0.0)
100 return 0.0;
101 return Math.sqrt(2.0 / Math.PI) / sigma * Math.exp(-Z * Z / 2.0);
102 }
103
112 public static double cdf(double mu, double sigma, double x) {
113 if (sigma <= 0.0)
114 throw new IllegalArgumentException("sigma <= 0");
115 final double Z = (x - mu) / sigma;
116 if (Z <= 0.0)
117 return 0.0;
118 return Num.erf(Z / Num.RAC2);
119 }
120
129 public static double barF(double mu, double sigma, double x) {
130 if (sigma <= 0.0)
131 throw new IllegalArgumentException("sigma <= 0");
132 final double Z = (x - mu) / sigma;
133 if (Z <= 0.0)
134 return 1.0;
135 return Num.erfc(Z / Num.RAC2);
136 }
137
146 public static double inverseF(double mu, double sigma, double u) {
147 if (sigma <= 0.0)
148 throw new IllegalArgumentException("sigma <= 0");
149 if (u > 1.0 || u < 0.0)
150 throw new IllegalArgumentException("u not in [0,1]");
151 if (u <= 0.0)
152 return mu;
153 if (u >= 1.0)
154 return Double.POSITIVE_INFINITY;
155
156 final double Z = Num.RAC2 * Num.erfInv(u);
157 return mu + sigma * Z;
158 }
159
175 public static double[] getMLE(double[] x, int n) {
176 if (n <= 0)
177 throw new IllegalArgumentException("n <= 0");
178
179 double mu = Double.MAX_VALUE;
180 for (int i = 0; i < n; ++i)
181 if (x[i] < mu)
182 mu = x[i];
183
184 double sigma = 0.0;
185 for (int i = 0; i < n; ++i)
186 sigma += (x[i] - mu) * (x[i] - mu);
187
188 double[] parametres = new double[2];
189 parametres[0] = mu;
190 parametres[1] = Math.sqrt(sigma / n);
191 return parametres;
192 }
193
209 public static double[] getMLE(double[] x, int n, double mu) {
210 if (n <= 0)
211 throw new IllegalArgumentException("n <= 0");
212
213 double sigma = 0.0;
214 for (int i = 0; i < n; ++i)
215 sigma += (x[i] - mu) * (x[i] - mu);
216
217 double[] parametres = new double[1];
218 parametres[0] = Math.sqrt(sigma / n);
219 return parametres;
220 }
221
229 public static double getMean(double mu, double sigma) {
230 if (sigma <= 0.0)
231 throw new IllegalArgumentException("sigma <= 0");
232 return mu + sigma * Math.sqrt(2.0 / Math.PI);
233 }
234
243 public static double getVariance(double mu, double sigma) {
244 if (sigma <= 0.0)
245 throw new IllegalArgumentException("sigma <= 0");
246 return (1.0 - 2.0 / Math.PI) * sigma * sigma;
247 }
248
257 public static double getStandardDeviation(double mu, double sigma) {
258 return Math.sqrt(HalfNormalDist.getVariance(mu, sigma));
259 }
260
266 public double getMu() {
267 return mu;
268 }
269
275 public double getSigma() {
276 return sigma;
277 }
278
285 public void setParams(double mu, double sigma) {
286 if (sigma <= 0.0)
287 throw new IllegalArgumentException("sigma <= 0");
288 this.mu = mu;
289 this.sigma = sigma;
290 C1 = Math.sqrt(2.0 / Math.PI) / sigma;
291 }
292
299 public double[] getParams() {
300 double[] retour = { mu, sigma };
301 return retour;
302 }
303
310 public String toString() {
311 return getClass().getSimpleName() + " : mu = " + mu + ", sigma = " + sigma;
312 }
313
314}
Classes implementing continuous distributions should inherit from this base class.
static double inverseF(double mu, double sigma, double u)
Computes the inverse of the distribution function.
static double getStandardDeviation(double mu, double sigma)
Computes the standard deviation of the half-normal distribution with parameters and .
double inverseF(double u)
Returns the inverse distribution function .
static double density(double mu, double sigma, double x)
Computes the density function of the half-normal distribution.
static double getMean(double mu, double sigma)
Computes and returns the mean .
double getStandardDeviation()
Returns the standard deviation.
static double barF(double mu, double sigma, double x)
Computes the complementary distribution function.
HalfNormalDist(double mu, double sigma)
Constructs a HalfNormalDist object with parameters mu and sigma.
double getVariance()
Returns the variance.
double getMu()
Returns the parameter of this object.
double barF(double x)
Returns the complementary distribution function.
double cdf(double x)
Returns the distribution function .
static double[] getMLE(double[] x, int n, double mu)
Estimates the parameter of the half-normal distribution using the maximum likelihood method from the...
static double getVariance(double mu, double sigma)
Computes and returns the variance .
void setParams(double mu, double sigma)
Sets the parameters and .
double[] getParams()
Return a table containing the parameters of the current distribution.
String toString()
Returns a String containing information about the current distribution.
double density(double x)
Returns , the density evaluated at .
static double cdf(double mu, double sigma, double x)
Computes the distribution function.
static double[] getMLE(double[] x, int n)
Estimates the parameters and of the half-normal distribution using the maximum likelihood method fr...
double getSigma()
Returns the parameter of this object.
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 erfInv(double u)
Returns the value of erf , the inverse of the error function.
Definition Num.java:998
static double erf(double x)
Returns the value of erf( ), the error function.
Definition Num.java:930
static double erfc(double x)
Returns the value of erfc( ), the complementary error function.
Definition Num.java:953