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

Private Member Functions

GUM_SCALAR EV (std::string name, Idx val)
 returns expected value of Bernouilli variable (called by it's name) of given parameter
GUM_SCALAR variance (std::string name, Idx val)
 returns variance of Bernouilli variable (called by it's name) of given parameter

Private Attributes

HashTable< std::string, std::vector< GUM_SCALAR > > estimator_
 estimator represented by hashtable between each variable name and a vector of cumulative sample weights
GUM_SCALAR wtotal_
 cumulated weights of all samples
Size ntotal_
 number of generated samples
const IBayesNet< GUM_SCALAR > * bn_
 Bayesian network on which approximation is done.
HashTable< std::string, Tensor< GUM_SCALAR > * > _target_posteriors_
 the set of single posteriors computed during the last inference

Detailed Description

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

Definition at line 58 of file estimator.h.

Constructor & Destructor Documentation

◆ Estimator() [1/2]

template<typename GUM_SCALAR>
Estimator::Estimator ( )

Default constructor.

Definition at line 53 of file estimator_tpl.h.

53 {
55 wtotal_ = (GUM_SCALAR)0.;
56 ntotal_ = (Size)0;
57 bn_ = nullptr;
58 }
GUM_SCALAR wtotal_
cumulated weights of all samples
Definition estimator.h:140
const IBayesNet< GUM_SCALAR > * bn_
Bayesian network on which approximation is done.
Definition estimator.h:146
Estimator()
Default constructor.
Size ntotal_
number of generated samples
Definition estimator.h:143

References Estimator(), bn_, ntotal_, and wtotal_.

Referenced by Estimator(), Estimator(), and ~Estimator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Estimator() [2/2]

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

Constructor with Bayesian network.

Definition at line 61 of file estimator_tpl.h.

61 : Estimator() {
62 bn_ = bn;
63
64 for (gum::NodeGraphPartIterator iter = bn->nodes().begin(); iter != bn->nodes().end(); ++iter)
65 estimator_.insert(bn->variable(*iter).name(),
66 std::vector< GUM_SCALAR >(bn->variable(*iter).domainSize(), 0.0));
67
69 }
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:137

References Estimator(), gum::NodeGraphPart::begin(), bn_, gum::DiscreteVariable::domainSize(), gum::NodeGraphPart::end(), estimator_, gum::Variable::name(), gum::DAGmodel::nodes(), and gum::IBayesNet< GUM_SCALAR >::variable().

Here is the call graph for this function:

◆ ~Estimator()

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

Definition at line 72 of file estimator_tpl.h.

72 {
74 // remove all the posteriors computed
75 clear();
76 }
void clear()
refresh the estimator state as empty

References Estimator(), and clear().

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 188 of file estimator_tpl.h.

188 {
189 estimator_.clear();
190 wtotal_ = (GUM_SCALAR)0;
191 ntotal_ = Size(0);
192 for (const auto& pot: _target_posteriors_)
193 delete pot.second;
194 _target_posteriors_.clear();
195 }
HashTable< std::string, Tensor< GUM_SCALAR > * > _target_posteriors_
the set of single posteriors computed during the last inference
Definition estimator.h:176

References _target_posteriors_, estimator_, ntotal_, and wtotal_.

Referenced by ~Estimator().

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 174 of file estimator_tpl.h.

174 {
175 GUM_SCALAR ic_max = 0;
176
177 for (auto iter = estimator_.begin(); iter != estimator_.end(); ++iter) {
178 for (Idx i = 0; i < iter.val().size(); i++) {
179 GUM_SCALAR ic = GUM_SCALAR(2 * 1.96 * std::sqrt(variance(iter.key(), i) / (ntotal_ - 1)));
180 if (ic > ic_max) ic_max = ic;
181 }
182 }
183
184 return ic_max;
185 }
GUM_SCALAR variance(std::string name, Idx val)
returns variance of Bernouilli variable (called by it's name) of given parameter

References estimator_, ntotal_, and variance().

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 158 of file estimator_tpl.h.

158 {
159 return estimator_[name][val] / wtotal_;
160 }

References estimator_, and wtotal_.

Referenced by variance().

Here is the caller graph for this function:

◆ posterior()

template<typename GUM_SCALAR>
const Tensor< 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 136 of file estimator_tpl.h.

136 {
137 Tensor< GUM_SCALAR >* p = nullptr;
138
139 if (!estimator_.exists(var.name())) GUM_ERROR(NotFound, "Target variable not found")
140
141 // check if we have already computed the posterior
143 p = _target_posteriors_[var.name()];
144 } else {
145 p = new Tensor< GUM_SCALAR >();
146 *p << var;
147 _target_posteriors_.insert(var.name(), p);
148 }
149
150 p->fillWith(estimator_[var.name()]);
151 p->normalize();
152 return *p;
153 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _target_posteriors_, estimator_, GUM_ERROR, and gum::Variable::name().

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 81 of file estimator_tpl.h.

82 {
83 for (gum::NodeGraphPartIterator iter = bn->nodes().begin(); iter != bn->nodes().end(); ++iter) {
84 auto v = bn->variable(*iter).name();
85
86 if (!hardEvidence.contains(*iter)) {
87 if (estimator_.exists(v))
89 = std::vector< GUM_SCALAR >(bn->variable(*iter).domainSize(), (GUM_SCALAR)0.0);
90 else
91 estimator_.insert(
92 v,
93 std::vector< GUM_SCALAR >(bn->variable(*iter).domainSize(), (GUM_SCALAR)0.0));
94 }
95 }
96 }

References gum::NodeGraphPart::begin(), gum::Set< Key >::contains(), gum::DiscreteVariable::domainSize(), gum::NodeGraphPart::end(), estimator_, gum::Variable::name(), gum::DAGmodel::nodes(), and gum::IBayesNet< GUM_SCALAR >::variable().

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 101 of file estimator_tpl.h.

103 {
104 for (const auto& node: lbp->BN().nodes()) {
105 if (!hardEvidence.contains(node)) {
107 auto p = lbp->posterior(node);
109
110 for (inst.setFirst(); !inst.end(); ++inst) {
111 v.push_back(p[inst] * virtualLBPSize);
112 }
113
114 estimator_.insert(lbp->BN().variable(node).name(), v);
115 }
116 }
119 }
const Tensor< GUM_SCALAR > & posterior(const DiscreteVariable &var)
returns the posterior of a node

References gum::BayesNetInference< GUM_SCALAR >::BN(), gum::Set< Key >::contains(), gum::Instantiation::end(), estimator_, ntotal_, gum::MarginalTargetedInference< GUM_SCALAR >::posterior(), gum::Instantiation::setFirst(), and wtotal_.

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 124 of file estimator_tpl.h.

124 {
125 wtotal_ += w;
126 ntotal_ += (Size)1;
127
128 for (Idx i = 0; i < I.nbrDim(); i++) {
129 if (estimator_.exists(I.variable(i).name())) estimator_[I.variable(i).name()][I.val(i)] += w;
130 }
131 }

References estimator_, gum::Variable::name(), gum::Instantiation::nbrDim(), ntotal_, gum::Instantiation::val(), gum::Instantiation::variable(), and wtotal_.

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 165 of file estimator_tpl.h.

165 {
166 GUM_SCALAR p = EV(name, val);
167 return p * (1 - p);
168 }
GUM_SCALAR EV(std::string name, Idx val)
returns expected value of Bernouilli variable (called by it's name) of given parameter

References EV().

Referenced by confidence().

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, Tensor< 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 176 of file estimator.h.

Referenced by clear(), and 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 146 of file estimator.h.

Referenced by Estimator(), and Estimator().

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

Referenced by Estimator(), clear(), confidence(), EV(), posterior(), setFromBN(), setFromLBP(), and update().

◆ ntotal_

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

number of generated samples

Definition at line 143 of file estimator.h.

Referenced by Estimator(), clear(), confidence(), setFromLBP(), and update().

◆ wtotal_

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

cumulated weights of all samples

Definition at line 140 of file estimator.h.

Referenced by Estimator(), clear(), EV(), setFromLBP(), and update().


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