vSMC
vSMC: Scalable Monte Carlo
Public Types | Public Member Functions | Friends | List of all members
vsmc::DiscreteDistribution< IntType > Class Template Reference

Draw a single sample given weights. More...

#include <vsmc/rng/discrete_distribution.hpp>

Public Types

typedef std::vector< double > param_type
 
typedef IntType result_type
 

Public Member Functions

 DiscreteDistribution ()
 
template<typename InputIter >
 DiscreteDistribution (InputIter first, InputIter last)
 
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 URNG >
result_type operator() (URNG &eng) const
 
template<typename URNG , typename InputIter >
result_type operator() (URNG &eng, InputIter first, InputIter last, bool normalized=false) const
 Draw sample with external probabilities. More...
 
param_type param () const
 
void param (const param_type &param)
 
void param (param_type &&param)
 
std::vector< double > probability () const
 
void reset ()
 

Friends

bool operator!= (const DiscreteDistribution< IntType > &rdisc1, const DiscreteDistribution< IntType > &rdisc2)
 
template<typename CharT , typename Traits >
std::basic_ostream< CharT, Traits > & operator<< (std::basic_ostream< CharT, Traits > &os, const DiscreteDistribution< IntType > &rdisc)
 
bool operator== (const DiscreteDistribution< IntType > &rdisc1, const DiscreteDistribution< IntType > &rdisc2)
 
template<typename CharT , typename Traits >
std::basic_istream< CharT, Traits > & operator>> (std::basic_istream< CharT, Traits > &is, DiscreteDistribution< IntType > &rdisc)
 

Detailed Description

template<typename IntType = int>
class vsmc::DiscreteDistribution< IntType >

Draw a single sample given weights.

Definition at line 47 of file discrete_distribution.hpp.

Member Typedef Documentation

template<typename IntType = int>
typedef std::vector<double> vsmc::DiscreteDistribution< IntType >::param_type

Definition at line 52 of file discrete_distribution.hpp.

template<typename IntType = int>
typedef IntType vsmc::DiscreteDistribution< IntType >::result_type

Definition at line 51 of file discrete_distribution.hpp.

Constructor & Destructor Documentation

template<typename IntType = int>
vsmc::DiscreteDistribution< IntType >::DiscreteDistribution ( )
inline

Definition at line 54 of file discrete_distribution.hpp.

template<typename IntType = int>
template<typename InputIter >
vsmc::DiscreteDistribution< IntType >::DiscreteDistribution ( InputIter  first,
InputIter  last 
)
inline

Definition at line 57 of file discrete_distribution.hpp.

template<typename IntType = int>
template<typename UnaryOperation >
vsmc::DiscreteDistribution< IntType >::DiscreteDistribution ( std::size_t  count,
double  xmin,
double  xmax,
UnaryOperation  unary_op 
)
inline

Definition at line 74 of file discrete_distribution.hpp.

template<typename IntType = int>
vsmc::DiscreteDistribution< IntType >::DiscreteDistribution ( const param_type param)
inlineexplicit

Definition at line 86 of file discrete_distribution.hpp.

template<typename IntType = int>
vsmc::DiscreteDistribution< IntType >::DiscreteDistribution ( param_type &&  param)
inlineexplicit

Definition at line 94 of file discrete_distribution.hpp.

Member Function Documentation

template<typename IntType = int>
result_type vsmc::DiscreteDistribution< IntType >::max ( ) const
inline

Definition at line 123 of file discrete_distribution.hpp.

template<typename IntType = int>
result_type vsmc::DiscreteDistribution< IntType >::min ( ) const
inline

Definition at line 122 of file discrete_distribution.hpp.

template<typename IntType = int>
template<typename URNG >
result_type vsmc::DiscreteDistribution< IntType >::operator() ( URNG &  eng) const
inline

Definition at line 129 of file discrete_distribution.hpp.

template<typename IntType = int>
template<typename URNG , typename InputIter >
result_type vsmc::DiscreteDistribution< IntType >::operator() ( URNG &  eng,
InputIter  first,
InputIter  last,
bool  normalized = false 
) const
inline

Draw sample with external probabilities.

Parameters
engA 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 weightsa 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 requires the normalized weights, specified using the iterators first and last, and return the index (counting from zero) of the random draw. No dynamic memory allocaiton will be invovled by calling this function.

Definition at line 155 of file discrete_distribution.hpp.

template<typename IntType = int>
param_type vsmc::DiscreteDistribution< IntType >::param ( ) const
inline

Definition at line 102 of file discrete_distribution.hpp.

template<typename IntType = int>
void vsmc::DiscreteDistribution< IntType >::param ( const param_type param)
inline

Definition at line 104 of file discrete_distribution.hpp.

template<typename IntType = int>
void vsmc::DiscreteDistribution< IntType >::param ( param_type &&  param)
inline

Definition at line 112 of file discrete_distribution.hpp.

template<typename IntType = int>
std::vector<double> vsmc::DiscreteDistribution< IntType >::probability ( ) const
inline

Definition at line 126 of file discrete_distribution.hpp.

template<typename IntType = int>
void vsmc::DiscreteDistribution< IntType >::reset ( )
inline

Definition at line 120 of file discrete_distribution.hpp.

Friends And Related Function Documentation

template<typename IntType = int>
bool operator!= ( const DiscreteDistribution< IntType > &  rdisc1,
const DiscreteDistribution< IntType > &  rdisc2 
)
friend

Definition at line 198 of file discrete_distribution.hpp.

template<typename IntType = int>
template<typename CharT , typename Traits >
std::basic_ostream<CharT, Traits>& operator<< ( std::basic_ostream< CharT, Traits > &  os,
const DiscreteDistribution< IntType > &  rdisc 
)
friend

Definition at line 204 of file discrete_distribution.hpp.

template<typename IntType = int>
bool operator== ( const DiscreteDistribution< IntType > &  rdisc1,
const DiscreteDistribution< IntType > &  rdisc2 
)
friend

Definition at line 193 of file discrete_distribution.hpp.

template<typename IntType = int>
template<typename CharT , typename Traits >
std::basic_istream<CharT, Traits>& operator>> ( std::basic_istream< CharT, Traits > &  is,
DiscreteDistribution< IntType > &  rdisc 
)
friend

Definition at line 229 of file discrete_distribution.hpp.