1#ifndef __PRNG_EXTENSION_HPP__
2#define __PRNG_EXTENSION_HPP__
4#include "Kokkos_Macros.hpp"
5#include "common/maths.hpp"
7#include <Kokkos_Core.hpp>
8#include <Kokkos_MathematicalConstants.hpp>
9#include <Kokkos_Random.hpp>
11#include <common/traits.hpp>
49 template <
typename T,
typename F,
class DeviceType>
52 requires(
const T& obj, Kokkos::Random_XorShift1024<DeviceType>& gen) {
53 { obj.draw(gen) } -> std::same_as<F>;
54 { obj.mean() } -> std::same_as<F>;
55 { obj.var() } -> std::same_as<F>;
56 { obj.skewness() } -> std::same_as<F>;
78 template <FloatingPo
intType F> KOKKOS_INLINE_FUNCTION F
erfinv(F x)
84 constexpr F a = 0.147;
85 constexpr F inv_a = 1. / a;
86 constexpr F tmp = (2 / (M_PI * a));
87 const double ln1mx2 = Kokkos::log((1. - x) * (1. + x));
88 const F term1 = tmp + (0.5 * ln1mx2);
89 const F term2 = inv_a * ln1mx2;
90 return Kokkos::copysign(
91 Kokkos::sqrt(Kokkos::sqrt(term1 * term1 - term2) - term1), x);
132 template <FloatingPo
intType F>
133 KOKKOS_INLINE_FUNCTION F
norminv(F p, F mean, F stddev)
135 constexpr F erfinv_lb = -5;
136 constexpr F erfinv_up = 5;
137 auto clamped_inverse =
138 Kokkos::clamp(
erfinv(2 * p - 1), erfinv_lb, erfinv_up);
140 Kokkos::isfinite(stddev * Kokkos::numbers::sqrt2 * clamped_inverse));
141 return mean + stddev * Kokkos::numbers::sqrt2 * clamped_inverse;
161 constexpr double inv_sqrt_2_pi = 0.3989422804014327;
162 return inv_sqrt_2_pi * Kokkos::exp(-0.5 * x * x);
183 return 0.5 * (1 + Kokkos::erf(x / Kokkos::numbers::sqrt2));
197 template <FloatingPo
intType F>
struct Normal
208 template <
class DeviceType>
209 KOKKOS_INLINE_FUNCTION F
210 draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
223 template <
class DeviceType>
224 static KOKKOS_INLINE_FUNCTION F
234 KOKKOS_INLINE_FUNCTION F
mean()
const
243 KOKKOS_INLINE_FUNCTION F
var()
const
292 template <
class DeviceType>
293 KOKKOS_INLINE_FUNCTION F
294 draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
299 template <
class DeviceType>
300 static KOKKOS_INLINE_FUNCTION F
308 const F rand =
static_cast<F
>(gen.drand());
314 F zl = Kokkos::clamp(
316 F zu = Kokkos::clamp(
319 F pl = 0.5 * Kokkos::erfc(-zl / Kokkos::numbers::sqrt2);
321 Kokkos::isfinite(pl) &&
322 "Truncated normal draw leads to Nan of Inf with given parameters");
323 F pu = 0.5 * Kokkos::erfc(-zu / Kokkos::numbers::sqrt2);
325 Kokkos::isfinite(pu) &&
326 "Truncated normal draw leads to Nan of Inf with given parameters");
327 F p = rand * (pu - pl) + pl;
330 Kokkos::isfinite(x) &&
331 "Truncated normal draw leads to Nan of Inf with given parameters");
337 KOKKOS_INLINE_FUNCTION F
mean()
const
345 KOKKOS_INLINE_FUNCTION F
var()
const
352 const auto tmp = (phi_a - phi_b) / Z;
353 const auto tmp2 = (alpha * phi_a - beta * phi_b) / Z;
354 return sigma *
sigma * (1 - tmp2 - tmp * tmp);
389 template <
class DeviceType>
390 static KOKKOS_INLINE_FUNCTION F
406 template <
class DeviceType>
407 KOKKOS_INLINE_FUNCTION F
408 draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
413 KOKKOS_INLINE_FUNCTION F
mean()
const
419 KOKKOS_INLINE_FUNCTION F
var()
const
441 template <
class DeviceType>
442 KOKKOS_INLINE_FUNCTION F
443 draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
445 return Kokkos::exp(gen.normal(
mu,
sigma));
448 KOKKOS_INLINE_FUNCTION F
mean()
const
452 KOKKOS_INLINE_FUNCTION F
var()
const
455 return (Kokkos::exp(sigma2) - 1) * Kokkos::exp(2 *
mu + sigma2);
460 return (Kokkos::exp(sigma2) + 2) * Kokkos::sqrt(Kokkos::exp(sigma2) - 1);
478 template <
class DeviceType>
479 KOKKOS_INLINE_FUNCTION F
480 draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
483 const double Z0 = gen.normal();
484 const double Z1 = gen.normal();
486 const double scale_factor = Kokkos::sqrt(1. + delta * delta);
487 const double X = (Z0 + delta * Kokkos::abs(Z1)) / scale_factor;
490 KOKKOS_INLINE_FUNCTION F
mean()
const
493 Kokkos::sqrt(2 / M_PI);
495 KOKKOS_INLINE_FUNCTION F
var()
const
498 return omega *
omega * (1 - 2 * delta * delta / M_PI);
503 return ((4 - M_PI) * Kokkos::pow(delta * std::sqrt(2 / M_PI), 3)) /
504 Kokkos::pow(1 - 2 * delta * delta / M_PI, 1.5);
508 static_assert(ProbabilityLaw<SkewNormal<float>, float,
ComputeSpace>);
523 template <
class DeviceType>
524 KOKKOS_INLINE_FUNCTION F
525 draw(Kokkos::Random_XorShift1024<DeviceType>& gen)
const
527 const float rnd = gen.frand();
531 [[nodiscard]] KOKKOS_INLINE_FUNCTION F
mean()
const
535 [[nodiscard]] KOKKOS_INLINE_FUNCTION F
var()
const
540 [[nodiscard]] KOKKOS_INLINE_FUNCTION F
skewness()
const
546 static_assert(ProbabilityLaw<Exponential<float>, float,
ComputeSpace>);
Concept for probability distribution laws.
Definition prng_extension.hpp:50
KOKKOS_INLINE_FUNCTION float _ln(float x)
Definition maths.hpp:10
Kokkos compatible method to draw from specific probability distribution.
Definition prng_extension.hpp:17
KOKKOS_INLINE_FUNCTION F erfinv(F x)
Computes an approximation of the inverse error function.
Definition prng_extension.hpp:78
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:133
KOKKOS_INLINE_FUNCTION F std_normal_pdf(F x)
Computes the standard normal probability density function (PDF).
Definition prng_extension.hpp:158
KOKKOS_INLINE_FUNCTION F std_normal_cdf(F x)
Computes the standard normal cumulative distribution function (CDF).
Definition prng_extension.hpp:180
Kokkos::DefaultExecutionSpace ComputeSpace
Definition alias.hpp:23
Represents a Exponential probability distribution.
Definition prng_extension.hpp:518
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:540
static constexpr bool use_kokkos_log
Definition prng_extension.hpp:521
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:531
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:535
F lambda
Definition prng_extension.hpp:519
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:525
Represents a LogNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:437
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:443
F sigma
Definition prng_extension.hpp:439
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:448
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:457
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:452
F mu
Definition prng_extension.hpp:438
Represents a normal (Gaussian) probability distribution.
Definition prng_extension.hpp:198
F mu
Mean.
Definition prng_extension.hpp:199
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Draws a random sample from the distribution.
Definition prng_extension.hpp:210
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:225
KOKKOS_INLINE_FUNCTION F mean() const
Returns the mean of the distribution.
Definition prng_extension.hpp:234
KOKKOS_INLINE_FUNCTION F skewness() const
Returns the skewness of the distribution.
Definition prng_extension.hpp:261
KOKKOS_INLINE_FUNCTION F stddev() const
Returns the standard deviation of the distribution.
Definition prng_extension.hpp:252
KOKKOS_INLINE_FUNCTION F var() const
Returns the variance of the distribution.
Definition prng_extension.hpp:243
F sigma
Standard deviation.
Definition prng_extension.hpp:200
F inverse_factor
Definition prng_extension.hpp:377
F scale_factor
Definition prng_extension.hpp:376
TruncatedNormal< F > dist
Definition prng_extension.hpp:378
constexpr ScaledTruncatedNormal(F factor, F m, F s, F l, F u)
Definition prng_extension.hpp:380
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:423
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:419
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:391
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:408
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:413
Represents a SkewNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:473
F alpha
Definition prng_extension.hpp:476
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:500
F omega
Definition prng_extension.hpp:475
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:495
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:490
F xi
Definition prng_extension.hpp:474
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:480
Represents a TruncatedNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:277
KOKKOS_INLINE_FUNCTION F mean() const
Definition prng_extension.hpp:337
KOKKOS_INLINE_FUNCTION F skewness() const
Definition prng_extension.hpp:356
KOKKOS_INLINE_FUNCTION F draw(Kokkos::Random_XorShift1024< DeviceType > &gen) const
Definition prng_extension.hpp:294
KOKKOS_INLINE_FUNCTION constexpr TruncatedNormal(F m, F s, F l, F u)
Definition prng_extension.hpp:284
KOKKOS_INLINE_FUNCTION F var() const
Definition prng_extension.hpp:345
F upper
Definition prng_extension.hpp:282
F lower
Definition prng_extension.hpp:281
F sigma
Definition prng_extension.hpp:280
F mu
Definition prng_extension.hpp:279
static KOKKOS_INLINE_FUNCTION F draw_from(Kokkos::Random_XorShift1024< DeviceType > &gen, F mu, F sigma, F lower, F upper)
Definition prng_extension.hpp:301