/* *
 * Title:        

* Description:

* Copyright: Copyright (c)

* Company:

* @author * @version 1.0 */ package umontreal.iro.lecuyer.probdist; /** * Extends the class {@link DiscreteDistributionInt} for * the discrete uniform distribution over the range [i, j]. * Its mass function is given by *

*

*
* p(x) = 1/(j - i + 1)         for x = i, i + 1,…, j *

* and 0 elsewhere. The distribution function is *

*

*
* F(x) = (floor(x) - i + 1)/(j - i + 1)         for i <= x <= j *

* and its inverse is *

*

*
* F-1(u) = i + (j - i + 1)u        for 0 <= u <= 1. *

* */ public class UniformIntDist extends DiscreteDistributionInt { private int i; private int j; /** * Constructs a discrete uniform distribution over the interval [i, j]. * */ public UniformIntDist (int i, int j) { setParams (i, j); } public double prob (int x) { return prob (i, j, x); } public double cdf (int x) { return cdf (i, j, x); } public double barF (int x) { return barF (i, j, x); } public int inverseFInt (double u) { return inverseF (i, j, u); } public double getMean() { return getMean (i, j); } public double getVariance() { return getVariance (i, j); } public double getStandardDeviation() { return getStandardDeviation (i, j); } /** * Computes the discrete uniform probability p(x). * */ public static double prob (int i, int j, int x) { if (j < i) throw new IllegalArgumentException ("j < i"); if (x < i || x > j) return 0.0; return (1.0 / (j - i + 1.0)); } /** * Computes the discrete uniform distribution function * defined in. * */ public static double cdf (int i, int j, int x) { if (j < i) throw new IllegalArgumentException ("j < i"); if (x < i) return 0.0; if (x >= j) return 1.0; return ((x - i + 1) / (j - i + 1.0)); } /** * Computes the discrete uniform complementary distribution function * * bar(F)(x). * WARNING: The complementary distribution function is defined as * * bar(F)(x) = P[X >= x]. * */ public static double barF (int i, int j, int x) { if (j < i) throw new IllegalArgumentException ("j < i"); if (x <= i) return 1.0; if (x > j) return 0.0; return ((j - x + 1.0) / (j - i + 1.0)); } /** * Computes the inverse of the discrete uniform distribution function. * */ public static int inverseF (int i, int j, double u) { if (j < i) throw new IllegalArgumentException ("j < i"); if (u > 1.0 || u < 0.0) throw new IllegalArgumentException ("u not in [0, 1]"); if (u <= 0.0) return i; if (u >= 1.0) return j; return i + (int) (u * (j - i + 1)); } /** * Estimates the parameters (i, j) of the uniform distribution * over integers using the maximum likelihood method, from the n observations * x[k], * k = 0, 1,…, n - 1. The estimates are returned in a two-element * array, in regular order: [i, j]. * * @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(&jnodot;)] * */ public static double[] getMLE (int[] x, int n) { if (n <= 0) throw new IllegalArgumentException ("n <= 0"); double parameters[] = new double[2]; parameters[0] = (double) Integer.MAX_VALUE; parameters[1] = (double) Integer.MIN_VALUE; for (int i = 0; i < n; i++) { if ((double) x[i] < parameters[0]) parameters[0] = (double) x[i]; if ((double) x[i] > parameters[1]) parameters[1] = (double) x[i]; } return parameters; } /** * Creates a new instance of a discrete uniform distribution over integers with parameters * i and j estimated using the maximum likelihood method based on the n observations * x[k], * k = 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 UniformIntDist getInstanceFromMLE (int[] x, int n) { double parameters[] = getMLE (x, n); return new UniformIntDist ((int) parameters[0], (int) parameters[1]); } /** * Computes and returns the mean * E[X] = (i + j)/2 * of the discrete uniform distribution. * * @return the mean of the discrete uniform distribution * */ public static double getMean (int i, int j) { if (j < i) throw new IllegalArgumentException ("j < i"); return ((i + j) / 2.0); } /** * Computes and returns the variance * * Var[X] = [(j - i + 1)2 -1]/12 * of the discrete uniform distribution. * * @return the variance of the discrete uniform distribution * */ public static double getVariance (int i, int j) { if (j < i) throw new IllegalArgumentException ("j < i"); return (((j - i + 1.0) * (j - i + 1.0) - 1.0) / 12.0); } /** * Computes and returns the standard deviation * of the discrete uniform distribution. * * @return the standard deviation of the discrete uniform distribution * */ public static double getStandardDeviation (int i, int j) { return Math.sqrt (UniformIntDist.getVariance (i, j)); } /** * Returns the parameter i. * */ public int getI() { return i; } /** * Returns the parameter j. * */ public int getJ() { return j; } /** * Sets the parameters i and j for this object. * */ public void setParams (int i, int j) { if (j < i) throw new IllegalArgumentException ("j < i"); supportA = this.i = i; supportB = this.j = j; } /** * Return a table containing the parameters of the current distribution. * This table is put in regular order: [i, j]. * * */ public double[] getParams () { double[] retour = {i, j}; return retour; } public String toString () { return getClass().getSimpleName() + " : i = " + i + ", j = " + j; } }

Ads help maintain this website.