aGrUM 3.0.0
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<GUM_Numeric 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<GUM_Numeric 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 59 of file gibbsOperator_tpl.h.

62 :
65 }
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<GUM_Numeric GUM_SCALAR>
gum::GibbsOperator< GUM_SCALAR >::~GibbsOperator ( )
virtual

Destructor.

Definition at line 68 of file gibbsOperator_tpl.h.

68 {
70 }

References GibbsOperator().

Here is the call graph for this function:

Member Function Documentation

◆ _drawVarMonteCarlo_()

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

Definition at line 103 of file gibbsOperator_tpl.h.

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

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<GUM_Numeric 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 123 of file gibbsOperator_tpl.h.

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

References gum::Instantiation::chgVal(), gum::Tensor< GUM_SCALAR >::draw(), gum::Instantiation::erase(), gum::MultiDimDecorator< GUM_ELEMENT >::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<GUM_Numeric GUM_SCALAR>
bool gum::GibbsOperator< GUM_SCALAR >::isDrawnAtRandom ( ) const

Definition at line 147 of file gibbsOperator_tpl.h.

147 {
148 return atRandom_;
149 }

References atRandom_.

◆ monteCarloSample()

template<GUM_Numeric 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 88 of file gibbsOperator_tpl.h.

88 {
90
91 for (const auto nod: samplingBn_.topologicalOrder()) {
92 I.add(samplingBn_.variable(nod));
93 if (hardEv_ != nullptr && hardEv_->exists(nod)) {
94 I.chgVal(samplingBn_.variable(nod), (*hardEv_)[nod]);
95 } else {
97 }
98 }
99 return I;
100 }
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<GUM_Numeric GUM_SCALAR>
Size gum::GibbsOperator< GUM_SCALAR >::nbrDrawnVar ( ) const

Getters and setters.

Definition at line 137 of file gibbsOperator_tpl.h.

137 {
138 return nbr_;
139 }

References nbr_.

◆ nextSample()

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

draws next sample of Gibbs sampling

Definition at line 110 of file gibbsOperator_tpl.h.

110 {
111 for (Idx i = 0; i < nbr_; i++) {
112 auto pos
115 counting_++;
116 }
117 return prev;
118 }
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<GUM_Numeric GUM_SCALAR>
void gum::GibbsOperator< GUM_SCALAR >::setDrawnAtRandom ( bool atRandom)

Definition at line 152 of file gibbsOperator_tpl.h.

152 {
154 }

References atRandom_.

◆ setNbrDrawnVar()

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

Definition at line 142 of file gibbsOperator_tpl.h.

142 {
143 nbr_ = nbr;
144 }

References nbr_.

◆ updateSamplingNodes_()

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

Definition at line 73 of file gibbsOperator_tpl.h.

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

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<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
Size gum::GibbsOperator< GUM_SCALAR >::counting_
protected

Definition at line 98 of file gibbsOperator.h.

Referenced by GibbsOperator(), and nextSample().

◆ hardEv_

template<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
Size gum::GibbsOperator< GUM_SCALAR >::nbr_
protected

◆ samplingBn_

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

◆ samplingNodes_

template<GUM_Numeric 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: