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 * Sampling from an <a href="http://mathworld.wolfram.com/ExponentialDistribution.html">exponential distribution</a>. 023 * 024 * <p>Sampling uses:</p> 025 * 026 * <ul> 027 * <li>{@link UniformRandomProvider#nextLong()} 028 * <li>{@link UniformRandomProvider#nextDouble()} 029 * </ul> 030 * 031 * @since 1.0 032 */ 033public class AhrensDieterExponentialSampler 034 extends SamplerBase 035 implements SharedStateContinuousSampler { 036 /** 037 * Table containing the constants 038 * \( q_i = sum_{j=1}^i (\ln 2)^j / j! = \ln 2 + (\ln 2)^2 / 2 + ... + (\ln 2)^i / i! \) 039 * until the largest representable fraction below 1 is exceeded. 040 * 041 * Note that 042 * \( 1 = 2 - 1 = \exp(\ln 2) - 1 = sum_{n=1}^\infinity (\ln 2)^n / n! \) 043 * thus \( q_i \rightarrow 1 as i \rightarrow +\infinity \), 044 * so the higher \( i \), the closer we get to 1 (the series is not alternating). 045 * 046 * By trying, n = 16 in Java is enough to reach 1. 047 */ 048 private static final double[] EXPONENTIAL_SA_QI = new double[16]; 049 /** The mean of this distribution. */ 050 private final double mean; 051 /** Underlying source of randomness. */ 052 private final UniformRandomProvider rng; 053 054 // 055 // Initialize tables. 056 // 057 static { 058 // 059 // Filling EXPONENTIAL_SA_QI table. 060 // Note that we don't want qi = 0 in the table. 061 // 062 final double ln2 = Math.log(2); 063 double qi = 0; 064 065 for (int i = 0; i < EXPONENTIAL_SA_QI.length; i++) { 066 qi += Math.pow(ln2, i + 1.0) / InternalUtils.factorial(i + 1); 067 EXPONENTIAL_SA_QI[i] = qi; 068 } 069 } 070 071 /** 072 * @param rng Generator of uniformly distributed random numbers. 073 * @param mean Mean of this distribution. 074 * @throws IllegalArgumentException if {@code mean <= 0} 075 */ 076 public AhrensDieterExponentialSampler(UniformRandomProvider rng, 077 double mean) { 078 super(null); 079 if (mean <= 0) { 080 throw new IllegalArgumentException("mean is not strictly positive: " + mean); 081 } 082 this.rng = rng; 083 this.mean = mean; 084 } 085 086 /** 087 * @param rng Generator of uniformly distributed random numbers. 088 * @param source Source to copy. 089 */ 090 private AhrensDieterExponentialSampler(UniformRandomProvider rng, 091 AhrensDieterExponentialSampler source) { 092 super(null); 093 this.rng = rng; 094 this.mean = source.mean; 095 } 096 097 /** {@inheritDoc} */ 098 @Override 099 public double sample() { 100 // Step 1: 101 double a = 0; 102 // Avoid u=0 which creates an infinite loop 103 double u = InternalUtils.makeNonZeroDouble(rng.nextLong()); 104 105 // Step 2 and 3: 106 while (u < 0.5) { 107 a += EXPONENTIAL_SA_QI[0]; 108 u *= 2; 109 } 110 111 // Step 4 (now u >= 0.5): 112 u += u - 1; 113 114 // Step 5: 115 if (u <= EXPONENTIAL_SA_QI[0]) { 116 return mean * (a + u); 117 } 118 119 // Step 6: 120 int i = 0; // Should be 1, be we iterate before it in while using 0. 121 double u2 = rng.nextDouble(); 122 double umin = u2; 123 124 // Step 7 and 8: 125 do { 126 ++i; 127 u2 = rng.nextDouble(); 128 129 if (u2 < umin) { 130 umin = u2; 131 } 132 133 // Step 8: 134 } while (u > EXPONENTIAL_SA_QI[i]); // Ensured to exit since EXPONENTIAL_SA_QI[MAX] = 1. 135 136 return mean * (a + umin * EXPONENTIAL_SA_QI[0]); 137 } 138 139 /** {@inheritDoc} */ 140 @Override 141 public String toString() { 142 return "Ahrens-Dieter Exponential deviate [" + rng.toString() + "]"; 143 } 144 145 /** 146 * {@inheritDoc} 147 * 148 * @since 1.3 149 */ 150 @Override 151 public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) { 152 return new AhrensDieterExponentialSampler(rng, this); 153 } 154 155 /** 156 * Create a new exponential distribution sampler. 157 * 158 * @param rng Generator of uniformly distributed random numbers. 159 * @param mean Mean of the distribution. 160 * @return the sampler 161 * @throws IllegalArgumentException if {@code mean <= 0} 162 * @since 1.3 163 */ 164 public static SharedStateContinuousSampler of(UniformRandomProvider rng, 165 double mean) { 166 return new AhrensDieterExponentialSampler(rng, mean); 167 } 168}