IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
NormalDistribution.h
Go to the documentation of this file.
1// Class NormalDistribution
2// This class can be used for sampling normal distribution function
3// on bounded domain, e.g. using Inverse Transform Sampling.
4//
5#ifndef IPPL_NORMAL_DISTRIBUTION_H
6#define IPPL_NORMAL_DISTRIBUTION_H
7
9#include "Random/Utility.h"
10
11namespace ippl {
12 namespace random {
13
22 template <typename T>
23 KOKKOS_FUNCTION T normal_cdf_func(T x, T mean, T stddev) {
24 return 0.5 * (1 + Kokkos::erf((x - mean) / (stddev * Kokkos::sqrt(2.0))));
25 }
26
35 template <typename T>
36 KOKKOS_FUNCTION T normal_pdf_func(T x, T mean, T stddev) {
37 const T pi = Kokkos::numbers::pi_v<T>;
38 return (1.0 / (stddev * Kokkos::sqrt(2 * pi)))
39 * Kokkos::exp(-(x - mean) * (x - mean) / (2 * stddev * stddev));
40 }
41
51 template <typename T>
52 KOKKOS_FUNCTION T normal_estimate_func(T u, T mean, T stddev) {
53 return mean + 0. * u * stddev;
54 }
55
65 template <typename T>
66 struct normal_cdf {
67 KOKKOS_INLINE_FUNCTION double operator()(T x, unsigned int d, const T* params_p) const {
68 T mean = params_p[2 * d + 0];
69 T stddev = params_p[2 * d + 1];
70 return ippl::random::normal_cdf_func<T>(x, mean, stddev);
71 }
72 };
73
83 template <typename T>
84 struct normal_pdf {
85 KOKKOS_INLINE_FUNCTION double operator()(T x, unsigned int d, T const* params_p) const {
86 T mean = params_p[2 * d + 0];
87 T stddev = params_p[2 * d + 1];
88 return ippl::random::normal_pdf_func<T>(x, mean, stddev);
89 }
90 };
91
100 template <typename T>
102 KOKKOS_INLINE_FUNCTION double operator()(T u, unsigned int d, T const* params_p) const {
103 T mean = params_p[2 * d + 0];
104 T stddev = params_p[2 * d + 1];
105 return ippl::random::normal_estimate_func<T>(u, mean, stddev);
106 }
107 };
108
109 template <typename T>
111 // Functor to calculate the probability density function (PDF) for a normal
112 // distribution.
113 struct PDF {
114 KOKKOS_INLINE_FUNCTION double operator()(T x, unsigned int d,
115 const T* params_p) const {
116 T mean = params_p[2 * d + 0];
117 T stddev = params_p[2 * d + 1];
118 return ippl::random::normal_pdf_func<T>(x, mean, stddev);
119 }
120 };
121
122 // Functor to calculate the cumulative distribution function (CDF) for a normal
123 // distribution.
124 struct CDF {
125 KOKKOS_INLINE_FUNCTION double operator()(T x, unsigned int d,
126 const T* params_p) const {
127 T mean = params_p[2 * d + 0];
128 T stddev = params_p[2 * d + 1];
129 return ippl::random::normal_cdf_func<T>(x, mean, stddev);
130 }
131 };
132
133 // Functor to estimate the initial guess for sampling a normal distribution.
134 struct Estimate {
135 KOKKOS_INLINE_FUNCTION double operator()(T u, unsigned int d,
136 T const* params_p) const {
137 T mean = params_p[2 * d + 0];
138 T stddev = params_p[2 * d + 1];
139 return ippl::random::normal_estimate_func<T>(u, mean, stddev);
140 }
141 };
142 };
143
148 template <typename T, unsigned Dim>
150 : public ippl::random::Distribution<T, Dim, 2 * Dim, NormalDistributionFunctions<T>> {
151 public:
157 KOKKOS_INLINE_FUNCTION NormalDistribution(const T* par_p)
159 par_p) {}
160 };
161
162 } // namespace random
163} // namespace ippl
164
165#endif
const double pi
constexpr unsigned Dim
Definition Archive.h:20
KOKKOS_FUNCTION T normal_estimate_func(T u, T mean, T stddev)
An estimator for the initial guess that is used in Newton-Raphson method of Inverste Transfrom Sampli...
KOKKOS_FUNCTION T normal_pdf_func(T x, T mean, T stddev)
Calculate the probability density function (PDF) for a normal distribution.
KOKKOS_FUNCTION T normal_cdf_func(T x, T mean, T stddev)
Calculate the cumulative distribution function (CDF) for a normal distribution.
The class that represents a distribution.
Functor to calculate the cumulative distribution function (CDF) for a normal distribution.
KOKKOS_INLINE_FUNCTION double operator()(T x, unsigned int d, const T *params_p) const
Functor to calculate the probability density function (PDF) for a normal distribution.
KOKKOS_INLINE_FUNCTION double operator()(T x, unsigned int d, T const *params_p) const
Functor to estimate the initial guess for sampling normal distribution.
KOKKOS_INLINE_FUNCTION double operator()(T u, unsigned int d, T const *params_p) const
KOKKOS_INLINE_FUNCTION double operator()(T x, unsigned int d, const T *params_p) const
KOKKOS_INLINE_FUNCTION double operator()(T x, unsigned int d, const T *params_p) const
KOKKOS_INLINE_FUNCTION double operator()(T u, unsigned int d, T const *params_p) const
KOKKOS_INLINE_FUNCTION NormalDistribution(const T *par_p)
Constructor for the Normal Distribution class. The constructor takes an array of parameters of normal...