AbstractRealDistribution.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.distribution;

  18. import org.apache.commons.statistics.distribution.ContinuousDistribution;
  19. import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
  20. import org.apache.commons.math4.legacy.analysis.solvers.UnivariateSolverUtils;
  21. import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
  22. import org.apache.commons.math4.legacy.exception.OutOfRangeException;
  23. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
  24. import org.apache.commons.rng.UniformRandomProvider;
  25. import org.apache.commons.rng.sampling.distribution.InverseTransformContinuousSampler;
  26. import org.apache.commons.math4.core.jdkmath.JdkMath;

  27. /**
  28.  * Base class for probability distributions on the reals.
  29.  * Default implementations are provided for some of the methods
  30.  * that do not vary from distribution to distribution.
  31.  *
  32.  * <p>
  33.  * This base class provides a default factory method for creating
  34.  * a {@link org.apache.commons.statistics.distribution.ContinuousDistribution.Sampler
  35.  * sampler instance} that uses the
  36.  * <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling">
  37.  * inversion method</a> for generating random samples that follow the
  38.  * distribution.
  39.  * </p>
  40.  *
  41.  * @since 3.0
  42.  */
  43. public abstract class AbstractRealDistribution
  44.     implements ContinuousDistribution {
  45.     /** Default absolute accuracy for inverse cumulative computation. */
  46.     public static final double SOLVER_DEFAULT_ABSOLUTE_ACCURACY = 1e-6;

  47.     /**
  48.      * For a random variable {@code X} whose values are distributed according
  49.      * to this distribution, this method returns {@code P(x0 < X <= x1)}.
  50.      *
  51.      * @param x0 Lower bound (excluded).
  52.      * @param x1 Upper bound (included).
  53.      * @return the probability that a random variable with this distribution
  54.      * takes a value between {@code x0} and {@code x1}, excluding the lower
  55.      * and including the upper endpoint.
  56.      * @throws NumberIsTooLargeException if {@code x0 > x1}.
  57.      *
  58.      * The default implementation uses the identity
  59.      * {@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}
  60.      *
  61.      * @since 3.1
  62.      */
  63.     @Override
  64.     public double probability(double x0,
  65.                               double x1) {
  66.         if (x0 > x1) {
  67.             throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT,
  68.                                                 x0, x1, true);
  69.         }
  70.         return cumulativeProbability(x1) - cumulativeProbability(x0);
  71.     }

  72.     /**
  73.      * {@inheritDoc}
  74.      *
  75.      * The default implementation returns
  76.      * <ul>
  77.      * <li>{@link #getSupportLowerBound()} for {@code p = 0},</li>
  78.      * <li>{@link #getSupportUpperBound()} for {@code p = 1}.</li>
  79.      * </ul>
  80.      */
  81.     @Override
  82.     public double inverseCumulativeProbability(final double p) throws OutOfRangeException {
  83.         /*
  84.          * IMPLEMENTATION NOTES
  85.          * --------------------
  86.          * Where applicable, use is made of the one-sided Chebyshev inequality
  87.          * to bracket the root. This inequality states that
  88.          * P(X - mu >= k * sig) <= 1 / (1 + k^2),
  89.          * mu: mean, sig: standard deviation. Equivalently
  90.          * 1 - P(X < mu + k * sig) <= 1 / (1 + k^2),
  91.          * F(mu + k * sig) >= k^2 / (1 + k^2).
  92.          *
  93.          * For k = sqrt(p / (1 - p)), we find
  94.          * F(mu + k * sig) >= p,
  95.          * and (mu + k * sig) is an upper-bound for the root.
  96.          *
  97.          * Then, introducing Y = -X, mean(Y) = -mu, sd(Y) = sig, and
  98.          * P(Y >= -mu + k * sig) <= 1 / (1 + k^2),
  99.          * P(-X >= -mu + k * sig) <= 1 / (1 + k^2),
  100.          * P(X <= mu - k * sig) <= 1 / (1 + k^2),
  101.          * F(mu - k * sig) <= 1 / (1 + k^2).
  102.          *
  103.          * For k = sqrt((1 - p) / p), we find
  104.          * F(mu - k * sig) <= p,
  105.          * and (mu - k * sig) is a lower-bound for the root.
  106.          *
  107.          * In cases where the Chebyshev inequality does not apply, geometric
  108.          * progressions 1, 2, 4, ... and -1, -2, -4, ... are used to bracket
  109.          * the root.
  110.          */
  111.         if (p < 0.0 || p > 1.0) {
  112.             throw new OutOfRangeException(p, 0, 1);
  113.         }

  114.         double lowerBound = getSupportLowerBound();
  115.         if (p == 0.0) {
  116.             return lowerBound;
  117.         }

  118.         double upperBound = getSupportUpperBound();
  119.         if (p == 1.0) {
  120.             return upperBound;
  121.         }

  122.         final double mu = getMean();
  123.         final double sig = JdkMath.sqrt(getVariance());
  124.         final boolean chebyshevApplies;
  125.         chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) ||
  126.                              Double.isInfinite(sig) || Double.isNaN(sig));

  127.         if (lowerBound == Double.NEGATIVE_INFINITY) {
  128.             if (chebyshevApplies) {
  129.                 lowerBound = mu - sig * JdkMath.sqrt((1. - p) / p);
  130.             } else {
  131.                 lowerBound = -1.0;
  132.                 while (cumulativeProbability(lowerBound) >= p) {
  133.                     lowerBound *= 2.0;
  134.                 }
  135.             }
  136.         }

  137.         if (upperBound == Double.POSITIVE_INFINITY) {
  138.             if (chebyshevApplies) {
  139.                 upperBound = mu + sig * JdkMath.sqrt(p / (1. - p));
  140.             } else {
  141.                 upperBound = 1.0;
  142.                 while (cumulativeProbability(upperBound) < p) {
  143.                     upperBound *= 2.0;
  144.                 }
  145.             }
  146.         }

  147.         final UnivariateFunction toSolve = new UnivariateFunction() {
  148.             /** {@inheritDoc} */
  149.             @Override
  150.             public double value(final double x) {
  151.                 return cumulativeProbability(x) - p;
  152.             }
  153.         };

  154.         return UnivariateSolverUtils.solve(toSolve,
  155.                                            lowerBound,
  156.                                            upperBound,
  157.                                            getSolverAbsoluteAccuracy());
  158.     }

  159.     /**
  160.      * Returns the solver absolute accuracy for inverse cumulative computation.
  161.      * You can override this method in order to use a Brent solver with an
  162.      * absolute accuracy different from the default.
  163.      *
  164.      * @return the maximum absolute error in inverse cumulative probability estimates
  165.      */
  166.     protected double getSolverAbsoluteAccuracy() {
  167.         return SOLVER_DEFAULT_ABSOLUTE_ACCURACY;
  168.     }

  169.     /**
  170.      * {@inheritDoc}
  171.      * <p>
  172.      * The default implementation simply computes the logarithm of {@code density(x)}.
  173.      */
  174.     @Override
  175.     public double logDensity(double x) {
  176.         return JdkMath.log(density(x));
  177.     }

  178.     /**
  179.      * Utility function for allocating an array and filling it with {@code n}
  180.      * samples generated by the given {@code sampler}.
  181.      *
  182.      * @param n Number of samples.
  183.      * @param sampler Sampler.
  184.      * @return an array of size {@code n}.
  185.      */
  186.     public static double[] sample(int n,
  187.                                   ContinuousDistribution.Sampler sampler) {
  188.         final double[] samples = new double[n];
  189.         for (int i = 0; i < n; i++) {
  190.             samples[i] = sampler.sample();
  191.         }
  192.         return samples;
  193.     }

  194.     /**{@inheritDoc} */
  195.     @Override
  196.     public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
  197.         // Inversion method distribution sampler.
  198.         return InverseTransformContinuousSampler.of(rng, this::inverseCumulativeProbability)::sample;
  199.     }
  200. }