aGrUM  0.16.0
gum::JointTargetedInference< GUM_SCALAR > Class Template Referenceabstract

<agrum/BN/inference/jointTargetedInference.h> More...

#include <BayesNetInference.h>

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

Public Member Functions

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) More...
 
Potential< GUM_SCALAR > evidenceJointImpact (const std::vector< std::string > &targets, const std::vector< std::string > &evs)
 Create a gum::Potential for P(joint targets|evs) (for all instanciation of targets and evs) More...
 
Potential< GUM_SCALAR > evidenceImpact (NodeId target, const NodeSet &evs)
 Create a gum::Potential for P(target|evs) (for all instanciation of target and evs) More...
 
Potential< GUM_SCALAR > evidenceImpact (const std::string &target, const std::vector< std::string > &evs)
 Create a gum::Potential for P(target|evs) (for all instanciation of target and evs) More...
 
Constructors / Destructors
 JointTargetedInference (const IBayesNet< GUM_SCALAR > *bn)
 default constructor More...
 
virtual ~JointTargetedInference ()
 destructor More...
 
Probability computations
virtual const Potential< GUM_SCALAR > & jointPosterior (const NodeSet &nodes) final
 Compute the joint posterior of a set of nodes. More...
 
virtual const Potential< GUM_SCALAR > & posterior (NodeId node) final
 Computes and returns the posterior of a node. More...
 
virtual const Potential< GUM_SCALAR > & posterior (const std::string &nodeName) final
 Computes and returns the posterior of a node. More...
 
Targets
virtual void eraseAllTargets ()
 Clear all previously defined targets (marginal and joint targets) More...
 
virtual void eraseAllJointTargets () final
 Clear all previously defined joint targets. More...
 
virtual void eraseAllMarginalTargets () final
 Clear all the previously defined marginal targets. More...
 
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 target. More...
 
virtual void eraseJointTarget (const NodeSet &joint_target) final
 removes an existing joint target More...
 
virtual bool isJointTarget (const NodeSet &vars) const final
 return true if target is a joint target. More...
 
virtual const Set< NodeSet > & jointTargets () const noexcept final
 returns the list of joint targets More...
 
virtual Size nbrJointTargets () const noexcept final
 returns the number of joint targets More...
 
Information Theory related functions
GUM_SCALAR I (NodeId X, NodeId Y)
 Mutual information between X and Y. More...
 
GUM_SCALAR VI (NodeId X, NodeId Y)
 Variation of information between X and Y. More...
 
GUM_SCALAR jointMutualInformation (const NodeSet &targets)
 Mutual information between targets. More...
 
GUM_SCALAR jointMutualInformation (const std::vector< std::string > &targets)
 Mutual information between targets. More...
 
Targets
virtual void addAllTargets () final
 adds all nodes as targets More...
 
virtual void addTarget (NodeId target) final
 Add a marginal target to the list of targets. More...
 
virtual void addTarget (const std::string &nodeName) final
 Add a marginal target to the list of targets. More...
 
virtual void eraseTarget (NodeId target) final
 removes an existing (marginal) target More...
 
virtual void eraseTarget (const std::string &nodeName) final
 removes an existing (marginal) target More...
 
virtual bool isTarget (NodeId node) const final
 return true if variable is a (marginal) target More...
 
virtual bool isTarget (const std::string &nodeName) const final
 return true if variable is a (marginal) target More...
 
virtual const Size nbrTargets () const noexcept final
 returns the number of marginal targets More...
 
virtual const NodeSettargets () const noexcept final
 returns the list of marginal targets More...
 
Information Theory related functions
virtual GUM_SCALAR H (NodeId X) final
 Entropy Compute Shanon's entropy of a node given the observation. More...
 
virtual GUM_SCALAR H (const std::string &nodeName) final
 Entropy Compute Shanon's entropy of a node given the observation. More...
 
Accessors / Modifiers
virtual void setBN (const IBayesNet< GUM_SCALAR > *bn)
 assigns a new BN to the inference engine More...
 
virtual const IBayesNet< GUM_SCALAR > & BN () const final
 Returns a constant reference over the IBayesNet referenced by this class. More...
 
virtual const NodeProperty< Size > & domainSizes () const final
 get the domain sizes of the random variables of the BN More...
 
virtual bool isInferenceReady () const noexcept final
 returns whether the inference object is in a ready state More...
 
virtual bool isInferenceOutdatedBNStructure () const noexcept final
 returns whether the inference object is in a OutdatedBNStructure state More...
 
virtual bool isInferenceOutdatedBNPotentials () const noexcept final
 returns whether the inference object is in a OutdatedBNPotential state More...
 
virtual bool isInferenceDone () const noexcept final
 returns whether the inference object is in a InferenceDone state More...
 
virtual bool isDone () const noexcept final
 returns whether the inference object is in a done state More...
 
virtual void prepareInference () final
 prepare the internal inference structures for the next inference More...
 
virtual void makeInference () final
 perform the heavy computations needed to compute the targets' posteriors More...
 
virtual void clear ()
 clears all the data structures allocated for the last inference More...
 
virtual StateOfInference state () const noexcept final
 returns the state of the inference engine More...
 
Evidence
virtual void addEvidence (NodeId id, const Idx val) final
 adds a new hard evidence on node id More...
 
virtual void addEvidence (const std::string &nodeName, const Idx val) final
 adds a new hard evidence on node named nodeName More...
 
virtual void addEvidence (NodeId id, const std::string &label) final
 adds a new hard evidence on node id More...
 
virtual void addEvidence (const std::string &nodeName, const std::string &label) final
 adds a new hard evidence on node named nodeName More...
 
virtual void addEvidence (NodeId id, const std::vector< GUM_SCALAR > &vals) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addEvidence (const std::string &nodeName, const std::vector< GUM_SCALAR > &vals) final
 adds a new evidence on node named nodeName (might be soft or hard) More...
 
virtual void addEvidence (const Potential< GUM_SCALAR > &pot) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addEvidence (Potential< GUM_SCALAR > &&pot) final
 adds a new evidence on node id (might be soft or hard) More...
 
virtual void addSetOfEvidence (const Set< const Potential< GUM_SCALAR > * > &potset) final
 adds a new set of evidence More...
 
virtual void addListOfEvidence (const List< const Potential< GUM_SCALAR > * > &potlist) final
 adds a new list of evidence More...
 
virtual void chgEvidence (NodeId id, const Idx val) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (const std::string &nodeName, const Idx val) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (NodeId id, const std::string &label) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (const std::string &nodeName, const std::string &label) final
 change the value of an already existing hard evidence More...
 
virtual void chgEvidence (NodeId id, const std::vector< GUM_SCALAR > &vals) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void chgEvidence (const std::string &nodeName, const std::vector< GUM_SCALAR > &vals) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void chgEvidence (const Potential< GUM_SCALAR > &pot) final
 change the value of an already existing evidence (might be soft or hard) More...
 
virtual void eraseAllEvidence () final
 removes all the evidence entered into the network More...
 
virtual void eraseEvidence (NodeId id) final
 removed the evidence, if any, corresponding to node id More...
 
virtual void eraseEvidence (const std::string &nodeName) final
 removed the evidence, if any, corresponding to node of name nodeName More...
 
virtual bool hasEvidence () const final
 indicates whether some node(s) have received evidence More...
 
virtual bool hasEvidence (NodeId id) const final
 indicates whether node id has received an evidence More...
 
virtual bool hasEvidence (const std::string &nodeName) const final
 indicates whether node id has received an evidence More...
 
virtual bool hasHardEvidence (NodeId id) const final
 indicates whether node id has received a hard evidence More...
 
virtual bool hasHardEvidence (const std::string &nodeName) const final
 indicates whether node id has received a hard evidence More...
 
virtual bool hasSoftEvidence (NodeId id) const final
 indicates whether node id has received a soft evidence More...
 
virtual bool hasSoftEvidence (const std::string &nodeName) const final
 indicates whether node id has received a soft evidence More...
 
virtual Size nbrEvidence () const final
 returns the number of evidence entered into the Bayesian network More...
 
virtual Size nbrHardEvidence () const final
 returns the number of hard evidence entered into the Bayesian network More...
 
virtual Size nbrSoftEvidence () const final
 returns the number of soft evidence entered into the Bayesian network More...
 
const NodeProperty< const Potential< GUM_SCALAR > *> & evidence () const
 returns the set of evidence More...
 
const NodeSetsoftEvidenceNodes () const
 returns the set of nodes with soft evidence More...
 
const NodeSethardEvidenceNodes () const
 returns the set of nodes with hard evidence More...
 
const NodeProperty< Idx > & hardEvidence () const
 indicate for each node with hard evidence which value it took More...
 

Public Types

enum  StateOfInference { StateOfInference::OutdatedBNStructure, StateOfInference::OutdatedBNPotentials, StateOfInference::ReadyForInference, StateOfInference::Done }
 current state of the inference More...
 

Protected Member Functions

virtual void _onBayesNetChanged (const IBayesNet< GUM_SCALAR > *bn)
 fired after a new Bayes net has been assigned to the engine More...
 
virtual void _onJointTargetAdded (const NodeSet &set)=0
 fired after a new joint target is inserted More...
 
virtual void _onJointTargetErased (const NodeSet &set)=0
 fired before a joint target is removed More...
 
virtual void _onAllTargetsErased ()=0
 fired before a all the marginal and joint targets are removed More...
 
virtual void _onAllJointTargetsErased ()=0
 fired before a all the joint targets are removed More...
 
virtual const Potential< GUM_SCALAR > & _jointPosterior (const NodeSet &set)=0
 asks derived classes for the joint posterior of a declared target set More...
 
virtual const Potential< GUM_SCALAR > & _jointPosterior (const NodeSet &wanted_target, const NodeSet &declared_target)=0
 asks derived classes for the joint posterior of a set of variables not declared as a joint target More...
 
virtual Potential< GUM_SCALAR > * _unnormalizedJointPosterior (const NodeSet &set)=0
 returns a fresh unnormalized joint posterior of a given set of variables More...
 
virtual Potential< GUM_SCALAR > * _unnormalizedJointPosterior (NodeId id)=0
 returns a fresh potential equal to P(argument,evidence) More...
 
virtual void _onMarginalTargetAdded (const NodeId id)=0
 fired after a new marginal target is inserted More...
 
virtual void _onMarginalTargetErased (const NodeId id)=0
 fired before a marginal target is removed More...
 
virtual void _onAllMarginalTargetsAdded ()=0
 fired after all the nodes of the BN are added as marginal targets More...
 
virtual void _onAllMarginalTargetsErased ()=0
 fired before a all marginal targets are removed More...
 
virtual const Potential< GUM_SCALAR > & _posterior (NodeId id)=0
 asks derived classes for the posterior of a given variable More...
 
void _setTargetedMode ()
 
bool _isTargetedMode () const
 
virtual void _onStateChanged ()=0
 fired when the stage is changed More...
 
virtual void _onEvidenceAdded (const NodeId id, bool isHardEvidence)=0
 fired after a new evidence is inserted More...
 
virtual void _onEvidenceErased (const NodeId id, bool isHardEvidence)=0
 fired before an evidence is removed More...
 
virtual void _onAllEvidenceErased (bool contains_hard_evidence)=0
 fired before all the evidence are erased More...
 
virtual void _onEvidenceChanged (const NodeId id, bool hasChangedSoftHard)=0
 fired after an evidence is changed, in particular when its status (soft/hard) changes More...
 
virtual void _updateOutdatedBNStructure ()=0
 prepares inference when the latter is in OutdatedBNStructure state More...
 
virtual void _updateOutdatedBNPotentials ()=0
 prepares inference when the latter is in OutdatedBNPotentials state More...
 
virtual void _makeInference ()=0
 called when the inference has to be performed effectively More...
 
void _setOutdatedBNStructureState ()
 put the inference into an outdated BN structure state More...
 
void _setOutdatedBNPotentialsState ()
 puts the inference into an OutdatedBNPotentials state if it is not already in an OutdatedBNStructure state More...
 

Detailed Description

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

<agrum/BN/inference/jointTargetedInference.h>

A generic class for the computation of (possibly incrementally) joint posteriors

The goal of this class is to take care of the joint targets used for computing joint posteriors. The JointTargetedInference class inherits from Inference that takes care of handling both evidence and the current state of the inference and from MarginalTargetedInference for the handling of marginal targets. Note that the JointTargetedInference is designed to be used in incremental inference engines.

Definition at line 63 of file BayesNetInference.h.

Member Enumeration Documentation

◆ StateOfInference

template<typename GUM_SCALAR >
enum gum::BayesNetInference::StateOfInference
stronginherited

current state of the inference

BayesNetInference can be in one of 4 different states:

  • OutdatedBNStructure: in this state, the inference is fully unprepared to be applied because some events changed the "logical" structure of the BN: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the BN, hence involving a structural change in the BN. As a consequence, the (incremental) inference (probably) needs a significant amount of preparation to be ready for the next inference. In a Lazy propagation, for instance, this step amounts to compute a new join tree, hence a new structure in which inference will be applied. Note that classes that inherit from BayesNetInference may be smarter than BayesNetInference and may, in some situations, find out that their data structures are still ok for inference and, therefore, only resort to perform the actions related to the OutdatedBNPotentials state. As an example, consider a LazyPropagation inference in Bayes Net A->B->C->D->E in which C has received hard evidence e_C and E is the only target. In this case, A and B are not needed for inference, the only potentials that matter are P(D|e_C) and P(E|D). So the smallest join tree needed for inference contains only one clique DE. Now, adding new evidence e_A on A has no impact on E given hard evidence e_C. In this case, LazyPropagation can be smart and not update its join tree.
  • OutdatedBNPotentials: in this state, the structure of the BN remains unchanged, only some potentials stored in it have changed. Therefore, the inference probably just needs to invalidate some already computed potentials to be ready. Only a light amount of preparation is needed to be able to perform inference.
  • Ready4Inference: in this state, all the data structures are ready for inference. There just remains to perform the inference computations.
  • Done: the heavy computations of inference have been done. There might still remain a few light computations to perform to get the posterior potentials we need. Typically, in Lazy Propagation, all the messages in the join tree have been computed but, to get the potentials, we still need to perform the combinations of the potentials in the cliques with the messages sent to the cliques. In some inference algorithms, this step may even be empty.
Enumerator
OutdatedBNStructure 
OutdatedBNPotentials 
ReadyForInference 
Done 

Definition at line 183 of file BayesNetInference.h.

183  {
184  OutdatedBNStructure,
185  OutdatedBNPotentials,
186  ReadyForInference,
187  Done
188  };

Constructor & Destructor Documentation

◆ JointTargetedInference()

template<typename GUM_SCALAR >
gum::JointTargetedInference< GUM_SCALAR >::JointTargetedInference ( const IBayesNet< GUM_SCALAR > *  bn)
explicit

default constructor

Warning
note that, by aGrUM's rule, the BN is not copied but only referenced by the inference algorithm.

Definition at line 36 of file jointTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, and gum::BayesNetInference< GUM_SCALAR >::__setBayesNetDuringConstruction().

37  :
39  // assign a BN if this has not been done before (due to virtual inheritance)
40  if (this->__bn == nullptr) {
42  }
43  GUM_CONSTRUCTOR(JointTargetedInference);
44  }
void __setBayesNetDuringConstruction(const IBayesNet< GUM_SCALAR > *bn)
assigns a BN during the inference engine construction
JointTargetedInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
friend MarginalTargetedInference< GUM_SCALAR >
allow JointInference to access the single targets and inference states
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
+ Here is the call graph for this function:

◆ ~JointTargetedInference()

template<typename GUM_SCALAR >
gum::JointTargetedInference< GUM_SCALAR >::~JointTargetedInference ( )
virtual

destructor

Definition at line 49 of file jointTargetedInference_tpl.h.

49  {
50  GUM_DESTRUCTOR(JointTargetedInference);
51  }
JointTargetedInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor

Member Function Documentation

◆ _isTargetedMode()

template<typename GUM_SCALAR >
INLINE bool gum::MarginalTargetedInference< GUM_SCALAR >::_isTargetedMode ( ) const
protectedinherited

Definition at line 338 of file marginalTargetedInference_tpl.h.

References gum::MarginalTargetedInference< GUM_SCALAR >::__targeted_mode.

338  {
339  return __targeted_mode;
340  }
bool __targeted_mode
whether the actual targets are default

◆ _jointPosterior() [1/2]

template<typename GUM_SCALAR >
virtual const Potential< GUM_SCALAR >& gum::JointTargetedInference< GUM_SCALAR >::_jointPosterior ( const NodeSet set)
protectedpure virtual

asks derived classes for the joint posterior of a declared target set

Parameters
setThe set of ids of the variables whose joint posterior is looked for.

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::jointPosterior().

+ Here is the caller graph for this function:

◆ _jointPosterior() [2/2]

template<typename GUM_SCALAR >
virtual const Potential< GUM_SCALAR >& gum::JointTargetedInference< GUM_SCALAR >::_jointPosterior ( const NodeSet wanted_target,
const NodeSet declared_target 
)
protectedpure virtual

asks derived classes for the joint posterior of a set of variables not declared as a joint target

Parameters
wanted_targetThe set of ids of the variables whose joint posterior is looked for.
declared_targetthe joint target declared by the user that contains set

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

◆ _makeInference()

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_makeInference ( )
protectedpure virtualinherited

called when the inference has to be performed effectively

Once the inference is done, _fillPosterior can be called.

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::SamplingInference< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::makeInference().

+ Here is the caller graph for this function:

◆ _onAllEvidenceErased()

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onAllEvidenceErased ( bool  contains_hard_evidence)
protectedpure virtualinherited

fired before all the evidence are erased

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence().

+ Here is the caller graph for this function:

◆ _onAllJointTargetsErased()

template<typename GUM_SCALAR >
virtual void gum::JointTargetedInference< GUM_SCALAR >::_onAllJointTargetsErased ( )
protectedpure virtual

fired before a all the joint targets are removed

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::_onBayesNetChanged(), and gum::JointTargetedInference< GUM_SCALAR >::eraseAllJointTargets().

+ Here is the caller graph for this function:

◆ _onAllMarginalTargetsAdded()

template<typename GUM_SCALAR>
virtual void gum::MarginalTargetedInference< GUM_SCALAR >::_onAllMarginalTargetsAdded ( )
protectedpure virtualinherited

fired after all the nodes of the BN are added as marginal targets

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::__setAllMarginalTargets().

+ Here is the caller graph for this function:

◆ _onAllMarginalTargetsErased()

template<typename GUM_SCALAR>
virtual void gum::MarginalTargetedInference< GUM_SCALAR >::_onAllMarginalTargetsErased ( )
protectedpure virtualinherited

fired before a all marginal targets are removed

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::eraseAllTargets().

+ Here is the caller graph for this function:

◆ _onAllTargetsErased()

template<typename GUM_SCALAR >
virtual void gum::JointTargetedInference< GUM_SCALAR >::_onAllTargetsErased ( )
protectedpure virtual

fired before a all the marginal and joint targets are removed

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

◆ _onBayesNetChanged()

template<typename GUM_SCALAR >
void gum::JointTargetedInference< GUM_SCALAR >::_onBayesNetChanged ( const IBayesNet< GUM_SCALAR > *  bn)
protectedvirtual

fired after a new Bayes net has been assigned to the engine

Reimplemented from gum::MarginalTargetedInference< GUM_SCALAR >.

Reimplemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

Definition at line 56 of file jointTargetedInference_tpl.h.

References gum::JointTargetedInference< GUM_SCALAR >::__joint_targets, gum::JointTargetedInference< GUM_SCALAR >::_onAllJointTargetsErased(), and gum::MarginalTargetedInference< GUM_SCALAR >::_onBayesNetChanged().

57  {
60  __joint_targets.clear();
61  }
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn)
fired after a new Bayes net has been assigned to the engine
virtual void _onAllJointTargetsErased()=0
fired before a all the joint targets are removed
Set< NodeSet > __joint_targets
the set of joint targets
+ Here is the call graph for this function:

◆ _onEvidenceAdded()

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onEvidenceAdded ( const NodeId  id,
bool  isHardEvidence 
)
protectedpure virtualinherited

fired after a new evidence is inserted

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::addEvidence().

+ Here is the caller graph for this function:

◆ _onEvidenceChanged()

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onEvidenceChanged ( const NodeId  id,
bool  hasChangedSoftHard 
)
protectedpure virtualinherited

fired after an evidence is changed, in particular when its status (soft/hard) changes

Parameters
nodeIdthe node of the changed evidence
hasChangedSoftHardtrue if the evidence has changed from Soft to Hard or from Hard to Soft

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

+ Here is the caller graph for this function:

◆ _onEvidenceErased()

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onEvidenceErased ( const NodeId  id,
bool  isHardEvidence 
)
protectedpure virtualinherited

fired before an evidence is removed

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::eraseEvidence().

+ Here is the caller graph for this function:

◆ _onJointTargetAdded()

template<typename GUM_SCALAR >
virtual void gum::JointTargetedInference< GUM_SCALAR >::_onJointTargetAdded ( const NodeSet set)
protectedpure virtual

fired after a new joint target is inserted

Parameters
setThe set of target variable's ids.

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::addJointTarget().

+ Here is the caller graph for this function:

◆ _onJointTargetErased()

template<typename GUM_SCALAR >
virtual void gum::JointTargetedInference< GUM_SCALAR >::_onJointTargetErased ( const NodeSet set)
protectedpure virtual

fired before a joint target is removed

Parameters
setThe set of target variable's ids.

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::eraseJointTarget().

+ Here is the caller graph for this function:

◆ _onMarginalTargetAdded()

template<typename GUM_SCALAR>
virtual void gum::MarginalTargetedInference< GUM_SCALAR >::_onMarginalTargetAdded ( const NodeId  id)
protectedpure virtualinherited

fired after a new marginal target is inserted

Parameters
idThe target variable's id.

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::addAllTargets(), and gum::MarginalTargetedInference< GUM_SCALAR >::addTarget().

+ Here is the caller graph for this function:

◆ _onMarginalTargetErased()

template<typename GUM_SCALAR>
virtual void gum::MarginalTargetedInference< GUM_SCALAR >::_onMarginalTargetErased ( const NodeId  id)
protectedpure virtualinherited

fired before a marginal target is removed

Parameters
idThe target variable's id.

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::eraseTarget().

+ Here is the caller graph for this function:

◆ _onStateChanged()

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_onStateChanged ( )
protectedpure virtualinherited

fired when the stage is changed

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::__setState().

+ Here is the caller graph for this function:

◆ _posterior()

template<typename GUM_SCALAR>
virtual const Potential< GUM_SCALAR >& gum::MarginalTargetedInference< GUM_SCALAR >::_posterior ( NodeId  id)
protectedpure virtualinherited

asks derived classes for the posterior of a given variable

Parameters
idThe variable's id.

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, gum::SamplingInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

+ Here is the caller graph for this function:

◆ _setOutdatedBNPotentialsState()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::_setOutdatedBNPotentialsState ( )
protectedinherited

puts the inference into an OutdatedBNPotentials state if it is not already in an OutdatedBNStructure state

OutdatedBNPotentials: in this state, the structure of the BN remains unchanged, only some potentials stored in it have changed. Therefore, the inference probably just needs to invalidate some already computed potentials to be ready. Only a light amount of preparation is needed to be able to perform inference.

Definition at line 685 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__setState(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials.

685  {
687  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
+ Here is the call graph for this function:

◆ _setOutdatedBNStructureState()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::_setOutdatedBNStructureState ( )
protectedinherited

put the inference into an outdated BN structure state

OutdatedBNStructure: in this state, the inference is fully unprepared to be applied because some events changed the "logical" structure of the BN: for instance a node received a hard evidence, which implies that its outgoing arcs can be removed from the BN, hence involving a structural change in the BN. As a consequence, the (incremental) inference (probably) needs a significant amount of preparation to be ready for the next inference. In a Lazy propagation, for instance, this step amounts to compute a new join tree, hence a new structure in which inference will be applied. Note that classes that inherit from BayesNetInference may be smarter than BayesNetInference and may, in some situations, find out that their data structures are still ok for inference and, therefore, only resort to perform the actions related to the OutdatedBNPotentials state. As an example, consider a LazyPropagation inference in Bayes Net A->B->C->D->E in which C has received hard evidence e_C and E is the only target. In this case, A and B are not needed for inference, the only potentials that matter are P(D|e_C) and P(E|D). So the smallest join tree needed for inference contains only one clique DE. Now, adding new evidence e_A on A has no impact on E given hard evidence e_C. In this case, LazyPropagation can be smart and not update its join tree.

Definition at line 677 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__setState(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

677  {
679  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
+ Here is the call graph for this function:

◆ _setTargetedMode()

template<typename GUM_SCALAR >
INLINE void gum::MarginalTargetedInference< GUM_SCALAR >::_setTargetedMode ( )
protectedinherited

Definition at line 342 of file marginalTargetedInference_tpl.h.

References gum::MarginalTargetedInference< GUM_SCALAR >::__targeted_mode, gum::MarginalTargetedInference< GUM_SCALAR >::__targets, and gum::Set< Key, Alloc >::clear().

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::addAllTargets(), gum::JointTargetedInference< GUM_SCALAR >::addJointTarget(), gum::MarginalTargetedInference< GUM_SCALAR >::addTarget(), and gum::MarginalTargetedInference< GUM_SCALAR >::eraseAllTargets().

342  {
343  if (!__targeted_mode) {
344  __targets.clear();
345  __targeted_mode = true;
346  }
347  }
NodeSet __targets
the set of marginal targets
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:375
bool __targeted_mode
whether the actual targets are default
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _unnormalizedJointPosterior() [1/2]

template<typename GUM_SCALAR >
virtual Potential< GUM_SCALAR >* gum::JointTargetedInference< GUM_SCALAR >::_unnormalizedJointPosterior ( const NodeSet set)
protectedpure virtual

returns a fresh unnormalized joint posterior of a given set of variables

Parameters
setThe set of ids of the variables whose joint posterior is looked for.

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::I().

+ Here is the caller graph for this function:

◆ _unnormalizedJointPosterior() [2/2]

template<typename GUM_SCALAR >
virtual Potential< GUM_SCALAR >* gum::JointTargetedInference< GUM_SCALAR >::_unnormalizedJointPosterior ( NodeId  id)
protectedpure virtual

returns a fresh potential equal to P(argument,evidence)

Implemented in gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, and gum::ShaferShenoyInference< GUM_SCALAR >.

◆ _updateOutdatedBNPotentials()

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_updateOutdatedBNPotentials ( )
protectedpure virtualinherited

prepares inference when the latter is in OutdatedBNPotentials state

Note that the values of evidence are not necessarily known and can be changed between _updateOutdatedBNPotentials and _makeInference.

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::prepareInference().

+ Here is the caller graph for this function:

◆ _updateOutdatedBNStructure()

template<typename GUM_SCALAR >
virtual void gum::BayesNetInference< GUM_SCALAR >::_updateOutdatedBNStructure ( )
protectedpure virtualinherited

prepares inference when the latter is in OutdatedBNStructure state

Note that the values of evidence are not necessarily known and can be changed between _updateOutdatedBNStructure and _makeInference.

Implemented in gum::SamplingInference< GUM_SCALAR >, gum::LazyPropagation< GUM_SCALAR >, gum::VariableElimination< GUM_SCALAR >, gum::ShaferShenoyInference< GUM_SCALAR >, and gum::LoopyBeliefPropagation< GUM_SCALAR >.

Referenced by gum::BayesNetInference< GUM_SCALAR >::prepareInference().

+ Here is the caller graph for this function:

◆ addAllTargets()

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::addAllTargets ( )
finalvirtualinherited

adds all nodes as targets

Definition at line 136 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::MarginalTargetedInference< GUM_SCALAR >::__targets, gum::MarginalTargetedInference< GUM_SCALAR >::_onMarginalTargetAdded(), gum::MarginalTargetedInference< GUM_SCALAR >::_setTargetedMode(), gum::Set< Key, Alloc >::contains(), GUM_ERROR, and gum::Set< Key, Alloc >::insert().

136  {
137  // check if the node belongs to the Bayesian network
138  if (this->__bn == nullptr)
139  GUM_ERROR(NullElement,
140  "No Bayes net has been assigned to the "
141  "inference algorithm");
142 
143 
144  _setTargetedMode(); // does nothing if already in targeted mode
145  for (const auto target : this->__bn->dag()) {
146  if (!__targets.contains(target)) {
147  __targets.insert(target);
148  _onMarginalTargetAdded(target);
149  this->__setState(
151  }
152  }
153  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeSet __targets
the set of marginal targets
virtual void _onMarginalTargetAdded(const NodeId id)=0
fired after a new marginal target is inserted
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ addEvidence() [1/8]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( NodeId  id,
const Idx  val 
)
finalvirtualinherited

adds a new hard evidence on node id

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id already has an evidence

Definition at line 247 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__createHardEvidence().

Referenced by gum::LoopySamplingInference< GUM_SCALAR, APPROX >::_makeInference(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::BayesNetInference< GUM_SCALAR >::addListOfEvidence(), gum::BayesNetInference< GUM_SCALAR >::addSetOfEvidence(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), and gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact().

248  {
250  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
Potential< GUM_SCALAR > __createHardEvidence(NodeId id, Idx val) const
create the internal structure for a hard evidence
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addEvidence() [2/8]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( const std::string &  nodeName,
const Idx  val 
)
finalvirtualinherited

adds a new hard evidence on node named nodeName

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif nodeName already has an evidence

Definition at line 255 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::BN().

256  {
257  addEvidence(this->BN().idFromName(nodeName), val);
258  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ addEvidence() [3/8]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( NodeId  id,
const std::string &  label 
)
finalvirtualinherited

adds a new hard evidence on node id

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id already has an evidence

Definition at line 263 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::BN().

264  {
265  addEvidence(id, this->BN().variable(id)[label]);
266  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ addEvidence() [4/8]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( const std::string &  nodeName,
const std::string &  label 
)
finalvirtualinherited

adds a new hard evidence on node named nodeName

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif nodeName already has an evidence

Definition at line 271 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::BN().

272  {
273  NodeId id = this->BN().idFromName(nodeName);
274  addEvidence(id, this->BN().variable(id)[label]);
275  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
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.
Definition: graphElements.h:98
+ Here is the call graph for this function:

◆ addEvidence() [5/8]

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( NodeId  id,
const std::vector< GUM_SCALAR > &  vals 
)
finalvirtualinherited

adds a new evidence on node id (might be soft or hard)

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif id already has an evidence
FatalErrorif vals=[0,0,...,0]
InvalidArgumentif the size of vals is different from the domain size of node id

Definition at line 279 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and GUM_ERROR.

280  {
281  // checks that the evidence is meaningful
282  if (__bn == nullptr)
283  GUM_ERROR(NullElement,
284  "No Bayes net has been assigned to the "
285  "inference algorithm");
286 
287  if (!__bn->dag().exists(id)) {
288  GUM_ERROR(UndefinedElement, id << " is not a NodeId in the bn");
289  }
290 
291  if (__bn->variable(id).domainSize() != vals.size()) {
292  GUM_ERROR(InvalidArgument,
293  "node " << __bn->variable(id)
294  << " and its evidence vector have different sizes.");
295  }
296 
297  Potential< GUM_SCALAR > pot;
298  pot.add(__bn->variable(id));
299  pot.fillWith(vals);
300  addEvidence(std::move(pot));
301  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ addEvidence() [6/8]

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( const std::string &  nodeName,
const std::vector< GUM_SCALAR > &  vals 
)
finalvirtualinherited

adds a new evidence on node named nodeName (might be soft or hard)

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif nodeName already has an evidence
FatalErrorif vals=[0,0,...,0]
InvalidArgumentif the size of vals is different from the domain size of node nodeName

Definition at line 305 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::addEvidence(), and gum::BayesNetInference< GUM_SCALAR >::BN().

306  {
307  addEvidence(this->BN().idFromName(nodeName), vals);
308  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ addEvidence() [7/8]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( const Potential< GUM_SCALAR > &  pot)
finalvirtualinherited

adds a new evidence on node id (might be soft or hard)

Exceptions
UndefinedElementif the potential is defined over several nodes
UndefinedElementif the node on which the potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of the potential already has an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 354 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::addEvidence().

355  {
356  Potential< GUM_SCALAR > new_pot(pot);
357  addEvidence(std::move(new_pot));
358  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
+ Here is the call graph for this function:

◆ addEvidence() [8/8]

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::addEvidence ( Potential< GUM_SCALAR > &&  pot)
finalvirtualinherited

adds a new evidence on node id (might be soft or hard)

Exceptions
UndefinedElementif the potential is defined over several nodes
UndefinedElementif the node on which the potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of the potential already has an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 313 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::__isHardEvidence(), gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::_onEvidenceAdded(), GUM_ERROR, gum::BayesNetInference< GUM_SCALAR >::hasEvidence(), gum::Set< Key, Alloc >::insert(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

313  {
314  // check if the potential corresponds to an evidence
315  if (pot.nbrDim() != 1) {
316  GUM_ERROR(InvalidArgument, pot << " is not mono-dimensional.");
317  }
318  if (__bn == nullptr)
319  GUM_ERROR(NullElement,
320  "No Bayes net has been assigned to the "
321  "inference algorithm");
322 
323  NodeId id = __bn->nodeId(pot.variable(0));
324 
325  if (hasEvidence(id)) {
326  GUM_ERROR(InvalidArgument,
327  " node " << id
328  << " already has an evidence. Please use chgEvidence().");
329  }
330 
331  // check whether we have a hard evidence (and also check whether the
332  // potential only contains 0 (in this case, this will automatically raise
333  // an exception) )
334  Idx val;
335  bool is_hard_evidence = __isHardEvidence(pot, val);
336 
337  // insert the evidence
338  __evidence.insert(
339  id,
340  new Potential< GUM_SCALAR >(std::forward< Potential< GUM_SCALAR > >(pot)));
341  if (is_hard_evidence) { // pot is deterministic
342  __hard_evidence.insert(id, val);
344  } else {
346  }
348  _onEvidenceAdded(id, is_hard_evidence);
349  }
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
virtual void _onEvidenceAdded(const NodeId id, bool isHardEvidence)=0
fired after a new evidence is inserted
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
bool __isHardEvidence(const Potential< GUM_SCALAR > &pot, Idx &val) const
checks whether a potential corresponds to a hard evidence or not
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ addJointTarget()

template<typename GUM_SCALAR >
void gum::JointTargetedInference< GUM_SCALAR >::addJointTarget ( const NodeSet joint_target)
finalvirtual

Add a set of nodes as a new joint target. As a collateral effect, every node is added as a marginal target.

Exceptions
UndefinedElementif some node(s) do not belong to the Bayes net

Definition at line 118 of file jointTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::JointTargetedInference< GUM_SCALAR >::__joint_targets, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::JointTargetedInference< GUM_SCALAR >::_onJointTargetAdded(), gum::MarginalTargetedInference< GUM_SCALAR >::_setTargetedMode(), gum::JointTargetedInference< GUM_SCALAR >::eraseJointTarget(), and GUM_ERROR.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), and gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation().

119  {
120  // check if the nodes in the target belong to the Bayesian network
121  if (this->__bn == nullptr)
122  GUM_ERROR(NullElement,
123  "No Bayes net has been assigned to the "
124  "inference algorithm");
125 
126  const auto& dag = this->__bn->dag();
127  for (const auto node : joint_target) {
128  if (!dag.exists(node)) {
129  GUM_ERROR(UndefinedElement,
130  "at least one one in " << joint_target
131  << " does not belong to the bn");
132  }
133  }
134 
135  // check that the joint_target set does not contain the new target
136  if (__joint_targets.contains(joint_target)) return;
137 
138  // check if joint_target is a subset of an already existing target
139  for (const auto& target : __joint_targets) {
140  if (target.isSupersetOf(joint_target)) return;
141  }
142 
143  // check if joint_target is not a superset of an already existing target
144  // in this case, we need to remove old existing target
145  for (auto iter = __joint_targets.beginSafe();
146  iter != __joint_targets.endSafe();
147  ++iter) {
148  if (iter->isSubsetOf(joint_target)) eraseJointTarget(*iter);
149  }
150 
151  this->_setTargetedMode(); // does nothing if already in targeted mode
152  __joint_targets.insert(joint_target);
153  _onJointTargetAdded(joint_target);
154  this->__setState(
156  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
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
Set< NodeSet > __joint_targets
the set of joint targets
#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:

◆ addListOfEvidence()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::addListOfEvidence ( const List< const Potential< GUM_SCALAR > * > &  potlist)
finalvirtualinherited

adds a new list of evidence

Exceptions
UndefinedElementif some potential is defined over several nodes
UndefinedElementif the node on which some potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of some potential already has an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 363 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::addEvidence().

364  {
365  for (const auto pot : potlist)
366  addEvidence(*pot);
367  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
+ Here is the call graph for this function:

◆ addSetOfEvidence()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::addSetOfEvidence ( const Set< const Potential< GUM_SCALAR > * > &  potset)
finalvirtualinherited

adds a new set of evidence

Exceptions
UndefinedElementif some potential is defined over several nodes
UndefinedElementif the node on which some potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of some potential already has an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 372 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::addEvidence().

373  {
374  for (const auto pot : potset)
375  addEvidence(*pot);
376  }
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
+ Here is the call graph for this function:

◆ addTarget() [1/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::addTarget ( NodeId  target)
finalvirtualinherited

Add a marginal target to the list of targets.

Exceptions
UndefinedElementif target is not a NodeId in the Bayes net

Definition at line 112 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::MarginalTargetedInference< GUM_SCALAR >::__targets, gum::MarginalTargetedInference< GUM_SCALAR >::_onMarginalTargetAdded(), gum::MarginalTargetedInference< GUM_SCALAR >::_setTargetedMode(), gum::Set< Key, Alloc >::contains(), GUM_ERROR, and gum::Set< Key, Alloc >::insert().

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::addTarget(), and gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact().

112  {
113  // check if the node belongs to the Bayesian network
114  if (this->__bn == nullptr)
115  GUM_ERROR(NullElement,
116  "No Bayes net has been assigned to the "
117  "inference algorithm");
118 
119  if (!this->__bn->dag().exists(target)) {
120  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the bn");
121  }
122 
123  _setTargetedMode(); // does nothing if already in targeted mode
124  // add the new target
125  if (!__targets.contains(target)) {
126  __targets.insert(target);
127  _onMarginalTargetAdded(target);
128  this->__setState(
130  }
131  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeSet __targets
the set of marginal targets
virtual void _onMarginalTargetAdded(const NodeId id)=0
fired after a new marginal target is inserted
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#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:

◆ addTarget() [2/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::addTarget ( const std::string &  nodeName)
finalvirtualinherited

Add a marginal target to the list of targets.

Exceptions
UndefinedElementif target is not a NodeId in the Bayes net

Definition at line 158 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::MarginalTargetedInference< GUM_SCALAR >::addTarget(), and GUM_ERROR.

159  {
160  // check if the node belongs to the Bayesian network
161  if (this->__bn == nullptr)
162  GUM_ERROR(NullElement,
163  "No Bayes net has been assigned to the "
164  "inference algorithm");
165 
166  addTarget(this->__bn->idFromName(nodeName));
167  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ BN()

template<typename GUM_SCALAR >
INLINE const IBayesNet< GUM_SCALAR > & gum::BayesNetInference< GUM_SCALAR >::BN ( ) const
finalvirtualinherited

Returns a constant reference over the IBayesNet referenced by this class.

Exceptions
UndefinedElementis raised if no Bayes net has been assigned to the inference.

Definition at line 121 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, and GUM_ERROR.

Referenced by gum::WeightedSampling< GUM_SCALAR >::_draw(), gum::MonteCarloSampling< GUM_SCALAR >::_draw(), gum::ImportanceSampling< GUM_SCALAR >::_draw(), gum::SamplingInference< GUM_SCALAR >::_posterior(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::SamplingInference< GUM_SCALAR >::contextualize(), gum::SamplingInference< GUM_SCALAR >::currentPosterior(), gum::BayesNetInference< GUM_SCALAR >::eraseEvidence(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), gum::MarginalTargetedInference< GUM_SCALAR >::H(), gum::BayesNetInference< GUM_SCALAR >::hasEvidence(), gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence(), gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence(), gum::JointTargetedInference< GUM_SCALAR >::I(), gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation(), gum::MarginalTargetedInference< GUM_SCALAR >::posterior(), gum::JointTargetedInference< GUM_SCALAR >::posterior(), gum::SamplingInference< GUM_SCALAR >::samplingBN(), and gum::Estimator< GUM_SCALAR >::setFromLBP().

121  {
122  if (__bn == nullptr)
123  GUM_ERROR(UndefinedElement,
124  "No Bayes net has been assigned to "
125  "the inference algorithm.");
126  return *__bn;
127  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ chgEvidence() [1/7]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::chgEvidence ( NodeId  id,
const Idx  val 
)
finalvirtualinherited

change the value of an already existing hard evidence

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id does not already have an evidence

Definition at line 432 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__createHardEvidence().

Referenced by gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), and gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact().

433  {
435  }
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
Potential< GUM_SCALAR > __createHardEvidence(NodeId id, Idx val) const
create the internal structure for a hard evidence
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ chgEvidence() [2/7]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::chgEvidence ( const std::string &  nodeName,
const Idx  val 
)
finalvirtualinherited

change the value of an already existing hard evidence

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id does not already have an evidence

Definition at line 440 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

441  {
442  chgEvidence(this->BN().idFromName(nodeName), val);
443  }
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ chgEvidence() [3/7]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::chgEvidence ( NodeId  id,
const std::string &  label 
)
finalvirtualinherited

change the value of an already existing hard evidence

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id does not already have an evidence

Definition at line 448 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

449  {
450  chgEvidence(id, this->BN().variable(id)[label]);
451  }
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ chgEvidence() [4/7]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::chgEvidence ( const std::string &  nodeName,
const std::string &  label 
)
finalvirtualinherited

change the value of an already existing hard evidence

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif val is not a value for id
InvalidArgumentif id does not already have an evidence

Definition at line 456 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

457  {
458  NodeId id = this->BN().idFromName(nodeName);
459  chgEvidence(id, this->BN().variable(id)[label]);
460  }
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
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.
Definition: graphElements.h:98
+ Here is the call graph for this function:

◆ chgEvidence() [5/7]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::chgEvidence ( NodeId  id,
const std::vector< GUM_SCALAR > &  vals 
)
finalvirtualinherited

change the value of an already existing evidence (might be soft or hard)

Exceptions
UndefinedElementif id does not belong to the Bayesian network
InvalidArgumentif the node does not already have an evidence
FatalErrorif vals=[0,0,...,0]
InvalidArgumentif the size of vals is different from the domain size of node id

Definition at line 464 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), and GUM_ERROR.

465  {
466  // check whether this corresponds to an evidence
467  if (__bn == nullptr)
468  GUM_ERROR(NullElement,
469  "No Bayes net has been assigned to the "
470  "inference algorithm");
471 
472  if (!__bn->dag().exists(id)) {
473  GUM_ERROR(UndefinedElement, id << " is not a NodeId in the bn");
474  }
475 
476  if (__bn->variable(id).domainSize() != vals.size()) {
477  GUM_ERROR(InvalidArgument,
478  "node " << __bn->variable(id)
479  << " and its evidence have different sizes.");
480  }
481 
482  // create the potential corresponding to vals
483  Potential< GUM_SCALAR > pot;
484  pot.add(__bn->variable(id));
485  pot.fillWith(vals);
486  chgEvidence(pot);
487  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ chgEvidence() [6/7]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::chgEvidence ( const std::string &  nodeName,
const std::vector< GUM_SCALAR > &  vals 
)
finalvirtualinherited

change the value of an already existing evidence (might be soft or hard)

Exceptions
UndefinedElementif nodeName does not belong to the Bayesian network
InvalidArgumentif the node does not already have an evidence
FatalErrorif vals=[0,0,...,0]
InvalidArgumentif the size of vals is different from the domain size of node id

Definition at line 491 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::chgEvidence().

492  {
493  chgEvidence(this->BN().idFromName(nodeName), vals);
494  }
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ chgEvidence() [7/7]

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::chgEvidence ( const Potential< GUM_SCALAR > &  pot)
finalvirtualinherited

change the value of an already existing evidence (might be soft or hard)

Exceptions
UndefinedElementif the potential is defined over several nodes
UndefinedElementif the node on which the potential is defined does not belong to the Bayesian network
InvalidArgumentif the node of the potential does not already have an evidence
FatalErrorif pot=[0,0,...,0]

Definition at line 499 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::__isHardEvidence(), gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::_onEvidenceChanged(), gum::Instantiation::end(), gum::Set< Key, Alloc >::erase(), GUM_ERROR, gum::BayesNetInference< GUM_SCALAR >::hasEvidence(), gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence(), gum::Instantiation::inc(), gum::Set< Key, Alloc >::insert(), gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNStructure(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials, gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure, gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

500  {
501  // check if the potential corresponds to an evidence
502  if (pot.nbrDim() != 1) {
503  GUM_ERROR(InvalidArgument, pot << " is not a mono-dimensional potential.");
504  }
505  if (__bn == nullptr)
506  GUM_ERROR(NullElement,
507  "No Bayes net has been assigned to the "
508  "inference algorithm");
509 
510  NodeId id = __bn->nodeId(pot.variable(0));
511 
512  if (!hasEvidence(id)) {
513  GUM_ERROR(InvalidArgument,
514  id << " has no evidence. Please use addEvidence().");
515  }
516 
517  // check whether we have a hard evidence (and also check whether the
518  // potential only contains 0 (in this case, this will automatically raise
519  // an exception) )
520  Idx val;
521  bool is_hard_evidence = __isHardEvidence(pot, val);
522 
523  // modify the evidence already stored
524  const Potential< GUM_SCALAR >* localPot = __evidence[id];
525  Instantiation I(pot);
526  for (I.setFirst(); !I.end(); I.inc()) {
527  localPot->set(I, pot[I]);
528  }
529 
530  // the inference state will be different
531  // whether evidence change from Hard to Soft or not.
532  bool hasChangedSoftHard = false;
533 
534  if (is_hard_evidence) {
535  if (!hasHardEvidence(id)) {
536  hasChangedSoftHard = true;
537  __hard_evidence.insert(id, val);
540  } else {
541  __hard_evidence[id] = val;
542  }
543  } else {
544  if (hasHardEvidence(id)) { // evidence was hard
545  __hard_evidence.erase(id);
548  hasChangedSoftHard = true;
549  }
550  }
551 
552  if (hasChangedSoftHard) {
554  } else {
557  }
558  }
559 
560  _onEvidenceChanged(id, hasChangedSoftHard);
561  }
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
virtual void _onEvidenceChanged(const NodeId id, bool hasChangedSoftHard)=0
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
virtual bool isInferenceOutdatedBNStructure() const noexcept final
returns whether the inference object is in a OutdatedBNStructure state
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
bool __isHardEvidence(const Potential< GUM_SCALAR > &pot, Idx &val) const
checks whether a potential corresponds to a hard evidence or not
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ clear()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::clear ( )
virtualinherited

clears all the data structures allocated for the last inference

Definition at line 153 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::BayesNetInference< GUM_SCALAR >::setBN().

153  {
156  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ domainSizes()

template<typename GUM_SCALAR >
INLINE const NodeProperty< Size > & gum::BayesNetInference< GUM_SCALAR >::domainSizes ( ) const
finalvirtualinherited

get the domain sizes of the random variables of the BN

Definition at line 174 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__domain_sizes.

174  {
175  return __domain_sizes;
176  }
NodeProperty< Size > __domain_sizes
the domain sizes of the random variables

◆ eraseAllEvidence()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence ( )
finalvirtualinherited

removes all the evidence entered into the network

Definition at line 595 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::_onAllEvidenceErased(), gum::Set< Key, Alloc >::clear(), gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNStructure(), gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials, and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::BayesNetInference< GUM_SCALAR >::clear(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), and gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation().

595  {
596  bool has_hard_evidence = !__hard_evidence.empty();
597  this->_onAllEvidenceErased(has_hard_evidence);
598 
599  for (const auto& pair : __evidence) {
600  if (pair.second != nullptr) { delete (pair.second); }
601  }
602 
603  __evidence.clear();
604  __hard_evidence.clear();
607 
608  if (has_hard_evidence) {
610  } else {
613  }
614  }
615  }
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
virtual void _onAllEvidenceErased(bool contains_hard_evidence)=0
fired before all the evidence are erased
virtual bool isInferenceOutdatedBNStructure() const noexcept final
returns whether the inference object is in a OutdatedBNStructure state
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:375
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eraseAllJointTargets()

template<typename GUM_SCALAR >
INLINE void gum::JointTargetedInference< GUM_SCALAR >::eraseAllJointTargets ( )
finalvirtual

Clear all previously defined joint targets.

Definition at line 97 of file jointTargetedInference_tpl.h.

References gum::JointTargetedInference< GUM_SCALAR >::__joint_targets, gum::BayesNetInference< GUM_SCALAR >::__setState(), and gum::JointTargetedInference< GUM_SCALAR >::_onAllJointTargetsErased().

Referenced by gum::JointTargetedInference< GUM_SCALAR >::eraseAllTargets().

97  {
98  if (__joint_targets.size() > 0) {
99  // we already are in target mode. So no this->_setTargetedMode(); is needed
101  __joint_targets.clear();
102  this->__setState(
104  }
105  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
virtual void _onAllJointTargetsErased()=0
fired before a all the joint targets are removed
Set< NodeSet > __joint_targets
the set of joint targets
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eraseAllMarginalTargets()

template<typename GUM_SCALAR >
INLINE void gum::JointTargetedInference< GUM_SCALAR >::eraseAllMarginalTargets ( )
finalvirtual

Clear all the previously defined marginal targets.

Definition at line 90 of file jointTargetedInference_tpl.h.

References gum::MarginalTargetedInference< GUM_SCALAR >::eraseAllTargets().

Referenced by gum::JointTargetedInference< GUM_SCALAR >::eraseAllTargets().

90  {
92  }
virtual void eraseAllTargets()
Clear all previously defined targets.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eraseAllTargets()

template<typename GUM_SCALAR >
INLINE void gum::JointTargetedInference< GUM_SCALAR >::eraseAllTargets ( )
virtual

Clear all previously defined targets (marginal and joint targets)

Clear all previously defined targets. As a result, no posterior can be computed (since we can only compute the posteriors of the marginal or joint targets that have been added by the user).

Reimplemented from gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 110 of file jointTargetedInference_tpl.h.

References gum::JointTargetedInference< GUM_SCALAR >::eraseAllJointTargets(), and gum::JointTargetedInference< GUM_SCALAR >::eraseAllMarginalTargets().

Referenced by gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), and gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation().

110  {
113  }
virtual void eraseAllMarginalTargets() final
Clear all the previously defined marginal targets.
virtual void eraseAllJointTargets() final
Clear all previously defined joint targets.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eraseEvidence() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::eraseEvidence ( NodeId  id)
finalvirtualinherited

removed the evidence, if any, corresponding to node id

Definition at line 566 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence, gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, gum::BayesNetInference< GUM_SCALAR >::_onEvidenceErased(), gum::Set< Key, Alloc >::erase(), gum::BayesNetInference< GUM_SCALAR >::hasEvidence(), gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence(), gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNStructure(), gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials, and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::BayesNetInference< GUM_SCALAR >::eraseEvidence().

566  {
567  if (hasEvidence(id)) {
568  if (hasHardEvidence(id)) {
569  _onEvidenceErased(id, true);
570  __hard_evidence.erase(id);
573  } else {
574  _onEvidenceErased(id, false);
578  }
579  }
580 
581  delete (__evidence[id]);
582  __evidence.erase(id);
583  }
584  }
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
virtual bool isInferenceOutdatedBNStructure() const noexcept final
returns whether the inference object is in a OutdatedBNStructure state
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
virtual void _onEvidenceErased(const NodeId id, bool isHardEvidence)=0
fired before an evidence is removed
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eraseEvidence() [2/2]

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::eraseEvidence ( const std::string &  nodeName)
finalvirtualinherited

removed the evidence, if any, corresponding to node of name nodeName

Definition at line 588 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::eraseEvidence().

588  {
589  eraseEvidence(this->BN().idFromName(nodeName));
590  }
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
virtual void eraseEvidence(NodeId id) final
removed the evidence, if any, corresponding to node id
+ Here is the call graph for this function:

◆ eraseJointTarget()

template<typename GUM_SCALAR >
void gum::JointTargetedInference< GUM_SCALAR >::eraseJointTarget ( const NodeSet joint_target)
finalvirtual

removes an existing joint target

Warning
If the joint target does not already exist, the method does nothing. In particular, it does not raise any exception.

Definition at line 161 of file jointTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::JointTargetedInference< GUM_SCALAR >::__joint_targets, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::JointTargetedInference< GUM_SCALAR >::_onJointTargetErased(), and GUM_ERROR.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::addJointTarget().

162  {
163  // check if the nodes in the target belong to the Bayesian network
164  if (this->__bn == nullptr)
165  GUM_ERROR(NullElement,
166  "No Bayes net has been assigned to the "
167  "inference algorithm");
168 
169  const auto& dag = this->__bn->dag();
170  for (const auto node : joint_target) {
171  if (!dag.exists(node)) {
172  GUM_ERROR(UndefinedElement,
173  "at least one one in " << joint_target
174  << " does not belong to the bn");
175  }
176  }
177 
178  // check that the joint_target set does not contain the new target
179  if (__joint_targets.contains(joint_target)) {
180  // note that we have to be in target mode when we are here
181  // so, no this->_setTargetedMode(); is necessary
182  _onJointTargetErased(joint_target);
183  __joint_targets.erase(joint_target);
184  this->__setState(
186  }
187  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void _onJointTargetErased(const NodeSet &set)=0
fired before a joint target is removed
Set< NodeSet > __joint_targets
the set of joint targets
#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:

◆ eraseTarget() [1/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::eraseTarget ( NodeId  target)
finalvirtualinherited

removes an existing (marginal) target

Warning
If the target does not already exist, the method does nothing. In particular, it does not raise any exception.

Definition at line 172 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::MarginalTargetedInference< GUM_SCALAR >::__targeted_mode, gum::MarginalTargetedInference< GUM_SCALAR >::__targets, gum::MarginalTargetedInference< GUM_SCALAR >::_onMarginalTargetErased(), gum::Set< Key, Alloc >::contains(), gum::Set< Key, Alloc >::erase(), and GUM_ERROR.

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::eraseTarget().

172  {
173  // check if the node belongs to the Bayesian network
174  if (this->__bn == nullptr)
175  GUM_ERROR(NullElement,
176  "No Bayes net has been assigned to the "
177  "inference algorithm");
178 
179  if (!this->__bn->dag().exists(target)) {
180  GUM_ERROR(UndefinedElement, target << " is not a NodeId in the bn");
181  }
182 
183 
184  if (__targets.contains(target)) {
185  __targeted_mode = true; // we do not use _setTargetedMode because we do not
186  // want to clear the targets
187  _onMarginalTargetErased(target);
188  __targets.erase(target);
189  this->__setState(
191  }
192  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
NodeSet __targets
the set of marginal targets
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
virtual void _onMarginalTargetErased(const NodeId id)=0
fired before a marginal target is removed
bool __targeted_mode
whether the actual targets are default
#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:

◆ eraseTarget() [2/2]

template<typename GUM_SCALAR >
void gum::MarginalTargetedInference< GUM_SCALAR >::eraseTarget ( const std::string &  nodeName)
finalvirtualinherited

removes an existing (marginal) target

Warning
If the target does not already exist, the method does nothing. In particular, it does not raise any exception.

Definition at line 197 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::MarginalTargetedInference< GUM_SCALAR >::eraseTarget(), and GUM_ERROR.

198  {
199  // check if the node belongs to the Bayesian network
200  if (this->__bn == nullptr)
201  GUM_ERROR(NullElement,
202  "No Bayes net has been assigned to the "
203  "inference algorithm");
204 
205  eraseTarget(this->__bn->idFromName(nodeName));
206  }
virtual void eraseTarget(NodeId target) final
removes an existing (marginal) target
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ evidence()

template<typename GUM_SCALAR >
INLINE const NodeProperty< const Potential< GUM_SCALAR > *> & gum::BayesNetInference< GUM_SCALAR >::evidence ( ) const
inherited

returns the set of evidence

Definition at line 650 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

Referenced by gum::ImportanceSampling< GUM_SCALAR >::_onContextualize(), and gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

650  {
651  return __evidence;
652  }
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network
+ Here is the caller graph for this function:

◆ evidenceImpact() [1/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact ( NodeId  target,
const NodeSet evs 
)
inherited

Create a gum::Potential for P(target|evs) (for all instanciation of target and evs)

Warning
If some evs are d-separated, they are not included in the Potential
Parameters
bnthe BayesNet
targetthe nodeId of the targetted variable
evsthe vector of nodeId of the observed variables
Returns
a Potential

Definition at line 285 of file marginalTargetedInference_tpl.h.

References gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::MarginalTargetedInference< GUM_SCALAR >::addTarget(), gum::BayesNetInference< GUM_SCALAR >::BN(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::Set< Key, Alloc >::contains(), gum::Instantiation::end(), gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence(), gum::MarginalTargetedInference< GUM_SCALAR >::eraseAllTargets(), GUM_ERROR, gum::Instantiation::incNotVar(), gum::Instantiation::incVar(), gum::BayesNetInference< GUM_SCALAR >::makeInference(), gum::MarginalTargetedInference< GUM_SCALAR >::posterior(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirst(), gum::Instantiation::setFirstVar(), and gum::Instantiation::val().

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact().

286  {
287  const auto& vtarget = this->BN().variable(target);
288 
289  if (evs.contains(target)) {
290  GUM_ERROR(InvalidArgument,
291  "Target <" << vtarget.name() << "> (" << target
292  << ") can not be in evs (" << evs << ").");
293  }
294  auto condset = this->BN().minimalCondSet(target, evs);
295 
296  Potential< GUM_SCALAR > res;
297  this->eraseAllTargets();
298  this->eraseAllEvidence();
299  res.add(this->BN().variable(target));
300  this->addTarget(target);
301  for (const auto& n : condset) {
302  res.add(this->BN().variable(n));
303  this->addEvidence(n, 0);
304  }
305 
306  Instantiation inst(res);
307  for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
308  // inferring
309  for (const auto& n : condset)
310  this->chgEvidence(n, inst.val(this->BN().variable(n)));
311  this->makeInference();
312  // populate res
313  for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
314  res.set(inst, this->posterior(target)[inst]);
315  }
316  inst.setFirstVar(vtarget); // remove inst.end() flag
317  }
318 
319  return res;
320  }
virtual void makeInference() final
perform the heavy computations needed to compute the targets&#39; posteriors
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
virtual void eraseAllEvidence() final
removes all the evidence entered into the network
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
virtual void eraseAllTargets()
Clear all previously defined targets.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
#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:

◆ evidenceImpact() [2/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact ( const std::string &  target,
const std::vector< std::string > &  evs 
)
inherited

Create a gum::Potential for P(target|evs) (for all instanciation of target and evs)

Warning
If some evs are d-separated, they are not included in the Potential
Parameters
targetthe nodeId of the target variable
evsthe nodeId of the observed variable
Returns
a Potential

Definition at line 324 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), and gum::Set< Key, Alloc >::insert().

325  {
326  const auto& bn = this->BN();
327 
328  gum::NodeSet evsId;
329  for (const auto& evname : evs) {
330  evsId.insert(bn.idFromName(evname));
331  }
332 
333  return evidenceImpact(bn.idFromName(target), evsId);
334  }
Potential< GUM_SCALAR > evidenceImpact(NodeId target, const NodeSet &evs)
Create a gum::Potential for P(target|evs) (for all instanciation of target and evs) ...
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
+ Here is the call graph for this function:

◆ evidenceJointImpact() [1/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::JointTargetedInference< 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)

Warning
If some evs are d-separated, they are not included in the Potential
Parameters
targetsthe NodeSet of the targeted variables
evsthe NodeSet of observed variables
Returns
a Potential

Definition at line 331 of file jointTargetedInference_tpl.h.

References gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::Instantiation::add(), gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::JointTargetedInference< GUM_SCALAR >::addJointTarget(), gum::BayesNetInference< GUM_SCALAR >::BN(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::Instantiation::end(), gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence(), gum::JointTargetedInference< GUM_SCALAR >::eraseAllTargets(), GUM_ERROR, gum::Instantiation::incIn(), gum::Instantiation::incOut(), gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), gum::BayesNetInference< GUM_SCALAR >::makeInference(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirstIn(), gum::Instantiation::setFirstOut(), and gum::Instantiation::val().

Referenced by gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact().

332  {
333  if (!(evs * targets).empty()) {
334  GUM_ERROR(InvalidArgument,
335  "Targets (" << targets << ") can not intersect evs (" << evs
336  << ").");
337  }
338  auto condset = this->BN().minimalCondSet(targets, evs);
339 
340  this->eraseAllTargets();
341  this->eraseAllEvidence();
342 
343  Instantiation iTarget;
344  Potential< GUM_SCALAR > res;
345  for (const auto& target : targets) {
346  res.add(this->BN().variable(target));
347  iTarget.add(this->BN().variable(target));
348  }
349  this->addJointTarget(targets);
350 
351  for (const auto& n : condset) {
352  res.add(this->BN().variable(n));
353  this->addEvidence(n, 0);
354  }
355 
356  Instantiation inst(res);
357  for (inst.setFirstOut(iTarget); !inst.end(); inst.incOut(iTarget)) {
358  // inferring
359  for (const auto& n : condset)
360  this->chgEvidence(n, inst.val(this->BN().variable(n)));
361  this->makeInference();
362  // populate res
363  for (inst.setFirstIn(iTarget); !inst.end(); inst.incIn(iTarget)) {
364  res.set(inst, this->jointPosterior(targets)[inst]);
365  }
366  inst.setFirstIn(iTarget); // remove inst.end() flag
367  }
368 
369  return res;
370  }
virtual void eraseAllTargets()
Clear all previously defined targets (marginal and joint targets)
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 makeInference() final
perform the heavy computations needed to compute the targets&#39; posteriors
virtual void chgEvidence(NodeId id, const Idx val) final
change the value of an already existing hard evidence
virtual void addEvidence(NodeId id, const Idx val) final
adds a new hard evidence on node id
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 NodeSet & targets() const noexcept final
returns the list of marginal targets
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
#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:

◆ evidenceJointImpact() [2/2]

template<typename GUM_SCALAR >
Potential< GUM_SCALAR > gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact ( const std::vector< std::string > &  targets,
const std::vector< std::string > &  evs 
)

Create a gum::Potential for P(joint targets|evs) (for all instanciation of targets and evs)

Warning
If some evs are d-separated, they are not included in the Potential
Parameters
targetsthe vector of std::string of the targeted variables
evsthe vector of std::string of observed variables
Returns
a Potential

Definition at line 374 of file jointTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), and gum::Set< Key, Alloc >::insert().

376  {
377  const auto& bn = this->BN();
378 
379  gum::NodeSet targetsId;
380  for (const auto& targetname : targets) {
381  targetsId.insert(bn.idFromName(targetname));
382  }
383 
384  gum::NodeSet evsId;
385  for (const auto& evname : evs) {
386  evsId.insert(bn.idFromName(evname));
387  }
388 
389  return evidenceJointImpact(targetsId, evsId);
390  }
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 const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
+ Here is the call graph for this function:

◆ H() [1/2]

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MarginalTargetedInference< GUM_SCALAR >::H ( NodeId  X)
finalvirtualinherited

Entropy Compute Shanon's entropy of a node given the observation.

See also
http://en.wikipedia.org/wiki/Information_entropy

Definition at line 269 of file marginalTargetedInference_tpl.h.

References gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::H(), and gum::JointTargetedInference< GUM_SCALAR >::VI().

269  {
270  return posterior(X).entropy();
271  }
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ H() [2/2]

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::MarginalTargetedInference< GUM_SCALAR >::H ( const std::string &  nodeName)
finalvirtualinherited

Entropy Compute Shanon's entropy of a node given the observation.

See also
http://en.wikipedia.org/wiki/Information_entropy

Definition at line 278 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::MarginalTargetedInference< GUM_SCALAR >::H().

278  {
279  return H(this->BN().idFromName(nodeName));
280  }
virtual GUM_SCALAR H(NodeId X) final
Entropy Compute Shanon&#39;s entropy of a node given the observation.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ hardEvidence()

template<typename GUM_SCALAR >
INLINE const NodeProperty< Idx > & gum::BayesNetInference< GUM_SCALAR >::hardEvidence ( ) const
inherited

indicate for each node with hard evidence which value it took

Definition at line 642 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__hard_evidence.

Referenced by gum::WeightedSampling< GUM_SCALAR >::_draw(), gum::MonteCarloSampling< GUM_SCALAR >::_draw(), gum::ImportanceSampling< GUM_SCALAR >::_draw(), and gum::SamplingInference< GUM_SCALAR >::contextualize().

642  {
643  return __hard_evidence;
644  }
NodeProperty< Idx > __hard_evidence
assign to each node with a hard evidence the index of its observed value
+ Here is the caller graph for this function:

◆ hardEvidenceNodes()

template<typename GUM_SCALAR >
INLINE const NodeSet & gum::BayesNetInference< GUM_SCALAR >::hardEvidenceNodes ( ) const
inherited

returns the set of nodes with hard evidence

the set of nodes that received hard evidence

Definition at line 666 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes.

Referenced by gum::WeightedSampling< GUM_SCALAR >::_draw(), gum::MonteCarloSampling< GUM_SCALAR >::_draw(), gum::ImportanceSampling< GUM_SCALAR >::_draw(), gum::ImportanceSampling< GUM_SCALAR >::_onContextualize(), gum::SamplingInference< GUM_SCALAR >::_setEstimatorFromBN(), gum::SamplingInference< GUM_SCALAR >::_setEstimatorFromLBP(), gum::SamplingInference< GUM_SCALAR >::contextualize(), and gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

666  {
667  return __hard_evidence_nodes;
668  }
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
+ Here is the caller graph for this function:

◆ hasEvidence() [1/3]

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasEvidence ( ) const
finalvirtualinherited

indicates whether some node(s) have received evidence

Definition at line 381 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

Referenced by gum::BayesNetInference< GUM_SCALAR >::addEvidence(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::BayesNetInference< GUM_SCALAR >::eraseEvidence(), and gum::BayesNetInference< GUM_SCALAR >::hasEvidence().

381  {
382  return !__evidence.empty();
383  }
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network
+ Here is the caller graph for this function:

◆ hasEvidence() [2/3]

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasEvidence ( NodeId  id) const
finalvirtualinherited

indicates whether node id has received an evidence

Definition at line 388 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

388  {
389  return __evidence.exists(id);
390  }
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network

◆ hasEvidence() [3/3]

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasEvidence ( const std::string &  nodeName) const
finalvirtualinherited

indicates whether node id has received an evidence

Definition at line 409 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::hasEvidence().

410  {
411  return hasEvidence(this->BN().idFromName(nodeName));
412  }
virtual bool hasEvidence() const final
indicates whether some node(s) have received evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ hasHardEvidence() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence ( NodeId  id) const
finalvirtualinherited

indicates whether node id has received a hard evidence

Definition at line 395 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, and gum::Set< Key, Alloc >::exists().

Referenced by gum::ImportanceSampling< GUM_SCALAR >::_draw(), gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::BayesNetInference< GUM_SCALAR >::eraseEvidence(), and gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence().

395  {
396  return __hard_evidence_nodes.exists(id);
397  }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasHardEvidence() [2/2]

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence ( const std::string &  nodeName) const
finalvirtualinherited

indicates whether node id has received a hard evidence

Definition at line 417 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::hasHardEvidence().

418  {
419  return hasHardEvidence(this->BN().idFromName(nodeName));
420  }
virtual bool hasHardEvidence(NodeId id) const final
indicates whether node id has received a hard evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ hasSoftEvidence() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence ( NodeId  id) const
finalvirtualinherited

indicates whether node id has received a soft evidence

Definition at line 402 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, and gum::Set< Key, Alloc >::exists().

Referenced by gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence().

402  {
403  return __soft_evidence_nodes.exists(id);
404  }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasSoftEvidence() [2/2]

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence ( const std::string &  nodeName) const
finalvirtualinherited

indicates whether node id has received a soft evidence

Definition at line 425 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::BayesNetInference< GUM_SCALAR >::hasSoftEvidence().

426  {
427  return hasSoftEvidence(this->BN().idFromName(nodeName));
428  }
virtual bool hasSoftEvidence(NodeId id) const final
indicates whether node id has received a soft evidence
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ I()

template<typename GUM_SCALAR >
GUM_SCALAR gum::JointTargetedInference< GUM_SCALAR >::I ( NodeId  X,
NodeId  Y 
)

Mutual information between X and Y.

See also
http://en.wikipedia.org/wiki/Mutual_information
Warning
Due to limitation of joint, may not be able to compute this value
Exceptions
OperationNotAllowedin these cases

Definition at line 273 of file jointTargetedInference_tpl.h.

References gum::JointTargetedInference< GUM_SCALAR >::_unnormalizedJointPosterior(), gum::BayesNetInference< GUM_SCALAR >::BN(), gum::Instantiation::end(), GUM_ERROR, gum::Potential< GUM_SCALAR >::margSumOut(), gum::Potential< GUM_SCALAR >::normalize(), and gum::Instantiation::setFirst().

Referenced by gum::JointTargetedInference< GUM_SCALAR >::VI().

273  {
274  Potential< GUM_SCALAR > pX, pY, *pXY = nullptr;
275  if (X == Y) {
276  GUM_ERROR(OperationNotAllowed, "Mutual Information I(X,Y) with X==Y");
277  }
278 
279  try {
280  // here use unnormalized joint posterior rather than just posterior
281  // to avoid saving the posterior in the cache of the inference engines
282  // like LazyPropagation or SahferShenoy.
283  pXY = this->_unnormalizedJointPosterior({X, Y});
284  pXY->normalize();
285  pX = pXY->margSumOut({&(this->BN().variable(Y))});
286  pY = pXY->margSumOut({&(this->BN().variable(X))});
287  } catch (...) {
288  if (pXY != nullptr) { delete pXY; }
289  throw;
290  }
291 
292  Instantiation i(*pXY);
293  auto res = (GUM_SCALAR)0;
294 
295  for (i.setFirst(); !i.end(); ++i) {
296  GUM_SCALAR vXY = (*pXY)[i];
297  GUM_SCALAR vX = pX[i];
298  GUM_SCALAR vY = pY[i];
299 
300  if (vXY > (GUM_SCALAR)0) {
301  if (vX == (GUM_SCALAR)0 || vY == (GUM_SCALAR)0) {
302  GUM_ERROR(OperationNotAllowed,
303  "Mutual Information (X,Y) with P(X)=0 or P(Y)=0 "
304  "and P(X,Y)>0");
305  }
306 
307  res += vXY * (log2(vXY) - log2(vX) - log2(vY));
308  }
309  }
310 
311  delete pXY;
312 
313  return res;
314  }
virtual Potential< GUM_SCALAR > * _unnormalizedJointPosterior(const NodeSet &set)=0
returns a fresh unnormalized joint posterior of a given set of variables
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
#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:

◆ isDone()

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isDone ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a done state

The inference object is in a done state when the posteriors can be retrieved without performing a new inference, i.e., all the heavy computations have already been performed. Typically, in a junction tree algorithm, this corresponds to a situation in which all the messages needed in the JT have been computed and sent.

Definition at line 96 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::Done.

Referenced by gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), gum::BayesNetInference< GUM_SCALAR >::makeInference(), gum::MarginalTargetedInference< GUM_SCALAR >::posterior(), and gum::BayesNetInference< GUM_SCALAR >::prepareInference().

96  {
97  return (__state == StateOfInference::Done);
98  }
StateOfInference __state
the current state of the inference (outdated/ready/done)
+ Here is the caller graph for this function:

◆ isInferenceDone()

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isInferenceDone ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a InferenceDone state

Definition at line 89 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::Done.

89  {
90  return (__state == StateOfInference::Done);
91  }
StateOfInference __state
the current state of the inference (outdated/ready/done)

◆ isInferenceOutdatedBNPotentials()

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNPotentials ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a OutdatedBNPotential state

Definition at line 83 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNPotentials.

84  {
86  }
StateOfInference __state
the current state of the inference (outdated/ready/done)

◆ isInferenceOutdatedBNStructure()

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isInferenceOutdatedBNStructure ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a OutdatedBNStructure state

Definition at line 76 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

Referenced by gum::BayesNetInference< GUM_SCALAR >::chgEvidence(), gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence(), and gum::BayesNetInference< GUM_SCALAR >::eraseEvidence().

77  {
79  }
StateOfInference __state
the current state of the inference (outdated/ready/done)
+ Here is the caller graph for this function:

◆ isInferenceReady()

template<typename GUM_SCALAR >
INLINE bool gum::BayesNetInference< GUM_SCALAR >::isInferenceReady ( ) const
finalvirtualnoexceptinherited

returns whether the inference object is in a ready state

Definition at line 70 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state, and gum::BayesNetInference< GUM_SCALAR >::ReadyForInference.

Referenced by gum::SamplingInference< GUM_SCALAR >::_onStateChanged(), gum::BayesNetInference< GUM_SCALAR >::makeInference(), and gum::BayesNetInference< GUM_SCALAR >::prepareInference().

70  {
72  }
StateOfInference __state
the current state of the inference (outdated/ready/done)
+ Here is the caller graph for this function:

◆ isJointTarget()

template<typename GUM_SCALAR >
INLINE bool gum::JointTargetedInference< GUM_SCALAR >::isJointTarget ( const NodeSet vars) const
finalvirtual

return true if target is a joint target.

Definition at line 70 of file jointTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::JointTargetedInference< GUM_SCALAR >::__joint_targets, and GUM_ERROR.

71  {
72  if (this->__bn == nullptr)
73  GUM_ERROR(NullElement,
74  "No Bayes net has been assigned to the "
75  "inference algorithm");
76 
77  const auto& dag = this->__bn->dag();
78  for (const auto var : vars) {
79  if (!dag.exists(var)) {
80  GUM_ERROR(UndefinedElement, var << " is not a NodeId in the bn");
81  }
82  }
83 
84  return __joint_targets.contains(vars);
85  }
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
Set< NodeSet > __joint_targets
the set of joint targets
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ isTarget() [1/2]

template<typename GUM_SCALAR >
INLINE bool gum::MarginalTargetedInference< GUM_SCALAR >::isTarget ( NodeId  node) const
finalvirtualinherited

return true if variable is a (marginal) target

Definition at line 76 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::MarginalTargetedInference< GUM_SCALAR >::__targets, gum::Set< Key, Alloc >::contains(), and GUM_ERROR.

Referenced by gum::MarginalTargetedInference< GUM_SCALAR >::isTarget(), gum::MarginalTargetedInference< GUM_SCALAR >::posterior(), and gum::JointTargetedInference< GUM_SCALAR >::posterior().

76  {
77  // check that the variable belongs to the bn
78  if (this->__bn == nullptr)
79  GUM_ERROR(NullElement,
80  "No Bayes net has been assigned to the "
81  "inference algorithm");
82  if (!this->__bn->dag().exists(node)) {
83  GUM_ERROR(UndefinedElement, node << " is not a NodeId in the bn");
84  }
85 
86  return __targets.contains(node);
87  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
NodeSet __targets
the set of marginal targets
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
#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:

◆ isTarget() [2/2]

template<typename GUM_SCALAR >
INLINE bool gum::MarginalTargetedInference< GUM_SCALAR >::isTarget ( const std::string &  nodeName) const
finalvirtualinherited

return true if variable is a (marginal) target

Definition at line 91 of file marginalTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, and gum::MarginalTargetedInference< GUM_SCALAR >::isTarget().

92  {
93  return isTarget(this->__bn->idFromName(nodeName));
94  }
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
+ Here is the call graph for this function:

◆ jointMutualInformation() [1/2]

template<typename GUM_SCALAR >
GUM_SCALAR gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation ( const NodeSet targets)

Mutual information between targets.

See also
https://en.wikipedia.org/wiki/Interaction_information
Parameters
targetsthe NodeSet of the targeted variables

Definition at line 394 of file jointTargetedInference_tpl.h.

References gum::Instantiation::add(), gum::JointTargetedInference< GUM_SCALAR >::addJointTarget(), gum::BayesNetInference< GUM_SCALAR >::BN(), gum::Set< Key, Alloc >::clear(), gum::Instantiation::end(), gum::BayesNetInference< GUM_SCALAR >::eraseAllEvidence(), gum::JointTargetedInference< GUM_SCALAR >::eraseAllTargets(), GUM_ERROR, gum::Instantiation::inc(), gum::Set< Key, Alloc >::insert(), gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), gum::BayesNetInference< GUM_SCALAR >::makeInference(), gum::Instantiation::nbrDim(), gum::Instantiation::setFirst(), gum::Set< Key, Alloc >::size(), gum::Instantiation::val(), and gum::Instantiation::variable().

Referenced by gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation().

395  {
396  const auto& bn = this->BN();
397  const Size siz = targets.size();
398  if (siz <= 1) {
399  GUM_ERROR(InvalidArgument,
400  "jointMutualInformation needs at least 2 variables (targets="
401  << targets << ")");
402  }
403 
404  this->eraseAllTargets();
405  this->eraseAllEvidence();
406  this->addJointTarget(targets);
407  this->makeInference();
408  const auto po = this->jointPosterior(targets);
409 
410  gum::Instantiation caracteristic;
411  gum::Instantiation variables;
412  for (const auto nod : targets) {
413  const auto& var = bn.variable(nod);
414  auto pv = new gum::RangeVariable(var.name(), "", 0, 1);
415  caracteristic.add(*pv);
416  variables.add(var);
417  }
418 
419  Set< const DiscreteVariable* > sov;
420 
421  const GUM_SCALAR start = (siz % 2 == 0) ? GUM_SCALAR(-1.0) : GUM_SCALAR(1.0);
422  GUM_SCALAR sign;
423  GUM_SCALAR res = GUM_SCALAR(0.0);
424 
425  caracteristic.setFirst();
426  for (caracteristic.inc(); !caracteristic.end(); caracteristic.inc()) {
427  sov.clear();
428  sign = start;
429  for (Idx i = 0; i < caracteristic.nbrDim(); i++) {
430  if (caracteristic.val(i) == 1) {
431  sign = -sign;
432  sov.insert(&variables.variable(i));
433  }
434  }
435  res += sign * po.margSumIn(sov).entropy();
436  }
437 
438  for (Idx i = 0; i < caracteristic.nbrDim(); i++) {
439  delete &caracteristic.variable(i);
440  }
441 
442  return res;
443  }
virtual void eraseAllTargets()
Clear all previously defined targets (marginal and joint targets)
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...
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Idx val(Idx i) const
Returns the current value of the variable at position i.
virtual void makeInference() final
perform the heavy computations needed to compute the targets&#39; posteriors
void inc()
Operator increment.
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.
Defines a discrete random variable over an integer interval.
Definition: rangeVariable.h:54
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
void setFirst()
Assign the first values to the tuple of the Instantiation.
virtual const NodeSet & targets() const noexcept final
returns the list of marginal targets
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
bool end() const
Returns true if the Instantiation reached the end.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ jointMutualInformation() [2/2]

template<typename GUM_SCALAR >
GUM_SCALAR gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation ( const std::vector< std::string > &  targets)

Mutual information between targets.

See also
https://en.wikipedia.org/wiki/Interaction_information
Parameters
targetsthe vector of std::string of the targeted variables

Definition at line 446 of file jointTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), gum::Set< Key, Alloc >::insert(), and gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation().

447  {
448  const auto& bn = this->BN();
449 
450  gum::NodeSet targetsId;
451  for (const auto& targetname : targets) {
452  targetsId.insert(bn.idFromName(targetname));
453  }
454 
455  return jointMutualInformation(targetsId);
456  }
GUM_SCALAR jointMutualInformation(const NodeSet &targets)
Mutual information between targets.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
+ Here is the call graph for this function:

◆ jointPosterior()

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::JointTargetedInference< GUM_SCALAR >::jointPosterior ( const NodeSet nodes)
finalvirtual

Compute the joint posterior of a set of nodes.

Returns
a const ref to the posterior joint probability of the set of nodes.
Parameters
nodesthe set of nodes whose posterior joint probability is wanted
Warning
for efficiency reasons, the potential is stored into the inference engine and is returned by reference. In order to ensure that the potential may still exist even if the Inference object is destroyed, the user has to copy it explicitly.
prepareInference and makeInference may be applied if needed.
Exceptions
UndefinedElementif nodes is not in the targets

Definition at line 212 of file jointTargetedInference_tpl.h.

References gum::JointTargetedInference< GUM_SCALAR >::__joint_targets, gum::JointTargetedInference< GUM_SCALAR >::_jointPosterior(), GUM_ERROR, gum::BayesNetInference< GUM_SCALAR >::isDone(), gum::Set< Key, Alloc >::isSubsetOf(), and gum::BayesNetInference< GUM_SCALAR >::makeInference().

Referenced by gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation(), and gum::JointTargetedInference< GUM_SCALAR >::posterior().

212  {
213  // try to get the smallest set of targets that contains "nodes"
214  NodeSet set;
215  bool found_exact_target = false;
216 
217  if (__joint_targets.contains(nodes)) {
218  set = nodes;
219  found_exact_target = true;
220  } else {
221  for (const auto& target : __joint_targets) {
222  if (nodes.isSubsetOf(target)) {
223  set = target;
224  break;
225  }
226  }
227  }
228 
229  if (set.empty()) {
230  GUM_ERROR(UndefinedElement,
231  " no joint target containing " << nodes << " could be found among "
232  << __joint_targets);
233  }
234 
235  if (!this->isDone()) { this->makeInference(); }
236 
237  if (found_exact_target)
238  return _jointPosterior(nodes);
239  else
240  return _jointPosterior(nodes, set);
241  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
virtual void makeInference() final
perform the heavy computations needed to compute the targets&#39; posteriors
virtual bool isDone() const noexcept final
returns whether the inference object is in a done state
virtual const Potential< GUM_SCALAR > & _jointPosterior(const NodeSet &set)=0
asks derived classes for the joint posterior of a declared target set
Set< NodeSet > __joint_targets
the set of joint targets
#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:

◆ jointTargets()

template<typename GUM_SCALAR >
INLINE const Set< NodeSet > & gum::JointTargetedInference< GUM_SCALAR >::jointTargets ( ) const
finalvirtualnoexcept

returns the list of joint targets

returns the list of target sets

Definition at line 193 of file jointTargetedInference_tpl.h.

References gum::JointTargetedInference< GUM_SCALAR >::__joint_targets.

193  {
194  return __joint_targets;
195  }
Set< NodeSet > __joint_targets
the set of joint targets

◆ makeInference()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::makeInference ( )
finalvirtualinherited

perform the heavy computations needed to compute the targets' posteriors

In a Junction tree propagation scheme, for instance, the heavy computations are those of the messages sent in the JT. This is precisely what makeInference should compute. Later, the computations of the posteriors can be done "lightly" by multiplying and projecting those messages.

Definition at line 711 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::_makeInference(), gum::BayesNetInference< GUM_SCALAR >::Done, gum::BayesNetInference< GUM_SCALAR >::isDone(), gum::BayesNetInference< GUM_SCALAR >::isInferenceReady(), and gum::BayesNetInference< GUM_SCALAR >::prepareInference().

Referenced by gum::LoopySamplingInference< GUM_SCALAR, APPROX >::_makeInference(), gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::disturbBN(), gum::MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(), gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), gum::JointTargetedInference< GUM_SCALAR >::jointMutualInformation(), gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), and gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

711  {
712  if (isDone()) { return; }
713 
714  if (!isInferenceReady()) { prepareInference(); }
715 
716  _makeInference();
717 
719  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
virtual void _makeInference()=0
called when the inference has to be performed effectively
virtual void prepareInference() final
prepare the internal inference structures for the next inference
virtual bool isDone() const noexcept final
returns whether the inference object is in a done state
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready state
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nbrEvidence()

template<typename GUM_SCALAR >
INLINE Size gum::BayesNetInference< GUM_SCALAR >::nbrEvidence ( ) const
finalvirtualinherited

returns the number of evidence entered into the Bayesian network

Definition at line 620 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__evidence.

620  {
621  return __evidence.size();
622  }
NodeProperty< const Potential< GUM_SCALAR > *> __evidence
the set of evidence entered into the network

◆ nbrHardEvidence()

template<typename GUM_SCALAR >
INLINE Size gum::BayesNetInference< GUM_SCALAR >::nbrHardEvidence ( ) const
finalvirtualinherited

returns the number of hard evidence entered into the Bayesian network

Definition at line 627 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__hard_evidence_nodes, and gum::Set< Key, Alloc >::size().

627  {
628  return __hard_evidence_nodes.size();
629  }
NodeSet __hard_evidence_nodes
the set of nodes that received hard evidence
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
+ Here is the call graph for this function:

◆ nbrJointTargets()

template<typename GUM_SCALAR >
INLINE Size gum::JointTargetedInference< GUM_SCALAR >::nbrJointTargets ( ) const
finalvirtualnoexcept

returns the number of joint targets

returns the number of target sets

Definition at line 199 of file jointTargetedInference_tpl.h.

References gum::JointTargetedInference< GUM_SCALAR >::__joint_targets.

200  {
201  return __joint_targets.size();
202  }
Set< NodeSet > __joint_targets
the set of joint targets

◆ nbrSoftEvidence()

template<typename GUM_SCALAR >
INLINE Size gum::BayesNetInference< GUM_SCALAR >::nbrSoftEvidence ( ) const
finalvirtualinherited

returns the number of soft evidence entered into the Bayesian network

Definition at line 634 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes, and gum::Set< Key, Alloc >::size().

634  {
635  return __soft_evidence_nodes.size();
636  }
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
+ Here is the call graph for this function:

◆ nbrTargets()

template<typename GUM_SCALAR >
INLINE const Size gum::MarginalTargetedInference< GUM_SCALAR >::nbrTargets ( ) const
finalvirtualnoexceptinherited

returns the number of marginal targets

Definition at line 218 of file marginalTargetedInference_tpl.h.

References gum::MarginalTargetedInference< GUM_SCALAR >::__targets, and gum::Set< Key, Alloc >::size().

219  {
220  return __targets.size();
221  }
NodeSet __targets
the set of marginal targets
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
+ Here is the call graph for this function:

◆ posterior() [1/2]

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::JointTargetedInference< GUM_SCALAR >::posterior ( NodeId  node)
finalvirtual

Computes and returns the posterior of a node.

Returns
a const ref to the posterior probability of the node.
Parameters
nodethe node for which we need a posterior probability
Warning
for efficiency reasons, the potential is stored into the inference engine and is returned by reference. In order to ensure that the potential may still exist even if the Inference object is destroyed, the user has to copy it explicitly.
prepareInference and makeInference may be applied if needed by the posterior method.
Exceptions
UndefinedElementif node is not in the set of targets

Reimplemented from gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 247 of file jointTargetedInference_tpl.h.

References gum::MarginalTargetedInference< GUM_SCALAR >::isTarget(), gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), and gum::MarginalTargetedInference< GUM_SCALAR >::posterior().

Referenced by gum::MCBayesNetGenerator< GUM_SCALAR, ICPTGenerator, ICPTDisturber >::disturbBN(), and gum::JointTargetedInference< GUM_SCALAR >::posterior().

247  {
248  if (this->isTarget(node))
250  else
251  return jointPosterior(NodeSet{node});
252  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
virtual bool isTarget(NodeId node) const final
return true if variable is a (marginal) target
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.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ posterior() [2/2]

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & gum::JointTargetedInference< GUM_SCALAR >::posterior ( const std::string &  nodeName)
finalvirtual

Computes and returns the posterior of a node.

Returns
a const ref to the posterior probability of the node.
Parameters
nodethe node for which we need a posterior probability
Warning
for efficiency reasons, the potential is stored into the inference engine and is returned by reference. In order to ensure that the potential may still exist even if the Inference object is destroyed, the user has to copy it explicitly.
prepareInference and makeInference may be applied if needed by the posterior method.
Exceptions
UndefinedElementif node is not in the set of targets

Reimplemented from gum::MarginalTargetedInference< GUM_SCALAR >.

Definition at line 257 of file jointTargetedInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::BN(), and gum::JointTargetedInference< GUM_SCALAR >::posterior().

257  {
258  return posterior(this->BN().idFromName(nodeName));
259  }
virtual const Potential< GUM_SCALAR > & posterior(NodeId node) final
Computes and returns the posterior of a node.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
+ Here is the call graph for this function:

◆ prepareInference()

template<typename GUM_SCALAR >
INLINE void gum::BayesNetInference< GUM_SCALAR >::prepareInference ( )
finalvirtualinherited

prepare the internal inference structures for the next inference

Definition at line 692 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::__state, gum::BayesNetInference< GUM_SCALAR >::_updateOutdatedBNPotentials(), gum::BayesNetInference< GUM_SCALAR >::_updateOutdatedBNStructure(), GUM_ERROR, gum::BayesNetInference< GUM_SCALAR >::isDone(), gum::BayesNetInference< GUM_SCALAR >::isInferenceReady(), gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure, and gum::BayesNetInference< GUM_SCALAR >::ReadyForInference.

Referenced by gum::BayesNetInference< GUM_SCALAR >::makeInference(), and gum::SamplingInference< GUM_SCALAR >::samplingBN().

692  {
693  if (isInferenceReady() || isDone()) { return; }
694 
695  if (__bn == nullptr)
696  GUM_ERROR(NullElement,
697  "No Bayes net has been assigned to the "
698  "inference algorithm");
699 
702  else
704 
706  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
StateOfInference __state
the current state of the inference (outdated/ready/done)
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void _updateOutdatedBNPotentials()=0
prepares inference when the latter is in OutdatedBNPotentials state
virtual bool isDone() const noexcept final
returns whether the inference object is in a done state
virtual void _updateOutdatedBNStructure()=0
prepares inference when the latter is in OutdatedBNStructure state
virtual bool isInferenceReady() const noexcept final
returns whether the inference object is in a ready 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:

◆ setBN()

template<typename GUM_SCALAR >
void gum::BayesNetInference< GUM_SCALAR >::setBN ( const IBayesNet< GUM_SCALAR > *  bn)
virtualinherited

assigns a new BN to the inference engine

Assigns a new BN to the BayesNetInference engine and sends messages to the descendants of BayesNetInference to inform them that the BN has changed.

Warning
By default, all the nodes of the Bayes net are targets.
note that, by aGrUM's rule, the bn is not copied into the inference engine but only referenced.

Definition at line 132 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__bn, gum::BayesNetInference< GUM_SCALAR >::__computeDomainSizes(), gum::BayesNetInference< GUM_SCALAR >::__setState(), gum::BayesNetInference< GUM_SCALAR >::_onBayesNetChanged(), gum::BayesNetInference< GUM_SCALAR >::clear(), and gum::BayesNetInference< GUM_SCALAR >::OutdatedBNStructure.

132  {
133  clear();
134  __bn = bn;
136  _onBayesNetChanged(bn);
138  }
virtual void __setState(const StateOfInference state) final
set the state of the inference engine and call the notification _onStateChanged when necessary (i...
virtual void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn)=0
fired after a new Bayes net has been assigned to the engine
const IBayesNet< GUM_SCALAR > * __bn
the Bayes net on which we perform inferences
virtual void clear()
clears all the data structures allocated for the last inference
void __computeDomainSizes()
computes the domain sizes of the random variables
+ Here is the call graph for this function:

◆ softEvidenceNodes()

template<typename GUM_SCALAR >
INLINE const NodeSet & gum::BayesNetInference< GUM_SCALAR >::softEvidenceNodes ( ) const
inherited

returns the set of nodes with soft evidence

the set of nodes that received soft evidence

Definition at line 658 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__soft_evidence_nodes.

Referenced by gum::SamplingInference< GUM_SCALAR >::contextualize().

658  {
659  return __soft_evidence_nodes;
660  }
NodeSet __soft_evidence_nodes
the set of nodes that received soft evidence
+ Here is the caller graph for this function:

◆ state()

template<typename GUM_SCALAR >
INLINE BayesNetInference< GUM_SCALAR >::StateOfInference gum::BayesNetInference< GUM_SCALAR >::state ( ) const
finalvirtualnoexceptinherited

returns the state of the inference engine

Definition at line 104 of file BayesNetInference_tpl.h.

References gum::BayesNetInference< GUM_SCALAR >::__state.

Referenced by gum::BayesNetInference< GUM_SCALAR >::__setState().

104  {
105  return __state;
106  }
StateOfInference __state
the current state of the inference (outdated/ready/done)
+ Here is the caller graph for this function:

◆ targets()

template<typename GUM_SCALAR >
INLINE const NodeSet & gum::MarginalTargetedInference< GUM_SCALAR >::targets ( ) const
finalvirtualnoexceptinherited

returns the list of marginal targets

Definition at line 211 of file marginalTargetedInference_tpl.h.

References gum::MarginalTargetedInference< GUM_SCALAR >::__targets.

Referenced by gum::SamplingInference< GUM_SCALAR >::contextualize().

212  {
213  return __targets;
214  }
NodeSet __targets
the set of marginal targets
+ Here is the caller graph for this function:

◆ VI()

template<typename GUM_SCALAR >
INLINE GUM_SCALAR gum::JointTargetedInference< GUM_SCALAR >::VI ( NodeId  X,
NodeId  Y 
)

Variation of information between X and Y.

See also
http://en.wikipedia.org/wiki/Variation_of_information
Warning
Due to limitation of joint, may not be able to compute this value
Exceptions
OperationNotAllowedin these cases
See also
http://en.wikipedia.org/wiki/Variation_of_information
Warning
Due to limitation of , may not be able to compute this value
Exceptions
OperationNotAllowedin these cases

Definition at line 324 of file jointTargetedInference_tpl.h.

References gum::MarginalTargetedInference< GUM_SCALAR >::H(), and gum::JointTargetedInference< GUM_SCALAR >::I().

324  {
325  return this->H(X) + this->H(Y) - 2 * I(X, Y);
326  }
GUM_SCALAR I(NodeId X, NodeId Y)
Mutual information between X and Y.
virtual GUM_SCALAR H(NodeId X) final
Entropy Compute Shanon&#39;s entropy of a node given the observation.
+ Here is the call graph for this function:

Member Data Documentation

◆ __joint_targets


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