![]() |
aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
|
class containing all variables and methods required for Gibbssampling More...
#include <agrum/BN/inference/gibbsOperator.h>
Public Member Functions | |
| GibbsOperator (const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false) | |
| constructor | |
| GibbsOperator (const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, const std::string §ion) | |
| Constructor whose Gibbs parameters are read from gum::Configuration. | |
| virtual | ~GibbsOperator () |
| Destructor. | |
| Size | nbrDrawnVar () const |
| Getters and setters. | |
| void | setNbrDrawnVar (Size nbr) |
| bool | isDrawnAtRandom () const |
| void | setDrawnAtRandom (bool atRandom) |
| Instantiation | monteCarloSample () |
| draws a first sample with a positive probability (starting point of a Gibbs chain) | |
| Instantiation | nextSample (Instantiation prev) |
| draws next sample of Gibbs sampling | |
Protected Member Functions | |
| void | updateSamplingNodes_ () |
Protected Attributes | |
| Size | counting_ |
| const IBayesNet< GUM_SCALAR > & | samplingBn_ |
| const NodeProperty< Idx > * | hardEv_ |
| Sequence< NodeId > | samplingNodes_ |
| Size | nbr_ |
| bool | atRandom_ |
Private Member Functions | |
| bool | _pruneDomains_ () |
| void | _mask_ (NodeId node, Tensor< GUM_SCALAR > &p) const |
| Instantiation | _forwardSample_ () |
| void | _drawVarMonteCarlo_ (NodeId nod, Instantiation *I) |
| bool | _isCompatible_ (const Instantiation &I) const |
| void | _smoothedGibbsSample_ (NodeId id, Instantiation *I, GUM_SCALAR epsilon) |
| void | _GibbsSample_ (NodeId id, Instantiation *I) |
| change in Instantiation I a new drawn value for id | |
Private Attributes | |
| NodeProperty< std::vector< bool > > | _allowed_ |
| the values of each node that may belong to a sample with a positive probability | |
| NodeProperty< GUM_SCALAR > | _minNonZero_ |
| the smallest positive probability of the CPT of each node (for the smoothed Gibbs moves) | |
Static Private Attributes | |
| static constexpr Size | _nbrRejectionTries_ = 100 |
| number of forward samples drawn before searching a compatible sample by smoothed Gibbs moves | |
| static constexpr Size | _nbrSmoothingRestarts_ = 10 |
| number of restarts (from a new forward sample) of the smoothed Gibbs moves | |
| static constexpr std::array< double, 4 > | _smoothingLevels_ = {1e-3, 1e-2, 1e-1, 0.5} |
| growing smoothing levels: a null probability is replaced by epsilon times the smallest positive probability of its CPT | |
| static constexpr Size | _nbrSweepsBySmoothingLevel_ = 50 |
| maximal number of sweeps (over the non-evidence nodes) for each smoothing level | |
class containing all variables and methods required for Gibbssampling
Definition at line 70 of file gibbsOperator.h.
| gum::GibbsOperator< GUM_SCALAR >::GibbsOperator | ( | const IBayesNet< GUM_SCALAR > & | BN, |
| const NodeProperty< Idx > * | hardEv, | ||
| Size | nbr = 1, | ||
| bool | atRandom = false ) |
constructor
Definition at line 79 of file gibbsOperator_tpl.h.
References GibbsOperator(), atRandom_, counting_, hardEv_, nbr_, and samplingBn_.
Referenced by gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), GibbsOperator(), GibbsOperator(), gum::GibbsSampling< GUM_SCALAR >::GibbsSampling(), and ~GibbsOperator().
| gum::GibbsOperator< GUM_SCALAR >::GibbsOperator | ( | const IBayesNet< GUM_SCALAR > & | BN, |
| const NodeProperty< Idx > * | hardEv, | ||
| const std::string & | section ) |
Constructor whose Gibbs parameters are read from gum::Configuration.
The key percentDrawnSample of the section gives the percentage (0 to 100) of the variables drawn between two samples and the key drawnAtRandom tells whether they are chosen at random or in turn.
| BN | The Bayesian network to sample. |
| hardEv | The hard evidence (may be nullptr). |
| section | The name of the section of the configuration. |
Definition at line 63 of file gibbsOperator_tpl.h.
References GibbsOperator().
|
virtual |
Destructor.
Definition at line 88 of file gibbsOperator_tpl.h.
References GibbsOperator().
|
private |
Definition at line 287 of file gibbsOperator_tpl.h.
References _mask_(), gum::Instantiation::chgVal(), gum::Instantiation::erase(), and samplingBn_.
Referenced by _forwardSample_().
|
private |
Definition at line 153 of file gibbsOperator_tpl.h.
References _drawVarMonteCarlo_(), gum::Instantiation::add(), gum::Instantiation::chgVal(), hardEv_, and samplingBn_.
Referenced by monteCarloSample().
|
private |
change in Instantiation I a new drawn value for id
Definition at line 312 of file gibbsOperator_tpl.h.
References gum::Instantiation::chgVal(), gum::Tensor< GUM_SCALAR >::draw(), gum::Instantiation::erase(), gum::Tensor< GUM_SCALAR >::extract(), gum::MultiDimDecorator< GUM_ELEMENT >::nbrDim(), gum::Tensor< GUM_SCALAR >::normalize(), samplingBn_, and gum::Tensor< GUM_SCALAR >::sum().
Referenced by nextSample().
|
private |
Definition at line 168 of file gibbsOperator_tpl.h.
References samplingBn_.
Referenced by monteCarloSample().
|
private |
Definition at line 278 of file gibbsOperator_tpl.h.
References _allowed_, gum::Instantiation::end(), gum::Instantiation::inc(), gum::Instantiation::setFirst(), and gum::Instantiation::val().
Referenced by _drawVarMonteCarlo_(), and _smoothedGibbsSample_().
|
private |
Definition at line 198 of file gibbsOperator_tpl.h.
References _allowed_, gum::Set< Key >::contains(), gum::Instantiation::end(), gum::Set< Key >::erase(), hardEv_, gum::Instantiation::inc(), gum::Set< Key >::insert(), samplingBn_, gum::Instantiation::setFirst(), and gum::Instantiation::val().
Referenced by monteCarloSample().
|
private |
Definition at line 175 of file gibbsOperator_tpl.h.
References _mask_(), _minNonZero_, gum::Instantiation::chgVal(), gum::Tensor< GUM_SCALAR >::draw(), gum::Instantiation::end(), gum::Instantiation::erase(), gum::Instantiation::inc(), gum::Tensor< GUM_SCALAR >::normalize(), samplingBn_, and gum::Instantiation::setFirst().
Referenced by monteCarloSample().
| bool gum::GibbsOperator< GUM_SCALAR >::isDrawnAtRandom | ( | ) | const |
Definition at line 336 of file gibbsOperator_tpl.h.
References atRandom_.
| Instantiation gum::GibbsOperator< GUM_SCALAR >::monteCarloSample | ( | ) |
draws a first sample with a positive probability (starting point of a Gibbs chain)
From a sample with a null probability, every Gibbs move may have a null mass: the chain would never leave it. Hence the search for a sample with a positive probability, which is a constraint satisfaction problem (NP-hard in general). The search is heuristic:
Limits: arc consistency is not complete (it may not detect an incompatibility involving several CPTs together, e.g. a parity constraint over a chain of XOR), and the search is stochastic and bounded. Hence, when the compatible samples are very rare (long chains of deterministic constraints, several deterministic plateaus), the search may fail even though such a sample exists, and the result may depend on the random seed. OperationNotAllowed then means "not found", not "does not exist". An exact inference (e.g. LazyPropagation) can decide whether the evidence is compatible (evidence probability > 0).
| IncompatibleEvidence | if the pruning proves that the evidence has a null probability |
| OperationNotAllowed | if no sample with a positive probability is found |
Definition at line 105 of file gibbsOperator_tpl.h.
References _forwardSample_(), _isCompatible_(), _minNonZero_, _nbrRejectionTries_, _nbrSmoothingRestarts_, _nbrSweepsBySmoothingLevel_, _pruneDomains_(), _smoothedGibbsSample_(), _smoothingLevels_, GUM_ERROR, hardEv_, and samplingBn_.
Referenced by gum::GibbsBNdistance< GUM_SCALAR >::computeKL_(), and gum::GibbsSampling< GUM_SCALAR >::monteCarloSample_().
| Size gum::GibbsOperator< GUM_SCALAR >::nbrDrawnVar | ( | ) | const |
| Instantiation gum::GibbsOperator< GUM_SCALAR >::nextSample | ( | Instantiation | prev | ) |
draws next sample of Gibbs sampling
Definition at line 299 of file gibbsOperator_tpl.h.
References _GibbsSample_(), atRandom_, counting_, nbr_, gum::randomValue(), and samplingNodes_.
Referenced by gum::GibbsBNdistance< GUM_SCALAR >::computeKL_(), and gum::GibbsSampling< GUM_SCALAR >::draw_().
| void gum::GibbsOperator< GUM_SCALAR >::setDrawnAtRandom | ( | bool | atRandom | ) |
Definition at line 341 of file gibbsOperator_tpl.h.
References atRandom_.
| void gum::GibbsOperator< GUM_SCALAR >::setNbrDrawnVar | ( | Size | nbr | ) |
|
protected |
Definition at line 93 of file gibbsOperator_tpl.h.
References GUM_ERROR, hardEv_, nbr_, samplingBn_, and samplingNodes_.
Referenced by gum::GibbsSampling< GUM_SCALAR >::burnIn_(), and gum::GibbsBNdistance< GUM_SCALAR >::computeKL_().
|
private |
the values of each node that may belong to a sample with a positive probability
Definition at line 166 of file gibbsOperator.h.
Referenced by _mask_(), and _pruneDomains_().
|
private |
the smallest positive probability of the CPT of each node (for the smoothed Gibbs moves)
Definition at line 169 of file gibbsOperator.h.
Referenced by _smoothedGibbsSample_(), and monteCarloSample().
|
staticconstexprprivate |
number of forward samples drawn before searching a compatible sample by smoothed Gibbs moves
Definition at line 153 of file gibbsOperator.h.
Referenced by monteCarloSample().
|
staticconstexprprivate |
number of restarts (from a new forward sample) of the smoothed Gibbs moves
Definition at line 156 of file gibbsOperator.h.
Referenced by monteCarloSample().
|
staticconstexprprivate |
maximal number of sweeps (over the non-evidence nodes) for each smoothing level
Definition at line 163 of file gibbsOperator.h.
Referenced by monteCarloSample().
|
staticconstexprprivate |
growing smoothing levels: a null probability is replaced by epsilon times the smallest positive probability of its CPT
Definition at line 160 of file gibbsOperator.h.
Referenced by monteCarloSample().
|
protected |
Definition at line 147 of file gibbsOperator.h.
Referenced by GibbsOperator(), isDrawnAtRandom(), nextSample(), and setDrawnAtRandom().
|
protected |
Definition at line 141 of file gibbsOperator.h.
Referenced by GibbsOperator(), and nextSample().
|
protected |
Definition at line 143 of file gibbsOperator.h.
Referenced by GibbsOperator(), _forwardSample_(), _pruneDomains_(), monteCarloSample(), and updateSamplingNodes_().
|
protected |
Definition at line 146 of file gibbsOperator.h.
Referenced by GibbsOperator(), nbrDrawnVar(), nextSample(), setNbrDrawnVar(), and updateSamplingNodes_().
|
protected |
Definition at line 142 of file gibbsOperator.h.
Referenced by GibbsOperator(), _drawVarMonteCarlo_(), _forwardSample_(), _GibbsSample_(), _isCompatible_(), _pruneDomains_(), _smoothedGibbsSample_(), monteCarloSample(), and updateSamplingNodes_().
|
protected |
Definition at line 144 of file gibbsOperator.h.
Referenced by nextSample(), and updateSamplingNodes_().