aGrUM  0.14.2
gum::Estimator< GUM_SCALAR > Class Template Reference

#include <estimator.h>

+ Collaboration diagram for gum::Estimator< GUM_SCALAR >:

Public Member Functions

 Estimator ()
 Default constructor. More...
 
 Estimator (const IBayesNet< GUM_SCALAR > *bn)
 Constructor with Bayesian Network. More...
 
 ~Estimator ()
 
GUM_SCALAR confidence ()
 computes the maximum length of confidence interval for each possible value of each variable More...
 
void update (Instantiation I, GUM_SCALAR w)
 updates the estimator with a given sample More...
 
const Potential< GUM_SCALAR > & posterior (const DiscreteVariable &var)
 returns the posterior of a node More...
 
void clear ()
 refresh the estimator state as empty More...
 
void setFromBN (const IBayesNet< GUM_SCALAR > *bn, const NodeSet &hardEvidence)
 estimator initializing More...
 
void setFromLBP (LoopyBeliefPropagation< GUM_SCALAR > *lbp, const NodeSet &hardEvidence, GUM_SCALAR virtualLBPSize)
 sets the estimatoor object with posteriors obtained by LoopyBeliefPropagation More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::Estimator< GUM_SCALAR >

Definition at line 37 of file estimator.h.

Constructor & Destructor Documentation

◆ Estimator() [1/2]

template<typename GUM_SCALAR >
Estimator::Estimator ( )

Default constructor.

Definition at line 31 of file estimator_tpl.h.

31  {
32  GUM_CONSTRUCTOR(Estimator);
33  _wtotal = (GUM_SCALAR)0.;
34  _ntotal = (Size)0;
35  _bn = nullptr;
36  }
GUM_SCALAR _wtotal
cumulated weights of all samples
Definition: estimator.h:119
Size _ntotal
number of generated samples
Definition: estimator.h:122
class for estimating tools for approximate inference
const IBayesNet< GUM_SCALAR > * _bn
bayesian network on which approximation is done
Definition: estimator.h:125
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ Estimator() [2/2]

template<typename GUM_SCALAR >
Estimator::Estimator ( const IBayesNet< GUM_SCALAR > *  bn)
explicit

Constructor with Bayesian Network.

Definition at line 40 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::_bn, gum::Estimator< GUM_SCALAR >::_estimator, gum::HashTable< Key, Val, Alloc >::insert(), gum::DAGmodel::nodes(), and gum::IBayesNet< GUM_SCALAR >::variable().

40  :
41  Estimator() {
42  _bn = bn;
43 
44  for (gum::NodeGraphPartIterator iter = bn->nodes().begin();
45  iter != bn->nodes().end();
46  ++iter)
48  bn->variable(*iter).name(),
49  std::vector< GUM_SCALAR >(bn->variable(*iter).domainSize(), 0.0));
50 
51  GUM_CONSTRUCTOR(Estimator);
52  }
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:116
Unsafe iterator on the node set of a graph.
Definition: nodeGraphPart.h:55
class for estimating tools for approximate inference
const IBayesNet< GUM_SCALAR > * _bn
bayesian network on which approximation is done
Definition: estimator.h:125
Estimator()
Default constructor.
Definition: estimator_tpl.h:31
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the call graph for this function:

◆ ~Estimator()

template<typename GUM_SCALAR >
INLINE Estimator::~Estimator ( )

Definition at line 56 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::clear().

56  {
57  GUM_DESTRUCTOR(Estimator);
58  // remove all the posteriors computed
59  clear();
60  }
void clear()
refresh the estimator state as empty
class for estimating tools for approximate inference
+ Here is the call graph for this function:

Member Function Documentation

◆ clear()

template<typename GUM_SCALAR >
void Estimator::clear ( )

refresh the estimator state as empty

this function remove all the statistics in order to restart the computations.

Definition at line 183 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::__target_posteriors, gum::Estimator< GUM_SCALAR >::_estimator, gum::Estimator< GUM_SCALAR >::_ntotal, gum::Estimator< GUM_SCALAR >::_wtotal, and gum::HashTable< Key, Val, Alloc >::clear().

Referenced by gum::Estimator< GUM_SCALAR >::~Estimator().

183  {
184  _estimator.clear();
185  _wtotal = (GUM_SCALAR)0;
186  _ntotal = Size(0);
187  for (const auto& pot : __target_posteriors)
188  delete pot.second;
189  __target_posteriors.clear();
190  }
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:116
GUM_SCALAR _wtotal
cumulated weights of all samples
Definition: estimator.h:119
Size _ntotal
number of generated samples
Definition: estimator.h:122
void clear()
Removes all the elements in the hash table.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
HashTable< std::string, Potential< GUM_SCALAR > *> __target_posteriors
the set of single posteriors computed during the last inference
Definition: estimator.h:155
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ confidence()

template<typename GUM_SCALAR >
GUM_SCALAR Estimator::confidence ( )

