aGrUM  0.16.0
gum::GibbsBNdistance< GUM_SCALAR > Class Template Reference

GibbsKL computes the KL divergence betweens 2 BNs using an approximation pattern: GIBBS sampling. More...

#include <GibbsBNdistance.h>

+ Inheritance diagram for gum::GibbsBNdistance< GUM_SCALAR >:
+ Collaboration diagram for gum::GibbsBNdistance< GUM_SCALAR >:

Public Attributes

Signaler3< Size, double, doubleonProgress
 Progression, error and time. More...
 
Signaler1< std::string > onStop
 Criteria messageApproximationScheme. More...
 

Public Member Functions

 GibbsBNdistance (const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
 constructor must give 2 BNs More...
 
 GibbsBNdistance (const BNdistance< GUM_SCALAR > &kl)
 copy constructor More...
 
 ~GibbsBNdistance ()
 destructor More...
 
void setBurnIn (Size b)
 Number of burn in for one iteration. More...
 
Size burnIn () const
 Returns the number of burn in. More...
 
Complexity difficulty () const
 return KL::Complexity::Heavy,KL::Complexity::Difficult,KL::Complexity::Correct depending on the BNs __p and __q 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...
 
Accessors to results. The first call do the computations. The

others do not.

double klPQ ()
 
Size errorPQ ()
 
double klQP ()
 
Size errorQP ()
 
double hellinger ()
 
double bhattacharya ()
 
double jsd ()
 
const IBayesNet< GUM_SCALAR > & p () const
 
const IBayesNet< GUM_SCALAR > & q () const
 
Getters and setters
void setEpsilon (double eps)
 Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|. More...
 
double epsilon () const
 Returns the value of epsilon. More...
 
void disableEpsilon ()
 Disable stopping criterion on epsilon. More...
 
void enableEpsilon ()
 Enable stopping criterion on epsilon. More...
 
bool isEnabledEpsilon () const
 Returns true if stopping criterion on epsilon is enabled, false otherwise. More...
 
void setMinEpsilonRate (double rate)
 Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|). More...
 
double minEpsilonRate () const
 Returns the value of the minimal epsilon rate. More...
 
void disableMinEpsilonRate ()
 Disable stopping criterion on epsilon rate. More...
 
void enableMinEpsilonRate ()
 Enable stopping criterion on epsilon rate. More...
 
bool isEnabledMinEpsilonRate () const
 Returns true if stopping criterion on epsilon rate is enabled, false otherwise. More...
 
void setMaxIter (Size max)
 Stopping criterion on number of iterations. More...
 
Size maxIter () const
 Returns the criterion on number of iterations. More...
 
void disableMaxIter ()
 Disable stopping criterion on max iterations. More...
 
void enableMaxIter ()
 Enable stopping criterion on max iterations. More...
 
bool isEnabledMaxIter () const
 Returns true if stopping criterion on max iterations is enabled, false otherwise. More...
 
void setMaxTime (double timeout)
 Stopping criterion on timeout. More...
 
double maxTime () const
 Returns the timeout (in seconds). More...
 
double currentTime () const
 Returns the current running time in second. More...
 
void disableMaxTime ()
 Disable stopping criterion on timeout. More...
 
void enableMaxTime ()
 Enable stopping criterion on timeout. More...
 
bool isEnabledMaxTime () const
 Returns true if stopping criterion on timeout is enabled, false otherwise. More...
 
void setPeriodSize (Size p)
 How many samples between two stopping is enable. More...
 
Size periodSize () const
 Returns the period size. More...
 
void setVerbosity (bool v)
 Set the verbosity on (true) or off (false). More...
 
bool verbosity () const
 Returns true if verbosity is enabled. More...
 
ApproximationSchemeSTATE stateApproximationScheme () const
 Returns the approximation scheme state. More...
 
Size nbrIterations () const
 Returns the number of iterations. More...
 
const std::vector< double > & history () const
 Returns the scheme history. More...
 
void initApproximationScheme ()
 Initialise the scheme. More...
 
bool startOfPeriod ()
 Returns true if we are at the beginning of a period (compute error is mandatory). More...
 
void updateApproximationScheme (unsigned int incr=1)
 Update the scheme w.r.t the new error and increment steps. More...
 
Size remainingBurnIn ()
 Returns the remaining burn in. More...
 
void stopApproximationScheme ()
 Stop the approximation scheme. More...
 
bool continueApproximationScheme (double error)
 Update the scheme w.r.t the new error. More...
 
Getters and setters
std::string messageApproximationScheme () const
 Returns the approximation scheme message. More...
 

Public Types

enum  ApproximationSchemeSTATE : char {
  ApproximationSchemeSTATE::Undefined, ApproximationSchemeSTATE::Continue, ApproximationSchemeSTATE::Epsilon, ApproximationSchemeSTATE::Rate,
  ApproximationSchemeSTATE::Limit, ApproximationSchemeSTATE::TimeLimit, ApproximationSchemeSTATE::Stopped
}
 The different state of an approximation scheme. More...
 

Protected Attributes

const IBayesNet< GUM_SCALAR > & _p
 
const IBayesNet< GUM_SCALAR > & _q
 
GUM_SCALAR _klPQ
 
GUM_SCALAR _klQP
 
GUM_SCALAR _hellinger
 
GUM_SCALAR _bhattacharya
 
GUM_SCALAR _jsd
 
Size _errorPQ
 
Size _errorQP
 
double _current_epsilon
 Current epsilon. More...
 
double _last_epsilon
 Last epsilon value. More...
 
double _current_rate
 Current rate. More...
 
Size _current_step
 The current step. More...
 
Timer _timer
 The timer. More...
 
ApproximationSchemeSTATE _current_state
 The current state. More...
 
std::vector< double_history
 The scheme history, used only if verbosity == true. More...
 
double _eps
 Threshold for convergence. More...
 
bool _enabled_eps
 If true, the threshold convergence is enabled. More...
 
double _min_rate_eps
 Threshold for the epsilon rate. More...
 
bool _enabled_min_rate_eps
 If true, the minimal threshold for epsilon rate is enabled. More...
 
double _max_time
 The timeout. More...
 
bool _enabled_max_time
 If true, the timeout is enabled. More...
 
Size _max_iter
 The maximum iterations. More...
 
bool _enabled_max_iter
 If true, the maximum iterations stopping criterion is enabled. More...
 
Size _burn_in
 Number of iterations before checking stopping criteria. More...
 
Size _period_size
 Checking criteria frequency. More...
 
bool _verbosity
 If true, verbosity is enabled. More...
 
Size _counting
 number of samples drawn More...
 
const IBayesNet< GUM_SCALAR > & _sampling_bn
 
const NodeProperty< Idx > * _hardEv
 
Sequence< NodeId_samplingNodes
 
Size _nbr
 
bool _atRandom
 

Protected Member Functions

void _computeKL () final
 
void _process ()
 

Detailed Description

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

GibbsKL computes the KL divergence betweens 2 BNs using an approximation pattern: GIBBS sampling.

KL.process() computes KL(P||Q) using klPQ() and KL(Q||P) using klQP(). The computations are made once. The second is for free :) GibbsKL allows as well to compute in the same time the Hellinger distance ( \( *\sqrt{\sum_i (\sqrt{p_i}-\sqrt{q_i})^2}\)) (Kokolakis and Nanopoulos, 2001) and Bhattacharya distance (Kaylath,T. 1967)

It may happen that P*ln(P/Q) is not computable (Q=0 and P!=0). In such a case, KL keeps working but trace this error (errorPQ() and errorQP()). In those cases, Hellinger distance approximation is under-evaluated.

Warning
: convergence and stop criteria are designed w.r.t the main computation : KL(P||Q). The 3 others have no guarantee.

snippets :

gum::KL base_kl(net1,net2);
if (base_kl.difficulty()!=KL::HEAVY) {
gum::ExactBNdistance kl(base_kl);
std::cout<<"KL net1||net2 :"<<kl.klPQ()<<std::endl;
} else {
gum::GibbsBNdistance kl(base_kl);
std::cout<<"KL net1||net2 :"<<kl.klPQ()<<std::endl;
}

Definition at line 79 of file GibbsBNdistance.h.

Member Enumeration Documentation

◆ ApproximationSchemeSTATE

The different state of an approximation scheme.

Enumerator
Undefined 
Continue 
Epsilon 
Rate 
Limit 
TimeLimit 
Stopped 

Definition at line 65 of file IApproximationSchemeConfiguration.h.

65  : char {
66  Undefined,
67  Continue,
68  Epsilon,
69  Rate,
70  Limit,
71  TimeLimit,
72  Stopped
73  };

Constructor & Destructor Documentation

◆ GibbsBNdistance() [1/2]

template<typename GUM_SCALAR >
gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance ( const IBayesNet< GUM_SCALAR > &  P,
const IBayesNet< GUM_SCALAR > &  Q 
)

constructor must give 2 BNs

Exceptions
gum::OperationNotAllowedif the 2 BNs have not the same domainSize or compatible node sets.

Definition at line 52 of file GibbsKL_tpl.h.

References GIBBSKL_DEFAULT_BURNIN, GIBBSKL_DEFAULT_EPSILON, GIBBSKL_DEFAULT_MAXITER, GIBBSKL_DEFAULT_MIN_EPSILON_RATE, GIBBSKL_DEFAULT_PERIOD_SIZE, GIBBSKL_DEFAULT_TIMEOUT, GIBBSKL_DEFAULT_VERBOSITY, gum::GibbsBNdistance< GUM_SCALAR >::setBurnIn(), gum::ApproximationScheme::setEpsilon(), gum::ApproximationScheme::setMaxIter(), gum::ApproximationScheme::setMaxTime(), gum::ApproximationScheme::setMinEpsilonRate(), gum::ApproximationScheme::setPeriodSize(), and gum::ApproximationScheme::setVerbosity().

53  :
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  }
#define GIBBSKL_DEFAULT_TIMEOUT
Definition: GibbsKL_tpl.h:43
#define GIBBSKL_DRAWN_AT_RANDOM
Definition: GibbsKL_tpl.h:46
#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
void setMinEpsilonRate(double rate)
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
void setVerbosity(bool v)
Set the verbosity on (true) or off (false).
ApproximationScheme(bool verbosity=false)
void setMaxTime(double timeout)
Stopping criterion on timeout.
#define GIBBSKL_DEFAULT_EPSILON
Definition: GibbsKL_tpl.h:38
#define GIBBSKL_DEFAULT_PERIOD_SIZE
Definition: GibbsKL_tpl.h:40
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.
#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)|.
#define GIBBSKL_DEFAULT_VERBOSITY
Definition: GibbsKL_tpl.h:41
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
+ Here is the call graph for this function:

◆ GibbsBNdistance() [2/2]

template<typename GUM_SCALAR >
gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance ( const BNdistance< GUM_SCALAR > &  kl)
explicit

copy constructor

Definition at line 73 of file GibbsKL_tpl.h.

References GIBBSKL_DEFAULT_BURNIN, GIBBSKL_DEFAULT_EPSILON, GIBBSKL_DEFAULT_MAXITER, GIBBSKL_DEFAULT_MIN_EPSILON_RATE, GIBBSKL_DEFAULT_PERIOD_SIZE, GIBBSKL_DEFAULT_TIMEOUT, GIBBSKL_DEFAULT_VERBOSITY, gum::GibbsBNdistance< GUM_SCALAR >::setBurnIn(), gum::ApproximationScheme::setEpsilon(), gum::ApproximationScheme::setMaxIter(), gum::ApproximationScheme::setMaxTime(), gum::ApproximationScheme::setMinEpsilonRate(), gum::ApproximationScheme::setPeriodSize(), and gum::ApproximationScheme::setVerbosity().

74  :
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  }
#define GIBBSKL_DEFAULT_TIMEOUT
Definition: GibbsKL_tpl.h:43
#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
void setMinEpsilonRate(double rate)
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
void setVerbosity(bool v)
Set the verbosity on (true) or off (false).
ApproximationScheme(bool verbosity=false)
void setMaxTime(double timeout)
Stopping criterion on timeout.
#define GIBBSKL_DEFAULT_EPSILON
Definition: GibbsKL_tpl.h:38
#define GIBBSKL_DEFAULT_PERIOD_SIZE
Definition: GibbsKL_tpl.h:40
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.
#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)|.
#define GIBBSKL_DEFAULT_VERBOSITY
Definition: GibbsKL_tpl.h:41
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
+ Here is the call graph for this function:

◆ ~GibbsBNdistance()

template<typename GUM_SCALAR >
gum::GibbsBNdistance< GUM_SCALAR >::~GibbsBNdistance ( )

destructor

Definition at line 96 of file GibbsKL_tpl.h.

96  {
97  GUM_DESTRUCTOR(GibbsBNdistance);
98  }
GibbsBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition: GibbsKL_tpl.h:52

Member Function Documentation

◆ _computeKL()

template<typename GUM_SCALAR >
void gum::GibbsBNdistance< GUM_SCALAR >::_computeKL ( )
finalprotectedvirtual

Reimplemented from gum::BNdistance< GUM_SCALAR >.

Definition at line 101 of file GibbsKL_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_bhattacharya, gum::BNdistance< GUM_SCALAR >::_errorPQ, gum::BNdistance< GUM_SCALAR >::_errorQP, gum::BNdistance< GUM_SCALAR >::_hellinger, gum::BNdistance< GUM_SCALAR >::_jsd, gum::BNdistance< GUM_SCALAR >::_klPQ, gum::BNdistance< GUM_SCALAR >::_klQP, gum::BNdistance< GUM_SCALAR >::_p, gum::BNdistance< GUM_SCALAR >::_q, gum::GibbsBNdistance< GUM_SCALAR >::burnIn(), gum::ApproximationScheme::continueApproximationScheme(), gum::ApproximationScheme::disableMinEpsilonRate(), gum::ApproximationScheme::enableMinEpsilonRate(), gum::ApproximationScheme::initApproximationScheme(), gum::HashTable< Key, Val, Alloc >::insert(), gum::ApproximationScheme::isEnabledMinEpsilonRate(), gum::GibbsOperator< GUM_SCALAR >::monteCarloSample(), gum::Variable::name(), gum::Instantiation::nbrDim(), gum::ApproximationScheme::nbrIterations(), gum::GibbsOperator< GUM_SCALAR >::nextSample(), gum::ApproximationScheme::updateApproximationScheme(), and gum::Instantiation::variable().

101  {
102  auto Iq = _q.completeInstantiation();
103 
106 
107  // map between particle() variables and _q variables (using name of vars)
108  HashTable< const DiscreteVariable*, const DiscreteVariable* > map;
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  }
void disableMinEpsilonRate()
Disable stopping criterion on epsilon rate.
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.
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:136
void enableMinEpsilonRate()
Enable stopping criterion on epsilon rate.
GUM_SCALAR _jsd
Definition: BNdistance.h:143
void initApproximationScheme()
Initialise the scheme.
Size nbrIterations() const
Returns the number of iterations.
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
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
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
GUM_SCALAR _klQP
Definition: BNdistance.h:140
Instantiation monteCarloSample()
draws a Monte Carlo sample
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.
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
+ Here is the call graph for this function:

◆ _process()

template<typename GUM_SCALAR >
void gum::BNdistance< GUM_SCALAR >::_process ( )
protectedinherited

Definition at line 178 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::__done, and gum::BNdistance< GUM_SCALAR >::_computeKL().

Referenced by gum::BNdistance< GUM_SCALAR >::bhattacharya(), gum::BNdistance< GUM_SCALAR >::errorPQ(), gum::BNdistance< GUM_SCALAR >::errorQP(), gum::BNdistance< GUM_SCALAR >::hellinger(), gum::BNdistance< GUM_SCALAR >::jsd(), gum::BNdistance< GUM_SCALAR >::klPQ(), and gum::BNdistance< GUM_SCALAR >::klQP().

178  {
179  if (!__done) {
180  _computeKL();
181  __done = true;
182  }
183  }
virtual void _computeKL()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ bhattacharya()

template<typename GUM_SCALAR >
INLINE double gum::BNdistance< GUM_SCALAR >::bhattacharya ( )
inherited
Returns
Bhattacharya distance (
See also
http://en.wikipedia.org/wiki/Bhattacharya_distance)

Definition at line 93 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_bhattacharya, and gum::BNdistance< GUM_SCALAR >::_process().

93  {
94  _process();
95  return _bhattacharya;
96  }
GUM_SCALAR _bhattacharya
Definition: BNdistance.h:142
+ Here is the call graph for this function:

◆ burnIn()

template<typename GUM_SCALAR >
Size gum::GibbsBNdistance< GUM_SCALAR >::burnIn ( ) const

Returns the number of burn in.

Returns
Returns the number of burn in.

Definition at line 189 of file GibbsKL_tpl.h.

References gum::ApproximationScheme::_burn_in.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::_computeKL().

189  {
190  return this->_burn_in;
191  }
Size _burn_in
Number of iterations before checking stopping criteria.
+ Here is the caller graph for this function:

◆ continueApproximationScheme()

INLINE bool gum::ApproximationScheme::continueApproximationScheme ( double  error)
inherited

Update the scheme w.r.t the new error.

Test the stopping criterion that are enabled.

Parameters
errorThe new error value.
Returns
false if state become != ApproximationSchemeSTATE::Continue
Exceptions
OperationNotAllowedRaised if state != ApproximationSchemeSTATE::Continue.

Definition at line 227 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_current_epsilon, gum::ApproximationScheme::_current_rate, gum::ApproximationScheme::_current_state, gum::ApproximationScheme::_current_step, gum::ApproximationScheme::_enabled_eps, gum::ApproximationScheme::_enabled_max_iter, gum::ApproximationScheme::_enabled_max_time, gum::ApproximationScheme::_enabled_min_rate_eps, gum::ApproximationScheme::_eps, gum::ApproximationScheme::_history, gum::ApproximationScheme::_last_epsilon, gum::ApproximationScheme::_max_iter, gum::ApproximationScheme::_max_time, gum::ApproximationScheme::_min_rate_eps, gum::ApproximationScheme::_stopScheme(), gum::ApproximationScheme::_timer, gum::IApproximationSchemeConfiguration::Continue, gum::IApproximationSchemeConfiguration::Epsilon, GUM_EMIT3, GUM_ERROR, gum::IApproximationSchemeConfiguration::Limit, gum::IApproximationSchemeConfiguration::messageApproximationScheme(), gum::IApproximationSchemeConfiguration::onProgress, gum::IApproximationSchemeConfiguration::Rate, gum::ApproximationScheme::startOfPeriod(), gum::ApproximationScheme::stateApproximationScheme(), gum::Timer::step(), gum::IApproximationSchemeConfiguration::TimeLimit, and gum::ApproximationScheme::verbosity().

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), gum::SamplingInference< GUM_SCALAR >::_loopApproxInference(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::learning::GreedyHillClimbing::learnStructure(), gum::learning::LocalSearchWithTabuList::learnStructure(), and gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::makeInference().

227  {
228  // For coherence, we fix the time used in the method
229 
230  double timer_step = _timer.step();
231 
232  if (_enabled_max_time) {
233  if (timer_step > _max_time) {
235  return false;
236  }
237  }
238 
239  if (!startOfPeriod()) { return true; }
240 
242  GUM_ERROR(OperationNotAllowed,
243  "state of the approximation scheme is not correct : "
245  }
246 
247  if (verbosity()) { _history.push_back(error); }
248 
249  if (_enabled_max_iter) {
250  if (_current_step > _max_iter) {
252  return false;
253  }
254  }
255 
257  _current_epsilon = error; // eps rate isEnabled needs it so affectation was
258  // moved from eps isEnabled below
259 
260  if (_enabled_eps) {
261  if (_current_epsilon <= _eps) {
263  return false;
264  }
265  }
266 
267  if (_last_epsilon >= 0.) {
268  if (_current_epsilon > .0) {
269  // ! _current_epsilon can be 0. AND epsilon
270  // isEnabled can be disabled !
271  _current_rate =
273  }
274  // limit with current eps ---> 0 is | 1 - ( last_eps / 0 ) | --->
275  // infinity the else means a return false if we isEnabled the rate below,
276  // as we would have returned false if epsilon isEnabled was enabled
277  else {
279  }
280 
281  if (_enabled_min_rate_eps) {
282  if (_current_rate <= _min_rate_eps) {
284  return false;
285  }
286  }
287  }
288 
290  if (onProgress.hasListener()) {
292  }
293 
294  return true;
295  } else {
296  return false;
297  }
298  }
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:42
Signaler3< Size, double, double > onProgress
Progression, error and time.
bool _enabled_max_iter
If true, the maximum iterations stopping criterion is enabled.
bool _enabled_eps
If true, the threshold convergence is enabled.
void _stopScheme(ApproximationSchemeSTATE new_state)
Stop the scheme given a new state.
double _current_epsilon
Current epsilon.
bool _enabled_min_rate_eps
If true, the minimal threshold for epsilon rate is enabled.
bool startOfPeriod()
Returns true if we are at the beginning of a period (compute error is mandatory). ...
double _eps
Threshold for convergence.
double _current_rate
Current rate.
bool _enabled_max_time
If true, the timeout is enabled.
Size _current_step
The current step.
std::vector< double > _history
The scheme history, used only if verbosity == true.
double _min_rate_eps
Threshold for the epsilon rate.
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
bool verbosity() const
Returns true if verbosity is enabled.
std::string messageApproximationScheme() const
Returns the approximation scheme message.
double _last_epsilon
Last epsilon value.
Size _max_iter
The maximum iterations.
#define GUM_EMIT3(signal, arg1, arg2, arg3)
Definition: signaler3.h:42
ApproximationSchemeSTATE _current_state
The current state.
double _max_time
The timeout.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ currentTime()

