aGrUM  0.15.1
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
 number of samples drawn More...
 
const IBayesNet< GUM_SCALAR > & _sampling_bn
 
const NodeProperty< Idx > * _hardEv
 
Sequence< NodeId_samplingNodes
 
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 50 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 37 of file gibbsOperator_tpl.h.

References gum::GibbsOperator< GUM_SCALAR >::__updateSamplingNodes().

40  :
41  _counting(0),
42  _sampling_bn(BN), _hardEv(hardEv), _nbr(nbr), _atRandom(atRandom) {
44  GUM_CONSTRUCTOR(GibbsOperator);
45  }
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor
const IBayesNet< GUM_SCALAR > & _sampling_bn
Definition: gibbsOperator.h:83
Size _counting
number of samples drawn
Definition: gibbsOperator.h:82
const NodeProperty< Idx > * _hardEv
Definition: gibbsOperator.h:84
+ Here is the call graph for this function:

◆ ~GibbsOperator()

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

Destructor.

Definition at line 48 of file gibbsOperator_tpl.h.

48  {
49  GUM_DESTRUCTOR(GibbsOperator);
50  }
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor

Member Function Documentation

◆ __drawVarMonteCarlo()

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

Definition at line 85 of file gibbsOperator_tpl.h.

References gum::GibbsOperator< GUM_SCALAR >::_sampling_bn, gum::Instantiation::chgVal(), and gum::Instantiation::erase().

Referenced by gum::GibbsOperator< GUM_SCALAR >::monteCarloSample().

86  {
87  gum::Instantiation Itop(*I);
88  Itop.erase(_sampling_bn.variable(nod));
89  I->chgVal(_sampling_bn.variable(nod),
90  _sampling_bn.cpt(nod).extract(Itop).draw());
91  }
const IBayesNet< GUM_SCALAR > & _sampling_bn
Definition: gibbsOperator.h:83
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
+ 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 107 of file gibbsOperator_tpl.h.