computes the maximum length of confidence interval for each possible value of each variable

Returns
maximum length of confidence interval

Definition at line 168 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::_estimator, gum::Estimator< GUM_SCALAR >::_ntotal, gum::HashTable< Key, Val, Alloc >::begin(), gum::HashTable< Key, Val, Alloc >::end(), and gum::Estimator< GUM_SCALAR >::variance().

168  {
169  GUM_SCALAR ic_max = 0;
170 
171  for (auto iter = _estimator.begin(); iter != _estimator.end(); ++iter) {
172  for (Idx i = 0; i < iter.val().size(); i++) {
173  GUM_SCALAR ic = GUM_SCALAR(
174  2 * 1.96 * std::sqrt(variance(iter.key(), i) / (_ntotal - 1)));
175  if (ic > ic_max) ic_max = ic;
176  }
177  }
178 
179  return ic_max;
180  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
const iterator & end() noexcept
Returns the unsafe iterator pointing to the end of the hashtable.
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:116
Size _ntotal
number of generated samples
Definition: estimator.h:122
GUM_SCALAR variance(std::string name, Idx val)
returns variance of Bernouilli variable (called by it&#39;s name) of given parameter
+ Here is the call graph for this function:

◆ EV()

template<typename GUM_SCALAR >
GUM_SCALAR Estimator::EV ( std::string  name,
Idx  val 
)
private

returns expected value of Bernouilli variable (called by it's name) of given parameter

Returns
Expected value of Bernouilli variable (called by it's name) of given parameter
Parameters
namevariable's name, considered as a Bernouilli variable
valthe parameter of the Bernouilli variable

computes the amount of cumulative weights for paramater val over the amount of total cumulative weights

Definition at line 150 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::_estimator, and gum::Estimator< GUM_SCALAR >::_wtotal.

Referenced by gum::Estimator< GUM_SCALAR >::variance().

150  {
151  return _estimator[name][val] / _wtotal;
152  }
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:116
GUM_SCALAR _wtotal
cumulated weights of all samples
Definition: estimator.h:119
+ Here is the caller graph for this function:

◆ posterior()

template<typename GUM_SCALAR >
const Potential< GUM_SCALAR > & Estimator::posterior ( const DiscreteVariable var)

returns the posterior of a node

Returns
a constant ref to the posterior probability of the variable node
Parameters
varthe variable node which we want posterior for

returns the vector of cumulated weight bias for each value of the variable normalized as a CPT

Exceptions
NotFoundif variable node is not in estimator.

Definition at line 126 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::__target_posteriors, gum::Estimator< GUM_SCALAR >::_estimator, gum::HashTable< Key, Val, Alloc >::exists(), gum::Potential< GUM_SCALAR >::fillWith(), GUM_ERROR, gum::Variable::name(), and gum::Potential< GUM_SCALAR >::normalize().

126  {
127  Potential< GUM_SCALAR >* p = nullptr;
128 
129  if (!_estimator.exists(var.name()))
130  GUM_ERROR(NotFound, "Target variable not found");
131 
132  // check if we have already computed the posterior
133  if (__target_posteriors.exists(var.name())) {
134  p = __target_posteriors[var.name()];
135  } else {
136  p = new Potential< GUM_SCALAR >();
137  *p << var;
138  __target_posteriors.insert(var.name(), p);
139  }
140 
141  p->fillWith(_estimator[var.name()]);
142  p->normalize();
143  return *p;
144  }
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:116
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< std::string, Potential< GUM_SCALAR > *> __target_posteriors
the set of single posteriors computed during the last inference
Definition: estimator.h:155
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:

◆ setFromBN()

template<typename GUM_SCALAR >
void Estimator::setFromBN ( const IBayesNet< GUM_SCALAR > *  bn,
const NodeSet hardEvidence 
)

estimator initializing

sets the estimator object with 0-filled vectors corresponding to each non evidence node

Definition at line 66 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::_estimator, gum::Set< Key, Alloc >::contains(), gum::HashTable< Key, Val, Alloc >::exists(), gum::HashTable< Key, Val, Alloc >::insert(), gum::DAGmodel::nodes(), and gum::IBayesNet< GUM_SCALAR >::variable().

67  {
68  for (gum::NodeGraphPartIterator iter = bn->nodes().begin();
69  iter != bn->nodes().end();
70  ++iter) {
71  auto v = bn->variable(*iter).name();
72 
73  if (!hardEvidence.contains(*iter)) {
74  if (_estimator.exists(v))
75  _estimator[v] = std::vector< GUM_SCALAR >(
76  bn->variable(*iter).domainSize(), (GUM_SCALAR)0.0);
77  else
79  std::vector< GUM_SCALAR >(
80  bn->variable(*iter).domainSize(), (GUM_SCALAR)0.0));
81  }
82  }
83  }
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:116
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Unsafe iterator on the node set of a graph.
Definition: nodeGraphPart.h:55
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the call graph for this function:

◆ setFromLBP()

template<typename GUM_SCALAR >
void Estimator::setFromLBP ( LoopyBeliefPropagation< GUM_SCALAR > *  lbp,
const NodeSet hardEvidence,
GUM_SCALAR  virtualLBPSize 
)

sets the estimatoor object with posteriors obtained by LoopyBeliefPropagation

Definition at line 89 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::_estimator, gum::Estimator< GUM_SCALAR >::_ntotal, gum::Estimator< GUM_SCALAR >::_wtotal, gum::BayesNetInference< GUM_SCALAR >::BN(), gum::Set< Key, Alloc >::contains(), gum::Instantiation::end(), gum::HashTable< Key, Val, Alloc >::insert(), gum::MarginalTargetedInference< GUM_SCALAR >::posterior(), and gum::Instantiation::setFirst().

91  {
92  for (const auto& node : lbp->BN().nodes()) {
93  if (!hardEvidence.contains(node)) {
94  std::vector< GUM_SCALAR > v;
95  auto p = lbp->posterior(node);
96  gum::Instantiation inst(p);
97 
98  for (inst.setFirst(); !inst.end(); ++inst) {
99  v.push_back(p[inst] * virtualLBPSize);
100  }
101 
102  _estimator.insert(lbp->BN().variable(node).name(), v);
103  }
104  }
105  _ntotal = (Size)virtualLBPSize;
106  _wtotal = virtualLBPSize;
107  }
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:116
GUM_SCALAR _wtotal
cumulated weights of all samples
Definition: estimator.h:119
Size _ntotal
number of generated samples
Definition: estimator.h:122
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the call graph for this function:

◆ update()

template<typename GUM_SCALAR >
void Estimator::update ( Instantiation  I,
GUM_SCALAR  w 
)

updates the estimator with a given sample

Parameters
Ithe sample used to update the estimators
weightbias for the given sample

adds the sample weight to each node's given value in the estimator

Definition at line 112 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::_estimator, gum::Estimator< GUM_SCALAR >::_ntotal, gum::Estimator< GUM_SCALAR >::_wtotal, gum::HashTable< Key, Val, Alloc >::exists(), gum::Variable::name(), gum::Instantiation::nbrDim(), gum::Instantiation::val(), and gum::Instantiation::variable().

112  {
113  _wtotal += w;
114  _ntotal += (Size)1;
115 
116  for (Idx i = 0; i < I.nbrDim(); i++) {
117  if (_estimator.exists(I.variable(i).name()))
118  _estimator[I.variable(i).name()][I.val(i)] += w;
119  }
120  }
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:116
GUM_SCALAR _wtotal
cumulated weights of all samples
Definition: estimator.h:119
Size _ntotal
number of generated samples
Definition: estimator.h:122
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the call graph for this function:

◆ variance()

template<typename GUM_SCALAR >
GUM_SCALAR Estimator::variance ( std::string  name,
Idx  val 
)
private

returns variance of Bernouilli variable (called by it's name) of given parameter

Returns
variance of Bernouilli variable (called by it's name) of given parameter
Parameters
namevariable's name, considered as a Bernouilli variable
valthe parameter of the Bernouilli variable

computes variance for Bernouilli law using EV(name, val)

Definition at line 158 of file estimator_tpl.h.

References gum::Estimator< GUM_SCALAR >::EV().

Referenced by gum::Estimator< GUM_SCALAR >::confidence().

158  {
159  GUM_SCALAR p = EV(name, val);
160  return p * (1 - p);
161  }
GUM_SCALAR EV(std::string name, Idx val)
returns expected value of Bernouilli variable (called by it&#39;s name) of given parameter ...
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ __target_posteriors

template<typename GUM_SCALAR>
HashTable< std::string, Potential< GUM_SCALAR >* > gum::Estimator< GUM_SCALAR >::__target_posteriors
private

the set of single posteriors computed during the last inference

the posteriors are owned by LazyPropagation.

Definition at line 155 of file estimator.h.

Referenced by gum::Estimator< GUM_SCALAR >::clear(), and gum::Estimator< GUM_SCALAR >::posterior().

◆ _bn

template<typename GUM_SCALAR>
const IBayesNet< GUM_SCALAR >* gum::Estimator< GUM_SCALAR >::_bn
private

bayesian network on which approximation is done

Definition at line 125 of file estimator.h.

Referenced by gum::Estimator< GUM_SCALAR >::Estimator().

◆ _estimator

template<typename GUM_SCALAR>
HashTable< std::string, std::vector< GUM_SCALAR > > gum::Estimator< GUM_SCALAR >::_estimator
private

◆ _ntotal

template<typename GUM_SCALAR>
Size gum::Estimator< GUM_SCALAR >::_ntotal
private

◆ _wtotal

template<typename GUM_SCALAR>
GUM_SCALAR gum::Estimator< GUM_SCALAR >::_wtotal
private

The documentation for this class was generated from the following files: