This module implements the sampling of non-uniform probability distributions that are constructed on the fly using the cumulated distribution function method.
This sampling is faster to construct than the alias-sampling, but it is slower to sample. For distributions that are known to be constant during a calculation use the alias sampling. This is done for example in PCHB.
Construct a CDF sampler from given weights.
The weights do not have to be normalized. If all weights are zero, or the array is empty, the sampler will return the (non-existent) index 0 with probability 1.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(in) | :: | w(:) |
Construct a CDF sampler from given weights.
The weights do not have to be normalized. If all weights are zero, or the array is empty, the sampler will return the (non-existent) index 0 with probability 1.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(in) | :: | w(:) | |||
real(kind=dp), | intent(in) | :: | total |
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
real(kind=dp), | private, | allocatable | :: | p(:) |
The probabilities. |
||
real(kind=dp), | private, | allocatable | :: | cum_p(:) |
The cumulated probabilities. |
||
integer, | private | :: | my_size |
We store the size in an additional integer,
because |
private pure function construct_CDF_sampler_t (w) | Construct a CDF sampler from given weights. |
private pure function construct_CDF_sampler_with_total_t (w, total) | Construct a CDF sampler from given weights. |
procedure , public :: sample Subroutine | |
procedure , public :: get_prob Function | |
procedure , public :: all_zero Function | |
procedure , public :: size => get_size Function |
Construct a CDF sampler from given weights.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(in) | :: | w(:) |
Construct a CDF sampler from given weights.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(in) | :: | w(:) | |||
real(kind=dp), | intent(in) | :: | total |
Return if all probabilities are zero, or the set of probabilities is empty.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(CDF_Sampler_t), | intent(in) | :: | this |
Get the probability of a given value val
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(CDF_Sampler_t), | intent(in) | :: | this | |||
integer, | intent(in) | :: | val |
Return the number of probabilites.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(CDF_Sampler_t), | intent(in) | :: | this |
Return randomly a value val
and its probability p
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(CDF_Sampler_t), | intent(in) | :: | this | |||
integer, | intent(out) | :: | val | |||
real(kind=dp), | intent(out) | :: | p |