aGrUM 3.1.1
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_view name, Idx val)
 returns expected value of Bernouilli variable (called by it's name) of given parameter
GUM_SCALAR variance (std::string_view 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<GUM_Numeric GUM_SCALAR>
class gum::Estimator< GUM_SCALAR >

Definition at line 60 of file estimator.h.

Constructor & Destructor Documentation

◆ Estimator() [1/2]

template<GUM_Numeric GUM_SCALAR>
Estimator::Estimator ( )

Default constructor.

Definition at line 56 of file estimator_tpl.h.

56 {
58 wtotal_ = (GUM_SCALAR)0.;
59 ntotal_ = (Size)0;
60 bn_ = nullptr;
61 }
GUM_SCALAR wtotal_
cumulated weights of all samples
Definition estimator.h:142
const IBayesNet< GUM_SCALAR > * bn_
Bayesian network on which approximation is done.
Definition estimator.h:148
Estimator()
Default constructor.
Size ntotal_
number of generated samples
Definition estimator.h:145

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<GUM_Numeric GUM_SCALAR>
Estimator::Estimator ( const IBayesNet< GUM_SCALAR > * bn)
explicit

Constructor with Bayesian network.

Definition at line 64 of file estimator_tpl.h.

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

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

Here is the call graph for this function:

◆ ~Estimator()

template<GUM_Numeric GUM_SCALAR>
Estimator::~Estimator ( )

Definition at line 75 of file estimator_tpl.h.

75 {
77 // remove all the posteriors computed
78 clear();
79 }
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<GUM_Numeric 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 191 of file estimator_tpl.h.

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

References _target_posteriors_, estimator_, ntotal_, and wtotal_.

Referenced by ~Estimator().

Here is the caller graph for this function:

◆ confidence()

template<GUM_Numeric 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 177 of file estimator_tpl.h.

177 {
178 GUM_SCALAR ic_max = 0;
179
180 for (auto iter = estimator_.begin(); iter != estimator_.end(); ++iter) {
181 for (Idx i = 0; i < iter.val().size(); i++) {
182 GUM_SCALAR ic = GUM_SCALAR(2 * 1.96 * std::sqrt(variance(iter.key(), i) / (ntotal_ - 1)));
183 if (ic > ic_max) ic_max = ic;
184 }
185 }
186
187 return ic_max;
188 }
GUM_SCALAR variance(std::string_view 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<GUM_Numeric GUM_SCALAR>
GUM_SCALAR Estimator::EV ( std::string_view 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 161 of file estimator_tpl.h.

161 {
162 return estimator_[name][val] / wtotal_;
163 }

References estimator_, and wtotal_.

Referenced by variance().

Here is the caller graph for this function:

◆ posterior()

template<GUM_Numeric 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 139 of file estimator_tpl.h.

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

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

Here is the call graph for this function:

◆ setFromBN()

template<GUM_Numeric 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 84 of file estimator_tpl.h.

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

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

Here is the call graph for this function:

◆ setFromLBP()

template<GUM_Numeric 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 104 of file estimator_tpl.h.

106 {
107 for (const auto& node: lbp->BN().nodes()) {
108 if (!hardEvidence.contains(node)) {
110 auto p = lbp->posterior(node);
112
113 for (inst.setFirst(); !inst.end(); ++inst) {
114 v.push_back(p[inst] * virtualLBPSize);
115 }
116
117 estimator_.insert(lbp->BN().variable(node).name(), v);
118 }
119 }
122 }
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<GUM_Numeric 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 127 of file estimator_tpl.h.

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

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<GUM_Numeric GUM_SCALAR>
GUM_SCALAR Estimator::variance ( std::string_view 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 168 of file estimator_tpl.h.

168 {
169 GUM_SCALAR p = EV(name, val);
170 return p * (1 - p);
171 }
GUM_SCALAR EV(std::string_view 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<GUM_Numeric 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 178 of file estimator.h.

Referenced by clear(), and posterior().

◆ bn_

template<GUM_Numeric GUM_SCALAR>
const IBayesNet< GUM_SCALAR >* gum::Estimator< GUM_SCALAR >::bn_
private

Bayesian network on which approximation is done.

Definition at line 148 of file estimator.h.

Referenced by Estimator(), and Estimator().

◆ estimator_

template<GUM_Numeric 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 139 of file estimator.h.

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

◆ ntotal_

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

number of generated samples

Definition at line 145 of file estimator.h.

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

◆ wtotal_

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

cumulated weights of all samples

Definition at line 142 of file estimator.h.

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


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