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:</p> 026 * 027 * <ul> 028 * <li>{@link UniformRandomProvider#nextDouble()} 029 * <li>{@link UniformRandomProvider#nextLong()} 030 * </ul> 031 * 032 * @since 1.0 033 * 034 * @deprecated Since version 1.1. Please use {@link BoxMullerNormalizedGaussianSampler} 035 * and {@link GaussianSampler} instead. 036 */ 037@Deprecated 038public class BoxMullerGaussianSampler 039 extends SamplerBase 040 implements ContinuousSampler { 041 /** Next gaussian. */ 042 private double nextGaussian = Double.NaN; 043 /** Mean. */ 044 private final double mean; 045 /** standardDeviation. */ 046 private final double standardDeviation; 047 /** Underlying source of randomness. */ 048 private final UniformRandomProvider rng; 049 050 /** 051 * Create an instance. 052 * 053 * @param rng Generator of uniformly distributed random numbers. 054 * @param mean Mean of the Gaussian distribution. 055 * @param standardDeviation Standard deviation of the Gaussian distribution. 056 * @throws IllegalArgumentException if {@code standardDeviation <= 0} 057 */ 058 public BoxMullerGaussianSampler(UniformRandomProvider rng, 059 double mean, 060 double standardDeviation) { 061 this(mean, InternalUtils.requireStrictlyPositiveFinite(standardDeviation, "standardDeviation"), rng); 062 } 063 064 /** 065 * @param rng Generator of uniformly distributed random numbers. 066 * @param mean Mean of the Gaussian distribution. 067 * @param standardDeviation Standard deviation of the Gaussian distribution. 068 */ 069 private BoxMullerGaussianSampler(double mean, 070 double standardDeviation, 071 UniformRandomProvider rng) { 072 super(null); 073 this.rng = rng; 074 this.mean = mean; 075 this.standardDeviation = standardDeviation; 076 } 077 078 /** {@inheritDoc} */ 079 @Override 080 public double sample() { 081 final double random; 082 if (Double.isNaN(nextGaussian)) { 083 // Generate a pair of Gaussian numbers. 084 085 // Avoid zero for the uniform deviate y. 086 // The extreme tail of the sample is: 087 // y = 2^-53 088 // r = 8.57167 089 final double x = rng.nextDouble(); 090 final double y = InternalUtils.makeNonZeroDouble(rng.nextLong()); 091 final double alpha = 2 * Math.PI * x; 092 final double r = Math.sqrt(-2 * Math.log(y)); 093 094 // Return the first element of the generated pair. 095 random = r * Math.cos(alpha); 096 097 // Keep second element of the pair for next invocation. 098 nextGaussian = r * Math.sin(alpha); 099 } else { 100 // Use the second element of the pair (generated at the 101 // previous invocation). 102 random = nextGaussian; 103 104 // Both elements of the pair have been used. 105 nextGaussian = Double.NaN; 106 } 107 108 return standardDeviation * random + mean; 109 } 110 111 /** {@inheritDoc} */ 112 @Override 113 public String toString() { 114 return "Box-Muller Gaussian deviate [" + rng.toString() + "]"; 115 } 116}