61 template < GUM_Numeric GUM_SCALAR >
70 template < GUM_Numeric GUM_SCALAR >
78 template < GUM_Numeric GUM_SCALAR >
83 template < GUM_Numeric GUM_SCALAR >
87 for (
const auto nod: topoOrder) {
88 I.
add(
p_.variable(nod));
91 I.
chgVal(
p_.variable(nod),
p_.cpt(nod).extract(Itop).draw());
96 template < GUM_Numeric GUM_SCALAR >
98 auto Iq =
q_.completeInstantiation();
99 const auto topoOrder =
p_.topologicalOrder();
105 auto Ip =
p_.completeInstantiation();
106 for (
Idx ite = 0; ite < Ip.nbrDim(); ++ite) {
107 map.
insert(&Ip.variable(ite), &
q_.variableFromName(Ip.variable(ite).name()));
113 GUM_SCALAR delta, ratio, error;
114 delta = ratio = error = (GUM_SCALAR)-1;
115 GUM_SCALAR oldPQ = 0.0;
116 GUM_SCALAR pp, pq, pmid;
123 Iq.setValsFrom(map, I);
125 pp =
p_.jointProbability(I);
126 pq =
q_.jointProbability(Iq);
127 pmid = (pp + pq) / 2.0;
129 if (pp != (GUM_SCALAR)0.0) {
130 hellinger_ += std::pow(std::sqrt(pp) - std::sqrt(pq), 2) / pp;
132 if (pq != (GUM_SCALAR)0.0) {
136 delta = (GUM_SCALAR)std::log2(ratio);
140 jsd_ -= std::log2(pp / pmid) + ratio * std::log2(pq / pmid);
146 if (pq != (GUM_SCALAR)0.0) {
147 if (pp != (GUM_SCALAR)0.0) {
150 klQP_ += (GUM_SCALAR)(-delta * ratio);
159 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...
This file contains general scheme for iteratively convergent algorithms.
ApproximationScheme()
Build a scheme whose parameters are read from the configuration.
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
void setFromConfiguration_(const std::string §ion, bool withMaxTime, bool withVerbosity=true)
Set the parameters of the scheme from a section of gum::Configuration.
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.
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.
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