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