/* *
* Title:
* Description:
* Copyright: Copyright (c)
* Company:
* @author
* @version 1.0
*/
package umontreal.iro.lecuyer.probdist;
import umontreal.iro.lecuyer.util.Num;
/**
* This class extends the class {@link ContinuousDistribution} for
* the Rayleigh distribution with
* location parameter a, and scale parameter β > 0.
* The density function is
*
*
*
* f (x) = (x-a)/β2 e-(x-a)2/(2β2) for x >= a,
*
* and f (x) = 0 for x < a.
* The distribution function is
*
*
*
* F(x) = 1 - e-(x-a)2/(2β2) for x >= a,
*
* and the inverse distribution function is
*
*
*
* F-1(u) = x = a + β(-2ln(1-u))1/2 for 0 <= u <= 1.
*
*
*/
public class RayleighDist extends ContinuousDistribution {
private double a;
private double beta;
/**
* Constructs a RayleighDist object with parameters
* a = 0 and β = beta.
*
*/
public RayleighDist (double beta) {
setParams (0.0, beta);
}
/**
* Constructs a RayleighDist object with parameters
* a = a, and β = beta.
*
*/
public RayleighDist (double a, double beta) {
setParams (a, beta);
}
public double density (double x) {
return density (a, beta, x);
}
public double cdf (double x) {
return cdf (a, beta, x);
}
public double barF (double x) {
return barF (a, beta, x);
}
public double inverseF (double u) {
return inverseF (a, beta, u);
}
public double getMean() {
return RayleighDist.getMean (a, beta);
}
public double getVariance() {
return RayleighDist.getVariance (beta);
}
public double getStandardDeviation() {
return RayleighDist.getStandardDeviation (beta);
}
/**
* Computes the density function.
*
* @param a the location parameter
*
* @param beta the scale parameter
*
* @param x the value at which the density is evaluated
*
* @return the density function
*
*/
public static double density (double a, double beta, double x) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
if (x <= a)
return 0.0;
final double Z = (x - a)/beta;
return Z/beta * Math.exp(-Z*Z/2.0);
}
/**
* Same as density (0, beta, x).
*
* @param beta the scale parameter
*
* @param x the value at which the density is evaluated
*
* @return returns the density function
*
*/
public static double density (double beta, double x) {
return density (0.0, beta, x);
}
/**
* Computes the distribution function.
*
* @param a the location parameter
*
* @param beta the scale parameter
*
* @param x the value at which the distribution is evaluated
*
* @return returns the distribution function
*
*/
public static double cdf (double a, double beta, double x) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
if (x <= a)
return 0.0;
final double Z = (x - a)/beta;
if (Z >= 10.0)
return 1.0;
return -Math.expm1(-Z*Z/2.0);
}
/**
* Same as cdf (0, beta, x).
*
* @param beta the scale parameter
*
* @param x the value at which the distribution is evaluated
*
* @return returns the distribution function
*
*/
public static double cdf (double beta, double x) {
return cdf (0.0, beta, x);
}
/**
* Computes the complementary distribution function.
*
* @param a the location parameter
*
* @param beta the scale parameter
*
* @param x the value at which the complementary distribution is evaluated
*
* @return returns the complementary distribution function
*
*/
public static double barF (double a, double beta, double x) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
if (x <= a)
return 1.0;
double z = (x - a)/beta;
if (z >= 44.0)
return 0.0;
return Math.exp(-z*z/2.0);
}
/**
* Same as barF (0, beta, x).
*
* @param beta the scale parameter
*
* @param x the value at which the complementary distribution is evaluated
*
* @return returns the complementary distribution function
*
*/
public static double barF (double beta, double x) {
return barF (0.0, beta, x);
}
/**
* Computes the inverse of the distribution function.
*
* @param a the location parameter
*
* @param beta the scale parameter
*
* @param u the value at which the inverse distribution is evaluated
*
* @return returns the inverse of the distribution function
*
*/
public static double inverseF (double a, double beta, double u) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
if (u < 0.0 || u > 1.0)
throw new IllegalArgumentException ("u not in [0, 1]");
if (u <= 0.0)
return a;
if (u >= 1.0)
return Double.POSITIVE_INFINITY;
return a + beta * Math.sqrt(-2.0 * Math.log1p(-u));
}
/**
* Same as inverseF (0, beta, u).
*
* @param beta the scale parameter
*
* @param u the value at which the inverse distribution is evaluated
*
* @return returns the inverse of the distribution function
*
*/
public static double inverseF (double beta, double u) {
return inverseF (0.0, beta, u);
}
/**
* Estimates the parameter β of the Rayleigh distribution
* using the maximum likelihood method, assuming that a is known,
* from the n observations x[i],
* i = 0, 1,…, n - 1.
* The estimate is returned in a one-element array: [hat(β)].
*
* @param x the list of observations to use to evaluate parameters
*
* @param n the number of observations to use to evaluate parameters
*
* @param a the location parameter
*
* @return returns the parameter [
* hat(β)]
*
*/
public static double[] getMLE (double[] x, int n, double a) {
if (n <= 0)
throw new IllegalArgumentException ("n <= 0");
double somme = 0;
for (int i = 0 ; i < n ; ++i) somme += (x[i]-a)*(x[i]-a);
double [] parametres = new double [1];
parametres[0] = Math.sqrt(somme/(2.0*n));
return parametres;
}
/**
* Creates a new instance of a Rayleigh distribution with parameters a and
* hat(β). This last is estimated using the maximum likelihood method
* based on the n observations x[i],
* i = 0,…, n - 1.
*
* @param x the list of observations to use to evaluate parameters
*
* @param n the number of observations to use to evaluate parameters
*
* @param a the location parameter
*
*
*/
public static RayleighDist getInstanceFromMLE (double[] x, int n,
double a) {
double parameters[] = getMLE (x, n, a);
return new RayleighDist (parameters[0], parameters[1]);
}
/**
* Returns the mean
* a + β(π/2)1/2 of the
* Rayleigh distribution with parameters a and β.
*
* @param a the location parameter
*
* @param beta the scale parameter
*
* @return the mean of the Rayleigh distribution
*
*/
public static double getMean (double a, double beta) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
return (a + beta * Math.sqrt(Math.PI/2.0));
}
/**
* Returns the variance
* of the Rayleigh distribution with parameter β.
*
* @param beta the scale parameter
*
* @return the variance of the Rayleigh distribution
*
*/
public static double getVariance (double beta) {
if (beta == 0.0)
throw new IllegalArgumentException ("beta = 0");
return (2.0 - 0.5*Math.PI) * beta * beta;
}
/**
* Returns the standard deviation
* β(2 - π/2)1/2 of
* the Rayleigh distribution with parameter β.
*
* @param beta the scale parameter
*
* @return the standard deviation of the Rayleigh distribution
*
*/
public static double getStandardDeviation (double beta) {
return Math.sqrt (RayleighDist.getVariance (beta));
}
/**
* Returns the parameter a.
*
* @return the location parameter a
*
*/
public double getA() {
return a;
}
/**
* Returns the parameter β.
*
* @return the scale parameter beta
*
*/
public double getSigma() {
return beta;
}
/**
* Sets the parameters a and β for this object.
*
* @param a the location parameter
*
* @param beta the scale parameter
*
*
*/
public void setParams (double a, double beta) {
if (beta <= 0.0)
throw new IllegalArgumentException ("beta <= 0");
this.a = a;
this.beta = beta;
supportA = a;
}
/**
* Return an array containing the parameters of the current distribution
* in the order: [a, β].
*
* @return [a, β]
*
*
*/
public double[] getParams () {
double[] retour = {a, beta};
return retour;
}
public String toString () {
return getClass().getSimpleName() + " : a = " + a + ", beta = " + beta;
}
}
Ads help maintain this website.