Java Code Examples for org.apache.commons.math.exception.util.LocalizedFormats#MEAN

The following examples show how to use org.apache.commons.math.exception.util.LocalizedFormats#MEAN . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Math_61_PoissonDistributionImpl_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a new Poisson distribution with the given mean, convergence criterion
 * and maximum number of iterations.
 *
 * @param p the Poisson mean
 * @param epsilon the convergence criteria for cumulative probabilites
 * @param maxIterations the maximum number of iterations for cumulative probabilites
 * @since 2.1
 */
public PoissonDistributionImpl(double p, double epsilon, int maxIterations) {
    if (p <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p);
    }
    mean = p;
    normal = new NormalDistributionImpl(p, FastMath.sqrt(p));
    this.epsilon = epsilon;
    this.maxIterations = maxIterations;
}
 
Example 2
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a random value from an Exponential distribution with the given
 * mean.
 * <p>
 * <strong>Algorithm Description</strong>: Uses the Algorithm SA (Ahrens)
 * from p. 876 in:
 * [1]: Ahrens, J. H. and Dieter, U. (1972). Computer methods for
 * sampling from the exponential and normal distributions.
 * Communications of the ACM, 15, 873-882.
 * </p>
 *
 * @param mean the mean of the distribution
 * @return the random Exponential value
 * @throws NotStrictlyPositiveException if {@code mean <= 0}.
 */
public double nextExponential(double mean) {
    if (mean <= 0.0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean);
    }

    // Step 1:
    double a = 0;
    double u = this.nextUniform(0, 1);

    // Step 2 and 3:
    while (u < 0.5) {
        a += EXPONENTIAL_SA_QI[0];
        u *= 2;
    }

    // Step 4 (now u >= 0.5):
    u += u - 1;

    // Step 5:
    if (u <= EXPONENTIAL_SA_QI[0]) {
        return mean * (a + u);
    }

    // Step 6:
    int i = 0; // Should be 1, be we iterate before it in while using 0
    double u2 = this.nextUniform(0, 1);
    double umin = u2;

    // Step 7 and 8:
    do {
        ++i;
        u2 = this.nextUniform(0, 1);

        if (u2 < umin) {
            umin = u2;
        }

        // Step 8:
    } while (u > EXPONENTIAL_SA_QI[i]); // Ensured to exit since EXPONENTIAL_SA_QI[MAX] = 1

    return mean * (a + umin * EXPONENTIAL_SA_QI[0]);
}
 
Example 3
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a random value from an Exponential distribution with the given
 * mean.
 * <p>
 * <strong>Algorithm Description</strong>: Uses the Algorithm SA (Ahrens)
 * from p. 876 in:
 * [1]: Ahrens, J. H. and Dieter, U. (1972). Computer methods for
 * sampling from the exponential and normal distributions.
 * Communications of the ACM, 15, 873-882.
 * </p>
 *
 * @param mean the mean of the distribution
 * @return the random Exponential value
 * @throws NotStrictlyPositiveException if {@code mean <= 0}.
 */
public double nextExponential(double mean) {
    if (mean <= 0.0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean);
    }

    // Step 1:
    double a = 0;
    double u = this.nextUniform(0, 1);

    // Step 2 and 3:
    while (u < 0.5) {
        a += EXPONENTIAL_SA_QI[0];
        u *= 2;
    }

    // Step 4 (now u >= 0.5):
    u += u - 1;

    // Step 5:
    if (u <= EXPONENTIAL_SA_QI[0]) {
        return mean * (a + u);
    }

    // Step 6:
    int i = 0; // Should be 1, be we iterate before it in while using 0
    double u2 = this.nextUniform(0, 1);
    double umin = u2;

    // Step 7 and 8:
    do {
        ++i;
        u2 = this.nextUniform(0, 1);

        if (u2 < umin) {
            umin = u2;
        }

        // Step 8:
    } while (u > EXPONENTIAL_SA_QI[i]); // Ensured to exit since EXPONENTIAL_SA_QI[MAX] = 1

    return mean * (a + umin * EXPONENTIAL_SA_QI[0]);
}
 
Example 4
Source File: ExponentialDistributionImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a exponential distribution with the given mean.
 *
 * @param mean Mean of this distribution.
 * @param inverseCumAccuracy Maximum absolute error in inverse
 * cumulative probability estimates (defaults to
 * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
 * @throws NotStrictlyPositiveException if {@code mean <= 0}.
 * @since 2.1
 */
public ExponentialDistributionImpl(double mean, double inverseCumAccuracy) {
    if (mean <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean);
    }
    this.mean = mean;
    solverAbsoluteAccuracy = inverseCumAccuracy;
}
 
