aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
gum::ASTposteriorProba< GUM_SCALAR > Class Template Reference

Posterior probability term ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ). More...

#include <doAST.h>

Inheritance diagram for gum::ASTposteriorProba< GUM_SCALAR >:
Collaboration diagram for gum::ASTposteriorProba< GUM_SCALAR >:

Public Member Functions

 ASTposteriorProba (const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
 Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ); knw will be minimalized using bn.
 ASTposteriorProba (const DAG &bn, const Bijection< NodeId, std::string > &id2name, const Set< std::string > &vars, const Set< std::string > &knw)
 Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ); knw will be minimalized using dag.
 ASTposteriorProba (const Set< std::string > &vars, const Set< std::string > &knw)
 Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ) with already minimalized knw.
const Set< std::string > & vars () const noexcept
const Set< std::string > & knw () const noexcept
std::string toString (std::string_view prefix="") const override
 Human-readable multi-line rendering (for debugging / logs).
std::string protectToLatex (HashTable< std::string, int > &nameOccur) const override
 LaTeX rendering with full name protection.
std::string fastToLatex (HashTable< std::string, int > &nameOccur) const override
 Fast LaTeX rendering (lighter protection).
std::unique_ptr< ASTtree< GUM_SCALAR > > copy () const override
 Deep clone of the sub-tree.
Tensor< GUM_SCALAR > eval (const IBayesNet< GUM_SCALAR > &contextual_bn) const override
 Evaluate the expression against a contextual BN.
const std::string & type () const noexcept
std::string toLatex (HashTable< std::string, int > nameOccur=HashTable< std::string, int >()) const
 Convenience wrapper using an empty occurrence table.

Static Protected Member Functions

static std::string _latexCorrect (std::string_view srcName, HashTable< std::string, int > &nameOccur)
 Sanitize a single variable name for LaTeX and ensure uniqueness.
static std::vector< std::string > _latexCorrect (const Set< std::string > &srcNames, HashTable< std::string, int > &nameOccur)
 Sanitize a set of variable names for LaTeX and ensure uniqueness.

Protected Attributes

std::string _type

Static Protected Attributes

static constexpr const char * CONTINUE_PREFIX = "| "
 Prefix used to draw tree branches in toString.

Static Private Member Functions

static void _ensure_nonempty (const Set< std::string > &vars)
static Set< std::string > _compute_knw_from_bn (const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
static Set< std::string > _compute_knw_from_dag (const DAG &dag, const Bijection< NodeId, std::string > &id2name, const Set< std::string > &vars, const Set< std::string > &knw)

Private Attributes

Set< std::string > _vars
 names of conditioned variables
Set< std::string > _knw
 names of conditioning variables (already minimalized)

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::ASTposteriorProba< GUM_SCALAR >

Posterior probability term ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ).

Two construction modes are provided:

  1. From a BN and raw sets (vars, knw): the constructor performs a minimal knowledge reduction (removing barren/irrelevant evidence) before storing.
  2. From pre-minimalized sets: assumes knw is already minimal.

eval queries the contextual BN and returns the corresponding posterior tensor.

Definition at line 304 of file doAST.h.

Constructor & Destructor Documentation

◆ ASTposteriorProba() [1/3]

template<GUM_Numeric GUM_SCALAR>
gum::ASTposteriorProba< GUM_SCALAR >::ASTposteriorProba ( const DAGmodel & bn,
const Set< std::string > & vars,
const Set< std::string > & knw )
explicit

Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ); knw will be minimalized using bn.

Definition at line 258 of file doAST_tpl.h.

260 :
Posterior probability term ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ).
Definition doAST.h:304
const Set< std::string > & knw() const noexcept
Definition doAST_tpl.h:284
ASTposteriorProba(const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ); knw will be minimalized using bn.
Definition doAST_tpl.h:258
static Set< std::string > _compute_knw_from_bn(const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
Definition doAST_tpl.h:411
const Set< std::string > & vars() const noexcept
Definition doAST_tpl.h:279

References ASTposteriorProba(), _compute_knw_from_bn(), knw(), and vars().

Referenced by ASTposteriorProba(), and ASTposteriorProba().

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

◆ ASTposteriorProba() [2/3]

template<GUM_Numeric GUM_SCALAR>
gum::ASTposteriorProba< GUM_SCALAR >::ASTposteriorProba ( const DAG & bn,
const Bijection< NodeId, std::string > & id2name,
const Set< std::string > & vars,
const Set< std::string > & knw )
explicit

Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ); knw will be minimalized using dag.

Definition at line 264 of file doAST_tpl.h.

268 :
static Set< std::string > _compute_knw_from_dag(const DAG &dag, const Bijection< NodeId, std::string > &id2name, const Set< std::string > &vars, const Set< std::string > &knw)
Definition doAST_tpl.h:427