INLINE double gum::ApproximationScheme::currentTime ( ) const
virtualinherited

Returns the current running time in second.

Returns
Returns the current running time in second.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 128 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_timer, and gum::Timer::step().

Referenced by gum::learning::genericBNLearner::currentTime().

128 { return _timer.step(); }
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ difficulty()

template<typename GUM_SCALAR >
Complexity gum::BNdistance< GUM_SCALAR >::difficulty ( ) const
inherited

return KL::Complexity::Heavy,KL::Complexity::Difficult,KL::Complexity::Correct depending on the BNs __p and __q

Definition at line 70 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::__difficulty.

70  {
71  return __difficulty;
72  }
Complexity __difficulty
Definition: BNdistance.h:150

◆ disableEpsilon()

INLINE void gum::ApproximationScheme::disableEpsilon ( )
virtualinherited

Disable stopping criterion on epsilon.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 54 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_eps.

Referenced by gum::learning::genericBNLearner::disableEpsilon().

54 { _enabled_eps = false; }
bool _enabled_eps
If true, the threshold convergence is enabled.
+ Here is the caller graph for this function:

◆ disableMaxIter()

INLINE void gum::ApproximationScheme::disableMaxIter ( )
virtualinherited

Disable stopping criterion on max iterations.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 105 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_max_iter.

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::__mcInitApproximationScheme(), gum::learning::genericBNLearner::disableMaxIter(), and gum::learning::GreedyHillClimbing::GreedyHillClimbing().

105 { _enabled_max_iter = false; }
bool _enabled_max_iter
If true, the maximum iterations stopping criterion is enabled.
+ Here is the caller graph for this function:

◆ disableMaxTime()

INLINE void gum::ApproximationScheme::disableMaxTime ( )
virtualinherited

Disable stopping criterion on timeout.

Returns
Disable stopping criterion on timeout.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 131 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_max_time.

Referenced by gum::learning::genericBNLearner::disableMaxTime(), and gum::learning::GreedyHillClimbing::GreedyHillClimbing().

131 { _enabled_max_time = false; }
bool _enabled_max_time
If true, the timeout is enabled.
+ Here is the caller graph for this function:

◆ disableMinEpsilonRate()

INLINE void gum::ApproximationScheme::disableMinEpsilonRate ( )
virtualinherited

Disable stopping criterion on epsilon rate.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 79 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_min_rate_eps.

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::__mcInitApproximationScheme(), gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), gum::learning::genericBNLearner::disableMinEpsilonRate(), and gum::learning::GreedyHillClimbing::GreedyHillClimbing().

79  {
80  _enabled_min_rate_eps = false;
81  }
bool _enabled_min_rate_eps
If true, the minimal threshold for epsilon rate is enabled.
+ Here is the caller graph for this function:

◆ enableEpsilon()

INLINE void gum::ApproximationScheme::enableEpsilon ( )
virtualinherited

Enable stopping criterion on epsilon.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 57 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_eps.

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::__mcInitApproximationScheme(), and gum::learning::genericBNLearner::enableEpsilon().

57 { _enabled_eps = true; }
bool _enabled_eps
If true, the threshold convergence is enabled.
+ Here is the caller graph for this function:

◆ enableMaxIter()

INLINE void gum::ApproximationScheme::enableMaxIter ( )
virtualinherited

Enable stopping criterion on max iterations.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 108 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_max_iter.

Referenced by gum::learning::genericBNLearner::enableMaxIter().

108 { _enabled_max_iter = true; }
bool _enabled_max_iter
If true, the maximum iterations stopping criterion is enabled.
+ Here is the caller graph for this function:

◆ enableMaxTime()

INLINE void gum::ApproximationScheme::enableMaxTime ( )
virtualinherited

Enable stopping criterion on timeout.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 134 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_max_time.

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::CNMonteCarloSampling(), and gum::learning::genericBNLearner::enableMaxTime().

134 { _enabled_max_time = true; }
bool _enabled_max_time
If true, the timeout is enabled.
+ Here is the caller graph for this function:

◆ enableMinEpsilonRate()

INLINE void gum::ApproximationScheme::enableMinEpsilonRate ( )
virtualinherited

Enable stopping criterion on epsilon rate.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 84 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_min_rate_eps.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), and gum::learning::genericBNLearner::enableMinEpsilonRate().

84  {
85  _enabled_min_rate_eps = true;
86  }
bool _enabled_min_rate_eps
If true, the minimal threshold for epsilon rate is enabled.
+ Here is the caller graph for this function:

◆ epsilon()

INLINE double gum::ApproximationScheme::epsilon ( ) const
virtualinherited

Returns the value of epsilon.

Returns
Returns the value of epsilon.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 51 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_eps.

Referenced by gum::ImportanceSampling< GUM_SCALAR >::_onContextualize(), and gum::learning::genericBNLearner::epsilon().

51 { return _eps; }
double _eps
Threshold for convergence.
+ Here is the caller graph for this function:

◆ errorPQ()

template<typename GUM_SCALAR >
INLINE Size gum::BNdistance< GUM_SCALAR >::errorPQ ( )
inherited
Returns
the number of errors while processing divergence KL(P||Q)

Definition at line 105 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_errorPQ, and gum::BNdistance< GUM_SCALAR >::_process().

105  {
106  _process();
107  return _errorPQ;
108  }
+ Here is the call graph for this function:

◆ errorQP()

template<typename GUM_SCALAR >
INLINE Size gum::BNdistance< GUM_SCALAR >::errorQP ( )
inherited
Returns
the number of errors while processing divergence KL(Q||P)

Definition at line 111 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_errorQP, and gum::BNdistance< GUM_SCALAR >::_process().

111  {
112  _process();
113  return _errorQP;
114  }
+ Here is the call graph for this function:

◆ hellinger()

template<typename GUM_SCALAR >
INLINE double gum::BNdistance< GUM_SCALAR >::hellinger ( )
inherited
Returns
hellinger distance (
See also
http://en.wikipedia.org/wiki/Hellinger_distance)

Definition at line 87 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_hellinger, and gum::BNdistance< GUM_SCALAR >::_process().

87  {
88  _process();
89  return _hellinger;
90  }
GUM_SCALAR _hellinger
Definition: BNdistance.h:141
+ Here is the call graph for this function:

◆ history()

INLINE const std::vector< double > & gum::ApproximationScheme::history ( ) const
virtualinherited

Returns the scheme history.

Returns
Returns the scheme history.
Exceptions
OperationNotAllowedRaised if the scheme did not performed or if verbosity is set to false.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 173 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_history, GUM_ERROR, gum::ApproximationScheme::stateApproximationScheme(), gum::IApproximationSchemeConfiguration::Undefined, and gum::ApproximationScheme::verbosity().

Referenced by gum::learning::genericBNLearner::history().

173  {
175  GUM_ERROR(OperationNotAllowed,
176  "state of the approximation scheme is udefined");
177  }
178 
179  if (verbosity() == false) {
180  GUM_ERROR(OperationNotAllowed, "No history when verbosity=false");
181  }
182 
183  return _history;
184  }
std::vector< double > _history
The scheme history, used only if verbosity == true.
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
bool verbosity() const
Returns true if verbosity is enabled.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initApproximationScheme()

INLINE void gum::ApproximationScheme::initApproximationScheme ( )
inherited

Initialise the scheme.

Definition at line 187 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_current_epsilon, gum::ApproximationScheme::_current_rate, gum::ApproximationScheme::_current_state, gum::ApproximationScheme::_current_step, gum::ApproximationScheme::_history, gum::ApproximationScheme::_timer, gum::IApproximationSchemeConfiguration::Continue, and gum::Timer::reset().

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::__mcInitApproximationScheme(), gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), gum::SamplingInference< GUM_SCALAR >::_loopApproxInference(), gum::SamplingInference< GUM_SCALAR >::_onStateChanged(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::learning::GreedyHillClimbing::learnStructure(), and gum::learning::LocalSearchWithTabuList::learnStructure().

187  {
189  _current_step = 0;
191  _history.clear();
192  _timer.reset();
193  }
double _current_epsilon
Current epsilon.
void reset()
Reset the timer.
Definition: timer_inl.h:32
double _current_rate
Current rate.
Size _current_step
The current step.
std::vector< double > _history
The scheme history, used only if verbosity == true.
ApproximationSchemeSTATE _current_state
The current state.
+ 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
inlineinherited

Definition at line 70 of file gibbsOperator.h.

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

70 { return _atRandom; }

◆ isEnabledEpsilon()

INLINE bool gum::ApproximationScheme::isEnabledEpsilon ( ) const
virtualinherited

Returns true if stopping criterion on epsilon is enabled, false otherwise.

Returns
Returns true if stopping criterion on epsilon is enabled, false otherwise.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 61 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_eps.

Referenced by gum::learning::genericBNLearner::isEnabledEpsilon().

61  {
62  return _enabled_eps;
63  }
bool _enabled_eps
If true, the threshold convergence is enabled.
+ Here is the caller graph for this function:

◆ isEnabledMaxIter()

INLINE bool gum::ApproximationScheme::isEnabledMaxIter ( ) const
virtualinherited

Returns true if stopping criterion on max iterations is enabled, false otherwise.

Returns
Returns true if stopping criterion on max iterations is enabled, false otherwise.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 112 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_max_iter.

Referenced by gum::learning::genericBNLearner::isEnabledMaxIter().

112  {
113  return _enabled_max_iter;
114  }
bool _enabled_max_iter
If true, the maximum iterations stopping criterion is enabled.
+ Here is the caller graph for this function:

◆ isEnabledMaxTime()

INLINE bool gum::ApproximationScheme::isEnabledMaxTime ( ) const
virtualinherited

Returns true if stopping criterion on timeout is enabled, false otherwise.

Returns
Returns true if stopping criterion on timeout is enabled, false otherwise.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 138 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_max_time.

Referenced by gum::learning::genericBNLearner::isEnabledMaxTime().

138  {
139  return _enabled_max_time;
140  }
bool _enabled_max_time
If true, the timeout is enabled.
+ Here is the caller graph for this function:

◆ isEnabledMinEpsilonRate()

INLINE bool gum::ApproximationScheme::isEnabledMinEpsilonRate ( ) const
virtualinherited

Returns true if stopping criterion on epsilon rate is enabled, false otherwise.

Returns
Returns true if stopping criterion on epsilon rate is enabled, false otherwise.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 90 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_min_rate_eps.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), and gum::learning::genericBNLearner::isEnabledMinEpsilonRate().

90  {
91  return _enabled_min_rate_eps;
92  }
bool _enabled_min_rate_eps
If true, the minimal threshold for epsilon rate is enabled.
+ Here is the caller graph for this function:

◆ jsd()

template<typename GUM_SCALAR >
INLINE double gum::BNdistance< GUM_SCALAR >::jsd ( )
inherited
Returns
Jensen-Shannon divergence(
See also
https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence)

Definition at line 99 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_jsd, and gum::BNdistance< GUM_SCALAR >::_process().

99  {
100  _process();
101  return _jsd;
102  }
GUM_SCALAR _jsd
Definition: BNdistance.h:143
+ Here is the call graph for this function:

◆ klPQ()

template<typename GUM_SCALAR >
INLINE double gum::BNdistance< GUM_SCALAR >::klPQ ( )
inherited
Returns
divergence KL(P||Q)

Definition at line 75 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_klPQ, and gum::BNdistance< GUM_SCALAR >::_process().

75  {
76  _process();
77  return _klPQ;
78  }
GUM_SCALAR _klPQ
Definition: BNdistance.h:139
+ Here is the call graph for this function:

◆ klQP()

template<typename GUM_SCALAR >
INLINE double gum::BNdistance< GUM_SCALAR >::klQP ( )
inherited
Returns
divergence KL(Q||P)

Definition at line 81 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_klQP, and gum::BNdistance< GUM_SCALAR >::_process().

81  {
82  _process();
83  return _klQP;
84  }
GUM_SCALAR _klQP
Definition: BNdistance.h:140
+ Here is the call graph for this function:

◆ maxIter()

INLINE Size gum::ApproximationScheme::maxIter ( ) const
virtualinherited

Returns the criterion on number of iterations.

Returns
Returns the criterion on number of iterations.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 102 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_max_iter.

Referenced by gum::learning::genericBNLearner::maxIter().

102 { return _max_iter; }
Size _max_iter
The maximum iterations.
+ Here is the caller graph for this function:

◆ maxTime()

INLINE double gum::ApproximationScheme::maxTime ( ) const
virtualinherited

Returns the timeout (in seconds).

Returns
Returns the timeout (in seconds).

Implements gum::IApproximationSchemeConfiguration.

Definition at line 125 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_max_time.

Referenced by gum::learning::genericBNLearner::maxTime().

125 { return _max_time; }
double _max_time
The timeout.
+ Here is the caller graph for this function:

◆ messageApproximationScheme()

INLINE std::string gum::IApproximationSchemeConfiguration::messageApproximationScheme ( ) const
inherited

Returns the approximation scheme message.

Returns
Returns the approximation scheme message.

Definition at line 40 of file IApproximationSchemeConfiguration_inl.h.

References gum::IApproximationSchemeConfiguration::Continue, gum::IApproximationSchemeConfiguration::Epsilon, gum::IApproximationSchemeConfiguration::epsilon(), gum::IApproximationSchemeConfiguration::Limit, gum::IApproximationSchemeConfiguration::maxIter(), gum::IApproximationSchemeConfiguration::maxTime(), gum::IApproximationSchemeConfiguration::minEpsilonRate(), gum::IApproximationSchemeConfiguration::Rate, gum::IApproximationSchemeConfiguration::stateApproximationScheme(), gum::IApproximationSchemeConfiguration::Stopped, gum::IApproximationSchemeConfiguration::TimeLimit, and gum::IApproximationSchemeConfiguration::Undefined.

Referenced by gum::ApproximationScheme::_stopScheme(), gum::ApproximationScheme::continueApproximationScheme(), and gum::credal::InferenceEngine< GUM_SCALAR >::getApproximationSchemeMsg().

40  {
41  std::stringstream s;
42 
43  switch (stateApproximationScheme()) {
44  case ApproximationSchemeSTATE::Continue: s << "in progress"; break;
45 
47  s << "stopped with epsilon=" << epsilon();
48  break;
49 
51  s << "stopped with rate=" << minEpsilonRate();
52  break;
53 
55  s << "stopped with max iteration=" << maxIter();
56  break;
57 
59  s << "stopped with timeout=" << maxTime();
60  break;
61 
62  case ApproximationSchemeSTATE::Stopped: s << "stopped on request"; break;
63 
64  case ApproximationSchemeSTATE::Undefined: s << "undefined state"; break;
65  };
66 
67  return s.str();
68  }
virtual double epsilon() const =0
Returns the value of epsilon.
virtual ApproximationSchemeSTATE stateApproximationScheme() const =0
Returns the approximation scheme state.
virtual double maxTime() const =0
Returns the timeout (in seconds).
virtual Size maxIter() const =0
Returns the criterion on number of iterations.
virtual double minEpsilonRate() const =0
Returns the value of the minimal epsilon rate.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ minEpsilonRate()

INLINE double gum::ApproximationScheme::minEpsilonRate ( ) const
virtualinherited

Returns the value of the minimal epsilon rate.

Returns
Returns the value of the minimal epsilon rate.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 74 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_min_rate_eps.

Referenced by gum::learning::genericBNLearner::minEpsilonRate().

74  {
75  return _min_rate_eps;
76  }
double _min_rate_eps
Threshold for the epsilon rate.
+ Here is the caller graph for this function:

◆ monteCarloSample()

template<typename GUM_SCALAR >
Instantiation gum::GibbsOperator< GUM_SCALAR >::monteCarloSample ( )
inherited

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
inlineinherited

Getters and setters.

Definition at line 66 of file gibbsOperator.h.

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

66 { return _nbr; }

◆ nbrIterations()

INLINE Size gum::ApproximationScheme::nbrIterations ( ) const
virtualinherited

Returns the number of iterations.

Returns
Returns the number of iterations.
Exceptions
OperationNotAllowedRaised if the scheme did not perform.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 163 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_current_step, GUM_ERROR, gum::ApproximationScheme::stateApproximationScheme(), and gum::IApproximationSchemeConfiguration::Undefined.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), and gum::learning::genericBNLearner::nbrIterations().

163  {
165  GUM_ERROR(OperationNotAllowed,
166  "state of the approximation scheme is undefined");
167  }
168 
169  return _current_step;
170  }
Size _current_step
The current step.
ApproximationSchemeSTATE stateApproximationScheme() const
Returns the approximation scheme state.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nextSample()

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

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:

◆ p()

template<typename GUM_SCALAR >
INLINE const IBayesNet< GUM_SCALAR > & gum::BNdistance< GUM_SCALAR >::p ( ) const
inherited
Returns
p

Definition at line 117 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_p.

117  {
118  return _p;
119  }
const IBayesNet< GUM_SCALAR > & _p
Definition: BNdistance.h:136

◆ periodSize()

INLINE Size gum::ApproximationScheme::periodSize ( ) const
virtualinherited

Returns the period size.

Returns
Returns the period size.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 149 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_period_size.

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::makeInference(), and gum::learning::genericBNLearner::periodSize().

149 { return _period_size; }
Size _period_size
Checking criteria frequency.
+ Here is the caller graph for this function:

◆ q()

template<typename GUM_SCALAR >
INLINE const IBayesNet< GUM_SCALAR > & gum::BNdistance< GUM_SCALAR >::q ( ) const
inherited
Returns
q

Definition at line 122 of file BNdistance_tpl.h.

References gum::BNdistance< GUM_SCALAR >::_q.

122  {
123  return _q;
124  }
const IBayesNet< GUM_SCALAR > & _q
Definition: BNdistance.h:137

◆ remainingBurnIn()

INLINE Size gum::ApproximationScheme::remainingBurnIn ( )
inherited

Returns the remaining burn in.

Returns
Returns the remaining burn in.

Definition at line 210 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_burn_in, and gum::ApproximationScheme::_current_step.

210  {
211  if (_burn_in > _current_step) {
212  return _burn_in - _current_step;
213  } else {
214  return 0;
215  }
216  }
Size _burn_in
Number of iterations before checking stopping criteria.
Size _current_step
The current step.

◆ setBurnIn()

template<typename GUM_SCALAR >
void gum::GibbsBNdistance< GUM_SCALAR >::setBurnIn ( Size  b)

Number of burn in for one iteration.

Parameters
bThe number of burn in.
Exceptions
OutOfLowerBoundRaised if b < 1.

Definition at line 184 of file GibbsKL_tpl.h.

References gum::ApproximationScheme::_burn_in.

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

184  {
185  this->_burn_in = b;
186  }
Size _burn_in
Number of iterations before checking stopping criteria.
+ Here is the caller graph for this function:

◆ setDrawnAtRandom()

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

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:

◆ setEpsilon()

INLINE void gum::ApproximationScheme::setEpsilon ( double  eps)
virtualinherited

Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.

If the criterion was disabled it will be enabled.

Parameters
epsThe new epsilon value.
Exceptions
OutOfLowerBoundRaised if eps < 0.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 43 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_eps, gum::ApproximationScheme::_eps, and GUM_ERROR.

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::__mcInitApproximationScheme(), gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), gum::GibbsSampling< GUM_SCALAR >::GibbsSampling(), gum::learning::GreedyHillClimbing::GreedyHillClimbing(), gum::SamplingInference< GUM_SCALAR >::SamplingInference(), and gum::learning::genericBNLearner::setEpsilon().

43  {
44  if (eps < 0.) { GUM_ERROR(OutOfLowerBound, "eps should be >=0"); }
45 
46  _eps = eps;
47  _enabled_eps = true;
48  }
bool _enabled_eps
If true, the threshold convergence is enabled.
double _eps
Threshold for convergence.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ setMaxIter()

INLINE void gum::ApproximationScheme::setMaxIter ( Size  max)
virtualinherited

Stopping criterion on number of iterations.

If the criterion was disabled it will be enabled.

Parameters
maxThe maximum number of iterations.
Exceptions
OutOfLowerBoundRaised if max <= 1.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 95 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_max_iter, gum::ApproximationScheme::_max_iter, and GUM_ERROR.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), gum::SamplingInference< GUM_SCALAR >::SamplingInference(), and gum::learning::genericBNLearner::setMaxIter().

95  {
96  if (max < 1) { GUM_ERROR(OutOfLowerBound, "max should be >=1"); }
97  _max_iter = max;
98  _enabled_max_iter = true;
99  }
bool _enabled_max_iter
If true, the maximum iterations stopping criterion is enabled.
Size _max_iter
The maximum iterations.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ setMaxTime()

INLINE void gum::ApproximationScheme::setMaxTime ( double  timeout)
virtualinherited

Stopping criterion on timeout.

If the criterion was disabled it will be enabled.

Parameters
timeoutThe timeout value in seconds.
Exceptions
OutOfLowerBoundRaised if timeout <= 0.0.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 118 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_max_time, gum::ApproximationScheme::_max_time, and GUM_ERROR.

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::CNMonteCarloSampling(), gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), gum::SamplingInference< GUM_SCALAR >::SamplingInference(), and gum::learning::genericBNLearner::setMaxTime().

118  {
119  if (timeout <= 0.) { GUM_ERROR(OutOfLowerBound, "timeout should be >0."); }
120  _max_time = timeout;
121  _enabled_max_time = true;
122  }
bool _enabled_max_time
If true, the timeout is enabled.
double _max_time
The timeout.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ setMinEpsilonRate()

INLINE void gum::ApproximationScheme::setMinEpsilonRate ( double  rate)
virtualinherited

Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).

If the criterion was disabled it will be enabled

Parameters
rateThe minimal epsilon rate.
Exceptions
OutOfLowerBoundif rate<0

Implements gum::IApproximationSchemeConfiguration.

Definition at line 66 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_enabled_min_rate_eps, gum::ApproximationScheme::_min_rate_eps, and GUM_ERROR.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), gum::GibbsSampling< GUM_SCALAR >::GibbsSampling(), gum::SamplingInference< GUM_SCALAR >::SamplingInference(), and gum::learning::genericBNLearner::setMinEpsilonRate().

66  {
67  if (rate < 0) { GUM_ERROR(OutOfLowerBound, "rate should be >=0"); }
68 
69  _min_rate_eps = rate;
70  _enabled_min_rate_eps = true;
71  }
bool _enabled_min_rate_eps
If true, the minimal threshold for epsilon rate is enabled.
double _min_rate_eps
Threshold for the epsilon rate.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ setNbrDrawnVar()

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

Definition at line 68 of file gibbsOperator.h.

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

68 { _nbr = nbr; }

◆ setPeriodSize()

INLINE void gum::ApproximationScheme::setPeriodSize ( Size  p)
virtualinherited

How many samples between two stopping is enable.

Parameters
pThe new period value.
Exceptions
OutOfLowerBoundRaised if p < 1.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 143 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_period_size, and GUM_ERROR.

Referenced by gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::CNMonteCarloSampling(), gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), gum::SamplingInference< GUM_SCALAR >::SamplingInference(), and gum::learning::genericBNLearner::setPeriodSize().

143  {
144  if (p < 1) { GUM_ERROR(OutOfLowerBound, "p should be >=1"); }
145 
146  _period_size = p;
147  }
Size _period_size
Checking criteria frequency.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ setVerbosity()

INLINE void gum::ApproximationScheme::setVerbosity ( bool  v)
virtualinherited

Set the verbosity on (true) or off (false).

Parameters
vIf true, then verbosity is turned on.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 152 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_verbosity.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::GibbsBNdistance(), gum::SamplingInference< GUM_SCALAR >::SamplingInference(), and gum::learning::genericBNLearner::setVerbosity().

152 { _verbosity = v; }
bool _verbosity
If true, verbosity is enabled.
+ Here is the caller graph for this function:

◆ startOfPeriod()

INLINE bool gum::ApproximationScheme::startOfPeriod ( )
inherited

Returns true if we are at the beginning of a period (compute error is mandatory).

Returns
Returns true if we are at the beginning of a period (compute error is mandatory).

Definition at line 197 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_burn_in, gum::ApproximationScheme::_current_step, and gum::ApproximationScheme::_period_size.

Referenced by gum::ApproximationScheme::continueApproximationScheme().

197  {
198  if (_current_step < _burn_in) { return false; }
199 
200  if (_period_size == 1) { return true; }
201 
202  return ((_current_step - _burn_in) % _period_size == 0);
203  }
Size _burn_in
Number of iterations before checking stopping criteria.
Size _current_step
The current step.
Size _period_size
Checking criteria frequency.
+ Here is the caller graph for this function:

◆ stateApproximationScheme()

INLINE IApproximationSchemeConfiguration::ApproximationSchemeSTATE gum::ApproximationScheme::stateApproximationScheme ( ) const
virtualinherited

Returns the approximation scheme state.

Returns
Returns the approximation scheme state.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 158 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_current_state.

Referenced by gum::ApproximationScheme::continueApproximationScheme(), gum::ApproximationScheme::history(), gum::ApproximationScheme::nbrIterations(), and gum::learning::genericBNLearner::stateApproximationScheme().

158  {
159  return _current_state;
160  }
ApproximationSchemeSTATE _current_state
The current state.
+ Here is the caller graph for this function:

◆ stopApproximationScheme()

INLINE void gum::ApproximationScheme::stopApproximationScheme ( )
inherited

Stop the approximation scheme.

Definition at line 219 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_current_state, gum::ApproximationScheme::_stopScheme(), gum::IApproximationSchemeConfiguration::Continue, and gum::IApproximationSchemeConfiguration::Stopped.

Referenced by gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::learning::GreedyHillClimbing::learnStructure(), and gum::learning::LocalSearchWithTabuList::learnStructure().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ updateApproximationScheme()

INLINE void gum::ApproximationScheme::updateApproximationScheme ( unsigned int  incr = 1)
inherited

Update the scheme w.r.t the new error and increment steps.

Parameters
incrThe new increment steps.

Definition at line 206 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_current_step.

Referenced by gum::GibbsBNdistance< GUM_SCALAR >::_computeKL(), gum::SamplingInference< GUM_SCALAR >::_loopApproxInference(), gum::learning::DAG2BNLearner< ALLOC >::createBN(), gum::learning::GreedyHillClimbing::learnStructure(), gum::learning::LocalSearchWithTabuList::learnStructure(), and gum::credal::CNMonteCarloSampling< GUM_SCALAR, BNInferenceEngine >::makeInference().

206  {
207  _current_step += incr;
208  }
Size _current_step
The current step.
+ Here is the caller graph for this function:

◆ verbosity()

INLINE bool gum::ApproximationScheme::verbosity ( ) const
virtualinherited

Returns true if verbosity is enabled.

Returns
Returns true if verbosity is enabled.

Implements gum::IApproximationSchemeConfiguration.

Definition at line 154 of file approximationScheme_inl.h.

References gum::ApproximationScheme::_verbosity.

Referenced by gum::ApproximationScheme::continueApproximationScheme(), gum::ApproximationScheme::history(), and gum::learning::genericBNLearner::verbosity().

154 { return _verbosity; }
bool _verbosity
If true, verbosity is enabled.
+ Here is the caller graph for this function:

Member Data Documentation

◆ _atRandom

template<typename GUM_SCALAR >
bool gum::GibbsOperator< GUM_SCALAR >::_atRandom
protectedinherited

◆ _bhattacharya

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_bhattacharya
protectedinherited

◆ _burn_in

◆ _counting

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

number of samples drawn

Definition at line 82 of file gibbsOperator.h.

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

◆ _current_epsilon

double gum::ApproximationScheme::_current_epsilon
protectedinherited

◆ _current_rate

double gum::ApproximationScheme::_current_rate
protectedinherited

◆ _current_state

◆ _current_step

◆ _enabled_eps

◆ _enabled_max_iter

bool gum::ApproximationScheme::_enabled_max_iter
protectedinherited

◆ _enabled_max_time

◆ _enabled_min_rate_eps

bool gum::ApproximationScheme::_enabled_min_rate_eps
protectedinherited

◆ _eps

double gum::ApproximationScheme::_eps
protectedinherited

◆ _errorPQ

template<typename GUM_SCALAR>
Size gum::BNdistance< GUM_SCALAR >::_errorPQ
protectedinherited

◆ _errorQP

template<typename GUM_SCALAR>
Size gum::BNdistance< GUM_SCALAR >::_errorQP
protectedinherited

◆ _hardEv

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

◆ _hellinger

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_hellinger
protectedinherited

◆ _history

std::vector< double > gum::ApproximationScheme::_history
protectedinherited

◆ _jsd

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_jsd
protectedinherited

◆ _klPQ

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_klPQ
protectedinherited

◆ _klQP

template<typename GUM_SCALAR>
GUM_SCALAR gum::BNdistance< GUM_SCALAR >::_klQP
protectedinherited

◆ _last_epsilon

double gum::ApproximationScheme::_last_epsilon
protectedinherited

Last epsilon value.

Definition at line 372 of file approximationScheme.h.

Referenced by gum::ApproximationScheme::continueApproximationScheme().

◆ _max_iter

Size gum::ApproximationScheme::_max_iter
protectedinherited

◆ _max_time

double gum::ApproximationScheme::_max_time
protectedinherited

◆ _min_rate_eps

double gum::ApproximationScheme::_min_rate_eps
protectedinherited

◆ _nbr

◆ _p

◆ _period_size

Size gum::ApproximationScheme::_period_size
protectedinherited

◆ _q

◆ _sampling_bn

◆ _samplingNodes

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

◆ _timer

◆ _verbosity

bool gum::ApproximationScheme::_verbosity
protectedinherited

If true, verbosity is enabled.

Definition at line 420 of file approximationScheme.h.

Referenced by gum::ApproximationScheme::setVerbosity(), and gum::ApproximationScheme::verbosity().

◆ onProgress

◆ onStop

Signaler1< std::string > gum::IApproximationSchemeConfiguration::onStop
inherited

Criteria messageApproximationScheme.

Definition at line 62 of file IApproximationSchemeConfiguration.h.

Referenced by gum::ApproximationScheme::_stopScheme(), and gum::learning::genericBNLearner::distributeStop().


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