32 template <
typename GUM_SCALAR >
37 if (this->
__bn ==
nullptr) {
45 template <
typename GUM_SCALAR >
52 template <
typename GUM_SCALAR >
66 template <
typename GUM_SCALAR >
69 if (this->
__bn ==
nullptr)
71 "No Bayes net has been assigned to the " 72 "inference algorithm");
74 const auto& dag = this->
__bn->dag();
75 for (
const auto var : vars) {
76 if (!dag.exists(var)) {
86 template <
typename GUM_SCALAR >
93 template <
typename GUM_SCALAR >
106 template <
typename GUM_SCALAR >
114 template <
typename GUM_SCALAR >
118 if (this->
__bn ==
nullptr)
120 "No Bayes net has been assigned to the " 121 "inference algorithm");
123 const auto& dag = this->
__bn->dag();
124 for (
const auto node : joint_target) {
125 if (!dag.exists(node)) {
127 "at least one one in " << joint_target
128 <<
" does not belong to the bn");
137 if (target.isSupersetOf(joint_target))
return;
142 for (
auto iter = __joint_targets.beginSafe();
143 iter != __joint_targets.endSafe();
149 __joint_targets.insert(joint_target);
157 template <
typename GUM_SCALAR >
161 if (this->
__bn ==
nullptr)
163 "No Bayes net has been assigned to the " 164 "inference algorithm");
166 const auto& dag = this->
__bn->dag();
167 for (
const auto node : joint_target) {
168 if (!dag.exists(node)) {
170 "at least one one in " << joint_target
171 <<
" does not belong to the bn");
188 template <
typename GUM_SCALAR >
195 template <
typename GUM_SCALAR >
207 template <
typename GUM_SCALAR >
212 bool found_exact_target =
false;
216 found_exact_target =
true;
228 " no joint target containing " << nodes <<
"could be found");
233 if (found_exact_target)
241 template <
typename GUM_SCALAR >
251 template <
typename GUM_SCALAR >
268 template <
typename GUM_SCALAR >
284 if (pXY !=
nullptr) {
delete pXY; }
289 auto res = (GUM_SCALAR)0;
292 GUM_SCALAR vXY = (*pXY)[i];
293 GUM_SCALAR vX = pX[i];
294 GUM_SCALAR vY = pY[i];
296 if (vXY > (GUM_SCALAR)0) {
297 if (vX == (GUM_SCALAR)0 || vY == (GUM_SCALAR)0) {
299 "Mutual Information (X,Y) with P(X)=0 or P(Y)=0 " 303 res += vXY * (log2(vXY) - log2(vX) - log2(vY));
319 template <
typename GUM_SCALAR >
321 return this->
H(X) + this->
H(Y) - 2 *
I(X, Y);
325 template <
typename GUM_SCALAR >
329 if (!(evs * targets).empty()) {
331 "Targets (" << targets <<
") can not intersect evs (" << evs
334 auto condset = this->
BN().minimalCondSet(targets, evs);
341 for (
const auto& target : targets) {
342 res.
add(this->
BN().variable(target));
343 iTarget.
add(this->
BN().variable(target));
347 for (
const auto& n : condset) {
348 res.
add(this->
BN().variable(n));
355 for (
const auto& n : condset)
368 template <
typename GUM_SCALAR >
371 const std::vector< std::string >&
targets,
372 const std::vector< std::string >& evs) {
373 const auto& bn = this->
BN();
376 for (
const auto& targetname : targets) {
377 targetsId.
insert(bn.idFromName(targetname));
381 for (
const auto& evname : evs) {
382 evsId.
insert(bn.idFromName(evname));
389 template <
typename GUM_SCALAR >
392 const auto& bn = this->
BN();
396 "jointMutualInformation needs at least 2 variables (targets=" 408 for (
const auto nod : targets) {
411 caracteristic.
add(*pv);
417 const GUM_SCALAR start = (siz % 2 == 0) ? GUM_SCALAR(-1.0) : GUM_SCALAR(1.0);
419 GUM_SCALAR res = GUM_SCALAR(0.0);
422 for (caracteristic.
inc(); !caracteristic.
end(); caracteristic.
inc()) {
425 for (
Idx i = 0; i < caracteristic.
nbrDim(); i++) {
426 if (caracteristic.
val(i) == 1) {
431 res += sign * po.margSumIn(sov).entropy();
434 for (
Idx i = 0; i < caracteristic.
nbrDim(); i++) {
441 template <
typename GUM_SCALAR >
443 const std::vector< std::string >&
targets) {
444 const auto& bn = this->
BN();
447 for (
const auto& targetname : targets) {
448 targetsId.
insert(bn.idFromName(targetname));
void __setBayesNetDuringConstruction(const IBayesNet< GUM_SCALAR > *bn)
assigns a BN during the inference engine construction
virtual void eraseAllMarginalTargets() final
Clear all the previously defined marginal targets.
virtual void eraseAllTargets()
Clear all previously defined targets (marginal and joint targets)
aGrUM's Potential is a multi-dimensional array with tensor operators.
virtual void addJointTarget(const NodeSet &joint_target) final
Add a set of nodes as a new joint target. As a collateral effect, every node is added as a marginal t...
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
virtual ~JointTargetedInference()
destructor
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn)
fired after a new Bayes net has been assigned to the engine
JointTargetedInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
GUM_SCALAR I(NodeId X, NodeId Y)
Mutual information between X and Y.
virtual GUM_SCALAR H(NodeId X) final
Entropy Compute Shanon's entropy of a node given the observation.
virtual void _onAllJointTargetsErased()=0
fired before a all the joint targets are removed
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
This file contains the abstract inference class definition for computing (incrementally) joint poster...
virtual bool isJointTarget(const NodeSet &vars) const final
return true if target is a joint target.
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
bool isSubsetOf(const Set< Key, OtherAlloc > &s) const
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual bool isTarget(NodeId node) const final
return true if variable is a (marginal) target
<agrum/BN/inference/marginalTargetedInference.h>
virtual Potential< GUM_SCALAR > * _unnormalizedJointPosterior(const NodeSet &set)=0
returns a fresh unnormalized joint posterior of a given set of variables
Class representing the minimal interface for Bayesian Network.
gum is the global namespace for all aGrUM entities
virtual void _onJointTargetAdded(const NodeSet &set)=0
fired after a new joint target is inserted
virtual void eraseJointTarget(const NodeSet &joint_target) final
removes an existing joint target
Idx val(Idx i) const
Returns the current value of the variable at position i.
Representation of a setA Set is a structure that contains arbitrary elements.
virtual void makeInference() final
perform the heavy computations needed to compute the targets' posteriors
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
<agrum/BN/inference/BayesNetInference.h>
void setFirstIn(const Instantiation &i)
Assign the first values in the Instantiation for the variables in i.
void inc()
Operator increment.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual bool isDone() const noexcept final
returns whether the inference object is in a done state
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual void eraseAllJointTargets() final
Clear all previously defined joint targets.
<agrum/BN/inference/jointTargetedInference.h>
virtual const Set< NodeSet > & jointTargets() const noexcept final
returns the list of joint targets
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
virtual const Potential< GUM_SCALAR > & jointPosterior(const NodeSet &nodes) final
Compute the joint posterior of a set of nodes.
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
Potential< GUM_SCALAR > evidenceJointImpact(const NodeSet &targets, const NodeSet &evs)
Create a gum::Potential for P(joint targets|evs) (for all instanciation of targets and evs) ...
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn)
fired after a new Bayes net has been assigned to the engine
Defines a discrete random variable over an integer interval.
void incIn(const Instantiation &i)
Operator increment for the variables in i.
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
Class for assigning/browsing values to tuples of discrete variables.
virtual const Potential< GUM_SCALAR > & _jointPosterior(const NodeSet &set)=0
asks derived classes for the joint posterior of a declared target set
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
virtual const Potential< GUM_SCALAR > & posterior(NodeId node) final
Computes and returns the posterior of a node.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
void setFirst()
Assign the first values to the tuple of the Instantiation.
Header of gumRangeVariable.
virtual Size nbrJointTargets() const noexcept final
returns the number of joint targets
Size Idx
Type for indexes.
virtual const NodeSet & targets() const noexcept final
returns the list of marginal targets
GUM_SCALAR jointMutualInformation(const NodeSet &targets)
Mutual information between targets.
virtual void eraseAllTargets()
Clear all previously defined targets.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
void clear()
Removes all the elements, if any, from the set.
GUM_SCALAR VI(NodeId X, NodeId Y)
Variation of information between X and Y.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size size() const noexcept
Returns the number of elements in the set.
virtual void _onJointTargetErased(const NodeSet &set)=0
fired before a joint target is removed
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Set< NodeSet > __joint_targets
the set of joint targets
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
Size NodeId
Type for node ids.
void insert(const Key &k)
Inserts a new element into the set.
#define GUM_ERROR(type, msg)
bool end() const
Returns true if the Instantiation reached the end.
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.