References ASTposteriorProba(), _compute_knw_from_dag(), knw(), and vars().

Here is the call graph for this function:

◆ ASTposteriorProba() [3/3]

template<GUM_Numeric GUM_SCALAR>
gum::ASTposteriorProba< GUM_SCALAR >::ASTposteriorProba ( const Set< std::string > & vars,
const Set< std::string > & knw )
explicit

Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ) with already minimalized knw.

Definition at line 272 of file doAST_tpl.h.

273 :
274 ASTtree< GUM_SCALAR >("_posterior_"), _vars(vars), _knw(knw) {
276 }
static void _ensure_nonempty(const Set< std::string > &vars)
Definition doAST_tpl.h:405
Set< std::string > _vars
names of conditioned variables
Definition doAST.h:342
Set< std::string > _knw
names of conditioning variables (already minimalized)
Definition doAST.h:343

References gum::ASTtree< GUM_SCALAR >::ASTtree(), _ensure_nonempty(), _knw, _vars, knw(), and vars().

Here is the call graph for this function:

Member Function Documentation

◆ _compute_knw_from_bn()

template<GUM_Numeric GUM_SCALAR>
Set< std::string > gum::ASTposteriorProba< GUM_SCALAR >::_compute_knw_from_bn ( const DAGmodel & bn,
const Set< std::string > & vars,
const Set< std::string > & knw )
staticprivate

Definition at line 411 of file doAST_tpl.h.

413 {
415 for (const auto& v: vars)
416 varIds.insert(bn.idFromName(v));
417 for (const auto& k: knw)
418 knwIds.insert(bn.idFromName(k));
420 const auto minK = bn.minimalCondSet(varIds, knwIds);
421 for (auto nid: minK)
422 out.insert(bn.variable(nid).name());
423 return out;
424 }

References gum::DiscreteGraphicalModel::idFromName(), gum::Set< Key >::insert(), knw(), gum::DAGmodel::minimalCondSet(), gum::Variable::name(), gum::DiscreteGraphicalModel::variable(), and vars().

Referenced by ASTposteriorProba().

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

◆ _compute_knw_from_dag()

template<GUM_Numeric GUM_SCALAR>
Set< std::string > gum::ASTposteriorProba< GUM_SCALAR >::_compute_knw_from_dag ( const DAG & dag,
const Bijection< NodeId, std::string > & id2name,
const Set< std::string > & vars,
const Set< std::string > & knw )
staticprivate

Definition at line 427 of file doAST_tpl.h.

431 {
433 for (const auto& v: vars)
434 varIds.insert(id2name.first(v));
435 for (const auto& k: knw)
436 knwIds.insert(id2name.first(k));
438 const auto minK = dag.minimalCondSet(varIds, knwIds);
439 for (auto nid: minK)
440 out.insert(id2name.second(nid));
441 return out;
442 }

References gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::first(), gum::Set< Key >::insert(), knw(), gum::DAG::minimalCondSet(), gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::second(), and vars().

Referenced by ASTposteriorProba().

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

◆ _ensure_nonempty()

template<GUM_Numeric GUM_SCALAR>
void gum::ASTposteriorProba< GUM_SCALAR >::_ensure_nonempty ( const Set< std::string > & vars)
staticprivate

Definition at line 405 of file doAST_tpl.h.

