59#define MCBNDISTANCE_DEFAULT_MAXITER 10000000
60#define MCBNDISTANCE_DEFAULT_EPSILON 1e-10
61#define MCBNDISTANCE_DEFAULT_MIN_EPSILON_RATE 1e-10
62#define MCBNDISTANCE_DEFAULT_PERIOD_SIZE 200
63#define MCBNDISTANCE_DEFAULT_VERBOSITY false
64#define MCBNDISTANCE_DEFAULT_TIMEOUT 6000
68 template < GUM_Numeric GUM_SCALAR >
82 template < GUM_Numeric GUM_SCALAR >
95 template < GUM_Numeric GUM_SCALAR >
100 template < GUM_Numeric GUM_SCALAR >
104 for (
const auto nod: topoOrder) {
105 I.
add(
p_.variable(nod));
108 I.
chgVal(
p_.variable(nod),
p_.cpt(nod).extract(Itop).draw());
113 template < GUM_Numeric GUM_SCALAR >
115 auto Iq =
q_.completeInstantiation();
116 const auto topoOrder =
p_.topologicalOrder();
122 auto Ip =
p_.completeInstantiation();
123 for (
Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
124 map.
insert(&Ip.variable(ite), &
q_.variableFromName(Ip.variable(ite).name()));
130 GUM_SCALAR delta, ratio, error;
131 delta = ratio = error = (GUM_SCALAR)-1;
132 GUM_SCALAR oldPQ = 0.0;
133 GUM_SCALAR pp, pq, pmid;
140 Iq.setValsFrom(map, I);
142 pp =
p_.jointProbability(I);
143 pq =
q_.jointProbability(Iq);
144 pmid = (pp + pq) / 2.0;
146 if (pp != (GUM_SCALAR)0.0) {
147 hellinger_ += std::pow(std::sqrt(pp) - std::sqrt(pq), 2) / pp;
149 if (pq != (GUM_SCALAR)0.0) {
153 delta = (GUM_SCALAR)std::log2(ratio);
157 jsd_ -= std::log2(pp / pmid) + ratio * std::log2(pq / pmid);
163 if (pq != (GUM_SCALAR)0.0) {
164 if (pp != (GUM_SCALAR)0.0) {
167 klQP_ += (GUM_SCALAR)(-delta * ratio);
176 error = (GUM_SCALAR)std::abs(delta - oldPQ);
Class representing the minimal interface for Bayesian network with no numerical data.
algorithm for approximated computation KL divergence between BNs using independent Monte Carlo sampli...
#define MCBNDISTANCE_DEFAULT_MAXITER
#define MCBNDISTANCE_DEFAULT_TIMEOUT
#define MCBNDISTANCE_DEFAULT_VERBOSITY
#define MCBNDISTANCE_DEFAULT_PERIOD_SIZE
#define MCBNDISTANCE_DEFAULT_MIN_EPSILON_RATE
#define MCBNDISTANCE_DEFAULT_EPSILON
This file contains general scheme for iteratively convergent algorithms.
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
void setMaxIter(Size max) override
Stopping criterion on number of iterations.
void setMaxTime(double timeout) override
Stopping criterion on timeout.
void setMinEpsilonRate(double rate) override
Given that we approximate f(t), stopping criterion on d/dt(|f(t+1)-f(t)|).
void setPeriodSize(Size p) override
How many samples between two stopping is enable.
bool isEnabledMinEpsilonRate() const override
Returns true if stopping criterion on epsilon rate is enabled, false otherwise.
void disableMinEpsilonRate() override
Disable stopping criterion on epsilon rate.
ApproximationScheme(bool verbosity=false)
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
void initApproximationScheme()
Initialise the scheme.
Size nbrIterations() const override
Returns the number of iterations.
void enableMinEpsilonRate() override
Enable stopping criterion on epsilon rate.
void setVerbosity(bool v) override
Set the verbosity on (true) or off (false).
void setEpsilon(double eps) override
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
BNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
const IBayesNet< GUM_SCALAR > & q_
const IBayesNet< GUM_SCALAR > & p_
The class for generic Hash Tables.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Class representing the minimal interface for Bayesian network with no numerical data.
Class for assigning/browsing values to tuples of discrete variables.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
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.
Instantiation drawSample_(const Sequence< NodeId > &topoOrder) const
draws an instantiation of p_'s variables, independently, following topoOrder (no evidence to handle i...
MCBNDistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
~MCBNDistance() override
destructor
Size Idx
Type for indexes.
Class hash tables iterators.
gum is the global namespace for all aGrUM entities