References gum::GibbsOperator< GUM_SCALAR >::_sampling_bn, gum::Instantiation::chgVal(), gum::Potential< GUM_SCALAR >::draw(), gum::Instantiation::erase(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::Potential< GUM_SCALAR >::normalize(), and gum::Potential< GUM_SCALAR >::sum().

Referenced by gum::GibbsOperator< GUM_SCALAR >::nextSample().

107  {
108  gum::Instantiation Itop(*I);
109  Itop.erase(_sampling_bn.variable(id));
110  gum::Potential< GUM_SCALAR > p = _sampling_bn.cpt(id).extract(Itop);
111  for (const auto nod : _sampling_bn.children(id))
112  p *= _sampling_bn.cpt(nod).extract(Itop);
113  GUM_ASSERT(p.nbrDim() == 1);
114  if (p.sum() != 0) {
115  p.normalize();
116  I->chgVal(_sampling_bn.variable(id), p.draw());
117  }
118  }
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
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
const IBayesNet< GUM_SCALAR > & _sampling_bn
Definition: gibbsOperator.h:83
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:83
GUM_SCALAR sum() const
sum of all elements in the Potential
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __updateSamplingNodes()

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

Definition at line 53 of file gibbsOperator_tpl.h.

References gum::GibbsOperator< GUM_SCALAR >::_hardEv, gum::GibbsOperator< GUM_SCALAR >::_nbr, gum::GibbsOperator< GUM_SCALAR >::_sampling_bn, gum::GibbsOperator< GUM_SCALAR >::_samplingNodes, gum::SequenceImplementation< Key, Alloc, Gen >::clear(), GUM_ERROR, gum::SequenceImplementation< Key, Alloc, Gen >::insert(), and gum::SequenceImplementation< Key, Alloc, Gen >::size().

Referenced by gum::GibbsOperator< GUM_SCALAR >::GibbsOperator().

53  {
55  for (const auto node : _sampling_bn.nodes())
56  if (_hardEv == nullptr || !_hardEv->exists(node))
57  _samplingNodes.insert(node);
58  if (_samplingNodes.size() == 0) {
59  GUM_ERROR(InvalidArgument,
60  "No node to sample (too many nodes or too much evidence)!")
61  }
63  }
void clear()
Clear the sequence.
Definition: sequence_tpl.h:271
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:38
Sequence< NodeId > _samplingNodes
Definition: gibbsOperator.h:85
const IBayesNet< GUM_SCALAR > & _sampling_bn
Definition: gibbsOperator.h:83
const NodeProperty< Idx > * _hardEv
Definition: gibbsOperator.h:84
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408
+ 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 70 of file gibbsOperator.h.

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

70 { 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 70 of file gibbsOperator_tpl.h.

References gum::GibbsOperator< GUM_SCALAR >::__drawVarMonteCarlo(), gum::GibbsOperator< GUM_SCALAR >::_hardEv, gum::GibbsOperator< GUM_SCALAR >::_sampling_bn, gum::Instantiation::add(), and gum::Instantiation::chgVal().

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), gum::GibbsSampling< GUM_SCALAR >::_monteCarloSample(), and gum::GibbsOperator< GUM_SCALAR >::setDrawnAtRandom().

70  {
72 
73  for (const auto nod : _sampling_bn.topologicalOrder()) {
74  I.add(_sampling_bn.variable(nod));
75  if (_hardEv != nullptr && _hardEv->exists(nod)) {
76  I.chgVal(_sampling_bn.variable(nod), (*_hardEv)[nod]);
77  } else {
78  __drawVarMonteCarlo(nod, &I);
79  }
80  }
81  return I;
82  }
void __drawVarMonteCarlo(NodeId nod, Instantiation *I)
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
const IBayesNet< GUM_SCALAR > & _sampling_bn
Definition: gibbsOperator.h:83
const NodeProperty< Idx > * _hardEv
Definition: gibbsOperator.h:84
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
+ 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 66 of file gibbsOperator.h.

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

66 { return _nbr; }

◆ nextSample()

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

draws next sample of Gibbs sampling

Definition at line 95 of file gibbsOperator_tpl.h.

References gum::GibbsOperator< GUM_SCALAR >::__GibbsSample(), gum::GibbsOperator< GUM_SCALAR >::_atRandom, gum::GibbsOperator< GUM_SCALAR >::_counting, gum::GibbsOperator< GUM_SCALAR >::_nbr, gum::GibbsOperator< GUM_SCALAR >::_samplingNodes, gum::randomValue(), and gum::SequenceImplementation< Key, Alloc, Gen >::size().

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), gum::GibbsSampling< GUM_SCALAR >::_draw(), and gum::GibbsOperator< GUM_SCALAR >::setDrawnAtRandom().

95  {
96  for (Idx i = 0; i < _nbr; i++) {
99  this->__GibbsSample(_samplingNodes[pos], &prev);
100  _counting++;
101  }
102  return prev;
103  }
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:38
Sequence< NodeId > _samplingNodes
Definition: gibbsOperator.h:85
Size _counting
number of samples drawn
Definition: gibbsOperator.h:82
void __GibbsSample(NodeId id, Instantiation *I)
change in Instantiation I a new drawn value for id
+ 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 72 of file gibbsOperator.h.

References gum::GibbsOperator< GUM_SCALAR >::_atRandom, gum::GibbsOperator< GUM_SCALAR >::monteCarloSample(), and gum::GibbsOperator< GUM_SCALAR >::nextSample().

72 { _atRandom = atRandom; }
+ Here is the call graph for this function:

◆ setNbrDrawnVar()

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

Definition at line 68 of file gibbsOperator.h.

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

68 { _nbr = nbr; }

Member Data Documentation

◆ _atRandom

◆ _counting

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

number of samples drawn

Definition at line 82 of file gibbsOperator.h.

Referenced by gum::GibbsOperator< GUM_SCALAR >::nextSample().

◆ _hardEv

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

◆ _nbr

◆ _sampling_bn

◆ _samplingNodes

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

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