405 {
406 if (vars.empty()) { GUM_ERROR(InvalidArgument, "ASTposteriorProba: vars must not be empty"); }
407 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References GUM_ERROR, and vars().

Referenced by ASTposteriorProba().

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

◆ _latexCorrect() [1/2]

template<GUM_Numeric GUM_SCALAR>
std::vector< std::string > gum::ASTtree< GUM_SCALAR >::_latexCorrect ( const Set< std::string > & srcNames,
HashTable< std::string, int > & nameOccur )
staticprotectedinherited

Sanitize a set of variable names for LaTeX and ensure uniqueness.

Definition at line 89 of file doAST_tpl.h.

90 {
91 // Transform each name using the single-name overload, then sort.
93 for (const auto& n: srcNames) {
94 out.push_back(_latexCorrect(n, nameOccur));
95 }
96 std::sort(out.begin(), out.end());
97 return out;
98 }
Root abstract node for the AST of algebraic expressions.
Definition doAST.h:91
static std::string _latexCorrect(std::string_view srcName, HashTable< std::string, int > &nameOccur)
Sanitize a single variable name for LaTeX and ensure uniqueness.
Definition doAST_tpl.h:79

References _latexCorrect().

Here is the call graph for this function:

◆ _latexCorrect() [2/2]

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTtree< GUM_SCALAR >::_latexCorrect ( std::string_view srcName,
HashTable< std::string, int > & nameOccur )
staticprotectedinherited

Sanitize a single variable name for LaTeX and ensure uniqueness.

Definition at line 79 of file doAST_tpl.h.

80 {
81 int count = nameOccur.getWithDefault(std::string{srcName}, 0);
82 const int nbr = (count > 1) ? (count - 1) : 0;
83 // append nbr primes
84 return std::string{srcName} + std::string(static_cast< size_t >(nbr), '\'');
85 }

References gum::HashTable< Key, Val >::getWithDefault().

Referenced by _latexCorrect(), gum::ASTjointProba< GUM_SCALAR >::fastToLatex(), gum::ASTposteriorProba< GUM_SCALAR >::fastToLatex(), gum::ASTsum< GUM_SCALAR >::fastToLatex(), gum::ASTjointProba< GUM_SCALAR >::toString(), and gum::ASTposteriorProba< GUM_SCALAR >::toString().

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

◆ copy()

template<GUM_Numeric GUM_SCALAR>
std::unique_ptr< ASTtree< GUM_SCALAR > > gum::ASTposteriorProba< GUM_SCALAR >::copy ( ) const
overridevirtual

Deep clone of the sub-tree.

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 360 of file doAST_tpl.h.

360 {
361 // Avoid recomputing minimal set: use private direct-ctor tag declared in header
363 }

References _knw, and _vars.

◆ eval()

template<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::ASTposteriorProba< GUM_SCALAR >::eval ( const IBayesNet< GUM_SCALAR > & contextual_bn) const
overridevirtual

Evaluate the expression against a contextual BN.

Parameters
contextual_bnBayesian network providing CPTs and variable domains
Returns
resulting factor/tensor

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 367 of file doAST_tpl.h.

367 {
368 // Build NodeSets from names
370 for (const auto& v: _vars)
371 set_vars.insert(contextual_bn.idFromName(v));
372 for (const auto& k: _knw)
373 set_knw.insert(contextual_bn.idFromName(k));
374
375
376 // --- simple CPT case: P(var | parents(var)) ---
377 if (_vars.size() == 1) {
378 const auto& x = *_vars.begin();
379 NodeId ix = contextual_bn.idFromName(x);
380
381 // Directly compare NodeSets: parents(ix) vs set_knw
382 if (contextual_bn.parents(ix) == set_knw) { return contextual_bn.cpt(ix); }
383 }
384
385
386 // --- otherwise: use inference ---
389
390 if (_knw.empty()) {
391 ie.addJointTarget(set_vars);
392 ie.makeInference();
393 p = ie.jointPosterior(set_vars);
394 } else {
395 ie.addJointTarget(set_vars);
396 ie.makeInference();
397 p = ie.evidenceJointImpact(set_vars, set_knw);
398 }
399
400
401 return p;
402 }

References _knw, _vars, gum::JointTargetedInference< GUM_SCALAR >::addJointTarget(), gum::IBayesNet< GUM_SCALAR >::cpt(), gum::JointTargetedInference< GUM_SCALAR >::evidenceJointImpact(), gum::DiscreteGraphicalModel::idFromName(), gum::Set< Key >::insert(), gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), gum::GraphicalModelInference< GUM_SCALAR >::makeInference(), and gum::DAGmodel::parents().

Here is the call graph for this function:

◆ fastToLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTposteriorProba< GUM_SCALAR >::fastToLatex ( HashTable< std::string, int > & nameOccur) const
overridevirtual

Fast LaTeX rendering (lighter protection).

Assumes prior hygiene of names; typically avoids heavier bookkeeping performed in protectToLatex.

Parameters
nameOccursymbol occurrence table (may be consulted/updated)

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 329 of file doAST_tpl.h.

329 {
330 std::string result = "P\\left(";
331
332 // vars
333 {
335 bool first = true;
336 for (const auto& v: corr) {
337 if (!first) result += ',';
338 result += v;
339 first = false;
340 }
341 }
342
343 // | knw
344 if (!_knw.empty()) {
345 result += "\\mid ";
347 bool first = true;
348 for (const auto& k: corr) {
349 if (!first) result += ',';
350 result += k;
351 first = false;
352 }
353 }
354
355 result += "\\right)";
356 return result;
357 }

References _knw, gum::ASTtree< GUM_SCALAR >::_latexCorrect(), and _vars.

Referenced by protectToLatex().

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

◆ knw()

template<GUM_Numeric GUM_SCALAR>
const Set< std::string > & gum::ASTposteriorProba< GUM_SCALAR >::knw ( ) const
noexcept
Returns
names in the conditioning set (minimalized)

Definition at line 284 of file doAST_tpl.h.

284 {
285 return _knw;
286 }

References _knw.

Referenced by ASTposteriorProba(), ASTposteriorProba(), ASTposteriorProba(), _compute_knw_from_bn(), and _compute_knw_from_dag().

Here is the caller graph for this function:

◆ protectToLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTposteriorProba< GUM_SCALAR >::protectToLatex ( HashTable< std::string, int > & nameOccur) const
overridevirtual

LaTeX rendering with full name protection.

Produces LaTeX with variable-name disambiguation, using nameOccur as a registry of encountered names and their occurrence counts.

Parameters
nameOccursymbol occurrence table (updated in place)

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 322 of file doAST_tpl.h.

323 {
324 return fastToLatex(nameOccur);
325 }
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:329

References fastToLatex().

Here is the call graph for this function:

◆ toLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTtree< GUM_SCALAR >::toLatex ( HashTable< std::string, int > nameOccur = HashTable< std::string, int >()) const
inherited

Convenience wrapper using an empty occurrence table.

Returns
LaTeX string for the whole sub-tree.

Definition at line 73 of file doAST_tpl.h.

73 {
74 // We do not rely on nameOccur mutation at this stage; pass through.
75 return fastToLatex(nameOccur);
76 }
virtual std::string fastToLatex(HashTable< std::string, int > &nameOccur) const =0
Fast LaTeX rendering (lighter protection).

References fastToLatex().

Referenced by fastToLatex().

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

◆ toString()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTposteriorProba< GUM_SCALAR >::toString ( std::string_view prefix = "") const
overridevirtual

Human-readable multi-line rendering (for debugging / logs).

Parameters
prefixindentation prefix propagated to children

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 289 of file doAST_tpl.h.

289 {
290 std::string result = "P(";
291
292 // Share the occurrence map across both sides so duplicate
293 // names (if any) get consistent LaTeX suffixes.
295
296 // LEFT: variables, sorted & latex-corrected
298 bool first = true;
299 for (const auto& v: left) {
300 if (!first) result += ',';
301 result += v;
302 first = false;
303 }
304
305 // RIGHT: conditioning set, sorted & latex-corrected
306 if (!_knw.empty()) {
307 result += '|';
309 first = true;
310 for (const auto& k: right) {
311 if (!first) result += ',';
312 result += k;
313 first = false;
314 }
315 }
316
317 result += ')';
318 return std::string{prefix} + result;
319 }

References _knw, gum::ASTtree< GUM_SCALAR >::_latexCorrect(), and _vars.

Here is the call graph for this function:

◆ type()

template<GUM_Numeric GUM_SCALAR>
const std::string & gum::ASTtree< GUM_SCALAR >::type ( ) const
noexceptinherited
Returns
the runtime type tag (human-readable)

Definition at line 68 of file doAST_tpl.h.

68 {
69 return _type;
70 }
std::string _type
Definition doAST.h:154

References _type.

Referenced by gum::ASTBinaryOp< GUM_SCALAR >::ASTBinaryOp(), ASTtree(), and operator=().

Here is the caller graph for this function:

◆ vars()

template<GUM_Numeric GUM_SCALAR>
const Set< std::string > & gum::ASTposteriorProba< GUM_SCALAR >::vars ( ) const
noexcept
Returns
names of variables in the left-hand side (the “query” set)

Definition at line 279 of file doAST_tpl.h.

279 {
280 return _vars;
281 }

References _vars.

Referenced by ASTposteriorProba(), ASTposteriorProba(), ASTposteriorProba(), _compute_knw_from_bn(), _compute_knw_from_dag(), and _ensure_nonempty().

Here is the caller graph for this function:

Member Data Documentation

◆ _knw

template<GUM_Numeric GUM_SCALAR>
Set< std::string > gum::ASTposteriorProba< GUM_SCALAR >::_knw
private

names of conditioning variables (already minimalized)

Definition at line 343 of file doAST.h.

Referenced by ASTposteriorProba(), copy(), eval(), fastToLatex(), knw(), and toString().

◆ _type

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTtree< GUM_SCALAR >::_type
protectedinherited

Definition at line 154 of file doAST.h.

Referenced by ASTtree(), gum::ASTBinaryOp< GUM_SCALAR >::toString(), and type().

◆ _vars

template<GUM_Numeric GUM_SCALAR>
Set< std::string > gum::ASTposteriorProba< GUM_SCALAR >::_vars
private

names of conditioned variables

Definition at line 342 of file doAST.h.

Referenced by ASTposteriorProba(), copy(), eval(), fastToLatex(), toString(), and vars().

◆ CONTINUE_PREFIX

template<GUM_Numeric GUM_SCALAR>
const char* gum::ASTtree< GUM_SCALAR >::CONTINUE_PREFIX = "| "
staticconstexprprotectedinherited

Prefix used to draw tree branches in toString.

Definition at line 153 of file doAST.h.

Referenced by gum::ASTBinaryOp< GUM_SCALAR >::toString(), and gum::ASTsum< GUM_SCALAR >::toString().


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