dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
dasmig::core::discrete Class Reference

Index chosen with probability proportional to a weight. More...

#include <distributions.hpp>

Public Member Functions

 discrete (std::span< const double > weights)
 Build from weights.
 
 discrete (std::initializer_list< double > weights)
 Build from a list of weights.
 
std::size_t operator() (rng &r) const
 Draw an index.
 
std::size_t size () const noexcept
 Number of weights.
 
double total () const noexcept
 Sum of the weights.
 
double probability (std::size_t i) const
 Probability of an index.
 

Detailed Description

Index chosen with probability proportional to a weight.

Keeps the cumulative sums of the weights (added left to right). A draw is the first index whose cumulative sum exceeds uniform01(r) * total, so a zero weight is never chosen. Consumes one value.

dasmig::core::discrete eye_colour{{0.7, 0.2, 0.1}};
std::size_t i = eye_colour(r);
Index chosen with probability proportional to a weight.

Definition at line 256 of file distributions.hpp.

Constructor & Destructor Documentation

◆ discrete() [1/2]

dasmig::core::discrete::discrete ( std::span< const double >  weights)
inlineexplicit

Build from weights.

Parameters
weightsNon-negative, finite weights, at least one positive.
Exceptions
std::invalid_argumentIf the weights are empty, a weight is negative or not finite, or they sum to 0 or overflow.

Definition at line 263 of file distributions.hpp.

◆ discrete() [2/2]

dasmig::core::discrete::discrete ( std::initializer_list< double >  weights)
inline

Build from a list of weights.

Parameters
weightsNon-negative, finite weights, at least one positive.
Exceptions
std::invalid_argumentAs the span constructor.

Definition at line 293 of file distributions.hpp.

Member Function Documentation

◆ operator()()

std::size_t dasmig::core::discrete::operator() ( rng &  r) const
inline

Draw an index.

Parameters
rEngine.
Returns
An index in [0, size()) with a positive weight.

Definition at line 301 of file distributions.hpp.

◆ probability()

double dasmig::core::discrete::probability ( std::size_t  i) const
inline

Probability of an index.

Parameters
iIndex.
Returns
weight(i) / total().
Exceptions
std::out_of_rangeIf i >= size().

Definition at line 318 of file distributions.hpp.

◆ size()

std::size_t dasmig::core::discrete::size ( ) const
inlinenoexcept

Number of weights.

Returns
The number of indices.

Definition at line 308 of file distributions.hpp.

◆ total()

double dasmig::core::discrete::total ( ) const
inlinenoexcept

Sum of the weights.

Returns
The total weight.

Definition at line 312 of file distributions.hpp.


The documentation for this class was generated from the following file: