1#ifndef __PRNG_EXTENSION_HPP__
2#define __PRNG_EXTENSION_HPP__
4#include "Kokkos_Macros.hpp"
5#include <Kokkos_Core.hpp>
6#include <Kokkos_MathematicalConstants.hpp>
7#include <Kokkos_Random.hpp>
9#include <common/traits.hpp>
46 template <
typename T,
typename F,
class DeviceType>
49 { obj.draw(gen) } -> std::same_as<F>;
50 { obj.mean() } -> std::same_as<F>;
51 { obj.var() } -> std::same_as<F>;
52 { obj.skewness() } -> std::same_as<F>;
73 template <FloatingPo
intType F> KOKKOS_INLINE_FUNCTION F
erfinv(F x)
78 constexpr F a = 0.147;
79 constexpr F inv_a = 1. / a;
80 constexpr F tmp = (2 / (M_PI * a));
81 const double ln1mx2 = Kokkos::log((1. - x) * (1. + x));
82 const F term1 = tmp + (0.5 * ln1mx2);
83 const F term2 = inv_a * ln1mx2;
84 return Kokkos::copysign(Kokkos::sqrt(Kokkos::sqrt(term1 * term1 - term2) - term1), x);
122 template <FloatingPo
intType F> KOKKOS_INLINE_FUNCTION F
norminv(F p, F mean, F stddev)
124 constexpr F erfinv_lb = -5;
125 constexpr F erfinv_up = 5;
126 auto clamped_inverse = Kokkos::clamp(
erfinv(2 * p - 1), erfinv_lb, erfinv_up);
127 KOKKOS_ASSERT(Kokkos::isfinite(stddev * Kokkos::numbers::sqrt2 * clamped_inverse));
128 return mean + stddev * Kokkos::numbers::sqrt2 * clamped_inverse;
148 constexpr double inv_sqrt_2_pi = 0.3989422804014327;
149 return inv_sqrt_2_pi * Kokkos::exp(-0.5 * x * x);
168 return 0.5 * (1 + Kokkos::erf(x / Kokkos::numbers::sqrt2));
182 template <FloatingPo
intType F>
struct Normal
193 template <
class DeviceType>
194 KOKKOS_INLINE_FUNCTION F
draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
207 template <
class DeviceType>
208 static KOKKOS_INLINE_FUNCTION F
draw_from(Kokkos::Random_XorShift1024<DeviceType>& gen,
219 KOKKOS_INLINE_FUNCTION F
mean()
const
228 KOKKOS_INLINE_FUNCTION F
var()
const
277 template <
class DeviceType>
278 KOKKOS_INLINE_FUNCTION F
draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
283 template <
class DeviceType>
284 static KOKKOS_INLINE_FUNCTION F
288 const F rand =
static_cast<F
>(gen.drand());
294 F zl = Kokkos::clamp((
lower -
mu) /
sigma, F(-5e3), F(0));
295 F zu = Kokkos::clamp((
upper -
mu) /
sigma, F(0), F(5e3));
297 F pl = 0.5 * Kokkos::erfc(-zl / Kokkos::numbers::sqrt2);
298 KOKKOS_ASSERT(Kokkos::isfinite(pl) &&
299 "Truncated normal draw leads is Nan of Inf with given parameters");
300 F pu = 0.5 * Kokkos::erfc(-zu / Kokkos::numbers::sqrt2);
301 KOKKOS_ASSERT(Kokkos::isfinite(pu) &&
302 "Truncated normal draw leads is Nan of Inf with given parameters");
303 F p = rand * (pu - pl) + pl;
305 KOKKOS_ASSERT(Kokkos::isfinite(x) &&
306 "Truncated normal draw leads is Nan of Inf with given parameters");
312 KOKKOS_INLINE_FUNCTION F
mean()
const
320 KOKKOS_INLINE_FUNCTION F
var()
const
327 const auto tmp = (phi_a - phi_b) / Z;
328 const auto tmp2 = (alpha * phi_a - beta * phi_b) / Z;
329 return sigma *
sigma * (1 - tmp2 - tmp * tmp);
361 template <
class DeviceType>
363 Kokkos::Random_XorShift1024<DeviceType>& gen, F factor, F mu, F sigma, F lower, F upper)
367 gen, factor * mu, factor * sigma, factor * lower, factor * upper);
370 template <
class DeviceType>
371 KOKKOS_INLINE_FUNCTION F
draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
376 KOKKOS_INLINE_FUNCTION F
mean()
const
382 KOKKOS_INLINE_FUNCTION F
var()
const
404 template <
class DeviceType>
405 KOKKOS_INLINE_FUNCTION F
draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
407 return Kokkos::exp(gen.normal(
mu,
sigma));
410 KOKKOS_INLINE_FUNCTION F
mean()
const
414 KOKKOS_INLINE_FUNCTION F
var()
const
417 return (Kokkos::exp(sigma2) - 1) * Kokkos::exp(2 *
mu + sigma2);
422 return (Kokkos::exp(sigma2) + 2) * Kokkos::sqrt(Kokkos::exp(sigma2) - 1);
439 template <
class DeviceType>
440 KOKKOS_INLINE_FUNCTION F
draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
443 const double Z0 = gen.normal();
444 const double Z1 = gen.normal();
446 const double scale_factor = Kokkos::sqrt(1. + delta * delta);
447 const double X = (Z0 + delta * Kokkos::abs(Z1)) / scale_factor;
450 KOKKOS_INLINE_FUNCTION F
mean()
const
454 KOKKOS_INLINE_FUNCTION F
var()
const
457 return omega *
omega * (1 - 2 * delta * delta / M_PI);
462 return ((4 - M_PI) * Kokkos::pow(delta * std::sqrt(2 / M_PI), 3)) /
463 Kokkos::pow(1 - 2 * delta * delta / M_PI, 1.5);
Concept for probability distribution laws.
Definition prng_extension.hpp:47
Kokkos compatible method to draw from specific probability distribution.
Definition prng_extension.hpp:15
KOKKOS_INLINE_FUNCTION F erfinv(F x)
Computes an approximation of the inverse error function.
Definition prng_extension.hpp:73
KOKKOS_INLINE_FUNCTION F norminv(F p, F mean, F stddev)
Computes the inverse CDF (probit function) of a normal distribution.
Definition prng_extension.hpp:122
KOKKOS_INLINE_FUNCTION F std_normal_pdf(F x)
Computes the standard normal probability density function (PDF).
Definition prng_extension.hpp:145
KOKKOS_INLINE_FUNCTION F std_normal_cdf(F x)
Computes the standard normal cumulative distribution function (CDF).
Definition prng_extension.hpp:165
Represents a LogNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:400
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:405
F sigma
Definition prng_extension.hpp:402
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:410
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:419
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:414
F mu
Definition prng_extension.hpp:401
Represents a normal (Gaussian) probability distribution.
Definition prng_extension.hpp:183
F mu
Mean.
Definition prng_extension.hpp:184
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Draws a random sample from the distribution.
Definition prng_extension.hpp:194
static KOKKOS_INLINE_FUNCTION F draw_from(Kokkos::Random_XorShift1024< DeviceType > &gen, F mu, F sigma)
Static method to draw a sample from N(μ, σ).
Definition prng_extension.hpp:208
KOKKOS_INLINE_FUNCTION F mean() const
Returns the mean of the distribution.
Definition prng_extension.hpp:219
KOKKOS_INLINE_FUNCTION F skewness() const
Returns the skewness of the distribution.
Definition prng_extension.hpp:246
KOKKOS_INLINE_FUNCTION F stddev() const
Returns the standard deviation of the distribution.
Definition prng_extension.hpp:237
KOKKOS_INLINE_FUNCTION F var() const
Returns the variance of the distribution.
Definition prng_extension.hpp:228
F sigma
Standard deviation.
Definition prng_extension.hpp:185
Represents a Scaled TruncatedNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:349
F inverse_factor
Definition prng_extension.hpp:352
F scale_factor
Definition prng_extension.hpp:351
TruncatedNormal< F > dist
Definition prng_extension.hpp:353
constexpr ScaledTruncatedNormal(F factor, F m, F s, F l, F u)
Definition prng_extension.hpp:355
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:386
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:382
static KOKKOS_INLINE_FUNCTION F draw_from(Kokkos::Random_XorShift1024< DeviceType > &gen, F factor, F mu, F sigma, F lower, F upper)
Definition prng_extension.hpp:362
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:371
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:376
Represents a SkewNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:434
F alpha
Definition prng_extension.hpp:437
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:459
F omega
Definition prng_extension.hpp:436
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:454
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:450
F xi
Definition prng_extension.hpp:435
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:440
Represents a TruncatedNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:262
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:312
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:331
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:278
KOKKOS_INLINE_FUNCTION constexpr TruncatedNormal(F m, F s, F l, F u)
Definition prng_extension.hpp:269
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:320
F upper
Definition prng_extension.hpp:267
F lower
Definition prng_extension.hpp:266
F sigma
Definition prng_extension.hpp:265
F mu
Definition prng_extension.hpp:264
static KOKKOS_INLINE_FUNCTION F draw_from(Kokkos::Random_XorShift1024< DeviceType > &gen, F mu, F sigma, F lower, F upper)
Definition prng_extension.hpp:285