25package umontreal.ssj.probdist;
27import java.util.Formatter;
28import java.util.Locale;
54 protected double cdf[] =
null;
55 protected double pr[] =
null;
56 protected int xmin = 0;
57 protected int xmax = 0;
58 protected int xmed = 0;
61 protected double sortedVal[];
62 protected double supportA = Double.NEGATIVE_INFINITY;
63 protected double supportB = Double.POSITIVE_INFINITY;
65 protected DiscreteDistribution() {
76 init(n, values,
prob);
84 double[] A =
new double[n];
85 for (
int i = 0; i < n; i++)
90 private void init(
int n,
double[] val,
double[]
prob) {
94 throw new IllegalArgumentException(
"n <= 0");
96 throw new IllegalArgumentException(
"Size of arrays 'values' or 'prob' less than 'n'");
102 sortedVal =
new double[nVal];
103 System.arraycopy(val, 0, sortedVal, 0, nVal);
105 supportA = sortedVal[0];
106 supportB = sortedVal[nVal - 1];
114 cdf =
new double[nVal];
117 while (i < xmax && cdf[i] < 0.5) {
119 cdf[i] = pr[i] + cdf[i - 1];
128 cdf[nVal - 1] = pr[nVal - 1];
131 cdf[i] = pr[i] + cdf[i + 1];
140 public double cdf(
double x) {
141 if (x < sortedVal[0])
143 if (x >= sortedVal[nVal - 1])
145 if ((xmax == xmed) || (x < sortedVal[xmed + 1])) {
146 for (
int i = 0; i <= xmed; i++)
147 if (x >= sortedVal[i] && x < sortedVal[i + 1])
150 for (
int i = xmed + 1; i < nVal - 1; i++)
151 if (x >= sortedVal[i] && x < sortedVal[i + 1])
152 return 1.0 - cdf[i + 1];
154 throw new IllegalStateException();
162 if (x <= sortedVal[0])
164 if (x > sortedVal[nVal - 1])
166 if ((xmax == xmed) || (x <= sortedVal[xmed + 1])) {
167 for (
int i = 0; i <= xmed; i++)
168 if (x > sortedVal[i] && x <= sortedVal[i + 1])
171 for (
int i = xmed + 1; i < nVal - 1; i++)
172 if (x > sortedVal[i] && x <= sortedVal[i + 1])
175 throw new IllegalStateException();
193 if (u < 0.0 || u > 1.0)
194 throw new IllegalArgumentException(
"u not in [0,1]");
204 if (u <= cdf[xmed - xmin]) {
207 return sortedVal[xmin];
220 if (u < cdf[xmax - xmin])
221 return sortedVal[xmax];
235 return sortedVal[i + xmin];
243 for (
int i = 0; i < nVal; i++)
244 mean += sortedVal[i] * pr[i];
254 double variance = 0.0;
255 for (
int i = 0; i < nVal; i++)
256 variance += (sortedVal[i] - mean) * (sortedVal[i] - mean) * pr[i];
273 double[] retour =
new double[1 + nVal * 2];
276 System.arraycopy(sortedVal, 0, retour, 1, nVal);
277 for (
int i = 0; i < nVal - 1; i++) {
278 retour[nVal + 1 + i] = cdf[i] - sum;
281 retour[2 * nVal] = 1.0 - sum;
301 if (i < 0 || i >= nVal)
335 StringBuilder sb =
new StringBuilder();
336 Formatter formatter =
new Formatter(sb, Locale.US);
337 formatter.format(
"%s%n", getClass().getSimpleName());
338 formatter.format(
"%s : %s%n",
"value",
"cdf");
339 for (
int i = 0; i < nVal - 1; i++)
340 formatter.format(
"%f : %f%n", sortedVal[i], cdf[i]);
341 formatter.format(
"%f : %f%n", sortedVal[nVal - 1], 1.0);
343 return sb.toString();
double getMean()
Computes the mean of the distribution.
int getN()
Returns the number of possible values .
double inverseF(double u)
double[] getParams()
Returns a table containing the parameters of the current distribution.
double getXsup()
Returns the upper limit of the support of the distribution.
double getXinf()
Returns the lower limit of the support of the distribution.
DiscreteDistribution(double[] values, double[] prob, int n)
Constructs a discrete distribution over the values contained in array values, with probabilities giv...
String toString()
Returns a String containing information about the current distribution.
double getVariance()
Computes the variance of the distribution.
DiscreteDistribution(int[] values, double[] prob, int n)
Similar to DiscreteDistribution(double[], double[], int).
double prob(int i)
Returns , the probability of the -th value, for.
double getStandardDeviation()
Computes the standard deviation of the distribution.
double getValue(int i)
Returns the -th value , for .
This interface should be implemented by all classes supporting discrete and continuous distributions.