Alias Sampler class
Given an (un-normalized) array of weights, draws random numbers from this distribution. Has additional features compared to the AliasTable_t, like the stored probabilities.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
type(AliasTable_t), | private | :: | table |
alias table used for sampling |
|||
type(shared_array_real_t), | private | :: | probs |
the probabilities |
constructor
allocate the resources of this and load the probability distribution from arr into this @param[in] arr array containing the (not necessarily normalized) probabilities we want to use for sampling
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | rank_with_info |
The intra-node rank that contains the weights to be used all other arr of all other ranks are ignored (and can be allocated with size 0). |
||
real(kind=dp), | intent(in) | :: | arr(:) |
only compute the data, i.e. bias tables etc., without allocation
load the probability distribution from arr into this we only use this in the sampler array, but fortran has no friend classes, so its public @param[in] arr array containing the (not necessarily normalized) probabilities we want to use for sampling
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | rank_with_info |
The intra-node rank that contains the weights to be used all other arr of all other ranks are ignored (and can be allocated with size 0). |
||
real(kind=dp), | intent(in) | :: | arr(:) |
initialize the probabilities
load the probability distribution from arr into this%probs @param[in] arr array containing the (not necessarily normalized) probabilities we want to use for sampling
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | rank_with_info |
The intra-node rank that contains the weights to be used all other arr of all other ranks are ignored (and can be allocated with size 0). |
||
real(kind=dp), | intent(in) | :: | arr(:) |
destructor
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(inout) | :: | this |
get a random element and the generation probability
draw a random element from 1:size(this%probs) with the probabilities listed in prob @param[in] tgt on return, this is a random number in the sampling range of this @param[out] prob on return, the probability of picking tgt
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(in) | :: | this | |||
integer, | intent(out) | :: | tgt | |||
real(kind=dp), | intent(out) | :: | prob |
get a random element from a constrained set and its normalized! generation probability
draw a random element from 1:size(this%probs) with the probabilities listed in prob
@param[in] constraint pick only elements from constraint
@param[out] tgt on return, this is a random number in the sampling range of this
@param[out] pos the position of tgt in contain
@param[out] prob on return, the probability of picking tgt from constraint
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(in) | :: | this | |||
integer, | intent(in) | :: | contain(:) | |||
integer(kind=n_int), | intent(in) | :: | contain_ilut(0:) | |||
real(kind=dp), | intent(in) | :: | renormalization | |||
integer, | intent(out) | :: | pos | |||
integer, | intent(out) | :: | val | |||
real(kind=dp), | intent(out) | :: | prob |
draw a random element from 1:size(this%probs) with the probabilities listed in prob
@param[in] constraint pick only elements from constraint
@param[out] tgt on return, this is a random number in the sampling range of this
@param[out] pos the position of tgt in contain
@param[out] prob on return, the probability of picking tgt from constraint
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(in) | :: | this | |||
integer, | intent(in) | :: | contain(:) | |||
real(kind=dp), | intent(in) | :: | renormalization | |||
integer, | intent(out) | :: | pos | |||
integer, | intent(out) | :: | tgt | |||
real(kind=dp), | intent(out) | :: | prob |
draw a random element from 1:size(this%probs) with the probabilities listed in prob
@param[in] constraint pick only elements from constraint
@param[out] tgt on return, this is a random number in the sampling range of this
@param[out] pos the position of tgt in contain
@param[out] prob on return, the probability of picking tgt from constraint
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(in) | :: | this | |||
integer, | intent(in) | :: | contain(:) | |||
integer(kind=n_int), | intent(in) | :: | contain_ilut(0:) | |||
real(kind=dp), | intent(in) | :: | renormalization | |||
integer, | intent(out) | :: | pos | |||
integer, | intent(out) | :: | val | |||
real(kind=dp), | intent(out) | :: | prob |
draw a random element from 1:size(this%probs) with the probabilities listed in prob
@param[in] constraint pick only elements from constraint
@param[out] tgt on return, this is a random number in the sampling range of this
@param[out] pos the position of tgt in contain
@param[out] prob on return, the probability of picking tgt from constraint
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(in) | :: | this | |||
integer, | intent(in) | :: | contain(:) | |||
real(kind=dp), | intent(in) | :: | renormalization | |||
integer, | intent(out) | :: | pos | |||
integer, | intent(out) | :: | tgt | |||
real(kind=dp), | intent(out) | :: | prob |
get the probability to produce a given value
Returns the probability to draw tgt from this sampler @param[in] tgt the number for which we request the probability of sampling @param[out] prob the probability of drawing tgt with the sample routine
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(in) | :: | this | |||
integer, | intent(in) | :: | tgt |
get the probability to draw a given value from a constrained set
Returns the probability to draw tgt from this sampler @param[in] tgt the number for which we request the probability of sampling @param[in] constraint pick only elements from constraint (has to be a set, i.e. unique and ordered) @param[out] prob the probability of picking tgt from constraint the probability of drawing anything from an empty sampler is 0
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(AliasSampler_t), | intent(in) | :: | this | |||
integer, | intent(in) | :: | contain(:) | |||
real(kind=dp), | intent(in) | :: | renorm | |||
integer, | intent(in) | :: | tgt |
This loosened threshhold might be a good idea if the renormalization was calculated via the complement, i.e. \Sum{ p_i } {i \in D_i} = 1 - \Sum{ p_i } {i \notin D_i}
type :: AliasSampler_t
!! Alias Sampler class
!!
!! Given an (un-normalized) array of weights, draws
!! random numbers from this distribution.
!! Has additional features compared to the AliasTable_t,
!! like the stored probabilities.
private
type(AliasTable_t) :: table
!! alias table used for sampling
! WARNING: DO NOT MANUALLY RE-ASSIGN THIS POINTER, THIS WILL MOST LIKELY BREAK STUFF
type(shared_array_real_t) :: probs
!! the probabilities
contains
private
procedure, public:: setup => setup_AliasSampler_t
!! constructor
procedure :: init => init_AliasSampler_t
!! only compute the data, i.e. bias tables etc., without allocation
procedure :: init_probs => init_probs_AliasSampler_t
!! initialize the probabilities
procedure, public :: finalize => finalize_AliasSampler_t
!! destructor
procedure, public :: sample
!! get a random element and the generation probability
generic, public :: constrained_sample => constrained_sample_fast, constrained_sample_nI
!! get a random element from a constrained set and its normalized! generation probability
procedure, private :: constrained_sample_nI, constrained_sample_fast
procedure, public :: get_prob => get_prob_AliasSampler_t
!! get the probability to produce a given value
procedure, public :: constrained_getProb
!! get the probability to draw a given value from a constrained set
end type AliasSampler_t