aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::GibbsOperator< GUM_SCALAR > Class Template Reference

class containing all variables and methods required for Gibbssampling More...

#include <agrum/BN/inference/gibbsOperator.h>

Inheritance diagram for gum::GibbsOperator< GUM_SCALAR >:
Collaboration diagram for gum::GibbsOperator< GUM_SCALAR >:

Public Member Functions

 GibbsOperator (const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
 constructor
virtual ~GibbsOperator ()
 Destructor.
Size nbrDrawnVar () const
 Getters and setters.
void setNbrDrawnVar (Size nbr)
bool isDrawnAtRandom () const
void setDrawnAtRandom (bool atRandom)
Instantiation monteCarloSample ()
 draws a Monte Carlo sample
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< NodeIdsamplingNodes_
Size nbr_
bool atRandom_

Private Member Functions

void _drawVarMonteCarlo_ (NodeId nod, Instantiation *I)
void _GibbsSample_ (NodeId id, Instantiation *I)
 change in Instantiation I a new drawn value for id

Detailed Description

template<typename GUM_SCALAR>
class gum::GibbsOperator< GUM_SCALAR >

class containing all variables and methods required for Gibbssampling

Definition at line 67 of file gibbsOperator.h.

Constructor & Destructor Documentation

◆ GibbsOperator()

template<typename GUM_SCALAR>
gum::GibbsOperator< GUM_SCALAR >::GibbsOperator ( const IBayesNet< GUM_SCALAR > & BN,
const NodeProperty< Idx > * hardEv,
Size nbr = 1,
bool atRandom = false )

constructor

Definition at line 58 of file gibbsOperator_tpl.h.

61 :
64 }
class containing all variables and methods required for Gibbssampling
const IBayesNet< GUM_SCALAR > & samplingBn_
const NodeProperty< Idx > * hardEv_
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor

References GibbsOperator(), atRandom_, counting_, hardEv_, nbr_, and samplingBn_.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), GibbsOperator(), gum::GibbsSampling< GUM_SCALAR >::GibbsSampling(), and ~GibbsOperator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~GibbsOperator()

template<typename GUM_SCALAR>
gum::GibbsOperator< GUM_SCALAR >::~GibbsOperator ( )
virtual

Destructor.

Definition at line 67 of file gibbsOperator_tpl.h.

67 {
69 }

References GibbsOperator().

Here is the call graph for this function:

Member Function Documentation

◆ _drawVarMonteCarlo_()

template<typename GUM_SCALAR>
void gum::GibbsOperator< GUM_SCALAR >::_drawVarMonteCarlo_ ( NodeId nod,
Instantiation * I )
private

Definition at line 102 of file gibbsOperator_tpl.h.

102 {
104 Itop.erase(samplingBn_.variable(nod));
105 I->chgVal(samplingBn_.variable(nod), samplingBn_.cpt(nod).extract(Itop).draw());
106 }

References gum::Instantiation::chgVal(), gum::Instantiation::erase(), and samplingBn_.

Referenced by monteCarloSample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _GibbsSample_()

template<typename GUM_SCALAR>
void gum::GibbsOperator< GUM_SCALAR >::_GibbsSample_ ( NodeId id,
Instantiation * I )
private

change in Instantiation I a new drawn value for id

Definition at line 122 of file gibbsOperator_tpl.h.

122 {
124 Itop.erase(samplingBn_.variable(id));
125 gum::Tensor< GUM_SCALAR > p = samplingBn_.cpt(id).extract(Itop);
126 for (const auto nod: samplingBn_.children(id))
127 p *= samplingBn_.cpt(nod).extract(Itop);
128 GUM_ASSERT(p.nbrDim() == 1);
129 if (p.sum() != 0) {
130 p.normalize();
131 I->chgVal(samplingBn_.variable(id), p.draw());
132 }
133 }

