aGrUM  0.16.0
GibbsKL_tpl.h
Go to the documentation of this file.
1 
30 #include <agrum/core/math/math.h>
31 #include <agrum/BN/IBayesNet.h>
35 #include <agrum/core/hashTable.h>
36 
37 #define GIBBSKL_DEFAULT_MAXITER 10000000
38 #define GIBBSKL_DEFAULT_EPSILON 1e-10
39 #define GIBBSKL_DEFAULT_MIN_EPSILON_RATE 1e-10
40 #define GIBBSKL_DEFAULT_PERIOD_SIZE 200
41 #define GIBBSKL_DEFAULT_VERBOSITY false
42 #define GIBBSKL_DEFAULT_BURNIN 2000
43 #define GIBBSKL_DEFAULT_TIMEOUT 6000
44 
45 #define GIBBSKL_POURCENT_DRAWN_SAMPLE 10 // percent drawn
46 #define GIBBSKL_DRAWN_AT_RANDOM false
47 
48 namespace gum {
49 
50 
51  template < typename GUM_SCALAR >
54  BNdistance< GUM_SCALAR >(P, Q),
56  GibbsOperator< GUM_SCALAR >(
57  P,
58  nullptr,
59  1 + (P.size() * GIBBSKL_POURCENT_DRAWN_SAMPLE / 100),
61  GUM_CONSTRUCTOR(GibbsBNdistance);
62 
70  }
71 
72  template < typename GUM_SCALAR >
74  const BNdistance< GUM_SCALAR >& kl) :
75  BNdistance< GUM_SCALAR >(kl),
77  // Gibbs operator with 10% of nodes changes at random between each samples
78  ,
79  GibbsOperator< GUM_SCALAR >(
80  kl.p(),
81  nullptr,
82  1 + (kl.p().size() * GIBBSKL_POURCENT_DRAWN_SAMPLE / 100),
83  true) {
84  GUM_CONSTRUCTOR(GibbsBNdistance);
85 
93  }
94 
95  template < typename GUM_SCALAR >
97  GUM_DESTRUCTOR(GibbsBNdistance);
98  }
99 
100  template < typename GUM_SCALAR >
102  auto Iq = _q.completeInstantiation();
103 
106 
107  // map between particle() variables and _q variables (using name of vars)
109 
110  for (Idx ite = 0; ite < I.nbrDim(); ++ite) {
111  map.insert(&I.variable(ite), &_q.variableFromName(I.variable(ite).name()));
112  }
113 
114  // BURN IN
115  for (Idx i = 0; i < burnIn(); i++)
116  I = this->nextSample(I);
117 
118  // SAMPLING
119  _klPQ = _klQP = _hellinger = _jsd = (GUM_SCALAR)0.0;
120  _errorPQ = _errorQP = 0;
122  GUM_SCALAR delta, ratio, error;
123  delta = ratio = error = (GUM_SCALAR)-1;
124  GUM_SCALAR oldPQ = 0.0;
125  GUM_SCALAR pp, pq, pmid;
126 
127  do {
128  this->disableMinEpsilonRate();
129  I = this->nextSample(I);
131 
132  //_p.synchroInstantiations( Ip,I);
133  Iq.setValsFrom(map, I);
134 
135  pp = _p.jointProbability(I);
136  pq = _q.jointProbability(Iq);
137  pmid = (pp + pq) / 2.0;
138 
139  if (pp != (GUM_SCALAR)0.0) {
140  _hellinger += std::pow(std::sqrt(pp) - std::sqrt(pq), 2) / pp;
141 
142  if (pq != (GUM_SCALAR)0.0) {
143  _bhattacharya += std::sqrt(pq / pp); // std::sqrt(pp*pq)/pp
145  this->enableMinEpsilonRate(); // replace check_rate=true;
146  ratio = pq / pp;
147  delta = (GUM_SCALAR)log2(ratio);
148  _klPQ += delta;
149 
150  // pmid!=0
151  _jsd -= log2(pp / pmid) + ratio * log2(pq / pmid);
152  } else {
153  _errorPQ++;
154  }
155  }
156 
157  if (pq != (GUM_SCALAR)0.0) {
158  if (pp != (GUM_SCALAR)0.0) {
159  // if we are here, it is certain that delta and ratio have been
160  // computed
161  // further lines above. (for now #112-113)
162  _klQP += (GUM_SCALAR)(-delta * ratio);
163  } else {
164  _errorQP++;
165  }
166  }
167 
168  if (this->isEnabledMinEpsilonRate()) { // replace check_rate
169  // delta is used as a temporary variable
170  delta = _klPQ / nbrIterations();
171  error = (GUM_SCALAR)std::abs(delta - oldPQ);
172  oldPQ = delta;
173  }
174  } while (continueApproximationScheme(error)); //
175 
176  _klPQ = -_klPQ / (nbrIterations());
177  _klQP = -_klQP / (nbrIterations());
178  _jsd = -0.5 * _jsd / (nbrIterations());
179  _hellinger = std::sqrt(_hellinger / nbrIterations());
180  _bhattacharya = -std::log(_bhattacharya / (nbrIterations()));
181  }
182 
183  template < typename GUM_SCALAR >
185  this->_burn_in = b;
186  }
187 
188  template < typename GUM_SCALAR >
190  return this->_burn_in;
191  }
192 } // namespace gum
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
#define GIBBSKL_DEFAULT_TIMEOUT
Definition: GibbsKL_tpl.h:43
void disableMinEpsilonRate()
Disable stopping criterion on epsilon rate.
void _computeKL() final
Definition: GibbsKL_tpl.h:101
GUM_SCALAR _klPQ
Definition: BNdistance.h:139
GUM_SCALAR _bhattacharya
Definition: BNdistance.h:142
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
#define GIBBSKL_DRAWN_AT_RANDOM
Definition: GibbsKL_tpl.h:46
Approximation Scheme.
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:136
void enableMinEpsilonRate()
Enable stopping criterion on epsilon rate.
#define GIBBSKL_DEFAULT_BURNIN
Definition: GibbsKL_tpl.h:42
void setPeriodSize(Size p)
How many samples between two stopping is enable.
#define GIBBSKL_POURCENT_DRAWN_SAMPLE
Definition: GibbsKL_tpl.h:45
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
GUM_SCALAR _jsd
Definition: BNdistance.h:143
const IBayesNet< GUM_SCALAR > & p() const
void initApproximationScheme()
Initialise the scheme.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void setMinEpsilonRate(double rate)
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void setVerbosity(bool v)
Set the verbosity on (true) or off (false).
The class for generic Hash Tables.
Definition: hashTable.h:679
void setMaxTime(double timeout)
Stopping criterion on timeout.
Size _burn_in
Number of iterations before checking stopping criteria.
Size nbrIterations() const
Returns the number of iterations.
~GibbsBNdistance()
destructor
Definition: GibbsKL_tpl.h:96
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
#define GIBBSKL_DEFAULT_EPSILON
Definition: GibbsKL_tpl.h:38
GUM_SCALAR _hellinger
Definition: BNdistance.h:141
bool isEnabledMinEpsilonRate() const
Returns true if stopping criterion on epsilon rate is enabled, false otherwise.
Instantiation nextSample(Instantiation prev)
draws next sample of Gibbs sampling
GibbsKL computes the KL divergence betweens 2 BNs using an approximation pattern: GIBBS sampling...
#define GIBBSKL_DEFAULT_PERIOD_SIZE
Definition: GibbsKL_tpl.h:40
Size burnIn() const
Returns the number of burn in.
Definition: GibbsKL_tpl.h:189
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
void setBurnIn(Size b)
Number of burn in for one iteration.
Definition: GibbsKL_tpl.h:184
void setMaxIter(Size max)
Stopping criterion on number of iterations.
GUM_SCALAR _klQP
Definition: BNdistance.h:140
Instantiation monteCarloSample()
draws a Monte Carlo sample
#define GIBBSKL_DEFAULT_MIN_EPSILON_RATE
Definition: GibbsKL_tpl.h:39
void setEpsilon(double eps)
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
Size Idx
Type for indexes.
Definition: types.h:53
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const std::string & name() const
returns the name of the variable
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
#define GIBBSKL_DEFAULT_VERBOSITY
Definition: GibbsKL_tpl.h:41
class containing all variables and methods required for Gibbssampling
Definition: gibbsOperator.h:50
GibbsBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition: GibbsKL_tpl.h:52
#define GIBBSKL_DEFAULT_MAXITER
Definition: GibbsKL_tpl.h:37
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.