21 #ifndef ROCRAND_POISSON_H_
22 #define ROCRAND_POISSON_H_
25 #define FQUALIFIERS __forceinline__ __device__
35 #include "rocrand/rocrand_lfsr113.h"
36 #include "rocrand/rocrand_mrg31k3p.h"
37 #include "rocrand/rocrand_mrg32k3a.h"
38 #include "rocrand/rocrand_mtgp32.h"
39 #include "rocrand/rocrand_philox4x32_10.h"
40 #include "rocrand/rocrand_scrambled_sobol32.h"
41 #include "rocrand/rocrand_scrambled_sobol64.h"
42 #include "rocrand/rocrand_sobol32.h"
43 #include "rocrand/rocrand_sobol64.h"
44 #include "rocrand/rocrand_threefry2x32_20.h"
45 #include "rocrand/rocrand_threefry2x64_20.h"
46 #include "rocrand/rocrand_threefry4x32_20.h"
47 #include "rocrand/rocrand_threefry4x64_20.h"
48 #include "rocrand/rocrand_xorwow.h"
50 #include "rocrand/rocrand_normal.h"
51 #include "rocrand/rocrand_uniform.h"
53 namespace rocrand_device {
56 constexpr
double lambda_threshold_small = 64.0;
57 constexpr
double lambda_threshold_huge = 4000.0;
59 template<
class State,
typename Result_Type =
unsigned int>
60 FQUALIFIERS Result_Type poisson_distribution_small(State& state,
double lambda)
64 const double limit = exp(-lambda);
73 while (product > limit);
79 double lgamma_approx(
const double x)
83 const double z = x - 1.0;
87 const double coefs[n] = {
88 0.99999999999980993227684700473478,
89 676.520368121885098567009190444019,
90 -1259.13921672240287047156078755283,
91 771.3234287776530788486528258894,
92 -176.61502916214059906584551354,
93 12.507343278686904814458936853,
94 -0.13857109526572011689554707,
95 9.984369578019570859563e-6,
96 1.50563273514931155834e-7
100 for (
int i = n - 1; i > 0; i--)
102 sum += coefs[i] / (z + i);
106 const double log_sqrt_2_pi = 0.9189385332046727418;
107 const double e = 2.718281828459045090796;
108 return (log_sqrt_2_pi + log(sum) - g) + (z + 0.5) * log((z + g + 0.5) / e);
111 template<
class State,
typename Result_Type =
unsigned int>
112 FQUALIFIERS Result_Type poisson_distribution_large(State& state,
double lambda)
116 const double c = 0.767 - 3.36 / lambda;
117 const double beta = ROCRAND_PI_DOUBLE / sqrt(3.0 * lambda);
118 const double alpha = beta * lambda;
119 const double k = log(c) - lambda - log(beta);
120 const double log_lambda = log(lambda);
125 const double x = (alpha - log((1.0 - u) / u)) / beta;
126 const double n = floor(x + 0.5);
132 const double y = alpha - beta * x;
133 const double t = 1.0 + exp(y);
134 const double lhs = y + log(v / (t * t));
135 const double rhs = k + n * log_lambda - lgamma_approx(n + 1.0);
138 return static_cast<Result_Type
>(n);
143 template<
class State,
typename Result_Type =
unsigned int>
144 FQUALIFIERS Result_Type poisson_distribution_huge(State& state,
double lambda)
149 return static_cast<Result_Type
>(round(sqrt(lambda) * n + lambda));
152 template<
class State,
typename Result_Type =
unsigned int>
153 FQUALIFIERS Result_Type poisson_distribution(State& state,
double lambda)
155 if (lambda < lambda_threshold_small)
157 return poisson_distribution_small<State, Result_Type>(state, lambda);
159 else if (lambda <= lambda_threshold_huge)
161 return poisson_distribution_large<State, Result_Type>(state, lambda);
165 return poisson_distribution_huge<State, Result_Type>(state, lambda);
169 template<
class State,
typename Result_Type =
unsigned int>
170 FQUALIFIERS Result_Type poisson_distribution_itr(State& state,
double lambda)
185 double upow = pow + 500.0;
186 double ex = exp(-500.0);
191 L = exp((
double)(pow - lambda));
199 x *= ((double)lambda / (
double) k);
202 }
while((
double)pow < lambda);
206 template<
class State,
typename Result_Type =
unsigned int>
207 FQUALIFIERS Result_Type poisson_distribution_inv(State& state,
double lambda)
211 return poisson_distribution_itr<State, Result_Type>(state, lambda);
215 return poisson_distribution_huge<State, Result_Type>(state, lambda);
233 #ifndef ROCRAND_DETAIL_PHILOX_BM_NOT_IN_STATE
237 return rocrand_device::detail::poisson_distribution<rocrand_state_philox4x32_10*, unsigned int>(
257 rocrand_device::detail::poisson_distribution<rocrand_state_philox4x32_10*, unsigned int>(
260 rocrand_device::detail::poisson_distribution<rocrand_state_philox4x32_10*, unsigned int>(
263 rocrand_device::detail::poisson_distribution<rocrand_state_philox4x32_10*, unsigned int>(
266 rocrand_device::detail::poisson_distribution<rocrand_state_philox4x32_10*, unsigned int>(
283 #ifndef ROCRAND_DETAIL_MRG31K3P_BM_NOT_IN_STATE
286 return rocrand_device::detail::poisson_distribution<rocrand_state_mrg31k3p*, unsigned int>(
303 #ifndef ROCRAND_DETAIL_MRG32K3A_BM_NOT_IN_STATE
307 return rocrand_device::detail::poisson_distribution<rocrand_state_mrg32k3a*, unsigned int>(
324 #ifndef ROCRAND_DETAIL_XORWOW_BM_NOT_IN_STATE
328 return rocrand_device::detail::poisson_distribution<rocrand_state_xorwow*, unsigned int>(
348 return rocrand_device::detail::poisson_distribution_inv<rocrand_state_mtgp32*, unsigned int>(
367 return rocrand_device::detail::poisson_distribution_inv<rocrand_state_sobol32*, unsigned int>(
386 return rocrand_device::detail::poisson_distribution_inv<rocrand_state_scrambled_sobol32*,
387 unsigned int>(state, lambda);
404 return rocrand_device::detail::poisson_distribution_inv<rocrand_state_sobol64*,
405 unsigned long long int>(state, lambda);
420 unsigned long long int rocrand_poisson(rocrand_state_scrambled_sobol64* state,
double lambda)
422 return rocrand_device::detail::poisson_distribution_inv<rocrand_state_scrambled_sobol64*,
423 unsigned long long int>(state, lambda);
440 return rocrand_device::detail::poisson_distribution_inv<rocrand_state_lfsr113*, unsigned int>(
458 return rocrand_device::detail::poisson_distribution_inv(state, lambda);
474 return rocrand_device::detail::poisson_distribution_inv(state, lambda);
490 return rocrand_device::detail::poisson_distribution_inv(state, lambda);
506 return rocrand_device::detail::poisson_distribution_inv(state, lambda);
FQUALIFIERS double rocrand_normal_double(rocrand_state_philox4x32_10 *state)
Returns a normally distributed double value.
Definition: rocrand_normal.h:456
FQUALIFIERS double rocrand_uniform_double(rocrand_state_philox4x32_10 *state)
Returns a uniformly distributed random double value from (0; 1] range.
Definition: rocrand_uniform.h:303
FQUALIFIERS uint4 rocrand_poisson4(rocrand_state_philox4x32_10 *state, double lambda)
Returns four Poisson-distributed unsigned int values using Philox generator.
Definition: rocrand_poisson.h:254
FQUALIFIERS unsigned int rocrand_poisson(rocrand_state_philox4x32_10 *state, double lambda)
Returns a Poisson-distributed unsigned int using Philox generator.
Definition: rocrand_poisson.h:235
#define FQUALIFIERS
Shorthand for commonly used function qualifiers.
Definition: rocrand_uniform.h:31