aGrUM  0.16.0
estimator_tpl.h
Go to the documentation of this file.
1 
31 namespace gum {
32 
33  template < typename GUM_SCALAR >
35  GUM_CONSTRUCTOR(Estimator);
36  _wtotal = (GUM_SCALAR)0.;
37  _ntotal = (Size)0;
38  _bn = nullptr;
39  }
40 
41 
42  template < typename GUM_SCALAR >
44  Estimator() {
45  _bn = bn;
46 
47  for (gum::NodeGraphPartIterator iter = bn->nodes().begin();
48  iter != bn->nodes().end();
49  ++iter)
51  bn->variable(*iter).name(),
52  std::vector< GUM_SCALAR >(bn->variable(*iter).domainSize(), 0.0));
53 
54  GUM_CONSTRUCTOR(Estimator);
55  }
56 
57 
58  template < typename GUM_SCALAR >
60  GUM_DESTRUCTOR(Estimator);
61  // remove all the posteriors computed
62  clear();
63  }
64 
65 
66  /* adds all potential target variables from a given BN to the Estimator */
67 
68  template < typename GUM_SCALAR >
70  const NodeSet& hardEvidence) {
71  for (gum::NodeGraphPartIterator iter = bn->nodes().begin();
72  iter != bn->nodes().end();
73  ++iter) {
74  auto v = bn->variable(*iter).name();
75 
76  if (!hardEvidence.contains(*iter)) {
77  if (_estimator.exists(v))
78  _estimator[v] = std::vector< GUM_SCALAR >(
79  bn->variable(*iter).domainSize(), (GUM_SCALAR)0.0);
80  else
82  std::vector< GUM_SCALAR >(
83  bn->variable(*iter).domainSize(), (GUM_SCALAR)0.0));
84  }
85  }
86  }
87 
88  // we multiply the posteriors obtained by LoopyBeliefPropagation by the it's
89  // number of iterations
90  template < typename GUM_SCALAR >
91  void
93  const NodeSet& hardEvidence,
94  GUM_SCALAR virtualLBPSize) {
95  for (const auto& node : lbp->BN().nodes()) {
96  if (!hardEvidence.contains(node)) {
97  std::vector< GUM_SCALAR > v;
98  auto p = lbp->posterior(node);
99  gum::Instantiation inst(p);
100 
101  for (inst.setFirst(); !inst.end(); ++inst) {
102  v.push_back(p[inst] * virtualLBPSize);
103  }
104 
105  _estimator.insert(lbp->BN().variable(node).name(), v);
106  }
107  }
108  _ntotal = (Size)virtualLBPSize;
109  _wtotal = virtualLBPSize;
110  }
111 
112  /*update the Estimator given an instantiation I with weight bias w*/
113 
114  template < typename GUM_SCALAR >
116  _wtotal += w;
117  _ntotal += (Size)1;
118 
119  for (Idx i = 0; i < I.nbrDim(); i++) {
120  if (_estimator.exists(I.variable(i).name()))
121  _estimator[I.variable(i).name()][I.val(i)] += w;
122  }
123  }
124 
125  /* returns the approximation CPT of a variable */
126 
127  template < typename GUM_SCALAR >
130  Potential< GUM_SCALAR >* p = nullptr;
131 
132  if (!_estimator.exists(var.name()))
133  GUM_ERROR(NotFound, "Target variable not found");
134 
135  // check if we have already computed the posterior
136  if (__target_posteriors.exists(var.name())) {
137  p = __target_posteriors[var.name()];
138  } else {
139  p = new Potential< GUM_SCALAR >();
140  *p << var;
141  __target_posteriors.insert(var.name(), p);
142  }
143 
144  p->fillWith(_estimator[var.name()]);
145  p->normalize();
146  return *p;
147  }
148 
149 
150  /* expected value considering a Bernouilli variable with parameter val */
151 
152  template < typename GUM_SCALAR >
153  GUM_SCALAR Estimator< GUM_SCALAR >::EV(std::string name, Idx val) {
154  return _estimator[name][val] / _wtotal;
155  }
156 
157 
158  /* variance considering a Bernouilli variable with parameter val */
159 
160  template < typename GUM_SCALAR >
161  GUM_SCALAR Estimator< GUM_SCALAR >::variance(std::string name, Idx val) {
162  GUM_SCALAR p = EV(name, val);
163  return p * (1 - p);
164  }
165 
166 
167  /* returns maximum length of confidence intervals for each variable, each
168  * parameter */
169 
170  template < typename GUM_SCALAR >
172  GUM_SCALAR ic_max = 0;
173 
174  for (auto iter = _estimator.begin(); iter != _estimator.end(); ++iter) {
175  for (Idx i = 0; i < iter.val().size(); i++) {
176  GUM_SCALAR ic = GUM_SCALAR(
177  2 * 1.96 * std::sqrt(variance(iter.key(), i) / (_ntotal - 1)));
178  if (ic > ic_max) ic_max = ic;
179  }
180  }
181 
182  return ic_max;
183  }
184 
185  template < typename GUM_SCALAR >
187  _estimator.clear();
188  _wtotal = (GUM_SCALAR)0;
189  _ntotal = Size(0);
190  for (const auto& pot : __target_posteriors)
191  delete pot.second;
192  __target_posteriors.clear();
193  }
194 } // namespace gum
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
void setFromBN(const IBayesNet< GUM_SCALAR > *bn, const NodeSet &hardEvidence)
estimator initializing
Definition: estimator_tpl.h:69
void clear()
refresh the estimator state as empty
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
const Potential< GUM_SCALAR > & posterior(const DiscreteVariable &var)
returns the posterior of a node
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
HashTable< std::string, std::vector< GUM_SCALAR > > _estimator
estimator represented by hashtable between each variable name and a vector of cumulative sample weigh...
Definition: estimator.h:119
GUM_SCALAR _wtotal
cumulated weights of all samples
Definition: estimator.h:122
Size _ntotal
number of generated samples
Definition: estimator.h:125
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
void update(Instantiation I, GUM_SCALAR w)
updates the estimator with a given sample
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
GUM_SCALAR EV(std::string name, Idx val)
returns expected value of Bernouilli variable (called by it&#39;s name) of given parameter ...
Idx val(Idx i) const
Returns the current value of the variable at position i.
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:92
Unsafe iterator on the node set of a graph.
Definition: nodeGraphPart.h:58
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
const Potential< GUM_SCALAR > & fillWith(const Potential< GUM_SCALAR > &src) const
copy a Potential data using name of variables and labels (not necessarily the same variables in the s...
const IBayesNet< GUM_SCALAR > * _bn
bayesian network on which approximation is done
Definition: estimator.h:128
<agrum/BN/inference/loopyBeliefPropagation.h>
virtual const Potential< GUM_SCALAR > & posterior(NodeId node)
Computes and returns the posterior of a node.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
GUM_SCALAR variance(std::string name, Idx val)
returns variance of Bernouilli variable (called by it&#39;s name) of given parameter
void clear()
Removes all the elements in the hash table.
void setFirst()
Assign the first values to the tuple of the Instantiation.
Size Idx
Type for indexes.
Definition: types.h:53
Estimator()
Default constructor.
Definition: estimator_tpl.h:34
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const std::string & name() const
returns the name of the variable
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
virtual const IBayesNet< GUM_SCALAR > & BN() const final
Returns a constant reference over the IBayesNet referenced by this class.
HashTable< std::string, Potential< GUM_SCALAR > *> __target_posteriors
the set of single posteriors computed during the last inference
Definition: estimator.h:158
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
bool end() const
Returns true if the Instantiation reached the end.
GUM_SCALAR confidence()
computes the maximum length of confidence interval for each possible value of each variable ...