BioCMAMC-ST
prng_extension.hpp
1#ifndef __PRNG_EXTENSION_HPP__
2#define __PRNG_EXTENSION_HPP__
3
4#include "Kokkos_Macros.hpp"
5#include "common/maths.hpp"
6#include "mc/alias.hpp"
7#include <Kokkos_Core.hpp>
8#include <Kokkos_MathematicalConstants.hpp>
9#include <Kokkos_Random.hpp>
10#include <cmath>
11#include <common/traits.hpp>
12
17{
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>;
57 };
58
78 template <FloatingPointType F> KOKKOS_INLINE_FUNCTION F erfinv(F x)
79 {
80
81 // Use the Winitzki’s method to calculate get an approached expression of
82 // erf(x) and inverse it
83
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);
92 }
93
94 // Inverse error function approximation (Using the rational approximation)
95 // template <FloatingPointType F> KOKKOS_INLINE_FUNCTION constexpr F
96 // erf_inv(F x)
97 // {
98 // constexpr F a[4] = {0.147, 0.147, 0.147, 0.147};
99 // constexpr F b[4] = {-1.0, 0.5, -0.5, 1.0};
100 // KOKKOS_ASSERT(x <= -1.0 || x >= 1.0);
101
102 // F z = (x < 0.0) ? -x : x;
103
104 // F t = 2.0 / (Kokkos::numbers::pi * 0.147) + 0.5 * Kokkos::log(1.0 - z);
105 // F result = a[0] * Kokkos::pow(t, b[0]) +
106 // a[1] * Kokkos::pow(t, b[1]) +
107 // a[2] * Kokkos::pow(t, b[2]) +
108 // a[3] * Kokkos::pow(t, b[3]);
109 // KOKKOS_ASSERT(Kokkos::isfinite(result));
110 // return result;
111 // }
112
132 template <FloatingPointType F>
133 KOKKOS_INLINE_FUNCTION F norminv(F p, F mean, F stddev)
134 {
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);
139 KOKKOS_ASSERT(
140 Kokkos::isfinite(stddev * Kokkos::numbers::sqrt2 * clamped_inverse));
141 return mean + stddev * Kokkos::numbers::sqrt2 * clamped_inverse;
142 }
143
158 template <FloatingPointType F> KOKKOS_INLINE_FUNCTION F std_normal_pdf(F x)
159 {
160 // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
161 constexpr double inv_sqrt_2_pi = 0.3989422804014327; // 1/sqrt(2pi)
162 return inv_sqrt_2_pi * Kokkos::exp(-0.5 * x * x);
163 // NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
164 }
165
180 template <FloatingPointType F> KOKKOS_INLINE_FUNCTION F std_normal_cdf(F x)
181 {
182 // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
183 return 0.5 * (1 + Kokkos::erf(x / Kokkos::numbers::sqrt2));
184 // NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
185 }
186
187 /*DISTRIBUTIONS*/
188
197 template <FloatingPointType F> struct Normal
198 {
199 F mu;
201
208 template <class DeviceType>
209 KOKKOS_INLINE_FUNCTION F
210 draw(Kokkos::Random_XorShift1024<DeviceType>& gen) const
211 {
212 return draw_from(gen, mu, sigma);
213 }
214
223 template <class DeviceType>
224 static KOKKOS_INLINE_FUNCTION F
225 draw_from(Kokkos::Random_XorShift1024<DeviceType>& gen, F mu, F sigma)
226 {
227 return gen.normal(mu, sigma);
228 }
229
234 KOKKOS_INLINE_FUNCTION F mean() const
235 {
236 return mu;
237 }
238
243 KOKKOS_INLINE_FUNCTION F var() const
244 {
245 return sigma * sigma;
246 }
247
252 KOKKOS_INLINE_FUNCTION F stddev() const
253 {
254 return sigma;
255 }
256
261 KOKKOS_INLINE_FUNCTION F skewness() const
262 {
263 return F(0);
264 }
265 };
266
276 template <FloatingPointType F> struct TruncatedNormal
277 {
278
279 F mu; // Mean
280 F sigma; // Standard deviation
281 F lower; // Standard deviation
282 F upper; // Standard deviation
283
284 KOKKOS_INLINE_FUNCTION constexpr TruncatedNormal(F m, F s, F l, F u)
285 : mu(m), sigma(s), lower(l), upper(u)
286 {
287 X_ASSERT(mu > lower);
288 X_ASSERT(mu < upper);
289 KOKKOS_ASSERT(mu > lower && mu < upper);
290 }
291
292 template <class DeviceType>
293 KOKKOS_INLINE_FUNCTION F
294 draw(Kokkos::Random_XorShift1024<DeviceType>& gen) const
295 {
296 return draw_from(gen, mu, sigma, lower, upper);
297 }
298
299 template <class DeviceType>
300 static KOKKOS_INLINE_FUNCTION F
301 draw_from(Kokkos::Random_XorShift1024<DeviceType>& gen,
302 F mu,
303 F sigma,
304 F lower,
305 F upper)
306 {
307 // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
308 const F rand = static_cast<F>(gen.drand());
309
310 // Max bounded because if sigma <<1 z -> Inf not wanted because
311 // erf/erfc/erfinv are not stable for extrem value Min bounded if
312 // |mu-bound| <<1 z -> 0 which is also not wanted for error function
313
314 F zl = Kokkos::clamp(
315 (lower - mu) / sigma, F(-5e3), F(0)); // upper-mu is by defintion <0
316 F zu = Kokkos::clamp(
317 (upper - mu) / sigma, F(0), F(5e3)); // upper-mu is by defintion >0
318
319 F pl = 0.5 * Kokkos::erfc(-zl / Kokkos::numbers::sqrt2);
320 KOKKOS_ASSERT(
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);
324 KOKKOS_ASSERT(
325 Kokkos::isfinite(pu) &&
326 "Truncated normal draw leads to Nan of Inf with given parameters");
327 F p = rand * (pu - pl) + pl;
328 F x = norminv(p, mu, sigma);
329 KOKKOS_ASSERT(
330 Kokkos::isfinite(x) &&
331 "Truncated normal draw leads to Nan of Inf with given parameters");
332 return x;
333
334 // NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
335 }
336
337 KOKKOS_INLINE_FUNCTION F mean() const
338 {
339 const F alpha = (lower - mu) / sigma;
340 const F beta = (upper - mu) / sigma;
341 F Z = std_normal_cdf(beta) - std_normal_cdf(alpha);
342 return mu + sigma * (std_normal_pdf(alpha) - std_normal_pdf(beta)) / Z;
343 }
344
345 KOKKOS_INLINE_FUNCTION F var() const
346 {
347 const F alpha = (lower - mu) / sigma;
348 const F beta = (upper - mu) / sigma;
349 F Z = std_normal_cdf(beta) - std_normal_cdf(alpha);
350 const auto phi_b = std_normal_pdf(beta);
351 const auto phi_a = std_normal_pdf(alpha);
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);
355 }
356 KOKKOS_INLINE_FUNCTION F skewness() const
357 {
358 return 0.;
359 }
360 };
361
373 template <FloatingPointType F> struct ScaledTruncatedNormal
374 {
375
379
380 constexpr ScaledTruncatedNormal(F factor, F m, F s, F l, F u)
382 dist(scale_factor * m,
383 s * scale_factor,
384 scale_factor * l,
385 scale_factor * u)
386 {
387 }
388
389 template <class DeviceType>
390 static KOKKOS_INLINE_FUNCTION F
391 draw_from(Kokkos::Random_XorShift1024<DeviceType>& gen,
392 F factor,
393 F mu,
394 F sigma,
395 F lower,
396 F upper)
397 {
398 return 1. / factor *
400 factor * mu,
401 factor * sigma,
402 factor * lower,
403 factor * upper);
404 }
405
406 template <class DeviceType>
407 KOKKOS_INLINE_FUNCTION F
408 draw(Kokkos::Random_XorShift1024<DeviceType>& gen) const
409 {
410 return inverse_factor * dist.draw(gen);
411 }
412
413 KOKKOS_INLINE_FUNCTION F mean() const
414 {
415
416 return inverse_factor * dist.mean();
417 }
418
419 KOKKOS_INLINE_FUNCTION F var() const
420 {
421 return (inverse_factor * inverse_factor) * dist.var();
422 }
423 KOKKOS_INLINE_FUNCTION F skewness() const
424 {
425 return 0.;
426 }
427 };
428
436 template <FloatingPointType F> struct LogNormal
437 {
438 F mu; // Mean
439 F sigma; // Standard deviation
440
441 template <class DeviceType>
442 KOKKOS_INLINE_FUNCTION F
443 draw(Kokkos::Random_XorShift1024<DeviceType>& gen) const
444 {
445 return Kokkos::exp(gen.normal(mu, sigma));
446 }
447
448 KOKKOS_INLINE_FUNCTION F mean() const
449 {
450 return Kokkos::exp(mu + sigma * sigma / 2);
451 }
452 KOKKOS_INLINE_FUNCTION F var() const
453 {
454 const auto sigma2 = sigma * sigma;
455 return (Kokkos::exp(sigma2) - 1) * Kokkos::exp(2 * mu + sigma2);
456 }
457 KOKKOS_INLINE_FUNCTION F skewness() const
458 {
459 const auto sigma2 = sigma * sigma;
460 return (Kokkos::exp(sigma2) + 2) * Kokkos::sqrt(Kokkos::exp(sigma2) - 1);
461 }
462 };
463
472 template <FloatingPointType F> struct SkewNormal
473 {
474 F xi; // Mean
475 F omega; // Standard deviation
477
478 template <class DeviceType>
479 KOKKOS_INLINE_FUNCTION F
480 draw(Kokkos::Random_XorShift1024<DeviceType>& gen) const
481 {
482
483 const double Z0 = gen.normal();
484 const double Z1 = gen.normal();
485 const double delta = alpha / Kokkos::sqrt(1. + alpha * alpha);
486 const double scale_factor = Kokkos::sqrt(1. + delta * delta);
487 const double X = (Z0 + delta * Kokkos::abs(Z1)) / scale_factor;
488 return xi + omega * X;
489 }
490 KOKKOS_INLINE_FUNCTION F mean() const
491 {
492 return xi + omega * (alpha / (Kokkos::sqrt(1 + alpha * alpha))) *
493 Kokkos::sqrt(2 / M_PI);
494 }
495 KOKKOS_INLINE_FUNCTION F var() const
496 {
497 const auto delta = alpha / (Kokkos::sqrt(1 + alpha * alpha));
498 return omega * omega * (1 - 2 * delta * delta / M_PI);
499 }
500 KOKKOS_INLINE_FUNCTION F skewness() const
501 {
502 const auto delta = alpha / (Kokkos::sqrt(1 + alpha * alpha));
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);
505 }
506 };
507
508 static_assert(ProbabilityLaw<SkewNormal<float>, float, ComputeSpace>);
509
517 template <FloatingPointType F> struct Exponential
518 {
520
521 static constexpr bool use_kokkos_log = true;
522
523 template <class DeviceType>
524 KOKKOS_INLINE_FUNCTION F
525 draw(Kokkos::Random_XorShift1024<DeviceType>& gen) const
526 {
527 const float rnd = gen.frand();
528 return F(-1) * CommonMaths::_ln<use_kokkos_log>(rnd) / lambda;
529 }
530
531 [[nodiscard]] KOKKOS_INLINE_FUNCTION F mean() const
532 {
533 return F(1) / lambda;
534 }
535 [[nodiscard]] KOKKOS_INLINE_FUNCTION F var() const
536 {
537
538 return F(1) / (lambda * lambda);
539 }
540 [[nodiscard]] KOKKOS_INLINE_FUNCTION F skewness() const
541 {
542 return 2;
543 }
544 };
545
546 static_assert(ProbabilityLaw<Exponential<float>, float, ComputeSpace>);
547
548} // namespace MC::Distributions
549
550#endif
Definition traits.hpp:20
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