BoxMullerGaussianSampler.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.rng.sampling.distribution;

  18. import org.apache.commons.rng.UniformRandomProvider;

  19. /**
  20.  * <a href="https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform">
  21.  * Box-Muller algorithm</a> for sampling from a Gaussian distribution.
  22.  *
  23.  * <p>Sampling uses:</p>
  24.  *
  25.  * <ul>
  26.  *   <li>{@link UniformRandomProvider#nextDouble()}
  27.  *   <li>{@link UniformRandomProvider#nextLong()}
  28.  * </ul>
  29.  *
  30.  * @since 1.0
  31.  *
  32.  * @deprecated Since version 1.1. Please use {@link BoxMullerNormalizedGaussianSampler}
  33.  * and {@link GaussianSampler} instead.
  34.  */
  35. @Deprecated
  36. public class BoxMullerGaussianSampler
  37.     extends SamplerBase
  38.     implements ContinuousSampler {
  39.     /** Next gaussian. */
  40.     private double nextGaussian = Double.NaN;
  41.     /** Mean. */
  42.     private final double mean;
  43.     /** standardDeviation. */
  44.     private final double standardDeviation;
  45.     /** Underlying source of randomness. */
  46.     private final UniformRandomProvider rng;

  47.     /**
  48.      * Create an instance.
  49.      *
  50.      * @param rng Generator of uniformly distributed random numbers.
  51.      * @param mean Mean of the Gaussian distribution.
  52.      * @param standardDeviation Standard deviation of the Gaussian distribution.
  53.      * @throws IllegalArgumentException if {@code standardDeviation <= 0}
  54.      */
  55.     public BoxMullerGaussianSampler(UniformRandomProvider rng,
  56.                                     double mean,
  57.                                     double standardDeviation) {
  58.         this(mean, InternalUtils.requireStrictlyPositiveFinite(standardDeviation, "standardDeviation"), rng);
  59.     }

  60.     /**
  61.      * @param rng Generator of uniformly distributed random numbers.
  62.      * @param mean Mean of the Gaussian distribution.
  63.      * @param standardDeviation Standard deviation of the Gaussian distribution.
  64.      */
  65.     private BoxMullerGaussianSampler(double mean,
  66.                                      double standardDeviation,
  67.                                      UniformRandomProvider rng) {
  68.         super(null);
  69.         this.rng = rng;
  70.         this.mean = mean;
  71.         this.standardDeviation = standardDeviation;
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public double sample() {
  76.         final double random;
  77.         if (Double.isNaN(nextGaussian)) {
  78.             // Generate a pair of Gaussian numbers.

  79.             // Avoid zero for the uniform deviate y.
  80.             // The extreme tail of the sample is:
  81.             // y = 2^-53
  82.             // r = 8.57167
  83.             final double x = rng.nextDouble();
  84.             final double y = InternalUtils.makeNonZeroDouble(rng.nextLong());
  85.             final double alpha = 2 * Math.PI * x;
  86.             final double r = Math.sqrt(-2 * Math.log(y));

  87.             // Return the first element of the generated pair.
  88.             random = r * Math.cos(alpha);

  89.             // Keep second element of the pair for next invocation.
  90.             nextGaussian = r * Math.sin(alpha);
  91.         } else {
  92.             // Use the second element of the pair (generated at the
  93.             // previous invocation).
  94.             random = nextGaussian;

  95.             // Both elements of the pair have been used.
  96.             nextGaussian = Double.NaN;
  97.         }

  98.         return standardDeviation * random + mean;
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public String toString() {
  103.         return "Box-Muller Gaussian deviate [" + rng.toString() + "]";
  104.     }
  105. }