Example 5
Source File: PoissonDistributionImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new Poisson distribution with the given mean, convergence criterion
 * and maximum number of iterations.
 *
 * @param p Poisson mean.
 * @param epsilon Convergence criterion for cumulative probabilities.
 * @param maxIterations the maximum number of iterations for cumulative
 * probabilities.
 * @since 2.1
 */
public PoissonDistributionImpl(double p, double epsilon, int maxIterations) {
    if (p <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p);
    }
    mean = p;
    normal = new NormalDistributionImpl(p, FastMath.sqrt(p));
    this.epsilon = epsilon;
    this.maxIterations = maxIterations;
}
 
Example 6
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a random value from an Exponential distribution with the given
 * mean.
 * <p>
 * <strong>Algorithm Description</strong>: Uses the <a
 * href="http://www.jesus.ox.ac.uk/~clifford/a5/chap1/node5.html"> Inversion
 * Method</a> to generate exponentially distributed random values from
 * uniform deviates.
 * </p>
 *
 * @param mean the mean of the distribution
 * @return the random Exponential value
 * @throws NotStrictlyPositiveException if {@code mean <= 0}.
 */
public double nextExponential(double mean) {
    if (mean <= 0.0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean);
    }
    final RandomGenerator generator = getRan();
    double unif = generator.nextDouble();
    while (unif == 0.0d) {
        unif = generator.nextDouble();
    }
    return -mean * FastMath.log(unif);
}
 
Example 7
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a random value from an Exponential distribution with the given
 * mean.
 * <p>
 * <strong>Algorithm Description</strong>: Uses the <a
 * href="http://www.jesus.ox.ac.uk/~clifford/a5/chap1/node5.html"> Inversion
 * Method</a> to generate exponentially distributed random values from
 * uniform deviates.
 * </p>
 *
 * @param mean the mean of the distribution
 * @return the random Exponential value
 * @throws NotStrictlyPositiveException if {@code mean <= 0}.
 */
public double nextExponential(double mean) {
    if (mean <= 0.0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean);
    }
    final RandomGenerator generator = getRan();
    double unif = generator.nextDouble();
    while (unif == 0.0d) {
        unif = generator.nextDouble();
    }
    return -mean * FastMath.log(unif);
}
 
Example 8
Source File: ExponentialDistributionImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a exponential distribution with the given mean.
 *
 * @param mean Mean of this distribution.
 * @param inverseCumAccuracy Maximum absolute error in inverse
 * cumulative probability estimates (defaults to
 * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
 * @throws NotStrictlyPositiveException if {@code mean <= 0}.
 * @since 2.1
 */
public ExponentialDistributionImpl(double mean, double inverseCumAccuracy) {
    if (mean <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean);
    }
    this.mean = mean;
    solverAbsoluteAccuracy = inverseCumAccuracy;
}
 
Example 9
Source File: PoissonDistributionImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new Poisson distribution with the given mean, convergence criterion
 * and maximum number of iterations.
 *
 * @param p Poisson mean.
 * @param epsilon Convergence criterion for cumulative probabilities.
 * @param maxIterations the maximum number of iterations for cumulative
 * probabilities.
 * @since 2.1
 */
public PoissonDistributionImpl(double p, double epsilon, int maxIterations) {
    if (p <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p);
    }
    mean = p;
    normal = new NormalDistributionImpl(p, FastMath.sqrt(p));
    this.epsilon = epsilon;
    this.maxIterations = maxIterations;
}
 
Example 10
Source File: ExponentialDistributionImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a exponential distribution with the given mean.
 *
 * @param mean Mean of this distribution.
 * @param inverseCumAccuracy Maximum absolute error in inverse
 * cumulative probability estimates (defaults to
 * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}).
 * @throws NotStrictlyPositiveException if {@code mean <= 0}.
 * @since 2.1
 */
public ExponentialDistributionImpl(double mean, double inverseCumAccuracy) {
    if (mean <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean);
    }
    this.mean = mean;
    solverAbsoluteAccuracy = inverseCumAccuracy;
}
 
Example 11
Source File: PoissonDistributionImpl.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create a new Poisson distribution with the given mean, convergence criterion
 * and maximum number of iterations.
 *
 * @param p Poisson mean.
 * @param epsilon Convergence criterion for cumulative probabilities.
 * @param maxIterations the maximum number of iterations for cumulative
 * probabilities.
 * @since 2.1
 */
public PoissonDistributionImpl(double p, double epsilon, int maxIterations) {
    if (p <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p);
    }
    mean = p;
    normal = new NormalDistributionImpl(p, FastMath.sqrt(p));
    this.epsilon = epsilon;
    this.maxIterations = maxIterations;
}