/* *
* Title:
* Description:
* Copyright: Copyright (c)
* Company:
* @author
* @version 1.0
*/
package umontreal.iro.lecuyer.probdist;
/**
* Extends the class {@link DiscreteDistributionInt} for the Bernoulli
* distribution with parameter p, where
* 0 <= p <= 1.
* Its mass function is given by
*
*
*
*
* f (x) =
* 1 - p,
* if x = 0;
*
* f (x) =
* p,
* if x = 1;
*
* f (x) =
* 0,
* otherwise.
*
*
*
* Its distribution function is
*
*
*
*
* F(x) =
* 0,
* if x < 0;
*
* F(x) =
* 1 - p,
* if 0 <= x < 1;
*
* F(x) =
* 1,
* if x >= 1.
*
*
*
*
*/
public class BernoulliDist extends DiscreteDistributionInt {
private double p;
private double q;
/**
* Creates a Bernoulli distribution object.
*
*/
public BernoulliDist (double p) {
if (p < 0.0 || p > 1.0)
throw new IllegalArgumentException ("p not in [0,1]");
this.p = p;
q = 1 - p;
supportA = 0;
supportB = 1;
}
public double prob (int x) {
if (1 == x) return p;
if (0 == x) return q;
return 0.0;
}
public double cdf (int x) {
if (x < 0) return 0.0;
if (x < 1) return q;
return 1.0;
}
public double barF (int x) {
if (x > 1) return 0.0;
if (x > 0) return p;
return 1.0;
}
public int inverseFInt (double u) {
if (u < 0.0 || u > 1.0)
throw new IllegalArgumentException ("u not in [0,1]");
if (u > q) return 1;
return 0;
}
public double getMean() {
return BernoulliDist.getMean (p);
}
public double getVariance() {
return BernoulliDist.getVariance (p);
}
public double getStandardDeviation() {
return BernoulliDist.getStandardDeviation (p);
}
/**
* Returns the Bernoulli probability f (x) with parameter p
* (see eq.).
*
*/
public static double prob (double p, int x) {
if (p < 0.0 || p > 1.0)
throw new IllegalArgumentException ("p not in [0,1]");
if (1 == x) return p;
if (0 == x) return 1.0 - p;
return 0.0;
}
/**
* Returns the Bernoulli distribution function F(x)
* with parameter p (see eq.).
*
*/
public static double cdf (double p, int x) {
if (p < 0.0 | p > 1.0)
throw new IllegalArgumentException ("p not in [0,1]");
if (x < 0) return 0.0;
if (x < 1) return 1.0 - p;
return 1.0;
}
/**
* Returns the complementary Bernoulli distribution
* function
* bar(F)(x) = P[X >= x] with parameter p.
*
*/
public static double barF (double p, int x) {
if (p < 0.0 | p > 1.0)
throw new IllegalArgumentException ("p not in [0,1]");
if (x > 1) return 0.0;
if (x > 0) return p;
return 1.0;
}
/**
* Returns the inverse of the Bernoulli distribution function
* with parameter p at u.
*
*/
public static int inverseF (double p, double u) {
if (p < 0.0 | p > 1.0)
throw new IllegalArgumentException ("p not in [0,1]");
if (u < 0.0 || u > 1.0)
throw new IllegalArgumentException ("u not in [0,1]");
if (u > 1.0 - p) return 1;
return 0;
}
/**
* Estimates the parameters p of the Bernoulli distribution
* using the maximum likelihood method, from the m observations
* x[i],
* i = 0, 1,…, m - 1. The estimate is returned in a one-element
* array: [p].
*
* @param x the list of observations used to evaluate parameters
*
* @param m the number of observations used to evaluate parameters
*
* @return returns the parameter [hat(p)]
*
*/
public static double[] getMLE (int[] x, int m) {
if (m < 2)
throw new UnsupportedOperationException(" m < 2");
// compute the empirical mean
double sum = 0.0;
for (int i = 0; i < m; i++)
sum += x[i];
sum /= (double) m;
double param[] = new double[1];
param[0] = sum;
return param;
}
/**
* Creates a new instance of a Bernoulli distribution with parameter
* p estimated using the maximum likelihood method, from
* the m observations x[i],
* i = 0, 1,…, m - 1.
*
* @param x the list of observations to use to estimate the parameters
*
* @param m the number of observations to use to estimate the parameters
*
*
*/
public static BernoulliDist getInstanceFromMLE (int[] x, int m) {
double param[] = new double[1];
param = getMLE (x, m);
return new BernoulliDist (param[0]);
}
/**
* Returns the mean E[X] = p of the Bernoulli distribution with
* parameter p.
*
* @return the mean of the Bernoulli distribution E[X] = np
*
*/
public static double getMean (double p) {
if (p < 0.0 || p > 1.0)
throw new IllegalArgumentException ("p not in [0, 1]");
return (p);
}
/**
* Computes the variance
* Var[X] = p(1 - p) of the Bernoulli
* distribution with parameter p.
*
* @return the variance of the Bernoulli distribution
*
*/
public static double getVariance (double p) {
if (p < 0.0 || p > 1.0)
throw new IllegalArgumentException ("p not in [0, 1]");
return (p * (1.0 - p));
}
/**
* Computes the standard deviation of the Bernoulli distribution
* with parameter p.
*
* @return the standard deviation of the Bernoulli distribution
*
*/
public static double getStandardDeviation (double p) {
return Math.sqrt (BernoulliDist.getVariance (p));
}
/**
* Returns the parameter p of this object.
*
*/
public double getP() {
return p;
}
/**
* Returns an array that contains the parameter p of the current
* distribution: [p].
*
*/
public double[] getParams () {
double[] retour = {p};
return retour;
}
/**
* Resets the parameter to this new value.
*
*
*/
public void setParams (double p) {
this.p = p;
q = 1 - p;
}
public String toString () {
return getClass().getSimpleName() + " : p = " + p;
}
}
Ads help maintain this website.