/* *
* Title:
* Description:
* Copyright: Copyright (c)
* Company:
* @author
* @version 1.0
*/
package umontreal.iro.lecuyer.probdist;
import umontreal.iro.lecuyer.util.Num;
/**
* Extends the class {@link ContinuousDistribution} for
* the Laplace distribution.
* It has location parameter μ and scale parameter β > 0.
* The density function is given by
*
*
*
* f (x) = e-| x-μ|/β/(2β) for - ∞ < x < ∞.
*
* The distribution function is
*
*
*
*
* F(x) =
* (1/2)e(x-μ)/β
* if x <= μ,
*
* F(x) =
* 1 - (1/2)e(μ-x)/β
* otherwise,
*
*
*
* and its inverse is
*
*
*
*
* F-1(u) =
* βlog(2u) + μ
* if 0 <= u <= 1/2,
*
* F-1(u) =
* μ - βlog(2(1 - u))
* otherwise.
*
*
*
*
*/
public class LaplaceDist extends ContinuousDistribution {
private double mu;
private double beta;
/**
* Constructs a LaplaceDist object with default
* parameters μ = 0 and β = 1.
*
*/
public LaplaceDist() {
mu = 0;
beta = 1;
}
/**
* Constructs a LaplaceDist object with parameters
* μ = mu and β = beta.
*
*/
public LaplaceDist (double mu, double beta) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
this.mu = mu;
this.beta = beta;
}
public double density (double x) {
return density (mu, beta, x);
}
public double cdf (double x) {
return cdf (mu, beta, x);
}
public double barF (double x) {
return barF (mu, beta, x);
}
public double inverseF (double u) {
return inverseF (mu, beta, u);
}
public double getMean() {
return LaplaceDist.getMean (mu, beta);
}
public double getVariance() {
return LaplaceDist.getVariance (mu, beta);
}
public double getStandardDeviation() {
return LaplaceDist.getStandardDeviation (mu, beta);
}
/**
* Computes the Laplace density function.
*
*/
public static double density (double mu, double beta, double x) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
return Math.exp (-Math.abs (x - mu)/beta)/(2.0*beta);
}
/**
* Computes the Laplace distribution function.
*
*/
public static double cdf (double mu, double beta, double x) {
if (x <= mu)
return Math.exp ((x - mu)/beta)/2.0;
else
return 1.0 - Math.exp ((mu - x)/beta)/2.0;
}
/**
* Computes the Laplace complementary distribution function.
*
*/
public static double barF (double mu, double beta, double x) {
if (x <= mu)
return 1.0 - Math.exp ((x - mu)/beta)/2.0;
else
return Math.exp ((mu - x)/beta)/2.0;
}
//====================================================
// code taken and adapted from unuran
// file /distribution/c_laplca_gen.c
//====================================================
/**
* Computes the inverse Laplace distribution function.
*
*/
public static double inverseF (double mu, double beta, double u) {
// transform to random variate
if (u < 0.0 || u > 1.0)
throw new IllegalArgumentException ("u should be in [0,1]");
if (u <= 0.0)
return Double.NEGATIVE_INFINITY;
if (u >= 1.0)
return Double.POSITIVE_INFINITY;
double x = (u>0.5) ? -Math.log(2.-2*u) : Math.log(2*u);
return mu + beta*x;
}
/**
* Estimates the parameters
* (μ, β) of the Laplace distribution
* using the maximum likelihood method, from the n observations
* x[i],
* i = 0, 1,…, n - 1. The estimates are returned in a two-element
* array, in regular order: [μ, β].
*
* @param x the list of observations used to evaluate parameters
*
* @param n the number of observations used to evaluate parameters
*
* @return returns the parameters [hat(μ),
* hat(β)]
*
*/
public static double[] getMLE (double[] x, int n) {
if (n <= 0)
throw new IllegalArgumentException ("n <= 0");
double parameters[];
parameters = new double[2];
parameters[0] = EmpiricalDist.getMedian (x, n);
double sum = 0.0;
for (int i = 0; i < n; i++)
sum += Math.abs (x[i] - parameters[0]);
parameters[1] = sum / (double) n;
return parameters;
}
/**
* Creates a new instance of a Laplace distribution with parameters μ
* and β estimated using the maximum likelihood method based on the
* n observations x[i],
* i = 0, 1,…, n - 1.
*
* @param x the list of observations to use to evaluate parameters
*
* @param n the number of observations to use to evaluate parameters
*
*
*/
public static LaplaceDist getInstanceFromMLE (double[] x, int n) {
double parameters[] = getMLE (x, n);
return new LaplaceDist (parameters[0], parameters[1]);
}
/**
* Computes and returns the mean
* E[X] = μ of the Laplace
* distribution with parameters μ and β.
*
* @return the mean of the Laplace distribution
* E[X] = μ
*
*/
public static double getMean (double mu, double beta) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
return mu;
}
/**
* Computes and returns the variance
* Var[X] = 2β2
* of the Laplace distribution with parameters μ and β.
*
* @return the variance of the Laplace distribution
* Var[X] = 2β2
*
*/
public static double getVariance (double mu, double beta) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
return (2.0 * beta * beta);
}
/**
* Computes and returns the standard deviation of the Laplace
* distribution with parameters μ and β.
*
* @return the standard deviation of the Laplace distribution
*
*/
public static double getStandardDeviation (double mu, double beta) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
return (Num.RAC2 * beta);
}
/**
* Returns the parameter μ.
*
*/
public double getMu() {
return mu;
}
/**
* Returns the parameter β.
*
*/
public double getBeta() {
return beta;
}
/**
* Return a table containing the parameters of the current distribution.
* This table is put in regular order: [μ, β].
*
*
*/
public double[] getParams () {
double[] retour = {mu, beta};
return retour;
}
public String toString () {
return getClass().getSimpleName() + " : mu = " + mu+ ", beta = " + beta;
}
}
Ads help maintain this website.