aGrUM  0.16.0
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 40 of file estimator.h.

Constructor & Destructor Documentation

◆ Estimator() [1/2]

template<typename GUM_SCALAR >
Estimator::Estimator ( )

Default constructor.

Definition at line 34 of file estimator_tpl.h.

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

◆ Estimator() [2/2]

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

Constructor with Bayesian Network.

Definition at line 43 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().

43  :
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  }
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
Unsafe iterator on the node set of a graph.
Definition: nodeGraphPart.h:58
class for estimating tools for approximate inference
const IBayesNet< GUM_SCALAR > * _bn
bayesian network on which approximation is done
Definition: estimator.h:128
Estimator()
Default constructor.
Definition: estimator_tpl.h:34
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 59 of file estimator_tpl.h.

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

59  {
60  GUM_DESTRUCTOR(Estimator);
61  // remove all the posteriors computed
62  clear();
63  }
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 186 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().

186  {
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  }
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
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:48
HashTable< std::string, Potential< GUM_SCALAR > *> __target_posteriors
the set of single posteriors computed during the last inference
Definition: estimator.h:158
+ 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 171 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().

171  {
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  }
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:119
Size _ntotal
number of generated samples
Definition: estimator.h:125
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 153 of file estimator_tpl.h.

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

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

153  {
154  return _estimator[name][val] / _wtotal;
155  }
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
+ 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 129 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().

129  {
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  }
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
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:158
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 69 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().

70  {
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  }
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
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:58
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 92 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().

94  {
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  }
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
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
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.
+ 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 115 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().

115  {
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  }
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.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ 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 161 of file estimator_tpl.h.

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

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

161  {
162  GUM_SCALAR p = EV(name, val);
163  return p * (1 - p);
164  }
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 158 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 128 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: