MCKL
Monte Carlo Kernel Library
Classes | Public Types | Public Member Functions | Friends | List of all members
mckl::DiscreteDistribution< IntType > Class Template Reference

Draw a single sample given weights. More...

#include <mckl/random/discrete_distribution.hpp>

Classes

class  param_type
 

Public Types

using distribution_type = DiscreteDistribution< IntType >
 
using result_type = IntType
 

Public Member Functions

 DiscreteDistribution ()=default
 
template<typename InputIter >
 DiscreteDistribution (InputIter first, InputIter last)
 
 DiscreteDistribution (std::initializer_list< double > weights)
 
template<typename UnaryOperation >
 DiscreteDistribution (std::size_t count, double xmin, double xmax, UnaryOperation &&unary_op)
 
 DiscreteDistribution (const param_type &param)
 
 DiscreteDistribution (param_type &&param)
 
result_type max () const
 
result_type min () const
 
template<typename RNGType >
result_type operator() (RNGType &rng) const
 
template<typename RNGType , typename InputIter >
result_type operator() (RNGType &rng, InputIter first, InputIter last, bool normalized=false) const
 Draw sample with external probabilities. More...
 
Vector< double > probability () const
 
void reset ()
 

Friends

bool operator!= (const distribution_type &dist1, const distribution_type &dist2)
 
template<typename CharT , typename Traits >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const distribution_type &dist)
 
bool operator== (const distribution_type &dist1, const distribution_type &dist2)
 
template<typename CharT , typename Traits >
std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &is, distribution_type &dist)
 

Detailed Description

template<typename IntType>
class mckl::DiscreteDistribution< IntType >

Draw a single sample given weights.

Definition at line 43 of file discrete_distribution.hpp.

Member Typedef Documentation

◆ distribution_type

template<typename IntType>
using mckl::DiscreteDistribution< IntType >::distribution_type = DiscreteDistribution<IntType>

Definition at line 48 of file discrete_distribution.hpp.

◆ result_type

template<typename IntType>
using mckl::DiscreteDistribution< IntType >::result_type = IntType

Definition at line 47 of file discrete_distribution.hpp.

Constructor & Destructor Documentation

◆ DiscreteDistribution() [1/6]

template<typename IntType>
mckl::DiscreteDistribution< IntType >::DiscreteDistribution ( )
default

◆ DiscreteDistribution() [2/6]

template<typename IntType>
template<typename InputIter >
mckl::DiscreteDistribution< IntType >::DiscreteDistribution ( InputIter  first,
InputIter  last 
)
inline

Definition at line 174 of file discrete_distribution.hpp.

◆ DiscreteDistribution() [3/6]

template<typename IntType>
mckl::DiscreteDistribution< IntType >::DiscreteDistribution ( std::initializer_list< double >  weights)
inline

Definition at line 178 of file discrete_distribution.hpp.

◆ DiscreteDistribution() [4/6]

template<typename IntType>
template<typename UnaryOperation >
mckl::DiscreteDistribution< IntType >::DiscreteDistribution ( std::size_t  count,
double  xmin,
double  xmax,
UnaryOperation &&  unary_op 
)
inline

Definition at line 184 of file discrete_distribution.hpp.

◆ DiscreteDistribution() [5/6]

template<typename IntType>
mckl::DiscreteDistribution< IntType >::DiscreteDistribution ( const param_type param)
inlineexplicit

Definition at line 190 of file discrete_distribution.hpp.

◆ DiscreteDistribution() [6/6]

template<typename IntType>
mckl::DiscreteDistribution< IntType >::DiscreteDistribution ( param_type &&  param)
inlineexplicit

Definition at line 192 of file discrete_distribution.hpp.

Member Function Documentation

◆ max()

template<typename IntType>
result_type mckl::DiscreteDistribution< IntType >::max ( ) const
inline

Definition at line 199 of file discrete_distribution.hpp.

◆ min()

template<typename IntType>
result_type mckl::DiscreteDistribution< IntType >::min ( ) const
inline

Definition at line 197 of file discrete_distribution.hpp.

◆ operator()() [1/2]

template<typename IntType>
template<typename RNGType >
result_type mckl::DiscreteDistribution< IntType >::operator() ( RNGType &  rng) const
inline

Definition at line 209 of file discrete_distribution.hpp.

◆ operator()() [2/2]

template<typename IntType>
template<typename RNGType , typename InputIter >
result_type mckl::DiscreteDistribution< IntType >::operator() ( RNGType &  rng,
InputIter  first,
InputIter  last,
bool  normalized = false 
) const
inline

Draw sample with external probabilities.

Parameters
rngA uniform random number generator
firstThe first iterator of the weights sequence.
lastThe one past the end iterator of the weights sequence.
normalizedIf the weights are already normalized

Given weights \((W_1,\dots,W_N)\), it is possible to draw the index \(i\) using the std::discrete_distribuiton template. However, there are two drawbacks with this approach. First, if the weights are already normalized, this template does uncessary extra work to normalized the weights. Second, whenever the weights change, a new distribution need to be constructed (the param_type of the distribution is implementation defined and cannot be used to write portable code), which will lead to uncessary dynamic memory allocation. This function does not use dynamic memory and improve performance for normalized weights.

Definition at line 234 of file discrete_distribution.hpp.

◆ probability()

template<typename IntType>
Vector<double> mckl::DiscreteDistribution< IntType >::probability ( ) const
inline

Definition at line 206 of file discrete_distribution.hpp.

◆ reset()

template<typename IntType>
void mckl::DiscreteDistribution< IntType >::reset ( )
inline

Definition at line 204 of file discrete_distribution.hpp.

Friends And Related Function Documentation

◆ operator!=

template<typename IntType>
bool operator!= ( const distribution_type dist1,
const distribution_type dist2 
)
friend

Definition at line 280 of file discrete_distribution.hpp.

◆ operator<<

template<typename IntType>
template<typename CharT , typename Traits >
std::basic_ostream<CharT, Traits>& operator<< ( std::basic_ostream< CharT, Traits > &  os,
const distribution_type dist 
)
friend

Definition at line 287 of file discrete_distribution.hpp.

◆ operator==

template<typename IntType>
bool operator== ( const distribution_type dist1,
const distribution_type dist2 
)
friend

Definition at line 274 of file discrete_distribution.hpp.

◆ operator>>

template<typename IntType>
template<typename CharT , typename Traits >
std::basic_istream<CharT, Traits>& operator>> ( std::basic_istream< CharT, Traits > &  is,
distribution_type dist 
)
friend

Definition at line 296 of file discrete_distribution.hpp.