66 template < GUM_Numeric GUM_SCALAR >
71 template < GUM_Numeric GUM_SCALAR >
77 template < GUM_Numeric GUM_SCALAR >
82 template < GUM_Numeric GUM_SCALAR >
88 template < GUM_Numeric GUM_SCALAR >
91 if (
this != &source) {
99 template < GUM_Numeric GUM_SCALAR >
101 if (
this != &source) {
108 template < GUM_Numeric GUM_SCALAR >
113 template < GUM_Numeric GUM_SCALAR >
117 for (
auto node:
nodes()) {
120 for (
auto parent:
parents(node))
129 template < GUM_Numeric GUM_SCALAR >
132 for (
auto node:
nodes()) {
133 auto v =
variable(node).domainSize();
134 if (v > res) { res = v; }
139 template < GUM_Numeric GUM_SCALAR >
141 GUM_SCALAR res = 1.0;
142 for (
auto node:
nodes()) {
144 if (v < res) { res = v; }
149 template < GUM_Numeric GUM_SCALAR >
151 GUM_SCALAR res = 1.0;
152 for (
auto node:
nodes()) {
154 if (v > res) { res = v; }
159 template < GUM_Numeric GUM_SCALAR >
161 GUM_SCALAR res = 1.0;
163 auto v =
cpt(node).minNonZero();
164 if (v < res) { res = v; }
169 template < GUM_Numeric GUM_SCALAR >
171 GUM_SCALAR res = 0.0;
172 for (
auto node:
nodes()) {
173 auto v =
cpt(node).maxNonOne();
174 if (v > res) { res = v; }
179 template < GUM_Numeric GUM_SCALAR >
184 usedMem +=
cpt(node).memoryFootprint();
188 template < GUM_Numeric GUM_SCALAR >
191 s << std::format(
"BN{{nodes: {}, arcs: {}, ",
size(),
dag().
sizeArcs());
197 template < GUM_Numeric GUM_SCALAR >
199 std::stringstream output;
203 output << std::format(
"digraph \"{}\" {{\n", bn_name);
204 output << std::format(
" graph [bgcolor=transparent,label=\"{}\"];\n", bn_name);
205 output <<
" node [style=filled fillcolor=\"#ffffaa\"];" << std::endl << std::endl;
207 for (
auto node:
nodes())
208 output << std::format(
"\"{}\" [comment=\"{}:{}\"];\n",
211 variable(node).toStringWithDescription());
215 std::string tab =
" ";
217 for (
auto node:
nodes()) {
220 output << std::format(
" \"{}\" -> \"{}\";\n",
224 }
else if (parents(node).size() == 0) {
225 output << std::format(
" \"{}\";\n", variable(node).name());
229 output <<
"}" << std::endl;
237 template < GUM_Numeric GUM_SCALAR >
239 auto value = (GUM_SCALAR)1.0;
243 for (
auto node:
nodes()) {
244 if ((tmp =
cpt(node)[i]) == (GUM_SCALAR)0) {
return (GUM_SCALAR)0; }
255 template < GUM_Numeric GUM_SCALAR >
257 auto value = (GUM_SCALAR)0.0;
261 for (
auto node:
nodes()) {
262 if ((tmp =
cpt(node)[i]) == (GUM_SCALAR)0) {
263 return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
266 value += std::log2(
cpt(node)[i]);
272 template < GUM_Numeric GUM_SCALAR >
274 if (
size() != from.
size()) {
return false; }
278 for (
auto node:
nodes()) {
280 if (!from.
exists(v1.name())) {
return false; }
282 if (v1 != v2) {
return false; }
285 for (
auto node:
nodes()) {
288 if (
cpt(node).nbrDim() != from.
cpt(fromnode).nbrDim()) {
return false; }
290 if (
cpt(node).domainSize() != from.
cpt(fromnode).domainSize()) {
return false; }
292 for (
Idx i = 0; i <
cpt(node).nbrDim(); ++i) {
303 for (
Idx indice = 0; indice <
cpt(node).nbrDim(); ++indice) {
308 if (cmp(
cpt(node).get(i), from.
cpt(fromnode).get(j))) {
return false; }
315 template < GUM_Numeric GUM_SCALAR >
321 template < GUM_Numeric GUM_SCALAR >
323 std::vector< std::string > comments;
325 const double epsilon = 1e-8;
326 const double error_epsilon = 1e-1;
329 for (
const auto i:
nodes())
332 s <<
"Variable " <<
variable(i).name() <<
": not consistent (domainSize=1).";
333 comments.push_back(s.str());
338 for (
const auto i:
nodes()) {
339 const auto [amin, minval] =
cpt(i).argmin();
340 if (minval < (GUM_SCALAR)0.0) {
342 s <<
"Variable " <<
variable(i).name() <<
" : P(" << *(amin.begin()) <<
") < 0.0";
343 comments.push_back(s.str());
347 for (
const auto i:
nodes()) {
348 const auto [amax, maxval] =
cpt(i).argmax();
349 if (maxval > (GUM_SCALAR)1.0) {
351 s <<
"Variable " <<
variable(i).name() <<
" : P(" << *(amax.begin()) <<
") > 1.0";
352 comments.push_back(s.str());
357 for (
const auto i:
nodes()) {
359 const auto [amin, minval] = p.argmin();
360 if (minval < (GUM_SCALAR)(1.0 - epsilon)) {
362 s <<
"Variable " <<
variable(i).name() <<
" : ";
363 if (!
parents(i).
empty()) s <<
"with (at least) parents " << *(amin.begin()) <<
", ";
364 s <<
"the CPT sum to less than 1";
365 if (minval > (GUM_SCALAR)(1.0 - error_epsilon)) s <<
" (normalization problem ?)";
367 comments.push_back(s.str());
370 const auto [amax, maxval] = p.argmax();
371 if (maxval > (GUM_SCALAR)(1.0 + epsilon)) {
373 s <<
"Variable " <<
variable(i).name() <<
" : ";
374 if (!
parents(i).
empty()) s <<
"with (at least) parents " << *(amax.begin()) <<
", ";
375 s <<
"the CPT sum to more than 1";
376 if (maxval < (GUM_SCALAR)(1.0 + error_epsilon)) s <<
" (normalization problem ?)";
378 comments.push_back(s.str());
385 template < GUM_Numeric GUM_SCALAR >
390 template < GUM_Numeric GUM_SCALAR >
396 template < GUM_Numeric GUM_SCALAR >
401 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
DAGmodel()
Default constructor.
const NodeSet & children(const NodeId id) const
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
Return the value of the property name of this GraphicalModel.
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.
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
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
gum is the global namespace for all aGrUM entities
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.
Header of the Tensor class.