aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
jointTargetedMNInference.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_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 >
57  public:
58  // ############################################################################
59  /// @name Constructors / Destructors
60  // ############################################################################
61  /// @{
62 
63  /// default constructor
64  /** @warning note that, by aGrUM's rule, the MN is not copied but only
65  * referenced by the inference algorithm. */
66  explicit JointTargetedMNInference(const IMarkovNet< GUM_SCALAR >* mn);
67 
68  /// destructor
69  virtual ~JointTargetedMNInference();
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 Markov 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 
215  /** Mutual information between X and Y
216  * @see http://en.wikipedia.org/wiki/Mutual_information
217  *
218  * @warning Due to limitation of @ref joint, may not be able to compute
219  * this value
220  * @throw OperationNotAllowed in these cases
221  */
222  GUM_SCALAR I(const std::string& X, const std::string& Y);
223 
224  /** Variation of information between X and Y
225  * @see http://en.wikipedia.org/wiki/Variation_of_information
226  *
227  * @warning Due to limitation of @ref joint, may not be able to compute
228  * this value
229  * @throw OperationNotAllowed in these cases
230  */
231  GUM_SCALAR VI(NodeId X, NodeId Y);
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& X, const std::string& Y);
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  /// check if the vars form a possible computable joint (can be redefined by
258  /// subclass)
259  virtual bool isExactJointComputable_(const NodeSet& vars);
261 
262  /// fired after a new Markov net has been assigned to the engine
263  virtual void onModelChanged_(const GraphicalModel* mn);
264 
265  /// fired after a new joint target is inserted
266  /** @param set The set of target variable's ids. */
267  virtual void onJointTargetAdded_(const NodeSet& set) = 0;
268 
269  /// fired before a joint target is removed
270  /** @param set The set of target variable's ids. */
271  virtual void onJointTargetErased_(const NodeSet& set) = 0;
272 
273  /// fired before a all the marginal and joint targets are removed
274  virtual void onAllTargetsErased_() = 0;
275 
276  /// fired before a all the joint targets are removed
277  virtual void onAllJointTargetsErased_() = 0;
278 
279 
280  /// asks derived classes for the joint posterior of a declared target set
281  /** @param set The set of ids of the variables whose joint posterior is
282  * looked for. */
283  virtual const Potential< GUM_SCALAR >& jointPosterior_(const NodeSet& set) = 0;
284 
285  /** @brief asks derived classes for the joint posterior of a set of
286  * variables not declared as a joint target
287  *
288  * @param wanted_target The set of ids of the variables whose joint
289  * posterior is looked for.
290  * @param declared_target the joint target declared by the user that
291  * contains set */
292  virtual const Potential< GUM_SCALAR >& jointPosterior_(const NodeSet& wanted_target,
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. */
301 
302  /// returns a fresh potential equal to P(argument,evidence)
304 
305 
306  private:
307  /// the set of joint targets
309  };
310 
311 
312 } // namespace gum
313 
314 
315 #include <agrum/MN/inference/tools/jointTargetedMNInference_tpl.h>
316 
317 
318 #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:643
virtual NodeSet superForJointComputable_(const NodeSet &vars)
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.
Set< NodeSet > _joint_targets_
the set of joint 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)