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

Joint probability term ( \mathbb{P}(\mathrm{vars}) ) in an observational BN. More...

#include <doAST.h>

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

Public Member Functions

 ASTjointProba (const Set< std::string > &varNames)
 Build a joint (\mathbb{P}(\mathrm{varNames})).
const Set< std::string > & varNames () 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.

Private Attributes

Set< std::string > _varNames

Detailed Description

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

Joint probability term ( \mathbb{P}(\mathrm{vars}) ) in an observational BN.

eval queries the contextual BN for the joint over the listed variables.

Definition at line 368 of file doAST.h.

Constructor & Destructor Documentation

◆ ASTjointProba()

template<GUM_Numeric GUM_SCALAR>
gum::ASTjointProba< GUM_SCALAR >::ASTjointProba ( const Set< std::string > & varNames)
explicit

Build a joint (\mathbb{P}(\mathrm{varNames})).

Definition at line 448 of file doAST_tpl.h.

448 :
450 // validate that vars is not empty
451 if (varNames.empty()) { GUM_ERROR(InvalidArgument, "ASTjointProba: vars must not be empty"); }
452 }
Joint probability term ( \mathbb{P}(\mathrm{vars}) ) in an observational BN.
Definition doAST.h:368
const Set< std::string > & varNames() const noexcept
Definition doAST_tpl.h:455
Set< std::string > _varNames
Definition doAST.h:389
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References gum::ASTtree< GUM_SCALAR >::ASTtree(), _varNames, and varNames().

Here is the call graph for this function:

Member Function Documentation

◆ _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::ASTjointProba< GUM_SCALAR >::copy ( ) const
overridevirtual

Deep clone of the sub-tree.

Implements gum::ASTtree< GUM_SCALAR >.

Definition at line 500 of file doAST_tpl.h.

References _varNames.

◆ eval()

template<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > gum::ASTjointProba< 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 506 of file doAST_tpl.h.

506 {
507 NodeSet ids;
508 for (const auto& v: _varNames)
509 ids.insert(contextual_bn.idFromName(v));
510
512 if (ids.size() == 1) {
513 // Special case: single variable, use its CPT directly
514 NodeId id = *ids.begin();
515 ie.addTarget(id);
516 ie.makeInference();
517 return ie.posterior(id);
518 } else {
519 // General case: add all variables as joint target
520 ie.addJointTarget(ids);
521 ie.makeInference();
522 return ie.jointPosterior(ids);
523 }
524 }

References _varNames, gum::JointTargetedInference< GUM_SCALAR >::addJointTarget(), gum::MarginalTargetedInference< GUM_SCALAR >::addTarget(), gum::Set< Key >::begin(), gum::DiscreteGraphicalModel::idFromName(), gum::Set< Key >::insert(), gum::JointTargetedInference< GUM_SCALAR >::jointPosterior(), gum::GraphicalModelInference< GUM_SCALAR >::makeInference(), gum::JointTargetedInference< GUM_SCALAR >::posterior(), and gum::Set< Key >::size().

Here is the call graph for this function:

◆ fastToLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTjointProba< 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 486 of file doAST_tpl.h.

486 {
487 std::string result = "P\\left(";
489 bool first = true;
490 for (const auto& v: corr) {
491 if (!first) result += ',';
492 result += v;
493 first = false;
494 }
495 result += "\\right)";
496 return result;
497 }

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

Referenced by protectToLatex().

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

◆ protectToLatex()

template<GUM_Numeric GUM_SCALAR>
std::string gum::ASTjointProba< 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 480 of file doAST_tpl.h.

480 {
481 return fastToLatex(nameOccur);
482 }
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:486

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::ASTjointProba< 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 460 of file doAST_tpl.h.

460 {
461 std::string result = "joint P(";
462
463 // Build a consistent, sorted, LaTeX-corrected list of names
464 gum::HashTable< std::string, int > occur; // tracks suffixes if needed
466
467 bool first = true;
468 for (const auto& v: names) {
469 if (!first) result += ',';
470 result += v;
471 first = false;
472 }
473
474 result += ')';
475 return std::string{prefix} + result;
476 }

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

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:

◆ varNames()

template<GUM_Numeric GUM_SCALAR>
const Set< std::string > & gum::ASTjointProba< GUM_SCALAR >::varNames ( ) const
noexcept
Returns
variable names in the joint

Definition at line 455 of file doAST_tpl.h.

455 {
456 return _varNames;
457 }

References _varNames.

Referenced by ASTjointProba().

Here is the caller graph for this function:

Member Data Documentation

◆ _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().

◆ _varNames

template<GUM_Numeric GUM_SCALAR>
Set< std::string > gum::ASTjointProba< GUM_SCALAR >::_varNames
private

Definition at line 389 of file doAST.h.

Referenced by ASTjointProba(), copy(), eval(), fastToLatex(), toString(), and varNames().

◆ 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: