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 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 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 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
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(AliasSampler_t), | intent(in) | :: | this | |||
| integer, | intent(out) | :: | tgt |
on return, this is a random number in the sampling range of this |
||
| real(kind=dp), | intent(out) | :: | prob |
on return, the probability of picking tgt |
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
| 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 |
the position of tgt in |
||
| integer, | intent(out) | :: | val |
the position of tgt in |
||
| real(kind=dp), | intent(out) | :: | prob |
on return, the probability of picking tgt from constraint |
draw a random element from 1:size(this%probs) with the probabilities listed in prob
| 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 |
the position of tgt in |
||
| integer, | intent(out) | :: | tgt |
the position of tgt in |
||
| real(kind=dp), | intent(out) | :: | prob |
on return, the probability of picking tgt from constraint |
draw a random element from 1:size(this%probs) with the probabilities listed in prob
| 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 |
the position of tgt in |
||
| integer, | intent(out) | :: | val |
the position of tgt in |
||
| real(kind=dp), | intent(out) | :: | prob |
on return, the probability of picking tgt from constraint |
draw a random element from 1:size(this%probs) with the probabilities listed in prob
| 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 |
the position of tgt in |
||
| integer, | intent(out) | :: | tgt |
the position of tgt in |
||
| real(kind=dp), | intent(out) | :: | prob |
on return, the probability of picking tgt from constraint |
get the probability to produce a given value
Returns the probability to draw tgt from this sampler
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(AliasSampler_t), | intent(in) | :: | this | |||
| integer, | intent(in) | :: | tgt |
the number for which we request the probability of sampling |
the probability of drawing tgt with the sample routine
get the probability to draw a given value from a constrained set
Returns the probability to draw tgt from this sampler (has to be a set, i.e. unique and ordered) 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 |
the number for which we request the probability of sampling |
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}
the probability of picking tgt from constraint
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(AliasSampler_t), | intent(in) | :: | this |
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 procedure, public :: get_memory_demand => get_memory_demand_AliasSampler end type AliasSampler_t