CDF_sampling_mod Module

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.



Contents


Interfaces

public interface CDF_Sampler_t

  • private pure function construct_CDF_sampler_t(w) result(res)

    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.

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: w(:)

    Return Value type(CDF_Sampler_t)

  • private pure function construct_CDF_sampler_with_total_t(w, total) result(res)

    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.

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: w(:)
    real(kind=dp), intent(in) :: total

    Return Value type(CDF_Sampler_t)


Derived Types

type, public ::  CDF_Sampler_t

Components

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 p and cum_p are allocated empty, if all probabilities are zero.

Constructor

private pure function construct_CDF_sampler_t (w)

Construct a CDF sampler from given weights.

Read more…
private pure function construct_CDF_sampler_with_total_t (w, total)

Construct a CDF sampler from given weights.

Read more…

Type-Bound Procedures

procedure , public :: sample Subroutine
procedure , public :: get_prob Function
procedure , public :: all_zero Function
procedure , public :: size => get_size Function

Functions

private pure function construct_CDF_sampler_t(w) result(res)

Construct a CDF sampler from given weights.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: w(:)

Return Value type(CDF_Sampler_t)

private pure function construct_CDF_sampler_with_total_t(w, total) result(res)

Construct a CDF sampler from given weights.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: w(:)
real(kind=dp), intent(in) :: total

Return Value type(CDF_Sampler_t)

private elemental function all_zero(this)

Return if all probabilities are zero, or the set of probabilities is empty.

Arguments

Type IntentOptional Attributes Name
class(CDF_Sampler_t), intent(in) :: this

Return Value logical

private elemental function get_prob(this, val)

Get the probability of a given value val.

Arguments

Type IntentOptional Attributes Name
class(CDF_Sampler_t), intent(in) :: this
integer, intent(in) :: val

Return Value real(kind=dp)

private elemental function get_size(this)

Return the number of probabilites.

Arguments

Type IntentOptional Attributes Name
class(CDF_Sampler_t), intent(in) :: this

Return Value integer


Subroutines

private subroutine sample(this, val, p)

Return randomly a value val and its probability p.

Arguments

Type IntentOptional Attributes Name
class(CDF_Sampler_t), intent(in) :: this
integer, intent(out) :: val
real(kind=dp), intent(out) :: p