aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
jointTargetedMNInference.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_MARKOV_NET_JOINT_TARGETED_INFERENCE_H
31 #define GUM_MARKOV_NET_JOINT_TARGETED_INFERENCE_H
32 
33 
34 #include <agrum/MN/inference/tools/marginalTargetedMNInference.h>
35 #include <agrum/agrum.h>
36 
37 
38 namespace gum {
39 
40 
41  /**
42  * @class JointTargetedMNInference jointTargetedMNInference.h
43  * <agrum/MN/inference/jointTargetedMNInference.h>
44  * @brief A generic class for the computation of (possibly incrementally)
45  * joint posteriors
46  * @ingroup mn_group
47  *
48  * The goal of this class is to take care of the joint targets used
49  * for computing joint posteriors. The JointTargetedMNInference class
50  * inherits from Inference that takes care of handling both evidence and
51  * the current state of the inference and from MarginalTargetedMNInference
52  * for the handling of marginal targets. Note that the JointTargetedMNInference
53  * is designed to be used in incremental inference engines.
54  */
55  template < typename GUM_SCALAR >
58  public:
59  // ############################################################################
60  /// @name Constructors / Destructors
61  // ############################################################################
62  /// @{
63 
64  /// default constructor
65  /** @warning note that, by aGrUM's rule, the MN is not copied but only
66  * referenced by the inference algorithm. */
67  explicit JointTargetedMNInference(const IMarkovNet< GUM_SCALAR >* mn);
68 
69  /// destructor
70  virtual ~JointTargetedMNInference();
71 
72  /// @}
73 
74 
75  // ############################################################################
76  /// @name Probability computations
77  // ############################################################################
78  /// @{
79 
80  /// Compute the joint posterior of a set of nodes.
81  /**
82  * @returns a const ref to the posterior joint probability of the set of
83  * nodes.
84  * @param nodes the set of nodes whose posterior joint probability is wanted
85  *
86  * @warning for efficiency reasons, the potential is stored into the
87  * inference engine and is returned by reference. In order to ensure
88  * that the potential may still exist even if the Inference object is
89  * destroyed, the user has to copy it explicitly.
90  *
91  * @warning prepareInference and makeInference may be applied if needed.
92  *
93  * @throw UndefinedElement if nodes is not in the targets
94  */
95  virtual const Potential< GUM_SCALAR >&
96  jointPosterior(const NodeSet& nodes) final;
97 
98  /// Computes and returns the posterior of a node.
99  /**
100  * @returns a const ref to the posterior probability of the node.
101  * @param node the node for which we need a posterior probability
102  *
103  * @warning for efficiency reasons, the potential is stored into the
104  * inference engine and is returned by reference. In order to ensure
105  * that the potential may still exist even if the Inference object is
106  * destroyed, the user has to copy it explicitly.
107  *
108  * @warning prepareInference and makeInference may be applied if needed by
109  * the posterior method.
110  *
111  * @throw UndefinedElement if node is not in the set of targets
112  */
113  virtual const Potential< GUM_SCALAR >& posterior(NodeId node) final;
114 
115  /// Computes and returns the posterior of a node.
116  /**
117  * @returns a const ref to the posterior probability of the node.
118  * @param node the node for which we need a posterior probability
119  *
120  * @warning for efficiency reasons, the potential is stored into the
121  * inference engine and is returned by reference. In order to ensure
122  * that the potential may still exist even if the Inference object is
123  * destroyed, the user has to copy it explicitly.
124  *
125  * @warning prepareInference and makeInference may be applied if needed by
126  * the posterior method.
127  *
128  * @throw UndefinedElement if node is not in the set of targets
129  */
130  virtual const Potential< GUM_SCALAR >&
131  posterior(const std::string& nodeName) final;
132  /// @}
133 
134 
135  // ############################################################################
136  /// @name Targets
137  // ############################################################################
138  /// @{
139 
140  /// Clear all previously defined targets (marginal and joint targets)
141  /**
142  * Clear all previously defined targets. As a result, no posterior can be
143  * computed (since we can only compute the posteriors of the marginal or
144  * joint
145  * targets that have been added by the user).
146  */
147  virtual void eraseAllTargets();
148 
149  /// Clear all previously defined joint targets
150  virtual void eraseAllJointTargets() final;
151 
152  /// Clear all the previously defined marginal targets
153  virtual void eraseAllMarginalTargets() final;
154 
155  /// Add a set of nodes as a new joint target. As a collateral effect, every
156  /// node is added as a marginal target.
157  /**
158  * @throw UndefinedElement if some node(s) do not belong to the Markov net
159  */
160  virtual void addJointTarget(const NodeSet& joint_target) final;
161 
162  /// removes an existing joint target
163  /** @warning If the joint target does not already exist, the method does
164  * nothing. In particular, it does not raise any exception. */
165  virtual void eraseJointTarget(const NodeSet& joint_target) final;
166 
167  /// return true if target is a joint target.
168  virtual bool isJointTarget(const NodeSet& vars) const final;
169 
170  /// returns the list of joint targets
171  virtual const Set< NodeSet >& jointTargets() const noexcept final;
172 
173  /// returns the number of joint targets
174  virtual Size nbrJointTargets() const noexcept final;
175  /// @}
176 
177  /**
178  * Create a gum::Potential for P(joint targets|evs) (for all instanciation of
179  * targets
180  * and evs)
181  *
182  * @warning If some evs are d-separated, they are not included in the Potential
183  *
184  * @param targets the NodeSet of the targeted variables
185  * @param evs the NodeSet of observed variables
186  * @return a Potential
187  */
189  const NodeSet& evs);
190 
191  /**
192  * Create a gum::Potential for P(joint targets|evs) (for all instanciation of
193  * targets
194  * and evs)
195  *
196  * @warning If some evs are d-separated, they are not included in the Potential
197  *
198  * @param targets the vector of std::string of the targeted variables
199  * @param evs the vector of std::string of observed variables
200  * @return a Potential
201  */
204  const std::vector< std::string >& evs);
205 
206  // ############################################################################
207  /// @name Information Theory related functions
208  // ############################################################################
209  /// @{
210 
211  /** Mutual information between X and Y
212  * @see http://en.wikipedia.org/wiki/Mutual_information
213  *
214  * @warning Due to limitation of @ref joint, may not be able to compute
215  * this value
216  * @throw OperationNotAllowed in these cases
217  */
218  GUM_SCALAR I(NodeId X, NodeId Y);
219 
220  /** Mutual information between X and Y
221  * @see http://en.wikipedia.org/wiki/Mutual_information
222  *
223  * @warning Due to limitation of @ref joint, may not be able to compute
224  * this value
225  * @throw OperationNotAllowed in these cases
226  */
227  GUM_SCALAR I(const std::string& X, const std::string& Y);
228 
229  /** Variation of information between X and Y
230  * @see http://en.wikipedia.org/wiki/Variation_of_information
231  *
232  * @warning Due to limitation of @ref joint, may not be able to compute
233  * this value
234  * @throw OperationNotAllowed in these cases
235  */
236  GUM_SCALAR VI(NodeId X, NodeId Y);
237  /** Variation of information between X and Y
238  * @see http://en.wikipedia.org/wiki/Variation_of_information
239  *
240  * @warning Due to limitation of @ref joint, may not be able to compute
241  * this value
242  * @throw OperationNotAllowed in these cases
243  */
244  GUM_SCALAR VI(const std::string& X, const std::string& Y);
245 
246  /** Mutual information between targets
247  * @see https://en.wikipedia.org/wiki/Interaction_information
248  * @param targets the NodeSet of the targeted variables
249  */
250  GUM_SCALAR jointMutualInformation(const NodeSet& targets);
251 
252  /** Mutual information between targets
253  * @see https://en.wikipedia.org/wiki/Interaction_information
254  * @param targets the vector of std::string of the targeted variables
255  */
256  GUM_SCALAR jointMutualInformation(const std::vector< std::string >& targets);
257 
258  /// @}
259 
260 
261  protected:
262  /// check if the vars form a possible computable joint (can be redefined by
263  /// subclass)
264  virtual bool isExactJointComputable_(const NodeSet& vars);
266 
267  /// fired after a new Markov net has been assigned to the engine
268  virtual void onModelChanged_(const GraphicalModel* mn);
269 
270  /// fired after a new joint target is inserted
271  /** @param set The set of target variable's ids. */
272  virtual void onJointTargetAdded_(const NodeSet& set) = 0;
273 
274  /// fired before a joint target is removed
275  /** @param set The set of target variable's ids. */
276  virtual void onJointTargetErased_(const NodeSet& set) = 0;
277 
278  /// fired before a all the marginal and joint targets are removed
279  virtual void onAllTargetsErased_() = 0;
280 
281  /// fired before a all the joint targets are removed
282  virtual void onAllJointTargetsErased_() = 0;
283 
284 
285  /// asks derived classes for the joint posterior of a declared target set
286  /** @param set The set of ids of the variables whose joint posterior is
287  * looked for. */
288  virtual const Potential< GUM_SCALAR >& jointPosterior_(const NodeSet& set) = 0;
289 
290  /** @brief asks derived classes for the joint posterior of a set of
291  * variables not declared as a joint target
292  *
293  * @param wanted_target The set of ids of the variables whose joint
294  * posterior is looked for.
295  * @param declared_target the joint target declared by the user that
296  * contains set */
297  virtual const Potential< GUM_SCALAR >&
299  const NodeSet& declared_target)
300  = 0;
301 
302  /** @brief returns a fresh unnormalized joint posterior of
303  * a given set of variables
304  * @param set The set of ids of the variables whose joint posterior is
305  * looked for. */
306  virtual Potential< GUM_SCALAR >*
308  = 0;
309 
310  /// returns a fresh potential equal to P(argument,evidence)
312 
313 
314  private:
315  /// the set of joint targets
317  };
318 
319 
320 } // namespace gum
321 
322 
323 #include <agrum/MN/inference/tools/jointTargetedMNInference_tpl.h>
324 
325 
326 #endif // GUM_MARKOV_NET_JOINT_TARGETED_INFERENCE_H
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...
GUM_SCALAR VI(const std::string &X, const std::string &Y)
Variation of information between X and Y.
virtual const Potential< GUM_SCALAR > & jointPosterior_(const NodeSet &set)=0
asks derived classes for the joint posterior of a declared target set
virtual void onModelChanged_(const GraphicalModel *mn)
fired after a new Markov net has been assigned to the engine
virtual const Set< NodeSet > & jointTargets() const noexcept final
returns the list of joint targets
virtual void eraseJointTarget(const NodeSet &joint_target) final
removes an existing joint target
virtual void eraseAllJointTargets() final
Clear all previously defined joint targets.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
virtual NodeSet superForJointComputable_(const NodeSet &vars)
Set< NodeSet > joint_targets__
the set of joint targets
virtual Size nbrJointTargets() const noexcept final
returns the number of joint targets
virtual void onAllTargetsErased_()=0
fired before a all the marginal and joint targets are removed
GUM_SCALAR I(NodeId X, NodeId Y)
Mutual information between X and Y.
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(const std::string &nodeName) final
Computes and returns the posterior of a node.
virtual void eraseAllMarginalTargets() final
Clear all the previously defined marginal targets.
virtual void onAllJointTargetsErased_()=0
fired before a all the joint targets are removed
virtual bool isJointTarget(const NodeSet &vars) const final
return true if target is a joint target.
GUM_SCALAR VI(NodeId X, NodeId Y)
Variation of information between X and Y.
GUM_SCALAR jointMutualInformation(const std::vector< std::string > &targets)
Mutual information between 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 ...
<agrum/MN/inference/jointTargetedMNInference.h>
GUM_SCALAR I(const std::string &X, const std::string &Y)
Mutual information between X and Y.
virtual Potential< GUM_SCALAR > * unnormalizedJointPosterior_(NodeId id)=0
returns a fresh potential equal to P(argument,evidence)
JointTargetedMNInference(const IMarkovNet< GUM_SCALAR > *mn)
default constructor
virtual bool isExactJointComputable_(const NodeSet &vars)
check if the vars form a possible computable joint (can be redefined by subclass) ...
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 onJointTargetAdded_(const NodeSet &set)=0
fired after a new joint target is inserted
virtual Potential< GUM_SCALAR > * unnormalizedJointPosterior_(const NodeSet &set)=0
returns a fresh unnormalized joint posterior of a given set of variables
virtual void onJointTargetErased_(const NodeSet &set)=0
fired before a joint target is removed
virtual const Potential< GUM_SCALAR > & posterior(NodeId node) final
Computes and returns the posterior of a node.
virtual void eraseAllTargets()
Clear all previously defined targets (marginal and joint targets)