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 * @since 1.0
026 *
027 * @deprecated Since version 1.1. Please use {@link BoxMullerNormalizedGaussianSampler}
028 * and {@link GaussianSampler} instead.
029 */
030@Deprecated
031public class BoxMullerGaussianSampler
032    extends SamplerBase
033    implements ContinuousSampler {
034    /** Next gaussian. */
035    private double nextGaussian = Double.NaN;
036    /** Mean. */
037    private final double mean;
038    /** standardDeviation. */
039    private final double standardDeviation;
040    /** Underlying source of randomness. */
041    private final UniformRandomProvider rng;
042
043    /**
044     * @param rng Generator of uniformly distributed random numbers.
045     * @param mean Mean of the Gaussian distribution.
046     * @param standardDeviation Standard deviation of the Gaussian distribution.
047     */
048    public BoxMullerGaussianSampler(UniformRandomProvider rng,
049                                    double mean,
050                                    double standardDeviation) {
051        super(null);
052        this.rng = rng;
053        this.mean = mean;
054        this.standardDeviation = standardDeviation;
055    }
056
057    /** {@inheritDoc} */
058    @Override
059    public double sample() {
060        final double random;
061        if (Double.isNaN(nextGaussian)) {
062            // Generate a pair of Gaussian numbers.
063
064            final double x = rng.nextDouble();
065            final double y = rng.nextDouble();
066            final double alpha = 2 * Math.PI * x;
067            final double r = Math.sqrt(-2 * Math.log(y));
068
069            // Return the first element of the generated pair.
070            random = r * Math.cos(alpha);
071
072            // Keep second element of the pair for next invocation.
073            nextGaussian = r * Math.sin(alpha);
074        } else {
075            // Use the second element of the pair (generated at the
076            // previous invocation).
077            random = nextGaussian;
078
079            // Both elements of the pair have been used.
080            nextGaussian = Double.NaN;
081        }
082
083        return standardDeviation * random + mean;
084    }
085
086    /** {@inheritDoc} */
087    @Override
088    public String toString() {
089        return "Box-Muller Gaussian deviate [" + rng.toString() + "]";
090    }
091}