aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
estimator.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 estimating tools for approximate inference
25  *
26  * @author Paul ALAM & Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_ESTIMATOR_H
29 #define GUM_ESTIMATOR_H
30 
31 #include <agrum/BN/IBayesNet.h>
32 #include <agrum/BN/inference/loopyBeliefPropagation.h>
33 #include <agrum/tools/core/hashTable.h>
34 #include <vector>
35 
36 namespace gum {
37 
38  template < typename GUM_SCALAR >
39  class Estimator {
40  public:
41  /**
42  * @class Estimator
43  * @headerfile estimator.h <agrum/BN/inference/estimator.h>
44  * @brief class for estimating tools for approximate inference
45  * @ingroup bn_approximation
46  *
47  */
48 
49  /**
50  * Default constructor
51  */
52  Estimator();
53 
54  /**
55  * Constructor with Bayesian network
56  */
57  explicit Estimator(const IBayesNet< GUM_SCALAR >* bn);
58 
59  /* Destructor */
60  ~Estimator();
61 
62  /** estimator initializing
63  * @{
64  */
65 
66  /**
67  * sets the estimator object with 0-filled vectors corresponding
68  * to each non evidence node
69  */
70  void setFromBN(const IBayesNet< GUM_SCALAR >* bn, const NodeSet& hardEvidence);
71 
72  /**
73  * sets the estimatoor object with posteriors obtained by
74  * LoopyBeliefPropagation
75  */
76  void setFromLBP(LoopyBeliefPropagation< GUM_SCALAR >* lbp,
77  const NodeSet& hardEvidence,
78  GUM_SCALAR virtualLBPSize);
79  /** @} */
80 
81  /// computes the maximum length of confidence interval for each possible value
82  /// of each variable
83  /**
84  * @returns maximum length of confidence interval
85  */
86  GUM_SCALAR confidence();
87 
88  /// updates the estimator with a given sample
89  /**
90  * @param I the sample used to update the estimators
91  * @param weight bias for the given sample
92  *
93  * adds the sample weight to each node's given value in the estimator
94  */
95  void update(Instantiation I, GUM_SCALAR w);
96 
97  /// returns the posterior of a node
98  /**
99  * @returns a constant ref to the posterior probability of the variable node
100  * @param var the variable node which we want posterior for
101  *
102  * returns the vector of cumulated weight bias for each value of the variable
103  * normalized as a CPT
104  *
105  * @throw NotFound if variable node is not in estimator.
106  */
108 
109  /// refresh the estimator state as empty
110  /** this function remove all the statistics in order to restart the
111  * computations.
112  */
113  void clear();
114 
115  private:
116  /// estimator represented by hashtable between each variable name and a vector
117  /// of cumulative sample weights
119 
120  /// cumulated weights of all samples
121  GUM_SCALAR wtotal_;
122 
123  /// number of generated samples
125 
126  /// Bayesian network on which approximation is done
128 
129  /// returns expected value of Bernouilli variable (called by it's name) of
130  /// given parameter
131  /**
132  * @returns Expected value of Bernouilli variable (called by it's name) of
133  * given parameter
134  * @param name variable's name, considered as a Bernouilli variable
135  * @param val the parameter of the Bernouilli variable
136  *
137  * computes the amount of cumulative weights for paramater val over the amount
138  * of total cumulative weights
139  */
140  GUM_SCALAR EV(std::string name, Idx val);
141 
142  /// returns variance of Bernouilli variable (called by it's name) of given
143  /// parameter
144  /**
145  * @returns variance of Bernouilli variable (called by it's name) of given
146  * parameter
147  * @param name variable's name, considered as a Bernouilli variable
148  * @param val the parameter of the Bernouilli variable
149  *
150  * computes variance for Bernouilli law using EV(name, val)
151  */
152  GUM_SCALAR variance(std::string name, Idx val); // variance corrigĂ©e
153 
154  private:
155  /// the set of single posteriors computed during the last inference
156  /** the posteriors are owned by LazyPropagation. */
158  };
159 
160 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
161  extern template class Estimator< double >;
162 #endif
163 
164 } // namespace gum
165 
166 #include <agrum/BN/inference/tools/estimator_tpl.h>
167 #endif
void setFromBN(const IBayesNet< GUM_SCALAR > *bn, const NodeSet &hardEvidence)
estimator initializing
Definition: estimator_tpl.h:64
void clear()
refresh the estimator state as empty
const Potential< GUM_SCALAR > & posterior(const DiscreteVariable &var)
returns the posterior of a node
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void update(Instantiation I, GUM_SCALAR w)
updates the estimator with a given sample
HashTable< std::string, Potential< GUM_SCALAR > *> _target_posteriors_
the set of single posteriors computed during the last inference
Definition: estimator.h:157
GUM_SCALAR wtotal_
cumulated weights of all samples
Definition: estimator.h:121
GUM_SCALAR EV(std::string name, Idx val)
returns expected value of Bernouilli variable (called by it&#39;s name) of given parameter ...
void setFromLBP(LoopyBeliefPropagation< GUM_SCALAR > *lbp, const NodeSet &hardEvidence, GUM_SCALAR virtualLBPSize)
sets the estimatoor object with posteriors obtained by LoopyBeliefPropagation
Definition: estimator_tpl.h:84
Estimator(const IBayesNet< GUM_SCALAR > *bn)
Constructor with Bayesian network.
Definition: estimator_tpl.h:42
GUM_SCALAR variance(std::string name, Idx val)
returns variance of Bernouilli variable (called by it&#39;s name) of given parameter
Size ntotal_
number of generated samples
Definition: estimator.h:124
Estimator()
Default constructor.
Definition: estimator_tpl.h:33
const IBayesNet< GUM_SCALAR > * bn_
Bayesian network on which approximation is done.
Definition: estimator.h:127
GUM_SCALAR confidence()
computes the maximum length of confidence interval for each possible value of each variable ...