59 template < GUM_Numeric GUM_SCALAR >
62 template < GUM_Numeric GUM_SCALAR >
67 template < GUM_Numeric GUM_SCALAR >
72 template < GUM_Numeric GUM_SCALAR >
78 template < GUM_Numeric GUM_SCALAR >
82 const int nbr = (count > 1) ? (count - 1) : 0;
84 return std::string{srcName} + std::string(
static_cast< size_t >(nbr),
'\'');
87 template < GUM_Numeric GUM_SCALAR >
88 std::vector< std::string >
92 std::vector< std::string > out;
93 for (
const auto& n: srcNames) {
96 std::sort(out.begin(), out.end());
103 template < GUM_Numeric GUM_SCALAR >
105 std::unique_ptr< ASTtree< GUM_SCALAR > >
op1,
106 std::unique_ptr< ASTtree< GUM_SCALAR > >
op2) :
109 template < GUM_Numeric GUM_SCALAR >
114 template < GUM_Numeric GUM_SCALAR >
119 template < GUM_Numeric GUM_SCALAR >
122 return std::format(
"{}{}\n{}\n{}",
125 _op1->toString(cont),
126 _op2->toString(cont));
132 template < GUM_Numeric GUM_SCALAR >
134 std::unique_ptr< ASTtree< GUM_SCALAR > >
op2) :
137 template < GUM_Numeric GUM_SCALAR >
140 return "\\left(" +
fastToLatex(nameOccur) +
"\\right)";
143 template < GUM_Numeric GUM_SCALAR >
145 return this->
_op1->fastToLatex(nameOccur) +
"+" + this->
_op2->fastToLatex(nameOccur);
148 template < GUM_Numeric GUM_SCALAR >
150 auto l = this->
_op1->copy();
151 auto r = this->
_op2->copy();
152 return std::make_unique< ASTplus< GUM_SCALAR > >(std::move(l), std::move(r));
155 template < GUM_Numeric GUM_SCALAR >
157 return this->
_op1->eval(bn) + this->
_op2->eval(bn);
163 template < GUM_Numeric GUM_SCALAR >
165 std::unique_ptr< ASTtree< GUM_SCALAR > >
op2) :
168 template < GUM_Numeric GUM_SCALAR >
171 return "\\left(" +
fastToLatex(nameOccur) +
"\\right)";
174 template < GUM_Numeric GUM_SCALAR >
176 return this->
_op1->fastToLatex(nameOccur) +
"-" + this->
_op2->fastToLatex(nameOccur);
179 template < GUM_Numeric GUM_SCALAR >
181 auto l = this->
_op1->copy();
182 auto r = this->
_op2->copy();
183 return std::make_unique< ASTminus< GUM_SCALAR > >(std::move(l), std::move(r));
186 template < GUM_Numeric GUM_SCALAR >
188 return this->
_op1->eval(bn) - this->
_op2->eval(bn);
194 template < GUM_Numeric GUM_SCALAR >
196 std::unique_ptr< ASTtree< GUM_SCALAR > >
op2) :
199 template < GUM_Numeric GUM_SCALAR >
202 return this->
_op1->protectToLatex(nameOccur) +
" \\cdot "
203 + this->
_op2->protectToLatex(nameOccur);
206 template < GUM_Numeric GUM_SCALAR >
208 return this->
_op1->fastToLatex(nameOccur) +
" \\cdot " + this->
_op2->fastToLatex(nameOccur);
211 template < GUM_Numeric GUM_SCALAR >
213 auto l = this->
_op1->copy();
214 auto r = this->
_op2->copy();
215 return std::make_unique< ASTmult< GUM_SCALAR > >(std::move(l), std::move(r));
218 template < GUM_Numeric GUM_SCALAR >
220 return this->
_op1->eval(bn) * this->
_op2->eval(bn);
226 template < GUM_Numeric GUM_SCALAR >
228 std::unique_ptr< ASTtree< GUM_SCALAR > >
op2) :
231 template < GUM_Numeric GUM_SCALAR >
236 template < GUM_Numeric GUM_SCALAR >
238 return " \\frac {" + this->
_op1->fastToLatex(nameOccur) +
"}{"
239 + this->
_op2->fastToLatex(nameOccur) +
"}";
242 template < GUM_Numeric GUM_SCALAR >
244 auto l = this->
_op1->copy();
245 auto r = this->
_op2->copy();
246 return std::make_unique< ASTdiv< GUM_SCALAR > >(std::move(l), std::move(r));
249 template < GUM_Numeric GUM_SCALAR >
251 return this->
_op1->eval(bn) / this->
_op2->eval(bn);
257 template < GUM_Numeric GUM_SCALAR >
263 template < GUM_Numeric GUM_SCALAR >
271 template < GUM_Numeric GUM_SCALAR >
278 template < GUM_Numeric GUM_SCALAR >
283 template < GUM_Numeric GUM_SCALAR >
288 template < GUM_Numeric GUM_SCALAR >
290 std::string result =
"P(";
299 for (
const auto& v: left) {
300 if (!first) result +=
',';
310 for (
const auto& k: right) {
311 if (!first) result +=
',';
318 return std::string{prefix} + result;
321 template < GUM_Numeric GUM_SCALAR >
327 template < GUM_Numeric GUM_SCALAR >
330 std::string result =
"P\\left(";
336 for (
const auto& v: corr) {
337 if (!first) result +=
',';
348 for (
const auto& k: corr) {
349 if (!first) result +=
',';
355 result +=
"\\right)";
359 template < GUM_Numeric GUM_SCALAR >
362 return std::make_unique< ASTposteriorProba< GUM_SCALAR > >(
_vars,
_knw);
365 template < GUM_Numeric GUM_SCALAR >
370 for (
const auto& v:
_vars)
372 for (
const auto& k:
_knw)
377 if (
_vars.size() == 1) {
378 const auto& x = *
_vars.begin();
382 if (contextual_bn.
parents(ix) == set_knw) {
return contextual_bn.
cpt(ix); }
388 Tensor< GUM_SCALAR > p;
404 template < GUM_Numeric GUM_SCALAR >
409 template < GUM_Numeric GUM_SCALAR >
415 for (
const auto& v:
vars)
417 for (
const auto& k:
knw)
426 template < GUM_Numeric GUM_SCALAR >
433 for (
const auto& v:
vars)
435 for (
const auto& k:
knw)
447 template < GUM_Numeric GUM_SCALAR >
451 if (
varNames.
empty()) { GUM_ERROR(InvalidArgument,
"ASTjointProba: vars must not be empty"); }
454 template < GUM_Numeric GUM_SCALAR >
459 template < GUM_Numeric GUM_SCALAR >
461 std::string result =
"joint P(";
468 for (
const auto& v: names) {
469 if (!first) result +=
',';
475 return std::string{prefix} + result;
478 template < GUM_Numeric GUM_SCALAR >
484 template < GUM_Numeric GUM_SCALAR >
487 std::string result =
"P\\left(";
490 for (
const auto& v: corr) {
491 if (!first) result +=
',';
495 result +=
"\\right)";
499 template < GUM_Numeric GUM_SCALAR >
501 return std::make_unique< ASTjointProba< GUM_SCALAR > >(
_varNames);
504 template < GUM_Numeric GUM_SCALAR >
512 if (ids.
size() == 1) {
529 template < GUM_Numeric GUM_SCALAR >
531 std::unique_ptr< ASTtree< GUM_SCALAR > >
term) :
534 template < GUM_Numeric GUM_SCALAR >
536 std::unique_ptr< ASTtree< GUM_SCALAR > >
term) :
537 ASTtree< GUM_SCALAR >(
"_sum_") {
538 if (vars.empty()) { throw gum::InvalidArgument(
"ASTsum: variable list cannot be empty"); }
542 if (vars.size() > 1) {
543 std::vector< std::string > tail(vars.begin() + 1, vars.end());
544 _term = std::make_unique< ASTsum< GUM_SCALAR > >(tail, std::move(term));
546 _term = std::move(term);
550 template < GUM_Numeric GUM_SCALAR >
555 template < GUM_Numeric GUM_SCALAR >
560 template < GUM_Numeric GUM_SCALAR >
563 "{}sum on {} for\n{}",
569 template < GUM_Numeric GUM_SCALAR >
571 return "\\left(" +
fastToLatex(nameOccur) +
"\\right)";
574 template < GUM_Numeric GUM_SCALAR >
577 std::vector< std::string > vars;
578 const ASTtree< GUM_SCALAR >* a =
this;
580 if (!nameOccur.
exists(s->_var)) nameOccur.
insert(s->_var, 0);
581 nameOccur[s->_var] += 1;
582 vars.push_back(s->_var);
588 for (
const auto& v: vars) {
595 for (
size_t i = 0; i < corr.size(); ++i) {
601 std::string out = std::format(
"\\sum_{{{}}}{{{}}}", names, a->protectToLatex(nameOccur));
604 for (
const auto& v: vars) {
612 template < GUM_Numeric GUM_SCALAR >
614 return std::make_unique< ASTsum< GUM_SCALAR > >(
_var,
_term->copy());
617 template < GUM_Numeric GUM_SCALAR >
620 auto base =
_term->eval(contextual_bn);
625 return base.sumOut(dv_set);
631 template < GUM_Numeric GUM_SCALAR >
632 std::unique_ptr< ASTtree< GUM_SCALAR > >
633 productOfTrees(std::vector< std::unique_ptr< ASTtree< GUM_SCALAR > > >&& lterms) {
636 auto it = lterms.begin();
637 auto end = lterms.end();
640 std::unique_ptr< ASTtree< GUM_SCALAR > > acc = std::move(*it);
644 for (; it != end; ++it) {
645 acc = std::make_unique< ASTmult< GUM_SCALAR > >(std::move(acc), std::move(*it));
Class representing the minimal interface for Bayesian network with no numerical data.
ASTBinaryOp(std::string_view typ, std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
const ASTtree< GUM_SCALAR > & op2() const
const ASTtree< GUM_SCALAR > & op1() const
std::unique_ptr< ASTtree< GUM_SCALAR > > _op2
std::unique_ptr< ASTtree< GUM_SCALAR > > _op1
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
ASTdiv(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
ASTjointProba(const Set< std::string > &varNames)
Build a joint (\mathbb{P}(\mathrm{varNames})).
const Set< std::string > & varNames() const noexcept
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
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.
Set< std::string > _varNames
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
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.
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
ASTminus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
ASTmult(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
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).
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
ASTplus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
static void _ensure_nonempty(const Set< std::string > &vars)
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Set< std::string > _vars
names of conditioned variables
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)
const Set< std::string > & knw() const noexcept
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Set< std::string > _knw
names of conditioning variables (already minimalized)
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.
static Set< std::string > _compute_knw_from_bn(const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
const Set< std::string > & vars() const noexcept
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
std::string _var
variable to eliminate
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
std::unique_ptr< ASTtree< GUM_SCALAR > > _term
sub-expression
ASTsum(std::string_view var, std::unique_ptr< ASTtree< GUM_SCALAR > > term)
Single-variable summation ( \sum_{\text{var}} \text{term} ).
const ASTtree< GUM_SCALAR > & term() const
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
const std::string & var() const
virtual std::string fastToLatex(HashTable< std::string, int > &nameOccur) const =0
Fast LaTeX rendering (lighter protection).
ASTtree(std::string_view type)
Construct an AST node with a descriptive type string (used in dumps).
std::string toLatex(HashTable< std::string, int > nameOccur=HashTable< std::string, int >()) const
Convenience wrapper using an empty occurrence table.
static constexpr const char * CONTINUE_PREFIX
Prefix used to draw tree branches in toString.
static std::string _latexCorrect(std::string_view srcName, HashTable< std::string, int > &nameOccur)
Sanitize a single variable name for LaTeX and ensure uniqueness.
const std::string & type() const noexcept
const T2 & second(const T1 &first) const
const T1 & first(const T2 &second) const
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
Virtual base class for PGMs using a DAG.
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeId idFromName(std::string_view name) const override
Returns the NodeId of a variable given its name.
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
virtual void makeInference() final
perform the heavy computations needed to compute the targets' posteriors
The class for generic Hash Tables.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
mapped_type & getWithDefault(const Key &key, const Val &default_value)
Returns a reference on the element the key of which is passed in argument.
Class representing the minimal interface for Bayesian network with no numerical data.
virtual const Tensor< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
Exception: at least one argument passed to a function is not what was expected.
const Tensor< GUM_SCALAR > & posterior(NodeId node) final
Computes and returns the posterior of a node.
Tensor< GUM_SCALAR > evidenceJointImpact(const NodeSet &targets, const NodeSet &evs)
Create a gum::Tensor for P(joint targets|evs) (for all instantiation of targets and evs).
virtual void addJointTarget(const NodeSet &joint_target) final
Add a set of nodes as a new joint target. As a collateral effect, every node is added as a marginal t...
virtual const Tensor< GUM_SCALAR > & jointPosterior(const NodeSet &nodes) final
Compute the joint posterior of a set of nodes.
Implementation of a Shafer-Shenoy's-like version of lazy propagation for inference in Bayesian networ...
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
bool empty() const noexcept
Indicates whether the set is the empty set.
iterator begin() const
The usual unsafe begin iterator to parse the set.
void insert(const Key &k)
Inserts a new element into the set.
Size size() const noexcept
Returns the number of elements in the set.
const std::string & name() const
returns the name of the variable
Abstract Syntax Tree (AST) for algebraic probability expressions used in do-Calculus.
#define GUM_ERROR(type, msg)
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Implementation of a Shafer-Shenoy's-like version of lazy propagation for inference in Bayesian networ...
gum is the global namespace for all aGrUM entities
Set< const DiscreteVariable * > VariableSet
std::unique_ptr< ASTtree< GUM_SCALAR > > productOfTrees(std::vector< std::unique_ptr< ASTtree< GUM_SCALAR > > > &<erms)
Build a product AST from a list of terms.