All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Static Public Attributes | Private Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
util::FastAndPoorGauss< N, T > Class Template Reference

Translates a number u uniformly distributed between 0 and 1 into a Gaussian distributed one z. More...

#include <FastAndPoorGauss.h>

Public Types

using Data_t = T
 Type of data to deal with. More...
 

Public Member Functions

Data_t transform (Data_t const u) const
 Returns the Gaussian distributed value corresponding to u. More...
 
Data_t operator() (Data_t const u) const
 

Static Public Attributes

static constexpr std::size_t NPoints = N
 Number of sampled points. More...
 

Private Member Functions

std::size_t indexOf (Data_t u) const
 Returns the index of the precomputed table serving the value u. More...
 

Static Private Member Functions

static std::array< Data_t,
NPoints
makeSamples ()
 Fills the pre-sampling table. More...
 

Static Private Attributes

static std::array< Data_t, N >
const 
fSamples = util::FastAndPoorGauss<N, T>::makeSamples()
 Sampled points of inverse Gaussian. More...
 

Detailed Description

template<std::size_t N, typename T>
class util::FastAndPoorGauss< N, T >

Translates a number u uniformly distributed between 0 and 1 into a Gaussian distributed one z.

Parameters
Nnumber of possible values returned; it must be a power of 2
T(default: double) type of number for u and z

The focus of this algorithm is speed, which is paid with some memory and a lot of precision. The algorithm picks one of only N precomputed possible values for each input. The mapping of $ u \in [ 0, +1 [ $ into a real number is a multi-step function with N steps. The steps are located at $ 1/2N $ steps through the domain of u.

The returned number z is distributed according to a standard normal distribution with mean 0 and standard deviation 1. The number can be turned into one distributed with arbitrary mean $ \mu $ and standard deviation $ \sigma $ with the usual transformation $ x = \mu + \sigma z $ (see e.g. util::GaussianTransformer).

Resources

Math is internally performed in T precision, except for the initialization that is performed in double precision. The precomputed value table is sizeof(T) * N bytes large.

Note
This class is tuned for performance, and it allocates its data on the stack. Stack overflows have been observed in Linux with a size of N 2^20^. Stack overflows are very puzzling since they do not present any standard diagnostics and debuggers may not notice them. Bottom line is: do not overdo with the number of samples, or ask the author to provide a special version using dynamic memory allocation.

Definition at line 46 of file FastAndPoorGauss.h.

Member Typedef Documentation

template<std::size_t N, typename T>
using util::FastAndPoorGauss< N, T >::Data_t = T

Type of data to deal with.

Definition at line 100 of file FastAndPoorGauss.h.

Member Function Documentation

template<std::size_t N, typename T >
std::size_t util::FastAndPoorGauss< N, T >::indexOf ( Data_t  u) const
private

Returns the index of the precomputed table serving the value u.

Definition at line 234 of file FastAndPoorGauss.h.

234  {
235  return static_cast<std::size_t>(u * NPoints);
236 } // util::FastAndPoorGauss<>::indexOf()
static constexpr std::size_t NPoints
Number of sampled points.
template<std::size_t N, typename T >
auto util::FastAndPoorGauss< N, T >::makeSamples ( )
staticprivate

Fills the pre-sampling table.

Definition at line 241 of file FastAndPoorGauss.h.

242 {
243 
244  std::array<Data_t, NPoints> samples;
245 
246  double const V2 = std::sqrt(2.0);
247 
249 
250  for(Data_t& value: samples)
251  value = static_cast<Data_t>(TMath::ErfInverse(extract() * 2.0 - 1.0) * V2);
252 
253  return samples;
254 } // util::FastAndPoorGauss<>::makeSamples()
T Data_t
Type of data to deal with.
static constexpr std::size_t NPoints
Number of sampled points.
temporary value
Samples the interval [ 0, 1 ] in sequence, cyclically.
template<std::size_t N, typename T>
Data_t util::FastAndPoorGauss< N, T >::operator() ( Data_t const  u) const
inline

Definition at line 107 of file FastAndPoorGauss.h.

107 { return transform(u); }
Data_t transform(Data_t const u) const
Returns the Gaussian distributed value corresponding to u.
template<std::size_t N, typename T>
Data_t util::FastAndPoorGauss< N, T >::transform ( Data_t const  u) const
inline

Returns the Gaussian distributed value corresponding to u.

Definition at line 106 of file FastAndPoorGauss.h.

106 { return fSamples[indexOf(u)]; }
static std::array< Data_t, N > const fSamples
Sampled points of inverse Gaussian.
std::size_t indexOf(Data_t u) const
Returns the index of the precomputed table serving the value u.

Member Data Documentation

template<std::size_t N, typename T>
std::array< T, N > const util::FastAndPoorGauss< N, T >::fSamples = util::FastAndPoorGauss<N, T>::makeSamples()
staticprivate

Sampled points of inverse Gaussian.

Definition at line 112 of file FastAndPoorGauss.h.

template<std::size_t N, typename T>
constexpr std::size_t util::FastAndPoorGauss< N, T >::NPoints = N
static

Number of sampled points.

Definition at line 102 of file FastAndPoorGauss.h.


The documentation for this class was generated from the following file: