aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
marginalTargetedMNInference.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) marginal posteriors.
26  *
27  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
28  */
29 
30 #ifndef GUM_MARKOV_NET_MARGINAL_TARGETED_INFERENCE_H
31 #define GUM_MARKOV_NET_MARGINAL_TARGETED_INFERENCE_H
32 
33 #include <string>
34 
35 #include <agrum/MN/inference/tools/MarkovNetInference.h>
36 #include <agrum/agrum.h>
37 
38 
39 namespace gum {
40 
41 
42  /**
43  * @class MarginalTargetedMNInference marginalTargetedMNInference.h
44  * <agrum/MN/inference/marginalTargetedMNInference.h>
45  * @brief A generic class for the computation of (possibly incrementally)
46  * marginal posteriors
47  * @ingroup mn_group
48  *
49  * The goal of this class is to take care of the marginal targets used
50  * for computing marginal posteriors. The MarginalTargetedMNInference class
51  * inherits from Inference that takes care of handling both evidence and
52  * the current state of the inference. Note that the MarginalTargetedMNInference
53  * is designed to be used in incremental inference engines.
54  */
55  template < typename GUM_SCALAR >
57  public virtual MarkovNetInference< GUM_SCALAR > {
58  public:
59  // ############################################################################
60  /// @name Constructors / Destructors
61  // ############################################################################
62  /// @{
63 
64  /// default constructor
65  /** @warning By default, all the nodes of the Markov net are targets.
66  * @warning note that, by aGrUM's rule, the MN is not copied but only
67  * referenced by the inference algorithm. */
68  explicit MarginalTargetedMNInference(const IMarkovNet< GUM_SCALAR >* mn);
69 
70  /// destructor
71  virtual ~MarginalTargetedMNInference();
72 
73  /// @}
74 
75 
76  // ############################################################################
77  /// @name Probability computations
78  // ############################################################################
79  /// @{
80 
81  /// Computes and returns the posterior of a node.
82  /**
83  * @returns a const ref to the posterior probability of the node.
84  * @param node the node for which we need a posterior probability
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 by
92  * the posterior method.
93  *
94  * @throw UndefinedElement if node is not in the set of targets
95  */
96  virtual const Potential< GUM_SCALAR >& posterior(NodeId node);
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 nodeName the anme of the node for which we need a posterior
102  * probability
103  *
104  * @warning for efficiency reasons, the potential is stored into the
105  * inference engine and is returned by reference. In order to ensure
106  * that the potential may still exist even if the Inference object is
107  * destroyed, the user has to copy it explicitly.
108  *
109  * @warning prepareInference and makeInference may be applied if needed by
110  * the posterior method.
111  *
112  * @throw UndefinedElement if node is not in the set of targets
113  */
114  virtual const Potential< GUM_SCALAR >& posterior(const std::string& nodeName);
115 
116  /// @}
117 
118 
119  // ############################################################################
120  /// @name Targets
121  // ############################################################################
122  /// @{
123 
124  /// Clear all previously defined targets
125  virtual void eraseAllTargets();
126 
127  /// adds all nodes as targets
128  virtual void addAllTargets() final;
129 
130  /// Add a marginal target to the list of targets
131  /**
132  * @throw UndefinedElement if target is not a NodeId in the Markov net
133  */
134  virtual void addTarget(NodeId target) final;
135 
136  /// Add a marginal target to the list of targets
137  /**
138  * @throw UndefinedElement if target is not a NodeId in the Markov net
139  */
140  virtual void addTarget(const std::string& nodeName) final;
141 
142  /// removes an existing (marginal) target
143  /** @warning If the target does not already exist, the method does nothing.
144  * In particular, it does not raise any exception. */
145  virtual void eraseTarget(NodeId target) final;
146 
147  /// removes an existing (marginal) target
148  /** @warning If the target does not already exist, the method does nothing.
149  * In particular, it does not raise any exception. */
150  virtual void eraseTarget(const std::string& nodeName) final;
151 
152  /// return true if variable is a (marginal) target
153  virtual bool isTarget(NodeId node) const final;
154 
155  /// return true if variable is a (marginal) target
156  virtual bool isTarget(const std::string& nodeName) const final;
157 
158  /// returns the number of marginal targets
159  virtual const Size nbrTargets() const noexcept final;
160 
161  /// returns the list of marginal targets
162  virtual const NodeSet& targets() const noexcept final;
163 
164  /// @}
165 
166  // ############################################################################
167  /// @name Information Theory related functions
168  // ############################################################################
169  /// @{
170 
171  /** Entropy
172  * Compute Shanon's entropy of a node given the observation
173  * @see http://en.wikipedia.org/wiki/Information_entropy
174  */
175  virtual GUM_SCALAR H(NodeId X) final;
176 
177  /** Entropy
178  * Compute Shanon's entropy of a node given the observation
179  * @see http://en.wikipedia.org/wiki/Information_entropy
180  */
181  virtual GUM_SCALAR H(const std::string& nodeName) final;
182 
183  ///@}
184 
185 
186  /**
187  * Create a gum::Potential for P(target|evs) (for all instanciation of target
188  * and evs)
189  *
190  * @warning If some evs are d-separated, they are not included in the Potential
191  *
192  * @param mn the MarkovNet
193  * @param target the nodeId of the targetted variable
194  * @param evs the vector of nodeId of the observed variables
195  * @return a Potential
196  */
198 
199  /**
200  * Create a gum::Potential for P(target|evs) (for all instanciation of target
201  * and evs)
202  *
203  * @warning If some evs are d-separated, they are not included in the Potential
204  *
205  * @param target the nodeId of the target variable
206  * @param evs the nodeId of the observed variable
207  * @return a Potential
208  */
210  const std::vector< std::string >& evs);
211 
212  protected:
213  /// fired after a new marginal target is inserted
214  /** @param id The target variable's id. */
215  virtual void onMarginalTargetAdded_(const NodeId id) = 0;
216 
217  /// fired before a marginal target is removed
218  /** @param id The target variable's id. */
219  virtual void onMarginalTargetErased_(const NodeId id) = 0;
220 
221  /// fired after all the nodes of the MN are added as marginal targets
222  virtual void onAllMarginalTargetsAdded_() = 0;
223 
224  /// fired before a all marginal targets are removed
225  virtual void onAllMarginalTargetsErased_() = 0;
226 
227  /// fired after a new Markov net has been assigned to the engine
228  virtual void onModelChanged_(const GraphicalModel* mn);
229 
230  /// asks derived classes for the posterior of a given variable
231  /** @param id The variable's id. */
232  virtual const Potential< GUM_SCALAR >& posterior_(NodeId id) = 0;
233 
234  protected:
235  void setTargetedMode_();
236  bool isTargetedMode_() const;
237 
238  private:
239  /// whether the actual targets are default
241 
242  /// the set of marginal targets
244 
245 
246  /*/// remove all the marginal posteriors computed
247  void invalidatePosteriors__() noexcept;*/
248 
249  /// sets all the nodes of the Markov net as targets
251  };
252 
253 
254 } // namespace gum
255 
256 
257 #include <agrum/MN/inference/tools/marginalTargetedMNInference_tpl.h>
258 
259 
260 #endif // GUM_MARKOV_NET_MARGINAL_TARGETED_INFERENCE_H
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 void addTarget(const std::string &nodeName) final
Add a marginal target to the list of targets.
virtual void onModelChanged_(const GraphicalModel *mn)
fired after a new Markov net has been assigned to the engine
virtual bool isTarget(const std::string &nodeName) const final
return true if variable is a (marginal) target
virtual const Potential< GUM_SCALAR > & posterior(const std::string &nodeName)
Computes and returns the posterior of a node.
virtual GUM_SCALAR H(const std::string &nodeName) final
Entropy Compute Shanon&#39;s entropy of a node given the observation.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
virtual const NodeSet & targets() const noexcept final
returns the list of marginal targets
virtual void onMarginalTargetAdded_(const NodeId id)=0
fired after a new marginal target is inserted
virtual void eraseTarget(const std::string &nodeName) final
removes an existing (marginal) target
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) ...
virtual bool isTarget(NodeId node) const final
return true if variable is a (marginal) target
virtual GUM_SCALAR H(NodeId X) final
Entropy Compute Shanon&#39;s entropy of a node given the observation.
virtual void onAllMarginalTargetsAdded_()=0
fired after all the nodes of the MN are added as marginal targets
virtual const Size nbrTargets() const noexcept final
returns the number of marginal targets
<agrum/MN/inference/marginalTargetedMNInference.h>
virtual void eraseAllTargets()
Clear all previously defined targets.
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
virtual void onAllMarginalTargetsErased_()=0
fired before a all marginal targets are removed
void setAllMarginalTargets__()
sets all the nodes of the Markov net as targets
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
NodeSet targets__
the set of marginal targets
virtual void eraseTarget(NodeId target) final
removes an existing (marginal) target
virtual void addAllTargets() final
adds all nodes as targets
virtual const Potential< GUM_SCALAR > & posterior_(NodeId id)=0
asks derived classes for the posterior of a given variable
bool targeted_mode__
whether the actual targets are default
MarginalTargetedMNInference(const IMarkovNet< GUM_SCALAR > *mn)
default constructor
virtual void onMarginalTargetErased_(const NodeId id)=0
fired before a marginal target is removed