SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
LaplaceDist.java
1/*
2 * Class: LaplaceDist
3 * Description: Laplace 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;
28
47public class LaplaceDist extends ContinuousDistribution {
48 private double mu;
49 private double beta;
50
55 public LaplaceDist() {
56 mu = 0;
57 beta = 1;
58 }
59
64 public LaplaceDist(double mu, double beta) {
65 if (beta <= 0.0)
66 throw new IllegalArgumentException("beta <= 0");
67
68 this.mu = mu;
69 this.beta = beta;
70 }
71
72 public double density(double x) {
73 return density(mu, beta, x);
74 }
75
76 public double cdf(double x) {
77 return cdf(mu, beta, x);
78 }
79
80 public double barF(double x) {
81 return barF(mu, beta, x);
82 }
83
84 public double inverseF(double u) {
85 return inverseF(mu, beta, u);
86 }
87
88 public double getMean() {
89 return LaplaceDist.getMean(mu, beta);
90 }
91
92 public double getVariance() {
93 return LaplaceDist.getVariance(mu, beta);
94 }
95
96 public double getStandardDeviation() {
97 return LaplaceDist.getStandardDeviation(mu, beta);
98 }
99
103 public static double density(double mu, double beta, double x) {
104 if (beta <= 0.0)
105 throw new IllegalArgumentException("beta <= 0");
106 return Math.exp(-Math.abs(x - mu) / beta) / (2.0 * beta);
107 }
108
112 public static double cdf(double mu, double beta, double x) {
113 if (x <= mu)
114 return Math.exp((x - mu) / beta) / 2.0;
115 else
116 return 1.0 - Math.exp((mu - x) / beta) / 2.0;
117 }
118
122 public static double barF(double mu, double beta, double x) {
123 if (x <= mu)
124 return 1.0 - Math.exp((x - mu) / beta) / 2.0;
125 else
126 return Math.exp((mu - x) / beta) / 2.0;
127 }
128
129 // ====================================================
130 // code taken and adapted from unuran
131 // file /distribution/c_laplca_gen.c
132 // ====================================================
133
137 public static double inverseF(double mu, double beta, double u) {
138 // transform to random variate
139 if (u < 0.0 || u > 1.0)
140 throw new IllegalArgumentException("u should be in [0,1]");
141 if (u <= 0.0)
142 return Double.NEGATIVE_INFINITY;
143 if (u >= 1.0)
144 return Double.POSITIVE_INFINITY;
145
146 double x = (u > 0.5) ? -Math.log(2. - 2 * u) : Math.log(2 * u);
147 return mu + beta * x;
148 }
149
166 public static double[] getMLE(double[] x, int n) {
167 if (n <= 0)
168 throw new IllegalArgumentException("n <= 0");
169
170 double parameters[];
171 parameters = new double[2];
172
173 parameters[0] = EmpiricalDist.getMedian(x, n);
174
175 double sum = 0.0;
176 for (int i = 0; i < n; i++)
177 sum += Math.abs(x[i] - parameters[0]);
178 parameters[1] = sum / (double) n;
179
180 return parameters;
181 }
182
191 public static LaplaceDist getInstanceFromMLE(double[] x, int n) {
192 double parameters[] = getMLE(x, n);
193 return new LaplaceDist(parameters[0], parameters[1]);
194 }
195
202 public static double getMean(double mu, double beta) {
203 if (beta <= 0.0)
204 throw new IllegalArgumentException("beta <= 0");
205
206 return mu;
207 }
208
216 public static double getVariance(double mu, double beta) {
217 if (beta <= 0.0)
218 throw new IllegalArgumentException("beta <= 0");
219
220 return (2.0 * beta * beta);
221 }
222
229 public static double getStandardDeviation(double mu, double beta) {
230 if (beta <= 0.0)
231 throw new IllegalArgumentException("beta <= 0");
232
233 return (Num.RAC2 * beta);
234 }
235
239 public double getMu() {
240 return mu;
241 }
242
246 public double getBeta() {
247 return beta;
248 }
249
254 public double[] getParams() {
255 double[] retour = { mu, beta };
256 return retour;
257 }
258
262 public String toString() {
263 return getClass().getSimpleName() + " : mu = " + mu + ", beta = " + beta;
264 }
265
266}
Classes implementing continuous distributions should inherit from this base class.
Extends DiscreteDistribution to an empirical distribution function, based on the observations (sorte...
double getMedian()
Returns the median.
static LaplaceDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a Laplace distribution with parameters.
static double getVariance(double mu, double beta)
Computes and returns the variance of the Laplace distribution with parameters and.
static double getStandardDeviation(double mu, double beta)
Computes and returns the standard deviation of the Laplace distribution with parameters and .
static double density(double mu, double beta, double x)
Computes the Laplace density function.
double getMu()
Returns the parameter .
double getBeta()
Returns the parameter .
LaplaceDist(double mu, double beta)
Constructs a LaplaceDist object with parameters = mu and = beta.
double[] getParams()
Return a table containing the parameters of the current distribution.
double getVariance()
Returns the variance.
LaplaceDist()
Constructs a LaplaceDist object with default parameters and .
static double cdf(double mu, double beta, double x)
Computes the Laplace distribution function.
double barF(double x)
Returns the complementary distribution function.
static double getMean(double mu, double beta)
Computes and returns the mean of the Laplace distribution with parameters and .
double getMean()
Returns the mean.
static double inverseF(double mu, double beta, double u)
Computes the inverse Laplace distribution function.
String toString()
Returns a String containing information about the current distribution.
double inverseF(double u)
Returns the inverse distribution function .
double cdf(double x)
Returns the distribution function .
static double barF(double mu, double beta, double x)
Computes the Laplace complementary distribution function.
static double[] getMLE(double[] x, int n)
Estimates the parameters of the Laplace distribution using the maximum likelihood method,...
double getStandardDeviation()
Returns the standard deviation.
double density(double x)
Returns , the density evaluated at .
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