aGrUM  0.20.3
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 More...
 
virtual ~GibbsOperator ()
 Destructor. More...
 
Size nbrDrawnVar () const
 Getters and setters. More...
 
void setNbrDrawnVar (Size nbr)
 
bool isDrawnAtRandom () const
 
void setDrawnAtRandom (bool atRandom)
 
Instantiation monteCarloSample ()
 draws a Monte Carlo sample More...
 
Instantiation nextSample (Instantiation prev)
 draws next sample of Gibbs sampling More...
 

Protected Attributes

Size counting_
 
const IBayesNet< GUM_SCALAR > & samplingBn_
 
const NodeProperty< Idx > * hardEv_
 
Sequence< NodeIdsamplingNodes_
 
Size nbr_
 
bool atRandom_
 

Detailed Description

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

class containing all variables and methods required for Gibbssampling

Definition at line 49 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 36 of file gibbsOperator_tpl.h.

39  :
40  counting_(0),
41  samplingBn_(BN), hardEv_(hardEv), nbr_(nbr), atRandom_(atRandom) {
43  GUM_CONSTRUCTOR(GibbsOperator);
44  }
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor
const IBayesNet< GUM_SCALAR > & samplingBn_
Definition: gibbsOperator.h:81
const NodeProperty< Idx > * hardEv_
Definition: gibbsOperator.h:82

◆ ~GibbsOperator()

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

Destructor.

Definition at line 47 of file gibbsOperator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

47  {
48  GUM_DESTRUCTOR(GibbsOperator);
49  }
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor
+ 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 82 of file gibbsOperator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

82  {
83  gum::Instantiation Itop(*I);
84  Itop.erase(samplingBn_.variable(nod));
85  I->chgVal(samplingBn_.variable(nod), samplingBn_.cpt(nod).extract(Itop).draw());
86  }
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:82
const IBayesNet< GUM_SCALAR > & samplingBn_
Definition: gibbsOperator.h:81
+ Here is the call 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 102 of file gibbsOperator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

102  {
103  gum::Instantiation Itop(*I);
104  Itop.erase(samplingBn_.variable(id));
105  gum::Potential< GUM_SCALAR > p = samplingBn_.cpt(id).extract(Itop);
106  for (const auto nod: samplingBn_.children(id))
107  p *= samplingBn_.cpt(nod).extract(Itop);
108  GUM_ASSERT(p.nbrDim() == 1);
109  if (p.sum() != 0) {
110  p.normalize();
111  I->chgVal(samplingBn_.variable(id), p.draw());
112  }
113  }
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:59
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Idx draw() const
get a value at random from a 1-D distribution
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:82
GUM_SCALAR sum() const
sum of all elements in the Potential
const IBayesNet< GUM_SCALAR > & samplingBn_
Definition: gibbsOperator.h:81
+ Here is the call graph for this function:

◆ _updateSamplingNodes_()

template<typename GUM_SCALAR >
void gum::GibbsOperator< GUM_SCALAR >::_updateSamplingNodes_ ( )
private

Definition at line 52 of file gibbsOperator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

52  {
54  for (const auto node: samplingBn_.nodes())
55  if (hardEv_ == nullptr || !hardEv_->exists(node)) samplingNodes_.insert(node);
56  if (samplingNodes_.size() == 0) {
57  GUM_ERROR(InvalidArgument, "No node to sample (too many nodes or too much evidence)!")
58  }
60  }
void clear()
Clear the sequence.
Definition: sequence_tpl.h:264
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:37
const IBayesNet< GUM_SCALAR > & samplingBn_
Definition: gibbsOperator.h:81
const NodeProperty< Idx > * hardEv_
Definition: gibbsOperator.h:82
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
Sequence< NodeId > samplingNodes_
Definition: gibbsOperator.h:83
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:393
+ Here is the call graph for this function:

◆ isDrawnAtRandom()

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

Definition at line 69 of file gibbsOperator.h.

References gum::GibbsOperator< GUM_SCALAR >::atRandom_.

69 { return 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 67 of file gibbsOperator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

67  {
69 
70  for (const auto nod: samplingBn_.topologicalOrder()) {
71  I.add(samplingBn_.variable(nod));
72  if (hardEv_ != nullptr && hardEv_->exists(nod)) {
73  I.chgVal(samplingBn_.variable(nod), (*hardEv_)[nod]);
74  } else {
75  _drawVarMonteCarlo_(nod, &I);
76  }
77  }
78  return I;
79  }
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:82
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void _drawVarMonteCarlo_(NodeId nod, Instantiation *I)
const IBayesNet< GUM_SCALAR > & samplingBn_
Definition: gibbsOperator.h:81
const NodeProperty< Idx > * hardEv_
Definition: gibbsOperator.h:82
+ Here is the call graph for this function:

◆ nbrDrawnVar()

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

Getters and setters.

Definition at line 65 of file gibbsOperator.h.

65 { return nbr_; }

◆ nextSample()

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

draws next sample of Gibbs sampling

Definition at line 90 of file gibbsOperator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

90  {
91  for (Idx i = 0; i < nbr_; i++) {
92  auto pos
94  this->_GibbsSample_(samplingNodes_[pos], &prev);
95  counting_++;
96  }
97  return prev;
98  }
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:37
void _GibbsSample_(NodeId id, Instantiation *I)
change in Instantiation I a new drawn value for id
Sequence< NodeId > samplingNodes_
Definition: gibbsOperator.h:83
+ Here is the call graph for this function:

◆ setDrawnAtRandom()

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

Definition at line 71 of file gibbsOperator.h.

References gum::GibbsOperator< GUM_SCALAR >::atRandom_.

71 { atRandom_ = atRandom; }

◆ setNbrDrawnVar()

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

Definition at line 67 of file gibbsOperator.h.

67 { nbr_ = nbr; }

Member Data Documentation

◆ atRandom_

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

◆ counting_

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

Definition at line 80 of file gibbsOperator.h.

◆ hardEv_

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

Definition at line 82 of file gibbsOperator.h.

◆ nbr_

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

Definition at line 85 of file gibbsOperator.h.

◆ samplingBn_

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

Definition at line 81 of file gibbsOperator.h.

◆ samplingNodes_

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

Definition at line 83 of file gibbsOperator.h.


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