65 template <
typename GUM_SCALAR >
70 template <
typename GUM_SCALAR >
76 template <
typename GUM_SCALAR >
81 template <
typename GUM_SCALAR >
89 template <
typename GUM_SCALAR >
94 template <
typename GUM_SCALAR >
98 for (
auto node:
nodes()) {
110 template <
typename GUM_SCALAR >
113 for (
auto node:
nodes()) {
114 auto v =
variable(node).domainSize();
115 if (v > res) { res = v; }
120 template <
typename GUM_SCALAR >
122 GUM_SCALAR res = 1.0;
123 for (
auto node:
nodes()) {
124 auto v =
cpt(node).min();
125 if (v < res) { res = v; }
130 template <
typename GUM_SCALAR >
132 GUM_SCALAR res = 1.0;
133 for (
auto node:
nodes()) {
134 auto v =
cpt(node).max();
135 if (v > res) { res = v; }
140 template <
typename GUM_SCALAR >
142 GUM_SCALAR res = 1.0;
143 for (
auto node:
nodes()) {
144 auto v =
cpt(node).minNonZero();
145 if (v < res) { res = v; }
150 template <
typename GUM_SCALAR >
152 GUM_SCALAR res = 0.0;
153 for (
auto node:
nodes()) {
154 auto v =
cpt(node).maxNonOne();
155 if (v > res) { res = v; }
160 template <
typename GUM_SCALAR >
164 for (
auto node:
nodes())
165 usedMem +=
cpt(node).memoryFootprint();
169 template <
typename GUM_SCALAR >
172 s <<
"BN{nodes: " <<
size() <<
", arcs: " <<
dag().sizeArcs() <<
", ";
178 template <
typename GUM_SCALAR >
180 std::stringstream output;
181 output <<
"digraph \"";
189 output << bn_name <<
"\" {" << std::endl;
190 output <<
" graph [bgcolor=transparent,label=\"" << bn_name <<
"\"];" << std::endl;
191 output <<
" node [style=filled fillcolor=\"#ffffaa\"];" << std::endl << std::endl;
193 for (
auto node: nodes())
194 output <<
"\"" << variable(node).name() <<
"\" [comment=\"" << node <<
":"
195 << variable(node).toStringWithDescription() <<
"\"];" << std::endl;
199 std::string tab =
" ";
201 for (
auto node:
nodes()) {
204 output << tab <<
"\"" <<
variable(node).name() <<
"\" -> " <<
"\""
205 <<
variable(child).name() <<
"\";" << std::endl;
207 }
else if (parents(node).size() == 0) {
208 output << tab <<
"\"" <<
variable(node).name() <<
"\";" << std::endl;
212 output <<
"}" << std::endl;
220 template <
typename GUM_SCALAR >
222 auto value = (GUM_SCALAR)1.0;
226 for (
auto node:
nodes()) {
227 if ((tmp =
cpt(node)[i]) == (GUM_SCALAR)0) {
return (GUM_SCALAR)0; }
238 template <
typename GUM_SCALAR >
240 auto value = (GUM_SCALAR)0.0;
244 for (
auto node:
nodes()) {
245 if ((tmp =
cpt(node)[i]) == (GUM_SCALAR)0) {
246 return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
249 value += std::log2(cpt(node)[i]);
255 template <
typename GUM_SCALAR >
257 if (
size() != from.
size()) {
return false; }
261 for (
auto node: nodes()) {
263 const auto& v1 = variable(node);
265 if (v1 != v2) {
return false; }
272 for (
auto node: nodes()) {
275 if (cpt(node).nbrDim() != from.
cpt(fromnode).nbrDim()) {
return false; }
277 if (cpt(node).domainSize() != from.
cpt(fromnode).domainSize()) {
return false; }
279 for (
Idx i = 0; i < cpt(node).nbrDim(); ++i) {
289 for (i.setFirst(); !i.end(); i.inc()) {
290 for (
Idx indice = 0; indice < cpt(node).nbrDim(); ++indice) {
295 if (cmp(cpt(node).get(i), from.
cpt(fromnode).get(j))) {
return false; }
302 template <
typename GUM_SCALAR >
307 template <
typename GUM_SCALAR >
313 template <
typename GUM_SCALAR >
315 std::vector< std::string > comments;
317 const double epsilon = 1e-8;
318 const double error_epsilon = 1e-1;
321 for (
const auto i:
nodes())
324 s <<
"Variable " <<
variable(i).name() <<
": not consistent (domainSize=1).";
325 comments.push_back(s.str());
330 for (
const auto i:
nodes()) {
331 const auto [amin, minval] =
cpt(i).argmin();
332 if (minval < (GUM_SCALAR)0.0) {
334 s <<
"Variable " <<
variable(i).name() <<
" : P(" << *(amin.begin()) <<
") < 0.0";
335 comments.push_back(s.str());
339 for (
const auto i:
nodes()) {
340 const auto [amax, maxval] =
cpt(i).argmax();
341 if (maxval > (GUM_SCALAR)1.0) {
343 s <<
"Variable " <<
variable(i).name() <<
" : P(" << *(amax.begin()) <<
") > 1.0";
344 comments.push_back(s.str());
349 for (
const auto i:
nodes()) {
351 const auto [amin, minval] = p.argmin();
352 if (minval < (GUM_SCALAR)(1.0 - epsilon)) {
354 s <<
"Variable " <<
variable(i).name() <<
" : ";
355 if (!
parents(i).
empty()) s <<
"with (at least) parents " << *(amin.begin()) <<
", ";
356 s <<
"the CPT sum to less than 1";
357 if (minval > (GUM_SCALAR)(1.0 - error_epsilon)) s <<
" (normalization problem ?)";
359 comments.push_back(s.str());
362 const auto [amax, maxval] = p.argmax();
363 if (maxval > (GUM_SCALAR)(1.0 + epsilon)) {
365 s <<
"Variable " <<
variable(i).name() <<
" : ";
366 if (!
parents(i).
empty()) s <<
"with (at least) parents " << *(amax.begin()) <<
", ";
367 s <<
"the CPT sum to more than 1";
368 if (maxval < (GUM_SCALAR)(1.0 + error_epsilon)) s <<
" (normalization problem ?)";
370 comments.push_back(s.str());
377 template <
typename GUM_SCALAR >
382 template <
typename GUM_SCALAR >
388 template <
typename GUM_SCALAR >
393 template <
typename GUM_SCALAR >
Class representing the minimal interface for Bayesian network with no numerical data.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
DAGmodel()
Default constructor.
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
const NodeSet & children(const NodeId id) const
DAGmodel & operator=(const DAGmodel &source)
Private copy operator.
const NodeSet & parents(const NodeId id) const
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Base class for discrete random variable.
static void spaceCplxToStream(std::stringstream &s, double dSize, int dim, Size usedMem)
send to the stream the space complexity with 3 parametrs
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
const std::string & property(const std::string &name) const
double log10DomainSize() const
virtual bool empty() const
Return true if this graphical model is empty.
Class representing the minimal interface for Bayesian network with no numerical data.
GUM_SCALAR minNonZeroParam() const
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.
Size memoryFootprint() const
compute the (approximated) footprint in memory of the model (the footprints of CPTs)
virtual const DiscreteVariable & variableFromName(const std::string &name) const =0
Getter by name.
GUM_SCALAR maxParam() const
std::string toString() const
std::vector< std::string > check() const
Check if the BayesNet is consistent (variables, CPT).
Tensor< GUM_SCALAR > evIn(const std::string &name, double val1, double val2) const
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
virtual const DiscreteVariable & variable(NodeId id) const=0
GUM_SCALAR minParam() const
Tensor< GUM_SCALAR > evEq(const std::string &name, double value) const
Tensor< GUM_SCALAR > evGt(const std::string &name, double value) const
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
Tensor< GUM_SCALAR > evLt(const std::string &name, double value) const
IBayesNet()
Default constructor.
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
virtual std::string toDot() 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.
bool operator!=(const IBayesNet< GUM_SCALAR > &from) const
virtual ~IBayesNet()
Destructor.
Class for assigning/browsing values to tuples of discrete variables.
Exception : the element we looked for cannot be found.
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
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
bool operator==(const HashTableIteratorSafe< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.
Abstract class for generating Conditional Probability Tables.
Indicate whether two elements are (almost) different or not.
Header of the Tensor class.