aGrUM  0.14.2
multiDimLogit_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #include <agrum/core/exceptions.h>
28 
29 namespace gum {
30 
31  // Default constructor
32  template < typename GUM_SCALAR >
33  INLINE MultiDimLogit< GUM_SCALAR >::MultiDimLogit(GUM_SCALAR external_weight,
34  GUM_SCALAR default_weight) :
35  MultiDimICIModel< GUM_SCALAR >(external_weight, default_weight) {
36  GUM_CONSTRUCTOR(MultiDimLogit);
37  }
38 
39  // Default constructor
40  template < typename GUM_SCALAR >
42  const MultiDimLogit< GUM_SCALAR >& from) :
43  MultiDimICIModel< GUM_SCALAR >(from) {
44  GUM_CONS_CPY(MultiDimLogit);
45  }
46 
47  // Copy constructor using a bijection to replace variables from source.
48  template < typename GUM_SCALAR >
51  const MultiDimLogit< GUM_SCALAR >& from) :
52  MultiDimICIModel< GUM_SCALAR >(bij, from) {
53  GUM_CONSTRUCTOR(MultiDimLogit);
54  }
55 
56  // destructor
57  template < typename GUM_SCALAR >
59  GUM_DESTRUCTOR(MultiDimLogit);
60  }
61 
62  template < typename GUM_SCALAR >
63  GUM_SCALAR MultiDimLogit< GUM_SCALAR >::get(const Instantiation& i) const {
64  if (this->nbrDim() < 1) {
65  GUM_ERROR(OperationNotAllowed, "Not enough variable for a Logit");
66  }
67 
68  const DiscreteVariable& C = this->variable((Idx)0);
69 
70  if (i.val(C) > 1) return (GUM_SCALAR)0.0;
71 
72  GUM_SCALAR fact = this->externalWeight();
73 
74  for (Idx j = 1; j < this->nbrDim(); j++) {
75  const DiscreteVariable& v = this->variable(j);
76  fact +=
77  GUM_SCALAR(this->causalWeight(v) * this->variable(j).numerical(i.val(v)));
78  }
79 
80  fact = 1 / (1 + std::exp(-fact)); // or std::exp(fact)/(1+std::exp(fact))
81  auto res = (i.val(C) == 1) ? fact : (GUM_SCALAR)1.0 - fact;
82 
83  return res;
84  }
85 
86  template < typename GUM_SCALAR >
87  const std::string MultiDimLogit< GUM_SCALAR >::toString() const {
88  std::stringstream s;
89  s << this->variable(0) << "=logit(" << this->externalWeight();
90 
91  for (Idx i = 1; i < this->nbrDim(); i++) {
92  GUM_SCALAR c = this->causalWeight(this->variable(i));
93 
94  if (c != GUM_SCALAR(0)) {
95  s << " ";
96 
97  if (c > 0) s << "+";
98 
99  s << this->causalWeight(this->variable(i)) << "*" << this->variable(i);
100  }
101  }
102 
103  s << ")";
104 
105  return s.str();
106  }
107 
108  // For friendly displaying the content of the variable.
109  template < typename GUM_SCALAR >
110  INLINE std::ostream& operator<<(std::ostream& s,
111  const MultiDimLogit< GUM_SCALAR >& ag) {
112  return s << ag.toString();
113  }
114 
115  template < typename GUM_SCALAR >
119  this->__default_weight);
120  }
121 
122  // returns the name of the implementation
123  template < typename GUM_SCALAR >
124  INLINE const std::string& MultiDimLogit< GUM_SCALAR >::name() const {
125  static const std::string str = "MultiDimLogit";
126  return str;
127  }
128 
129 } /* 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.
class for LOGIT implementation as multiDim
Base class for discrete random variable.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the real name of the multiDimArray.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
Headers of gum::MultiDimImplementation.
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:583
Logit representation.
Definition: multiDimLogit.h:50
aGrUM&#39;s exceptions
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:1803
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:80
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:50
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
MultiDimLogit(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 0.0)
Default constructor.