aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
jointTargetedInference.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief This file contains the abstract inference class definition for
25  * computing (incrementally) joint posteriors.
26  *
27  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
28  */
29 
30 #ifndef GUM_BAYES_NET_JOINT_TARGETED_INFERENCE_H
31 #define GUM_BAYES_NET_JOINT_TARGETED_INFERENCE_H
32 
33 
34 #include <agrum/BN/inference/tools/marginalTargetedInference.h>
35 #include <agrum/agrum.h>
36 
37 
38 namespace gum {
39 
40 
41  /**
42  * @class JointTargetedInference jointTargetedInference.h
43  * <agrum/BN/inference/jointTargetedInference.h>
44  * @brief A generic class for the computation of (possibly incrementally)
45  * joint posteriors
46  * @ingroup bn_group
47  *
48  * The goal of this class is to take care of the joint targets used
49  * for computing joint posteriors. The JointTargetedInference class
50  * inherits from Inference that takes care of handling both evidence and
51  * the current state of the inference and from MarginalTargetedInference
52  * for the handling of marginal targets. Note that the JointTargetedInference
53  * is designed to be used in incremental inference engines.
54  */
55  template < typename GUM_SCALAR >
56  class JointTargetedInference: public MarginalTargetedInference< GUM_SCALAR > {
57  public:
58  // ############################################################################
59  /// @name Constructors / Destructors
60  // ############################################################################
61  /// @{
62 
63  /// default constructor
64  /** @warning note that, by aGrUM's rule, the BN is not copied but only
65  * referenced by the inference algorithm. */
66  explicit JointTargetedInference(const IBayesNet< GUM_SCALAR >* bn);
67 
68  /// destructor
69  virtual ~JointTargetedInference();
70 
71  /// @}
72 
73 
74  // ############################################################################
75  /// @name Probability computations
76  // ############################################################################
77  /// @{
78 
79  /// Compute the joint posterior of a set of nodes.
80  /**
81  * @returns a const ref to the posterior joint probability of the set of
82  * nodes.
83  * @param nodes the set of nodes whose posterior joint probability is wanted
84  *
85  * @warning for efficiency reasons, the potential is stored into the
86  * inference engine and is returned by reference. In order to ensure
87  * that the potential may still exist even if the Inference object is
88  * destroyed, the user has to copy it explicitly.
89  *
90  * @warning prepareInference and makeInference may be applied if needed.
91  *
92  * @throw UndefinedElement if nodes is not in the targets
93  */
94  virtual const Potential< GUM_SCALAR >& jointPosterior(const NodeSet& nodes) final;
95 
96  /// Computes and returns the posterior of a node.
97  /**
98  * @returns a const ref to the posterior probability of the node.
99  * @param node the node for which we need a posterior probability
100  *
101  * @warning for efficiency reasons, the potential is stored into the
102  * inference engine and is returned by reference. In order to ensure
103  * that the potential may still exist even if the Inference object is
104  * destroyed, the user has to copy it explicitly.
105  *
106  * @warning prepareInference and makeInference may be applied if needed by
107  * the posterior method.
108  *
109  * @throw UndefinedElement if node is not in the set of targets
110  */
111  virtual const Potential< GUM_SCALAR >& posterior(NodeId node) final;
112 
113  /// Computes and returns the posterior of a node.
114  /**
115  * @returns a const ref to the posterior probability of the node.
116  * @param node the node for which we need a posterior probability
117  *
118  * @warning for efficiency reasons, the potential is stored into the
119  * inference engine and is returned by reference. In order to ensure
120  * that the potential may still exist even if the Inference object is
121  * destroyed, the user has to copy it explicitly.
122  *
123  * @warning prepareInference and makeInference may be applied if needed by
124  * the posterior method.
125  *
126  * @throw UndefinedElement if node is not in the set of targets
127  */
128  virtual const Potential< GUM_SCALAR >& posterior(const std::string& nodeName) final;
129  /// @}
130 
131 
132  // ############################################################################
133  /// @name Targets
134  // ############################################################################
135  /// @{
136 
137  /// Clear all previously defined targets (marginal and joint targets)
138  /**
139  * Clear all previously defined targets. As a result, no posterior can be
140  * computed (since we can only compute the posteriors of the marginal or
141  * joint
142  * targets that have been added by the user).
143  */
144  virtual void eraseAllTargets();
145 
146  /// Clear all previously defined joint targets
147  virtual void eraseAllJointTargets() final;
148 
149  /// Clear all the previously defined marginal targets
150  virtual void eraseAllMarginalTargets() final;
151 
152  /// Add a set of nodes as a new joint target. As a collateral effect, every
153  /// node is added as a marginal target.
154  /**
155  * @throw UndefinedElement if some node(s) do not belong to the Bayes net
156  */
157  virtual void addJointTarget(const NodeSet& joint_target) final;
158 
159  /// removes an existing joint target
160  /** @warning If the joint target does not already exist, the method does
161  * nothing. In particular, it does not raise any exception. */
162  virtual void eraseJointTarget(const NodeSet& joint_target) final;
163 
164  /// return true if target is a joint target.
165  virtual bool isJointTarget(const NodeSet& vars) const final;
166 
167  /// returns the list of joint targets
168  virtual const Set< NodeSet >& jointTargets() const noexcept final;
169 
170  /// returns the number of joint targets
171  virtual Size nbrJointTargets() const noexcept final;
172  /// @}
173 
174  /**
175  * Create a gum::Potential for P(joint targets|evs) (for all instanciation of
176  * targets
177  * and evs)
178  *
179  * @warning If some evs are d-separated, they are not included in the Potential
180  *
181  * @param targets the NodeSet of the targeted variables
182  * @param evs the NodeSet of observed variables
183  * @return a Potential
184  */
186 
187  /**
188  * Create a gum::Potential for P(joint targets|evs) (for all instanciation of
189  * targets
190  * and evs)
191  *
192  * @warning If some evs are d-separated, they are not included in the Potential
193  *
194  * @param targets the vector of std::string of the targeted variables
195  * @param evs the vector of std::string of observed variables
196  * @return a Potential
197  */
199  const std::vector< std::string >& evs);
200 
201  // ############################################################################
202  /// @name Information Theory related functions
203  // ############################################################################
204  /// @{
205 
206  /** Mutual information between X and Y
207  * @see http://en.wikipedia.org/wiki/Mutual_information
208  *
209  * @warning Due to limitation of @ref joint, may not be able to compute
210  * this value
211  * @throw OperationNotAllowed in these cases
212  */
213  GUM_SCALAR I(NodeId X, NodeId Y);
214  /** Mutual information between X and Y
215  * @see http://en.wikipedia.org/wiki/Mutual_information
216  *
217  * @warning Due to limitation of @ref joint, may not be able to compute
218  * this value
219  * @throw OperationNotAllowed in these cases
220  */
221  GUM_SCALAR I(const std::string& Xname, const std::string& Yname);
222 
223  /** Variation of information between X and Y
224  * @see http://en.wikipedia.org/wiki/Variation_of_information
225  *
226  * @warning Due to limitation of @ref joint, may not be able to compute
227  * this value
228  * @throw OperationNotAllowed in these cases
229  */
230  GUM_SCALAR VI(NodeId X, NodeId Y);
231 
232  /** Variation of information between X and Y
233  * @see http://en.wikipedia.org/wiki/Variation_of_information
234  *
235  * @warning Due to limitation of @ref joint, may not be able to compute
236  * this value
237  * @throw OperationNotAllowed in these cases
238  */
239  GUM_SCALAR VI(const std::string& Xname, const std::string& Yname);
240 
241  /** Mutual information between targets
242  * @see https://en.wikipedia.org/wiki/Interaction_information
243  * @param targets the NodeSet of the targeted variables
244  */
245  GUM_SCALAR jointMutualInformation(const NodeSet& targets);
246 
247  /** Mutual information between targets
248  * @see https://en.wikipedia.org/wiki/Interaction_information
249  * @param targets the vector of std::string of the targeted variables
250  */
251  GUM_SCALAR jointMutualInformation(const std::vector< std::string >& targets);
252 
253  /// @}
254 
255 
256  protected:
257  /// fired after a new Bayes net has been assigned to the engine
258  virtual void onModelChanged_(const GraphicalModel* bn);
259 
260  /// fired after a new joint target is inserted
261  /** @param set The set of target variable's ids. */
262  virtual void onJointTargetAdded_(const NodeSet& set) = 0;
263 
264  /// fired before a joint target is removed
265  /** @param set The set of target variable's ids. */
266  virtual void onJointTargetErased_(const NodeSet& set) = 0;
267 
268  /// fired before a all the marginal and joint targets are removed
269  virtual void onAllTargetsErased_() = 0;
270 
271  /// fired before a all the joint targets are removed
272  virtual void onAllJointTargetsErased_() = 0;
273 
274 
275  /// asks derived classes for the joint posterior of a declared target set
276  /** @param set The set of ids of the variables whose joint posterior is
277  * looked for. */
278  virtual const Potential< GUM_SCALAR >& jointPosterior_(const NodeSet& set) = 0;
279 
280  /** @brief asks derived classes for the joint posterior of a set of
281  * variables not declared as a joint target
282  *
283  * @param wanted_target The set of ids of the variables whose joint
284  * posterior is looked for.
285  * @param declared_target the joint target declared by the user that
286  * contains set */
287  virtual const Potential< GUM_SCALAR >& jointPosterior_(const NodeSet& wanted_target,
288  const NodeSet& declared_target)
289  = 0;
290 
291  /** @brief returns a fresh unnormalized joint posterior of
292  * a given set of variables
293  * @param set The set of ids of the variables whose joint posterior is
294  * looked for. */
296 
297  /// returns a fresh potential equal to P(argument,evidence)
299 
300 
301  private:
302  /// the set of joint targets
304  };
305 
306 
307 } // namespace gum
308 
309 
310 #include <agrum/BN/inference/tools/jointTargetedInference_tpl.h>
311 
312 
313 #endif // GUM_BAYES_NET_JOINT_TARGETED_INFERENCE_H
virtual void eraseAllMarginalTargets() final
Clear all the previously defined marginal targets.
virtual void eraseAllTargets()
Clear all previously defined targets (marginal and joint targets)
virtual Potential< GUM_SCALAR > * unnormalizedJointPosterior_(const NodeSet &set)=0
returns a fresh unnormalized joint posterior of a given set of variables
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 ~JointTargetedInference()
destructor
JointTargetedInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
GUM_SCALAR I(NodeId X, NodeId Y)
Mutual information between X and Y.
virtual void onJointTargetAdded_(const NodeSet &set)=0
fired after a new joint target is inserted
GUM_SCALAR VI(const std::string &Xname, const std::string &Yname)
Variation of information between X and Y.
Set< NodeSet > _joint_targets_
the set of joint targets
virtual const Potential< GUM_SCALAR > & posterior(const std::string &nodeName) final
Computes and returns the posterior of a node.
virtual bool isJointTarget(const NodeSet &vars) const final
return true if target is a joint target.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual const Potential< GUM_SCALAR > & jointPosterior_(const NodeSet &set)=0
asks derived classes for the joint posterior of a declared target set
virtual void eraseJointTarget(const NodeSet &joint_target) final
removes an existing joint target
virtual void onAllTargetsErased_()=0
fired before a all the marginal and joint targets are removed
virtual void onAllJointTargetsErased_()=0
fired before a all the joint targets are removed
GUM_SCALAR jointMutualInformation(const std::vector< std::string > &targets)
Mutual information between targets.
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) ...
virtual void eraseAllJointTargets() final
Clear all previously defined joint targets.
virtual const Set< NodeSet > & jointTargets() const noexcept final
returns the list of joint targets
virtual const Potential< GUM_SCALAR > & jointPosterior(const NodeSet &nodes) final
Compute the joint posterior of a set of nodes.
GUM_SCALAR I(const std::string &Xname, const std::string &Yname)
Mutual information between X and Y.
virtual void onModelChanged_(const GraphicalModel *bn)
fired after a new Bayes net has been assigned to the engine
virtual Potential< GUM_SCALAR > * unnormalizedJointPosterior_(NodeId id)=0
returns a fresh potential equal to P(argument,evidence)
virtual const Potential< GUM_SCALAR > & posterior(NodeId node) final
Computes and returns the posterior of a node.
virtual Size nbrJointTargets() const noexcept final
returns the number of joint targets
virtual void onJointTargetErased_(const NodeSet &set)=0
fired before a joint target is removed
GUM_SCALAR VI(NodeId X, NodeId Y)
Variation of information between X and Y.
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 ...