EGSnrc C++ class library
Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
|
A class for sampling random values from a given probability distribution using the alias table technique. More...
#include <egs_alias_table.h>
Public Member Functions | |
EGS_AliasTable () | |
Construct an empty (uninitialized) alias table. More... | |
EGS_AliasTable (const EGS_AliasTable &t) | |
Copy constructor. Performs a deep copy. | |
EGS_AliasTable (int N, const EGS_Float *x, const EGS_Float *f, int Type=1) | |
Construct an alias table from the tabulated distribution with N bins with probabilities f at the bins x. More... | |
EGS_AliasTable (EGS_Float xmin, EGS_Float xmax, EGS_Float accu, int nmax, EGS_AtFunction func, void *data) | |
Construct an alias table for sampling values in the interval xmin ... xmax for the function func. More... | |
void | initialize (int N, const EGS_Float *x, const EGS_Float *f, int Type=1) |
Initialize the alias table. More... | |
int | initialize (EGS_Float xmin, EGS_Float xmax, EGS_Float accu, int nmax, EGS_AtFunction func, void *data) |
Initialize the alias table. More... | |
EGS_Float | sample (EGS_RandomGenerator *rndm) const |
Get a random point from this table using the RNG rndm. | |
int | sampleBin (EGS_RandomGenerator *rndm) const |
Get a random bin from this table. | |
EGS_Float | getAverage () const |
Get the average of the probability distribution represented by this alias table object. | |
EGS_Float | getMaximum () const |
Get the maximum abscissa of this alias table object. | |
A class for sampling random values from a given probability distribution using the alias table technique.
Instances of this class can be used to efficiently sample random variables from any probability distribution. The probability distribution is represented internally as an alias table. EGS_AliasTable objects can be initialized from a distribution tabulated as discrete values, as a histogram, or as function values at bin edges and a linear interpolation between these values (see this constructor ), or from a function within a specified interval (see this constructor ) Random values are then drawn using the sample() method.
Definition at line 63 of file egs_alias_table.h.
EGS_AliasTable::EGS_AliasTable | ( | ) |
Construct an empty (uninitialized) alias table.
The table should be initialized using one of the initialize() methods before using it for sampling.
Definition at line 72 of file egs_alias_table.h.
EGS_AliasTable::EGS_AliasTable | ( | int | N, |
const EGS_Float * | x, | ||
const EGS_Float * | f, | ||
int | Type = 1 |
||
) |
Construct an alias table from the tabulated distribution with N bins with probabilities f at the bins x.
If Type = 0, the N x values are interpreted as discrete abscissas, i.e. the probability distribution is assumend to be , where is Dirac's delta function. If Type is 1, the distribution is assumed to be a histogram with N-1 bins with the 'th bin between and having a probability . If Type is 2, the distribution is assumed to be continuous with a linear variation between the and .
Definition at line 93 of file egs_alias_table.h.
EGS_AliasTable::EGS_AliasTable | ( | EGS_Float | xmin, |
EGS_Float | xmax, | ||
EGS_Float | accu, | ||
int | nmax, | ||
EGS_AtFunction | func, | ||
void * | data | ||
) |
Construct an alias table for sampling values in the interval xmin ... xmax for the function func.
The interval xmin ... xmax is divided into interpolation intervals in such a way that the error using a linear interpolation does not exceed accu. However, if the number of bins needed to achieve an interpolation accuracy of accu is larger than nmax, no more than nmax bins are used. Internally the alias table will be set to be of type 2.
Definition at line 109 of file egs_alias_table.h.
void EGS_AliasTable::initialize | ( | int | N, |
const EGS_Float * | x, | ||
const EGS_Float * | f, | ||
int | Type = 1 |
||
) |
Initialize the alias table.
See the constructor with corresponding arguments.
Definition at line 100 of file egs_alias_table.cpp.
int EGS_AliasTable::initialize | ( | EGS_Float | xmin, |
EGS_Float | xmax, | ||
EGS_Float | accu, | ||
int | nmax, | ||
EGS_AtFunction | func, | ||
void * | data | ||
) |
Initialize the alias table.
See the constructor with corresponding arguments. Return value is 0 on success (the accuracy goal was satisfied with less than nmax bins), 1 if it was not possible to get the required interpolation accuracy with nmax bins.
Definition at line 195 of file egs_alias_table.cpp.