/** * {@inheritDoc} * * The default implementation generates the sample by calling * {@link #sample()} in a loop. */ public int[] sample(int sampleSize) { if (sampleSize <= 0) { throw new NotStrictlyPositiveException( LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize); } int[] out = new int[sampleSize]; for (int i = 0; i < sampleSize; i++) { out[i] = sample(); } return out; }
int lower = getSupportLowerBound(); if (p == 0.0) { return lower; if (checkedCumulativeProbability(lower) >= p) { return lower; int upper = getSupportUpperBound(); if (p == 1.0) { return upper; final double mu = getNumericalMean(); final double sigma = FastMath.sqrt(getNumericalVariance()); final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) || Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0); return solveInverseCumulativeProbability(p, lower, upper);
/** * {@inheritDoc} * * The default implementation uses the identity * <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p> */ public double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException { if (x1 < x0) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } return cumulativeProbability(x1) - cumulativeProbability(x0); }
/** * {@inheritDoc} * * The default implementation uses the * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> * inversion method</a>. */ public int sample() { return inverseCumulativeProbability(random.nextDouble()); }
/** * For a random variable {@code X} whose values are distributed according to * this distribution, this method returns {@code log(P(X = x))}, where * {@code log} is the natural logarithm. In other words, this method * represents the logarithm of the probability mass function (PMF) for the * distribution. Note that due to the floating point precision and * under/overflow issues, this method will for some distributions be more * precise and faster than computing the logarithm of * {@link #probability(int)}. * <p> * The default implementation simply computes the logarithm of {@code probability(x)}.</p> * * @param x the point at which the PMF is evaluated * @return the logarithm of the value of the probability mass function at {@code x} */ public double logProbability(int x) { return FastMath.log(probability(x)); } }
double pm = checkedCumulativeProbability(xm); if (pm >= p) { upper = xm;
/** * {@inheritDoc} * * The default implementation uses the * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> * inversion method</a>. */ public int sample() { return inverseCumulativeProbability(random.nextDouble()); }
/** * For a random variable {@code X} whose values are distributed according to * this distribution, this method returns {@code log(P(X = x))}, where * {@code log} is the natural logarithm. In other words, this method * represents the logarithm of the probability mass function (PMF) for the * distribution. Note that due to the floating point precision and * under/overflow issues, this method will for some distributions be more * precise and faster than computing the logarithm of * {@link #probability(int)}. * <p> * The default implementation simply computes the logarithm of {@code probability(x)}.</p> * * @param x the point at which the PMF is evaluated * @return the logarithm of the value of the probability mass function at {@code x} */ public double logProbability(int x) { return Math.log(probability(x)); } }
double pm = checkedCumulativeProbability(xm); if (pm >= p) { upper = xm;
int lower = getSupportLowerBound(); if (p == 0.0) { return lower; if (checkedCumulativeProbability(lower) >= p) { return lower; int upper = getSupportUpperBound(); if (p == 1.0) { return upper; final double mu = getNumericalMean(); final double sigma = Math.sqrt(getNumericalVariance()); final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) || Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0); return solveInverseCumulativeProbability(p, lower, upper);
private Object generateSingleRowValue() { Object ret = null; ValueType type = schema.getType(); if (distribution instanceof AbstractIntegerDistribution) { ret = ((AbstractIntegerDistribution) distribution).sample(); } else if (distribution instanceof AbstractRealDistribution) { ret = ((AbstractRealDistribution) distribution).sample(); } else if (distribution instanceof EnumeratedDistribution) { ret = ((EnumeratedDistribution) distribution).sample(); } ret = convertType(ret, type); return ret; }
/** * Computes the cumulative probability function and checks for {@code NaN} * values returned. Throws {@code MathInternalError} if the value is * {@code NaN}. Rethrows any exception encountered evaluating the cumulative * probability function. Throws {@code MathInternalError} if the cumulative * probability function returns {@code NaN}. * * @param argument input value * @return the cumulative probability * @throws MathInternalError if the cumulative probability is {@code NaN} */ private double checkedCumulativeProbability(int argument) throws MathInternalError { double result = Double.NaN; result = cumulativeProbability(argument); if (Double.isNaN(result)) { throw new MathInternalError(LocalizedFormats .DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument); } return result; }
/** * {@inheritDoc} * * The default implementation uses the * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling"> * inversion method</a>. */ public int sample() { return inverseCumulativeProbability(random.nextDouble()); }
/** * For a random variable {@code X} whose values are distributed according to * this distribution, this method returns {@code log(P(X = x))}, where * {@code log} is the natural logarithm. In other words, this method * represents the logarithm of the probability mass function (PMF) for the * distribution. Note that due to the floating point precision and * under/overflow issues, this method will for some distributions be more * precise and faster than computing the logarithm of * {@link #probability(int)}. * <p> * The default implementation simply computes the logarithm of {@code probability(x)}.</p> * * @param x the point at which the PMF is evaluated * @return the logarithm of the value of the probability mass function at {@code x} */ public double logProbability(int x) { return FastMath.log(probability(x)); } }
double pm = checkedCumulativeProbability(xm); if (pm >= p) { upper = xm;
int lower = getSupportLowerBound(); if (p == 0.0) { return lower; if (checkedCumulativeProbability(lower) >= p) { return lower; int upper = getSupportUpperBound(); if (p == 1.0) { return upper; final double mu = getNumericalMean(); final double sigma = FastMath.sqrt(getNumericalVariance()); final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) || Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0); return solveInverseCumulativeProbability(p, lower, upper);
@Override public double sample () { return impl.sample(); } }
/** * {@inheritDoc} * * The default implementation uses the identity * <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p> */ public double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException { if (x1 < x0) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } return cumulativeProbability(x1) - cumulativeProbability(x0); }
/** * {@inheritDoc} * * The default implementation generates the sample by calling * {@link #sample()} in a loop. */ public int[] sample(int sampleSize) { if (sampleSize <= 0) { throw new NotStrictlyPositiveException( LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize); } int[] out = new int[sampleSize]; for (int i = 0; i < sampleSize; i++) { out[i] = sample(); } return out; }
/** * {@inheritDoc} * * The default implementation uses the identity * <p>{@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}</p> */ public double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException { if (x1 < x0) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } return cumulativeProbability(x1) - cumulativeProbability(x0); }