aGrUM  0.16.0
multiDimLogit_tpl.h
Go to the documentation of this file.
1 
28 #include <agrum/core/exceptions.h>
31 
32 namespace gum {
33 
34  // Default constructor
35  template < typename GUM_SCALAR >
36  INLINE MultiDimLogit< GUM_SCALAR >::MultiDimLogit(GUM_SCALAR external_weight,
37  GUM_SCALAR default_weight) :
38  MultiDimICIModel< GUM_SCALAR >(external_weight, default_weight) {
39  GUM_CONSTRUCTOR(MultiDimLogit);
40  }
41 
42  // Default constructor
43  template < typename GUM_SCALAR >
45  const MultiDimLogit< GUM_SCALAR >& from) :
46  MultiDimICIModel< GUM_SCALAR >(from) {
47  GUM_CONS_CPY(MultiDimLogit);
48  }
49 
50  // Copy constructor using a bijection to replace variables from source.
51  template < typename GUM_SCALAR >
54  const MultiDimLogit< GUM_SCALAR >& from) :
55  MultiDimICIModel< GUM_SCALAR >(bij, from) {
56  GUM_CONSTRUCTOR(MultiDimLogit);
57  }
58 
59  // destructor
60  template < typename GUM_SCALAR >
62  GUM_DESTRUCTOR(MultiDimLogit);
63  }
64 
65  template < typename GUM_SCALAR >
66  GUM_SCALAR MultiDimLogit< GUM_SCALAR >::get(const Instantiation& i) const {
67  if (this->nbrDim() < 1) {
68  GUM_ERROR(OperationNotAllowed, "Not enough variable for a Logit");
69  }
70 
71  const DiscreteVariable& C = this->variable((Idx)0);
72 
73  if (i.val(C) > 1) return (GUM_SCALAR)0.0;
74 
75  GUM_SCALAR fact = this->externalWeight();
76 
77  for (Idx j = 1; j < this->nbrDim(); j++) {
78  const DiscreteVariable& v = this->variable(j);
79  fact +=
80  GUM_SCALAR(this->causalWeight(v) * this->variable(j).numerical(i.val(v)));
81  }
82 
83  fact = 1 / (1 + std::exp(-fact)); // or std::exp(fact)/(1+std::exp(fact))
84  auto res = (i.val(C) == 1) ? fact : (GUM_SCALAR)1.0 - fact;
85 
86  return res;
87  }
88 
89  template < typename GUM_SCALAR >
90  const std::string MultiDimLogit< GUM_SCALAR >::toString() const {
91  std::stringstream s;
92  s << this->variable(0) << "=logit(" << this->externalWeight();
93 
94  for (Idx i = 1; i < this->nbrDim(); i++) {
95  GUM_SCALAR c = this->causalWeight(this->variable(i));
96 
97  if (c != GUM_SCALAR(0)) {
98  s << " ";
99 
100  if (c > 0) s << "+";
101 
102  s << this->causalWeight(this->variable(i)) << "*" << this->variable(i);
103  }
104  }
105 
106  s << ")";
107 
108  return s.str();
109  }
110 
111  // For friendly displaying the content of the variable.
112  template < typename GUM_SCALAR >
113  INLINE std::ostream& operator<<(std::ostream& s,
114  const MultiDimLogit< GUM_SCALAR >& ag) {
115  return s << ag.toString();
116  }
117 
118  template < typename GUM_SCALAR >
122  this->__default_weight);
123  }
124 
125  // returns the name of the implementation
126  template < typename GUM_SCALAR >
127  INLINE const std::string& MultiDimLogit< GUM_SCALAR >::name() const {
128  static const std::string str = "MultiDimLogit";
129  return str;
130  }
131 
132 } /* namespace gum */
virtual const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
virtual const std::string & name() const
Returns the real name of the multiDimArray.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Base class for discrete random variable.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the real name of the multiDimArray.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const std::string toString() const
Returns the real name of the multiDimArray.
GUM_SCALAR __external_weight
in Henrion (89).
Idx val(Idx i) const
Returns the current value of the variable at position i.
GUM_SCALAR externalWeight() const
Copy of a multiDimICIModel.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
Logit representation.
Definition: multiDimLogit.h:53
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
abstract class for Conditional Indepency Models
GUM_SCALAR __default_weight
in Henrion (89) in a hashtable with a default_value.
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
This method creates a clone of this object, withouth its content (including variable), you must use this method if you want to ensure that the generated object has the same type than the object containing the called newFactory()
virtual ~MultiDimLogit()
Destructor.
Size Idx
Type for indexes.
Definition: types.h:53
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
MultiDimLogit(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 0.0)
Default constructor.