aGrUM  0.13.2
gibbsOperator_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES et Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
30 
31 namespace gum {
32 
33  template < typename GUM_SCALAR >
35  const NodeProperty< Idx >* hardEv,
36  Size nbr,
37  bool atRandom) :
38  _counting(0),
39  _sampling_bn(BN), _hardEv(hardEv), _nbr(nbr), _atRandom(atRandom) {
41  GUM_CONSTRUCTOR(GibbsOperator);
42  }
43 
44  template < typename GUM_SCALAR >
46  GUM_DESTRUCTOR(GibbsOperator);
47  }
48 
49  template < typename GUM_SCALAR >
52  for (const auto node : _sampling_bn.nodes())
53  if (_hardEv == nullptr || !_hardEv->exists(node))
54  _samplingNodes.insert(node);
55  if (_samplingNodes.size() == 0) {
57  "No node to sample (too many nodes or too much evidence)!")
58  }
60  }
61 
66  template < typename GUM_SCALAR >
69 
70  for (const auto nod : _sampling_bn.topologicalOrder()) {
71  I.add(_sampling_bn.variable(nod));
72  if (_hardEv != nullptr && _hardEv->exists(nod)) {
73  I.chgVal(_sampling_bn.variable(nod), (*_hardEv)[nod]);
74  } else {
75  __drawVarMonteCarlo(nod, &I);
76  }
77  }
78  return I;
79  }
80 
81  template < typename GUM_SCALAR >
83  Instantiation* I) {
84  gum::Instantiation Itop(*I);
85  Itop.erase(_sampling_bn.variable(nod));
86  I->chgVal(_sampling_bn.variable(nod),
87  _sampling_bn.cpt(nod).extract(Itop).draw());
88  }
89 
90 
91  template < typename GUM_SCALAR >
93  for (Idx i = 0; i < _nbr; i++) {
96  this->__GibbsSample(_samplingNodes[pos], &prev);
97  _counting++;
98  }
99  return prev;
100  }
102 
103  template < typename GUM_SCALAR >
105  gum::Instantiation Itop(*I);
106  Itop.erase(_sampling_bn.variable(id));
107  gum::Potential< GUM_SCALAR > p = _sampling_bn.cpt(id).extract(Itop);
108  for (const auto nod : _sampling_bn.children(id))
109  p *= _sampling_bn.cpt(nod).extract(Itop);
110  GUM_ASSERT(p.nbrDim() == 1);
111  if (p.sum() != 0) {
112  p.normalize();
113  I->chgVal(_sampling_bn.variable(id), p.draw());
114  }
115  }
116 } // namespace gum
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
This file contains Gibbs sampling (for BNs) class definitions.
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
void clear()
Clear the sequence.
Definition: sequence_tpl.h:268
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
GibbsOperator(const IBayesNet< GUM_SCALAR > &BN, const NodeProperty< Idx > *hardEv, Size nbr=1, bool atRandom=false)
constructor
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:35
Sequence< NodeId > _samplingNodes
Definition: gibbsOperator.h:82
void __drawVarMonteCarlo(NodeId nod, Instantiation *I)
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:676
const IBayesNet< GUM_SCALAR > & _sampling_bn
Definition: gibbsOperator.h:80
Size _counting
number of samples drawn
Definition: gibbsOperator.h:79
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Instantiation nextSample(Instantiation prev)
draws next sample of Gibbs sampling
void __GibbsSample(NodeId id, Instantiation *I)
change in Instantiation I a new drawn value for id
virtual ~GibbsOperator()
Destructor.
const NodeProperty< Idx > * _hardEv
Definition: gibbsOperator.h:81
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
Idx draw() const
get a value at random from a 1-D distribution
Instantiation monteCarloSample()
draws a Monte Carlo sample
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
Contains usefull methods for random stuff.
GUM_SCALAR sum() const
sum of all elements in the Potential
unsigned long Idx
Type for indexes.
Definition: types.h:43
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
class containing all variables and methods required for Gibbssampling
Definition: gibbsOperator.h:47
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:405