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.


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

    Arguments

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

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

    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

    the position of tgt in contain

    integer, intent(out) :: val

    the position of tgt in contain

    real(kind=dp), intent(out) :: prob

    on return, the probability of picking tgt from constraint

  • 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

    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

    the position of tgt in contain

    integer, intent(out) :: tgt

    the position of tgt in contain 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 from constraint

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

    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

    the position of tgt in contain

    integer, intent(out) :: val

    the position of tgt in contain

    real(kind=dp), intent(out) :: prob

    on return, the probability of picking tgt from constraint

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

    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

    the position of tgt in contain

    integer, intent(out) :: tgt

    the position of tgt in contain 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 from constraint

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

    Arguments

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

    the number for which we request the probability of sampling

    Return Value real(kind=dp)

    the probability of drawing tgt with the sample routine

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 (has to be a set, i.e. unique and ordered) 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

    the number for which we request the probability of sampling

    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}

    the probability of picking tgt from constraint

procedure, public :: get_memory_demand => get_memory_demand_AliasSampler

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
        procedure, public :: get_memory_demand => get_memory_demand_AliasSampler
    end type AliasSampler_t