ThePEG::CompSelector< T, WeightType > Class Template Reference

The CompSelector class works like the Selector class in that it can be used to randomly select objects according to associated probabilities. More...

#include <CompSelector.h>

List of all members.

Public Member Functions

Standard constructors and destructors.
 CompSelector (double newMargin=1.1, double newTolerance=1.0e-6)
 The default constructor.
The main function controlling the selection and compensation.
WeightType insert (WeightType d, const T &t)
 Insert an object given a probability for this object.
template<typename RNDGEN>
T & select (RNDGEN &rnd) throw (range_error)
 Selct an object randomly.
WeightType reweight (double &weight)
 Report the weight associated with the last selected object.
void reset ()
 Exit compensation mode and start selection procedure from scratch.
void clear ()
 Erases all objects.
void margin (double m)
 Set the margin used to get a new overestimated probability for an object when entering compensation mode.
void tolerance (double t)
 Set the tolerance for how much a weight is allowed to be larger than unity before starting the compensation.
Simple access functions.
bool compensating ()
 Return true if this CompSelector is in a compensating state.
long compleft () const
 If in a compensating mode, return the number of selection needed before exiting this mode.
WeightType sum () const
 Return the sum of probabilities of the objects inserted.
double margin () const
 Return the margin used to get a new overestimated probability for an object when entering compensation mode.
double tolerance () const
 Return the tolerance for how much a weight is allowed to be larger than unity before starting the compensation.
I/O functions.
template<typename OStream>
void output (OStream &os) const
 Output to a stream.
template<typename IStream>
void input (IStream &is)
 Input from a stream.

Private Attributes

Selector< T, WeightType > selector
 The underlying selector.
long N
 The number of selections so far.
last
 The last selected object.
double theMargin
 The margin used to get a new overestimated probability for an object when entering compensation mode.
double theTolerance
 Set the tolerance for how much a weight is allowed to be larger than unity before starting the compensation.
vector< Levellevels
 The currently active compensation levels.

Classes

struct  Level
 Internal struct used for bookkeeping when compensating. More...


Detailed Description

template<typename T, typename WeightType = double>
class ThePEG::CompSelector< T, WeightType >

The CompSelector class works like the Selector class in that it can be used to randomly select objects according to associated probabilities.

In addition, the CompSelector class is able to handle the case where the associated probabilities are overestimates and the selected object will be discarded according to some weight. If then a weight above one is encountered, this means that the overestimated probability for the selected object was wrong and it should in fact have been higher. If this happens, the CompSelecteor will go into compensation mode, which means that the selected object will be oversampled a period after the violation to compensate for having been undersampled before. Also the associated probability is adjusted to reflect the new overestimate.

The available functions are not as many as in Selector, and some of the works somewhat differently. Before starting sampling the objects should be added to a CompSelector object with the insert() function. To selct an object the select() function should be used. After that the weight with which the object should be accepted should be presented with the reweight() function which normally returns zero. If, however, the weight is larger than unity the new overestimated probability is returned and the CompSelector enters the compensating mode. Note that the weight is passed as a reference and may be changed in by the reweight function if in the compensating mode.

Definition at line 47 of file CompSelector.h.


Constructor & Destructor Documentation

template<typename T, typename WeightType = double>
ThePEG::CompSelector< T, WeightType >::CompSelector ( double  newMargin = 1.1,
double  newTolerance = 1.0e-6 
) [inline]

The default constructor.

The optional argument gives the margin used to get a new overestimated probability for an object when entering compensation mode.

Definition at line 58 of file CompSelector.h.


Member Function Documentation

template<typename T, typename WeightType = double>
WeightType ThePEG::CompSelector< T, WeightType >::insert ( WeightType  d,
const T &  t 
) [inline]

Insert an object given a probability for this object.

If the probability is zero or negative, the object will not be inserted and the probability itself is returned. Otherwise the sum of probabilities so far is returned. Note that if selection has already started and this CompSelector is in compensating mode, it will immediately leave this mode and the selection procedure will start from scratch.

Definition at line 75 of file CompSelector.h.

template<typename T, typename WeightType = double>
template<typename RNDGEN>
T& ThePEG::CompSelector< T, WeightType >::select ( RNDGEN &  rnd  )  throw (range_error) [inline]

Selct an object randomly.

Given a random number generator which generates flat random numbers in the interval ]0,1[ with the operator()() function, select an object according to the individual probabilities specified when they were inserted. If the generated number is outside the allowed range or the Selector is empty, a range_error will be thrown. The generator should have a push_back function which will be used push back a uniform random number in the interval ]0,1[ calculated from the fraction of rnd which was in the range of the selected object.

Definition at line 93 of file CompSelector.h.

template<typename T, typename WeightType = double>
WeightType ThePEG::CompSelector< T, WeightType >::reweight ( double &  weight  )  [inline]

Report the weight associated with the last selected object.

Returns the zero if weight was below unity, otherwise the compensation mode will be entered and the new overestimated probabilty for the last selected object will be returned.

Definition at line 105 of file CompSelector.h.

template<typename T, typename WeightType = double>
WeightType ThePEG::CompSelector< T, WeightType >::sum (  )  const [inline]

Return the sum of probabilities of the objects inserted.

Note that probabilities specified when objects are inserted are rescaled with this number to give unit probability for 'select()'.

Definition at line 193 of file CompSelector.h.


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

Generated on Thu Apr 29 12:43:25 2010 for ThePEG by  doxygen 1.5.5