aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
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 39 of file estimator.h.

Constructor & Destructor Documentation

◆ Estimator() [1/2]

template<typename GUM_SCALAR >
Estimator::Estimator ( )

Default constructor.

Definition at line 33 of file estimator_tpl.h.

33  {
34  GUM_CONSTRUCTOR(Estimator);
35  wtotal_ = (GUM_SCALAR)0.;
36  ntotal_ = (Size)0;
37  bn_ = nullptr;
38  }
GUM_SCALAR wtotal_
cumulated weights of all samples
Definition: estimator.h:121
class for estimating tools for approximate inference
Size ntotal_
number of generated samples
Definition: estimator.h:124
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
const IBayesNet< GUM_SCALAR > * bn_
Bayesian network on which approximation is done.
Definition: estimator.h:127

◆ Estimator() [2/2]

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

Constructor with Bayesian network.

Definition at line 42 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

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

◆ ~Estimator()

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

Definition at line 54 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

54  {
55  GUM_DESTRUCTOR(Estimator);
56  // remove all the posteriors computed
57  clear();
58  }
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 174 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

174  {
175  estimator_.clear();
176  wtotal_ = (GUM_SCALAR)0;
177  ntotal_ = Size(0);
178  for (const auto& pot: _target_posteriors_)
179  delete pot.second;
180  _target_posteriors_.clear();
181  }
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
void clear()
Removes all the elements in the hash table.
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:118
Size ntotal_
number of generated samples
Definition: estimator.h:124
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call 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 160 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

160  {
161  GUM_SCALAR ic_max = 0;
162 
163  for (auto iter = estimator_.begin(); iter != estimator_.end(); ++iter) {
164  for (Idx i = 0; i < iter.val().size(); i++) {
165  GUM_SCALAR ic = GUM_SCALAR(2 * 1.96 * std::sqrt(variance(iter.key(), i) / (ntotal_ - 1)));
166  if (ic > ic_max) ic_max = ic;
167  }
168  }
169 
170  return ic_max;
171  }
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.
GUM_SCALAR variance(std::string name, Idx val)
returns variance of Bernouilli variable (called by it&#39;s name) of given parameter
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:118
Size ntotal_
number of generated samples
Definition: estimator.h:124
+ 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 142 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

142  {
143  return estimator_[name][val] / wtotal_;
144  }
GUM_SCALAR wtotal_
cumulated weights of all samples
Definition: estimator.h:121
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:118
+ Here is the call 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 119 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

119  {
120  Potential< GUM_SCALAR >* p = nullptr;
121 
122  if (!estimator_.exists(var.name())) GUM_ERROR(NotFound, "Target variable not found")
123 
124  // check if we have already computed the posterior
125  if (_target_posteriors_.exists(var.name())) {
126  p = _target_posteriors_[var.name()];
127  } else {
128  p = new Potential< GUM_SCALAR >();
129  *p << var;
130  _target_posteriors_.insert(var.name(), p);
131  }
132 
133  p->fillWith(estimator_[var.name()]);
134  p->normalize();
135  return *p;
136  }
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:157
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:118
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 64 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

65  {
66  for (gum::NodeGraphPartIterator iter = bn->nodes().begin(); iter != bn->nodes().end(); ++iter) {
67  auto v = bn->variable(*iter).name();
68 
69  if (!hardEvidence.contains(*iter)) {
70  if (estimator_.exists(v))
71  estimator_[v]
72  = std::vector< GUM_SCALAR >(bn->variable(*iter).domainSize(), (GUM_SCALAR)0.0);
73  else
75  v,
76  std::vector< GUM_SCALAR >(bn->variable(*iter).domainSize(), (GUM_SCALAR)0.0));
77  }
78  }
79  }
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:57
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:118
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 84 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

86  {
87  for (const auto& node: lbp->BN().nodes()) {
88  if (!hardEvidence.contains(node)) {
89  std::vector< GUM_SCALAR > v;
90  auto p = lbp->posterior(node);
91  gum::Instantiation inst(p);
92 
93  for (inst.setFirst(); !inst.end(); ++inst) {
94  v.push_back(p[inst] * virtualLBPSize);
95  }
96 
97  estimator_.insert(lbp->BN().variable(node).name(), v);
98  }
99  }
100  ntotal_ = (Size)virtualLBPSize;
101  wtotal_ = virtualLBPSize;
102  }
GUM_SCALAR wtotal_
cumulated weights of all samples
Definition: estimator.h:121
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:82
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:118
Size ntotal_
number of generated samples
Definition: estimator.h:124
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
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 107 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

107  {
108  wtotal_ += w;
109  ntotal_ += (Size)1;
110 
111  for (Idx i = 0; i < I.nbrDim(); i++) {
112  if (estimator_.exists(I.variable(i).name())) estimator_[I.variable(i).name()][I.val(i)] += w;
113  }
114  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
GUM_SCALAR wtotal_
cumulated weights of all samples
Definition: estimator.h:121
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:118
Size ntotal_
number of generated samples
Definition: estimator.h:124
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ 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 150 of file estimator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

150  {
151  GUM_SCALAR p = EV(name, val);
152  return p * (1 - p);
153  }
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:

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 157 of file estimator.h.

◆ 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 127 of file estimator.h.

◆ estimator_

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

estimator represented by hashtable between each variable name and a vector of cumulative sample weights

Definition at line 118 of file estimator.h.

◆ ntotal_

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

number of generated samples

Definition at line 124 of file estimator.h.

◆ wtotal_

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

cumulated weights of all samples

Definition at line 121 of file estimator.h.


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