68 template < GUM_Numeric GUM_SCALAR >
73 template < GUM_Numeric GUM_SCALAR >
79 template < GUM_Numeric GUM_SCALAR >
84 template < GUM_Numeric GUM_SCALAR >
90 template < GUM_Numeric GUM_SCALAR >
93 if (
this != &source) {
101 template < GUM_Numeric GUM_SCALAR >
103 if (
this != &source) {
110 template < GUM_Numeric GUM_SCALAR >
115 template < GUM_Numeric GUM_SCALAR >
119 for (
auto node:
nodes()) {
122 for (
auto parent:
parents(node))
131 template < GUM_Numeric GUM_SCALAR >
134 for (
auto node:
nodes()) {
135 auto v =
variable(node).domainSize();
136 if (v > res) { res = v; }
141 template < GUM_Numeric GUM_SCALAR >
143 GUM_SCALAR res = 1.0;
144 for (
auto node:
nodes()) {
145 auto v =
cpt(node).min();
146 if (v < res) { res = v; }
151 template < GUM_Numeric GUM_SCALAR >
153 GUM_SCALAR res = 1.0;
154 for (
auto node:
nodes()) {
155 auto v =
cpt(node).max();
156 if (v > res) { res = v; }
161 template < GUM_Numeric GUM_SCALAR >
163 GUM_SCALAR res = 1.0;
164 for (
auto node:
nodes()) {
165 auto v =
cpt(node).minNonZero();
166 if (v < res) { res = v; }
171 template < GUM_Numeric GUM_SCALAR >
173 GUM_SCALAR res = 0.0;
174 for (
auto node:
nodes()) {
175 auto v =
cpt(node).maxNonOne();
176 if (v > res) { res = v; }
181 template < GUM_Numeric GUM_SCALAR >
185 for (
auto node:
nodes())
186 usedMem +=
cpt(node).memoryFootprint();
190 template < GUM_Numeric GUM_SCALAR >
199 template < GUM_Numeric GUM_SCALAR >
204 template < GUM_Numeric GUM_SCALAR >
210 for (
const auto node:
nodes()) {
211 const auto& name =
variable(node).name();
216 "(" + std::to_string(node) +
") " + name));
219 for (
const auto& arc: arcs()) {
223 std::to_string(arc.tail()) +
" → "
224 + std::to_string(arc.head()));
226 writer.edge(
variable(arc.tail()).name(),
variable(arc.head()).name(), attributes);
235 template < GUM_Numeric GUM_SCALAR >
237 auto value = (GUM_SCALAR)1.0;
241 for (
auto node:
nodes()) {
242 if ((tmp =
cpt(node)[i]) == (GUM_SCALAR)0) {
return (GUM_SCALAR)0; }
253 template < GUM_Numeric GUM_SCALAR >
255 auto value = (GUM_SCALAR)0.0;
260 if ((tmp =
cpt(node)[i]) == (GUM_SCALAR)0) {
261 return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
264 value += std::log2(
cpt(node)[i]);
270 template < GUM_Numeric GUM_SCALAR >
272 if (
size() != from.
size()) {
return false; }
276 for (
auto node:
nodes()) {
278 if (!from.
exists(v1.name())) {
return false; }
280 if (v1 != v2) {
return false; }
283 for (
auto node:
nodes()) {
286 if (
cpt(node).nbrDim() != from.
cpt(fromnode).nbrDim()) {
return false; }
288 if (
cpt(node).domainSize() != from.
cpt(fromnode).domainSize()) {
return false; }
301 for (
Idx indice = 0; indice <
cpt(node).nbrDim(); ++indice) {
306 if (cmp(
cpt(node).get(i), from.
cpt(fromnode).get(j))) {
return false; }
313 template < GUM_Numeric GUM_SCALAR >
319 template < GUM_Numeric GUM_SCALAR >
321 std::vector< std::string > comments;
327 for (
const auto i:
nodes())
330 s <<
"Variable " <<
variable(i).name() <<
": not consistent (domainSize=1).";
331 comments.push_back(s.str());
336 for (
const auto i:
nodes()) {
337 const auto [amin, minval] =
cpt(i).argmin();
338 if (minval < (GUM_SCALAR)0.0) {
340 s <<
"Variable " <<
variable(i).name() <<
" : P(" << *(amin.begin()) <<
") < 0.0";
341 comments.push_back(s.str());
345 for (
const auto i:
nodes()) {
346 const auto [amax, maxval] =
cpt(i).argmax();
347 if (maxval > (GUM_SCALAR)1.0) {
349 s <<
"Variable " <<
variable(i).name() <<
" : P(" << *(amax.begin()) <<
") > 1.0";
350 comments.push_back(s.str());
355 for (
const auto i:
nodes()) {
357 const auto [amin, minval] = p.argmin();
358 if (minval < (GUM_SCALAR)(1.0 - epsilon)) {
360 s <<
"Variable " <<
variable(i).name() <<
" : ";
361 if (!
parents(i).
empty()) s <<
"with (at least) parents " << *(amin.begin()) <<
", ";
362 s <<
"the CPT sum to less than 1";
363 if (minval > (GUM_SCALAR)(1.0 - error_epsilon)) s <<
" (normalization problem ?)";
365 comments.push_back(s.str());
368 const auto [amax, maxval] = p.argmax();
369 if (maxval > (GUM_SCALAR)(1.0 + epsilon)) {
371 s <<
"Variable " <<
variable(i).name() <<
" : ";
372 if (!
parents(i).
empty()) s <<
"with (at least) parents " << *(amax.begin()) <<
", ";
373 s <<
"the CPT sum to more than 1";
374 if (maxval < (GUM_SCALAR)(1.0 + error_epsilon)) s <<
" (normalization problem ?)";
376 comments.push_back(s.str());
383 template < GUM_Numeric GUM_SCALAR >
388 template < GUM_Numeric GUM_SCALAR >
394 template < GUM_Numeric GUM_SCALAR >
399 template < GUM_Numeric GUM_SCALAR >
Class representing the minimal interface for Bayesian network with no numerical data.
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
const DiscreteVariable & variable(NodeId id) const override
double epsilonStandard() const noexcept
double epsilonCoarse() const noexcept
The coarse tolerance (key core.epsilonCoarse, 1e-2 by default).
DAGmodel()
Default constructor.
DAGmodel & operator=(const DAGmodel &source)
Private copy operator.
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
const NodeGraphPart & nodes() const final
Returns a named copy of the internal DAG: each node id is assigned the name of the corresponding vari...
NodeId idFromName(std::string_view name) const override
Returns the NodeId of a variable given its name.
const DiscreteVariable & variableFromName(std::string_view name) const override
Returns a constant reference over a variable given its name.
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
Base class for discrete random variable.
void setProperty(std::string_view name, std::string_view value)
Add or change a property of this GraphicalModel.
double log10DomainSize() const
virtual bool empty() const
Return true if this graphical model is empty.
static std::string spaceCplxToString(double dSize, int dim, Size usedMem)
const std::string & propertyWithDefault(std::string_view name, const std::string &byDefault) const
Class representing the minimal interface for Bayesian network with no numerical data.
Tensor< GUM_SCALAR > evGt(std::string_view name, double value) const
GUM_SCALAR minNonZeroParam() const
IBayesNet()
Default constructor.
GUM_SCALAR jointProbability(const Instantiation &i) const
Compute a parameter of the joint probability for the BN (given an instantiation of the vars).
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.
GUM_SCALAR maxParam() const
std::vector< std::string > check() const
Check if the BayesNet is consistent (variables, CPT).
~IBayesNet() override
Destructor.
Tensor< GUM_SCALAR > evIn(std::string_view name, double val1, double val2) const
Tensor< GUM_SCALAR > evLt(std::string_view name, double value) const
Size memoryFootprint() const
compute the (approximated) footprint in memory of the model (the footprints of CPTs)
Size maxVarDomainSize() const
GUM_SCALAR log2JointProbability(const Instantiation &i) const
Compute a parameter of the log joint probability for the BN (given an instantiation of the vars).
GUM_SCALAR maxNonOneParam() const
std::string toString() const
GUM_SCALAR minParam() const
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
virtual std::string toDot() const
Tensor< GUM_SCALAR > evEq(std::string_view name, double value) const
bool operator==(const IBayesNet< GUM_SCALAR > &from) const
This operator compares 2 BNs !
virtual const Tensor< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
Class for assigning/browsing values to tuples of discrete variables.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
bool end() const
Returns true if the Instantiation reached the end.
void inc()
Operator increment.
Idx pos(const DiscreteVariable &v) const final
Returns the position of the variable v.
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setFirst()
Assign the first values to the tuple of the Instantiation.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
static Tensor< GUM_SCALAR > evEq(const DiscreteVariable &v, double val)
numerical evidence generator
static Tensor< GUM_SCALAR > evGt(const DiscreteVariable &v, double val)
numerical evidence generator
static Tensor< GUM_SCALAR > evIn(const DiscreteVariable &v, double val1, double val2)
numerical evidence generator
static Tensor< GUM_SCALAR > evLt(const DiscreteVariable &v, double val)
numerical evidence generator
const std::string & name() const
returns the name of the variable
The options given to a toDot(const DotOptions&).
const DotArcOptions * arc(Size tail, Size head) const
the options of the arc from tail to head, or nullptr if none was given for it
const DotNodeOptions * node(Size node) const
the options of a node, or nullptr if none was given for it
Writes a graph in the dot language, statement after statement, in the order of the calls.
Typed, documented, sectioned configuration singleton read from an .ini content.
The statements common to the graphs drawn with graphviz : preamble, attributes of nodes and arcs.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
class for NoisyAND-net implementation as multiDim
class for multiDimNoisyORCompound
class for NoisyOR-net implementation as multiDim
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
void addDefaultAttribute(DotAttributes &attributes, std::string_view key, std::string_view value)
Adds an attribute at the beginning of the list, unless the list already gives a value for this key : ...
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...
gum is the global namespace for all aGrUM entities
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Abstract class for generating Conditional Probability Tables.
Indicate whether two elements are (almost) different or not.
static DotStyle read()
the style of the graphs of the section notebook
Header of the Tensor class.