References gum::Instantiation::chgVal(), gum::Tensor< GUM_SCALAR >::draw(), gum::Instantiation::erase(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::Tensor< GUM_SCALAR >::normalize(), samplingBn_, and gum::Tensor< GUM_SCALAR >::sum().

Referenced by nextSample().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isDrawnAtRandom()

template<typename GUM_SCALAR>
bool gum::GibbsOperator< GUM_SCALAR >::isDrawnAtRandom ( ) const
inline

Definition at line 87 of file gibbsOperator.h.

87{ return atRandom_; }

References atRandom_.

◆ monteCarloSample()

template<typename GUM_SCALAR>
Instantiation gum::GibbsOperator< GUM_SCALAR >::monteCarloSample ( )

draws a Monte Carlo sample

returns a MC sample This is not a really sample since we take into account evidence without care about parent of evidence, etc. This is just a not-so-bad first sample for GibbsSampler

Definition at line 87 of file gibbsOperator_tpl.h.

87 {
89
90 for (const auto nod: samplingBn_.topologicalOrder()) {
91 I.add(samplingBn_.variable(nod));
92 if (hardEv_ != nullptr && hardEv_->exists(nod)) {
93 I.chgVal(samplingBn_.variable(nod), (*hardEv_)[nod]);
94 } else {
96 }
97 }
98 return I;
99 }
void _drawVarMonteCarlo_(NodeId nod, Instantiation *I)

References _drawVarMonteCarlo_(), gum::Instantiation::add(), gum::Instantiation::chgVal(), hardEv_, and samplingBn_.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::computeKL_(), and gum::GibbsSampling< GUM_SCALAR >::monteCarloSample_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nbrDrawnVar()

template<typename GUM_SCALAR>
Size gum::GibbsOperator< GUM_SCALAR >::nbrDrawnVar ( ) const
inline

Getters and setters.

Definition at line 83 of file gibbsOperator.h.

83{ return nbr_; }

References nbr_.

◆ nextSample()

template<typename GUM_SCALAR>
Instantiation gum::GibbsOperator< GUM_SCALAR >::nextSample ( Instantiation prev)

draws next sample of Gibbs sampling

Definition at line 109 of file gibbsOperator_tpl.h.

109 {
110 for (Idx i = 0; i < nbr_; i++) {
111 auto pos
114 counting_++;
115 }
116 return prev;
117 }
void _GibbsSample_(NodeId id, Instantiation *I)
change in Instantiation I a new drawn value for id
Sequence< NodeId > samplingNodes_
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.

References _GibbsSample_(), atRandom_, counting_, nbr_, gum::randomValue(), and samplingNodes_.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::computeKL_(), and gum::GibbsSampling< GUM_SCALAR >::draw_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setDrawnAtRandom()

template<typename GUM_SCALAR>
void gum::GibbsOperator< GUM_SCALAR >::setDrawnAtRandom ( bool atRandom)
inline

Definition at line 89 of file gibbsOperator.h.

References atRandom_.

◆ setNbrDrawnVar()

template<typename GUM_SCALAR>
void gum::GibbsOperator< GUM_SCALAR >::setNbrDrawnVar ( Size nbr)
inline

Definition at line 85 of file gibbsOperator.h.

85{ nbr_ = nbr; }

References nbr_.

◆ updateSamplingNodes_()

template<typename GUM_SCALAR>
void gum::GibbsOperator< GUM_SCALAR >::updateSamplingNodes_ ( )
protected

Definition at line 72 of file gibbsOperator_tpl.h.

72 {
73 samplingNodes_.clear();
74 for (const auto node: samplingBn_.nodes())
75 if (hardEv_ == nullptr || !hardEv_->exists(node)) { samplingNodes_.insert(node); }
76 if (samplingNodes_.size() == 0) {
77 GUM_ERROR(InvalidArgument, "No node to sample (too many nodes or too much evidence)!")
78 }
79 if (nbr_ > samplingNodes_.size()) nbr_ = samplingNodes_.size();
80 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References GUM_ERROR, hardEv_, nbr_, samplingBn_, and samplingNodes_.

Referenced by gum::GibbsSampling< GUM_SCALAR >::burnIn_(), and gum::GibbsBNdistance< GUM_SCALAR >::computeKL_().

Here is the caller graph for this function:

Member Data Documentation

◆ atRandom_

template<typename GUM_SCALAR>
bool gum::GibbsOperator< GUM_SCALAR >::atRandom_
protected

Definition at line 104 of file gibbsOperator.h.

Referenced by GibbsOperator(), isDrawnAtRandom(), nextSample(), and setDrawnAtRandom().

◆ counting_

template<typename GUM_SCALAR>
Size gum::GibbsOperator< GUM_SCALAR >::counting_
protected

Definition at line 98 of file gibbsOperator.h.

Referenced by GibbsOperator(), and nextSample().

◆ hardEv_

template<typename GUM_SCALAR>
const NodeProperty< Idx >* gum::GibbsOperator< GUM_SCALAR >::hardEv_
protected

Definition at line 100 of file gibbsOperator.h.

Referenced by GibbsOperator(), monteCarloSample(), and updateSamplingNodes_().

◆ nbr_

template<typename GUM_SCALAR>
Size gum::GibbsOperator< GUM_SCALAR >::nbr_
protected

◆ samplingBn_

template<typename GUM_SCALAR>
const IBayesNet< GUM_SCALAR >& gum::GibbsOperator< GUM_SCALAR >::samplingBn_
protected

◆ samplingNodes_

template<typename GUM_SCALAR>
Sequence< NodeId > gum::GibbsOperator< GUM_SCALAR >::samplingNodes_
protected

Definition at line 101 of file gibbsOperator.h.

Referenced by nextSample(), and updateSamplingNodes_().


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