aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
jointTargetedInference.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 >&
95  jointPosterior(const NodeSet& nodes) final;
96 
97  /// Computes and returns the posterior of a node.
98  /**
99  * @returns a const ref to the posterior probability of the node.
100  * @param node the node for which we need a posterior probability
101  *
102  * @warning for efficiency reasons, the potential is stored into the
103  * inference engine and is returned by reference. In order to ensure
104  * that the potential may still exist even if the Inference object is
105  * destroyed, the user has to copy it explicitly.
106  *
107  * @warning prepareInference and makeInference may be applied if needed by
108  * the posterior method.
109  *
110  * @throw UndefinedElement if node is not in the set of targets
111  */
112  virtual const Potential< GUM_SCALAR >& posterior(NodeId node) final;
113 
114  /// Computes and returns the posterior of a node.
115  /**
116  * @returns a const ref to the posterior probability of the node.
117  * @param node the node for which we need a posterior probability
118  *
119  * @warning for efficiency reasons, the potential is stored into the
120  * inference engine and is returned by reference. In order to ensure
121  * that the potential may still exist even if the Inference object is
122  * destroyed, the user has to copy it explicitly.
123  *
124  * @warning prepareInference and makeInference may be applied if needed by
125  * the posterior method.
126  *
127  * @throw UndefinedElement if node is not in the set of targets
128  */
129  virtual const Potential< GUM_SCALAR >&
130  posterior(const std::string& nodeName) final;
131  /// @}
132 
133 
134  // ############################################################################
135  /// @name Targets
136  // ############################################################################
137  /// @{
138 
139  /// Clear all previously defined targets (marginal and joint targets)
140  /**
141  * Clear all previously defined targets. As a result, no posterior can be
142  * computed (since we can only compute the posteriors of the marginal or
143  * joint
144  * targets that have been added by the user).
145  */
146  virtual void eraseAllTargets();
147 
148  /// Clear all previously defined joint targets
149  virtual void eraseAllJointTargets() final;
150 
151  /// Clear all the previously defined marginal targets
152  virtual void eraseAllMarginalTargets() final;
153 
154  /// Add a set of nodes as a new joint target. As a collateral effect, every
155  /// node is added as a marginal target.
156  /**
157  * @throw UndefinedElement if some node(s) do not belong to the Bayes net
158  */
159  virtual void addJointTarget(const NodeSet& joint_target) final;
160 
161  /// removes an existing joint target
162  /** @warning If the joint target does not already exist, the method does
163  * nothing. In particular, it does not raise any exception. */
164  virtual void eraseJointTarget(const NodeSet& joint_target) final;
165 
166  /// return true if target is a joint target.
167  virtual bool isJointTarget(const NodeSet& vars) const final;
168 
169  /// returns the list of joint targets
170  virtual const Set< NodeSet >& jointTargets() const noexcept final;
171 
172  /// returns the number of joint targets
173  virtual Size nbrJointTargets() const noexcept final;
174  /// @}
175 
176  /**
177  * Create a gum::Potential for P(joint targets|evs) (for all instanciation of
178  * targets
179  * and evs)
180  *
181  * @warning If some evs are d-separated, they are not included in the Potential
182  *
183  * @param targets the NodeSet of the targeted variables
184  * @param evs the NodeSet of observed variables
185  * @return a Potential
186  */
188  const NodeSet& evs);
189 
190  /**
191  * Create a gum::Potential for P(joint targets|evs) (for all instanciation of
192  * targets
193  * and evs)
194  *
195  * @warning If some evs are d-separated, they are not included in the Potential
196  *
197  * @param targets the vector of std::string of the targeted variables
198  * @param evs the vector of std::string of observed variables
199  * @return a Potential
200  */
203  const std::vector< std::string >& evs);
204 
205  // ############################################################################
206  /// @name Information Theory related functions
207  // ############################################################################
208  /// @{
209 
210  /** Mutual information between X and Y
211  * @see http://en.wikipedia.org/wiki/Mutual_information
212  *
213  * @warning Due to limitation of @ref joint, may not be able to compute
214  * this value
215  * @throw OperationNotAllowed in these cases
216  */
217  GUM_SCALAR I(NodeId X, NodeId Y);
218  /** Mutual information between X and Y
219  * @see http://en.wikipedia.org/wiki/Mutual_information
220  *
221  * @warning Due to limitation of @ref joint, may not be able to compute
222  * this value
223  * @throw OperationNotAllowed in these cases
224  */
225  GUM_SCALAR I(const std::string& Xname, const std::string& Yname);
226 
227  /** Variation of information between X and Y
228  * @see http://en.wikipedia.org/wiki/Variation_of_information
229  *
230  * @warning Due to limitation of @ref joint, may not be able to compute
231  * this value
232  * @throw OperationNotAllowed in these cases
233  */
234  GUM_SCALAR VI(NodeId X, NodeId Y);
235 
236  /** Variation of information between X and Y
237  * @see http://en.wikipedia.org/wiki/Variation_of_information
238  *
239  * @warning Due to limitation of @ref joint, may not be able to compute
240  * this value
241  * @throw OperationNotAllowed in these cases
242  */
243  GUM_SCALAR VI(const std::string& Xname, const std::string& Yname);
244 
245  /** Mutual information between targets
246  * @see https://en.wikipedia.org/wiki/Interaction_information
247  * @param targets the NodeSet of the targeted variables
248  */
249  GUM_SCALAR jointMutualInformation(const NodeSet& targets);
250 
251  /** Mutual information between targets
252  * @see https://en.wikipedia.org/wiki/Interaction_information
253  * @param targets the vector of std::string of the targeted variables
254  */
255  GUM_SCALAR jointMutualInformation(const std::vector< std::string >& targets);
256 
257  /// @}
258 
259 
260  protected:
261  /// fired after a new Bayes net has been assigned to the engine
262  virtual void onModelChanged_(const GraphicalModel* bn);
263 
264  /// fired after a new joint target is inserted
265  /** @param set The set of target variable's ids. */
266  virtual void onJointTargetAdded_(const NodeSet& set) = 0;
267 
268  /// fired before a joint target is removed
269  /** @param set The set of target variable's ids. */
270  virtual void onJointTargetErased_(const NodeSet& set) = 0;
271 
272  /// fired before a all the marginal and joint targets are removed
273  virtual void onAllTargetsErased_() = 0;
274 
275  /// fired before a all the joint targets are removed
276  virtual void onAllJointTargetsErased_() = 0;
277 
278 
279  /// asks derived classes for the joint posterior of a declared target set
280  /** @param set The set of ids of the variables whose joint posterior is
281  * looked for. */
282  virtual const Potential< GUM_SCALAR >& jointPosterior_(const NodeSet& set) = 0;
283 
284  /** @brief asks derived classes for the joint posterior of a set of
285  * variables not declared as a joint target
286  *
287  * @param wanted_target The set of ids of the variables whose joint
288  * posterior is looked for.
289  * @param declared_target the joint target declared by the user that
290  * contains set */
291  virtual const Potential< GUM_SCALAR >&
293  const NodeSet& declared_target)
294  = 0;
295 
296  /** @brief returns a fresh unnormalized joint posterior of
297  * a given set of variables
298  * @param set The set of ids of the variables whose joint posterior is
299  * looked for. */
300  virtual Potential< GUM_SCALAR >*
302  = 0;
303 
304  /// returns a fresh potential equal to P(argument,evidence)
306 
307 
308  private:
309  /// the set of joint targets
311  };
312 
313 
314 } // namespace gum
315 
316 
317 #include <agrum/BN/inference/tools/jointTargetedInference_tpl.h>
318 
319 
320 #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.
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:669
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.
Set< NodeSet > joint_targets__
the set of joint targets
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 ...