/* *
* Title:
* Description:
* Copyright: Copyright (c)
* Company:
* @author
* @version 1.0
*/
package umontreal.iro.lecuyer.probdist;
/**
* Extends the class {@link ContinuousDistribution} for
* the uniform distribution
* over the interval [a, b].
* Its density is
*
*
*
* f (x) = 1/(b - a) for a <= x <= b
*
* and 0 elsewhere. The distribution function is
*
*
*
* F(x) = (x - a)/(b - a) for a <= x <= b
*
* and its inverse is
*
*
*
* F-1(u) = a + (b - a)u for 0 <= u <= 1.
*
*
*/
public class UniformDist extends ContinuousDistribution {
private double a;
private double b;
/**
* Constructs a uniform distribution over the interval
* (a, b) = (0, 1).
*
*/
public UniformDist() {
setParams (0.0, 1.0);
}
/**
* Constructs a uniform distribution over the interval (a, b).
*
*/
public UniformDist (double a, double b) {
setParams (a, b);
}
public double density (double x) {
return density (a, b, x);
}
public double cdf (double x) {
return cdf (a, b, x);
}
public double barF (double x) {
return barF (a, b, x);
}
public double inverseF (double u) {
return inverseF (a, b, u);
}
public double getMean() {
return UniformDist.getMean (a, b);
}
public double getVariance() {
return UniformDist.getVariance (a, b);
}
public double getStandardDeviation() {
return UniformDist.getStandardDeviation (a, b);
}
/**
* Computes the uniform density function
* f (x).
*
*/
public static double density (double a, double b, double x) {
if (b <= a)
throw new IllegalArgumentException ("b <= a");
if (x <= a || x >= b)
return 0.0;
return 1.0 / (b - a);
}
/**
* Computes the uniform distribution function as in.
*
*/
public static double cdf (double a, double b, double x) {
if (b <= a)
throw new IllegalArgumentException ("b <= a");
if (x <= a)
return 0.0;
if (x >= b)
return 1.0;
return (x - a)/(b - a);
}
/**
* Computes the uniform complementary distribution function
*
* bar(F)(x).
*
*/
public static double barF (double a, double b, double x) {
if (b <= a)
throw new IllegalArgumentException ("b <= a");
if (x <= a)
return 1.0;
if (x >= b)
return 0.0;
return (b - x)/(b - a);
}
/**
* Computes the inverse of the uniform distribution function.
*
*/
public static double inverseF (double a, double b, double u) {
if (b <= a)
throw new IllegalArgumentException ("b <= a");
if (u > 1.0 || u < 0.0)
throw new IllegalArgumentException ("u not in [0, 1]");
if (u <= 0.0)
return a;
if (u >= 1.0)
return b;
return a + (b - a)*u;
}
/**
* Estimates the parameter (a, b) of the uniform 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: [a, b].
*
* @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(a), hat(b)]
*
*/
public static double[] getMLE (double[] x, int n) {
if (n <= 0)
throw new IllegalArgumentException ("n <= 0");
double parameters[] = new double[2];
parameters[0] = Double.POSITIVE_INFINITY;
parameters[1] = Double.NEGATIVE_INFINITY;
for (int i = 0; i < n; i++) {
if (x[i] < parameters[0])
parameters[0] = x[i];
if (x[i] > parameters[1])
parameters[1] = x[i];
}
return parameters;
}
/**
* Creates a new instance of a uniform distribution with parameters a and b
* 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 UniformDist getInstanceFromMLE (double[] x, int n) {
double parameters[] = getMLE (x, n);
return new UniformDist (parameters[0], parameters[1]);
}
/**
* Computes and returns the mean
* E[X] = (a + b)/2
* of the uniform distribution with parameters a and b.
*
* @return the mean of the uniform distribution
* E[X] = (a + b)/2
*
*/
public static double getMean (double a, double b) {
if (b <= a)
throw new IllegalArgumentException ("b <= a");
return ((a + b) / 2);
}
/**
* Computes and returns the variance
* Var[X] = (b - a)2/12
* of the uniform distribution with parameters a and b.
*
* @return the variance of the uniform distribution
* Var[X] = (b - a)2/12
*
*/
public static double getVariance (double a, double b) {
if (b <= a)
throw new IllegalArgumentException ("b <= a");
return ((b - a) * (b - a) / 12);
}
/**
* Computes and returns the standard deviation
* of the uniform distribution with parameters a and b.
*
* @return the standard deviation of the uniform distribution
*
*/
public static double getStandardDeviation (double a, double b) {
return Math.sqrt (UniformDist.getVariance (a, b));
}
/**
* Returns the parameter a.
*
*/
public double getA() {
return a;
}
/**
* Returns the parameter b.
*
*/
public double getB() {
return b;
}
/**
* Sets the parameters a and b for this object.
*
*/
public void setParams (double a, double b) {
if (b <= a)
throw new IllegalArgumentException ("b <= a");
this.a = a;
this.b = b;
supportA = a;
supportB = b;
}
/**
* Return a table containing the parameters of the current distribution.
* This table is put in regular order: [a, b].
*
*
*/
public double[] getParams () {
double[] retour = {a, b};
return retour;
}
public String toString () {
return getClass().getSimpleName() + " : a = " + a + ", b = " + b;
}
}
Ads help maintain this website.