AliasSampler_t Derived Type

type, public :: 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.


Contents

Source Code


Components

Type Visibility Attributes Name Initial
type(AliasTable_t), private :: table

alias table used for sampling

type(shared_array_real_t), private :: probs

the probabilities


Type-Bound Procedures

procedure, public :: setup => setup_AliasSampler_t

constructor

  • private subroutine setup_AliasSampler_t(this, rank_with_info, arr)

    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

    Arguments

    Type IntentOptional 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(:)

procedure, private, :: init => init_AliasSampler_t

only compute the data, i.e. bias tables etc., without allocation

  • private subroutine init_AliasSampler_t(this, rank_with_info, arr)

    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

    Arguments

    Type IntentOptional 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(:)

procedure, private, :: init_probs => init_probs_AliasSampler_t

initialize the probabilities

  • private subroutine init_probs_AliasSampler_t(this, rank_with_info, arr)

    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

    Arguments

    Type IntentOptional 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(:)

procedure, public :: finalize => finalize_AliasSampler_t

destructor

procedure, public :: sample

get a random element and the generation probability

  • private subroutine sample(this, tgt, prob)

    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

    Arguments

    Type IntentOptional Attributes Name
    class(AliasSampler_t), intent(in) :: this
    integer, intent(out) :: tgt
    real(kind=dp), intent(out) :: prob

generic, public :: constrained_sample => constrained_sample_fast, constrained_sample_nI

get a random element from a constrained set and its normalized! generation probability

  • private subroutine constrained_sample_fast(this, contain, contain_ilut, renormalization, pos, val, 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

    Arguments

    Type IntentOptional 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
  • private subroutine constrained_sample_nI(this, contain, renormalization, pos, tgt, 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

    Arguments

    Type IntentOptional 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

procedure, private :: constrained_sample_fast

  • private subroutine constrained_sample_fast(this, contain, contain_ilut, renormalization, pos, val, 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

    Arguments

    Type IntentOptional 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

procedure, private :: constrained_sample_nI

  • private subroutine constrained_sample_nI(this, contain, renormalization, pos, tgt, 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

    Arguments

    Type IntentOptional 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

procedure, public :: get_prob => get_prob_AliasSampler_t

get the probability to produce a given value

  • private elemental function get_prob_AliasSampler_t(this, tgt) result(prob)

    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

    Arguments

    Type IntentOptional Attributes Name
    class(AliasSampler_t), intent(in) :: this
    integer, intent(in) :: tgt

    Return Value real(kind=dp)

procedure, public :: constrained_getProb

get the probability to draw a given value from a constrained set

  • private pure function constrained_getProb(this, contain, renorm, tgt) result(prob)

    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

    Arguments

    Type IntentOptional Attributes Name
    class(AliasSampler_t), intent(in) :: this
    integer, intent(in) :: contain(:)
    real(kind=dp), intent(in) :: renorm
    integer, intent(in) :: tgt

    Return Value real(kind=dp)

    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}

Source Code

    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