001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.rng.sampling.distribution;
018
019import org.apache.commons.rng.UniformRandomProvider;
020
021/**
022 * <a href="https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform">
023 * Box-Muller algorithm</a> for sampling from a Gaussian distribution.
024 *
025 * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
026 *
027 * @since 1.0
028 *
029 * @deprecated Since version 1.1. Please use {@link BoxMullerNormalizedGaussianSampler}
030 * and {@link GaussianSampler} instead.
031 */
032@Deprecated
033public class BoxMullerGaussianSampler
034    extends SamplerBase
035    implements ContinuousSampler {
036    /** Next gaussian. */
037    private double nextGaussian = Double.NaN;
038    /** Mean. */
039    private final double mean;
040    /** standardDeviation. */
041    private final double standardDeviation;
042    /** Underlying source of randomness. */
043    private final UniformRandomProvider rng;
044
045    /**
046     * @param rng Generator of uniformly distributed random numbers.
047     * @param mean Mean of the Gaussian distribution.
048     * @param standardDeviation Standard deviation of the Gaussian distribution.
049     * @throws IllegalArgumentException if {@code standardDeviation <= 0}
050     */
051    public BoxMullerGaussianSampler(UniformRandomProvider rng,
052                                    double mean,
053                                    double standardDeviation) {
054        super(null);
055        if (standardDeviation <= 0) {
056            throw new IllegalArgumentException("standard deviation is not strictly positive: " +
057                standardDeviation);
058        }
059        this.rng = rng;
060        this.mean = mean;
061        this.standardDeviation = standardDeviation;
062    }
063
064    /** {@inheritDoc} */
065    @Override
066    public double sample() {
067        double random;
068        if (Double.isNaN(nextGaussian)) {
069            // Generate a pair of Gaussian numbers.
070
071            // Avoid zero for the uniform deviate y.
072            // The extreme tail of the sample is:
073            // y = 2^-53
074            // r = 8.57167
075            final double x = rng.nextDouble();
076            final double y = InternalUtils.makeNonZeroDouble(rng.nextLong());
077            final double alpha = 2 * Math.PI * x;
078            final double r = Math.sqrt(-2 * Math.log(y));
079
080            // Return the first element of the generated pair.
081            random = r * Math.cos(alpha);
082
083            // Keep second element of the pair for next invocation.
084            nextGaussian = r * Math.sin(alpha);
085        } else {
086            // Use the second element of the pair (generated at the
087            // previous invocation).
088            random = nextGaussian;
089
090            // Both elements of the pair have been used.
091            nextGaussian = Double.NaN;
092        }
093
094        return standardDeviation * random + mean;
095    }
096
097    /** {@inheritDoc} */
098    @Override
099    public String toString() {
100        return "Box-Muller Gaussian deviate [" + rng.toString() + "]";
101    }
102}