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 final double x = rng.nextDouble(); 072 final double y = rng.nextDouble(); 073 final double alpha = 2 * Math.PI * x; 074 final double r = Math.sqrt(-2 * Math.log(y)); 075 076 // Return the first element of the generated pair. 077 random = r * Math.cos(alpha); 078 079 // Keep second element of the pair for next invocation. 080 nextGaussian = r * Math.sin(alpha); 081 } else { 082 // Use the second element of the pair (generated at the 083 // previous invocation). 084 random = nextGaussian; 085 086 // Both elements of the pair have been used. 087 nextGaussian = Double.NaN; 088 } 089 090 return standardDeviation * random + mean; 091 } 092 093 /** {@inheritDoc} */ 094 @Override 095 public String toString() { 096 return "Box-Muller Gaussian deviate [" + rng.toString() + "]"; 097 } 098}