aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
multiDimLogit_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4
* info_at_agrum_dot_org
5
*
6
* This library is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18
*
19
*/
20
21
22
/**
23
* @brief class for LOGIT implementation as multiDim
24
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
25
*/
26
27
#
include
<
agrum
/
tools
/
core
/
exceptions
.
h
>
28
#
include
<
agrum
/
tools
/
multidim
/
ICIModels
/
multiDimLogit
.
h
>
29
#
include
<
agrum
/
tools
/
multidim
/
implementations
/
multiDimImplementation
.
h
>
30
31
namespace
gum
{
32
33
// Default constructor
34
template
<
typename
GUM_SCALAR >
35
INLINE MultiDimLogit<
GUM_SCALAR
>::
MultiDimLogit
(
GUM_SCALAR
external_weight
,
36
GUM_SCALAR
default_weight
) :
37
MultiDimICIModel
<
GUM_SCALAR
>(
external_weight
,
default_weight
) {
38
GUM_CONSTRUCTOR
(
MultiDimLogit
);
39
}
40
41
// Default constructor
42
template
<
typename
GUM_SCALAR
>
43
INLINE
MultiDimLogit
<
GUM_SCALAR
>::
MultiDimLogit
(
44
const
MultiDimLogit
<
GUM_SCALAR
>&
from
) :
45
MultiDimICIModel
<
GUM_SCALAR
>(
from
) {
46
GUM_CONS_CPY
(
MultiDimLogit
);
47
}
48
49
// Copy constructor using a bijection to replace variables from source.
50
template
<
typename
GUM_SCALAR
>
51
INLINE
MultiDimLogit
<
GUM_SCALAR
>::
MultiDimLogit
(
52
const
Bijection
<
const
DiscreteVariable
*,
const
DiscreteVariable
* >&
bij
,
53
const
MultiDimLogit
<
GUM_SCALAR
>&
from
) :
54
MultiDimICIModel
<
GUM_SCALAR
>(
bij
,
from
) {
55
GUM_CONSTRUCTOR
(
MultiDimLogit
);
56
}
57
58
// destructor
59
template
<
typename
GUM_SCALAR
>
60
INLINE
MultiDimLogit
<
GUM_SCALAR
>::~
MultiDimLogit
() {
61
GUM_DESTRUCTOR
(
MultiDimLogit
);
62
}
63
64
template
<
typename
GUM_SCALAR
>
65
GUM_SCALAR
MultiDimLogit
<
GUM_SCALAR
>::
get
(
const
Instantiation
&
i
)
const
{
66
if
(
this
->
nbrDim
() < 1) {
67
GUM_ERROR
(
OperationNotAllowed
,
"Not enough variable for a Logit"
);
68
}
69
70
const
DiscreteVariable
&
C
=
this
->
variable
((
Idx
)0);
71
72
if
(
i
.
val
(
C
) > 1)
return
(
GUM_SCALAR
)0.0;
73
74
GUM_SCALAR
fact
=
this
->
externalWeight
();
75
76
for
(
Idx
j
= 1;
j
<
this
->
nbrDim
();
j
++) {
77
const
DiscreteVariable
&
v
=
this
->
variable
(
j
);
78
fact
+=
GUM_SCALAR
(
this
->
causalWeight
(
v
)
79
*
this
->
variable
(
j
).
numerical
(
i
.
val
(
v
)));
80
}
81
82
fact
= 1 / (1 +
std
::
exp
(-
fact
));
// or std::exp(fact)/(1+std::exp(fact))
83
auto
res
= (
i
.
val
(
C
) == 1) ?
fact
: (
GUM_SCALAR
)1.0 -
fact
;
84
85
return
res
;
86
}
87
88
template
<
typename
GUM_SCALAR
>
89
std
::
string
MultiDimLogit
<
GUM_SCALAR
>::
toString
()
const
{
90
std
::
stringstream
s
;
91
s
<<
this
->
variable
(0) <<
"=logit("
<<
this
->
externalWeight
();
92
93
for
(
Idx
i
= 1;
i
<
this
->
nbrDim
();
i
++) {
94
GUM_SCALAR
c
=
this
->
causalWeight
(
this
->
variable
(
i
));
95
96
if
(
c
!=
GUM_SCALAR
(0)) {
97
s
<<
" "
;
98
99
if
(
c
> 0)
s
<<
"+"
;
100
101
s
<<
this
->
causalWeight
(
this
->
variable
(
i
)) <<
"*"
<<
this
->
variable
(
i
);
102
}
103
}
104
105
s
<<
")"
;
106
107
return
s
.
str
();
108
}
109
110
// For friendly displaying the content of the variable.
111
template
<
typename
GUM_SCALAR
>
112
INLINE
std
::
ostream
&
operator
<<(
std
::
ostream
&
s
,
113
const
MultiDimLogit
<
GUM_SCALAR
>&
ag
) {
114
return
s
<<
ag
.
toString
();
115
}
116
117
template
<
typename
GUM_SCALAR
>
118
INLINE
MultiDimContainer
<
GUM_SCALAR
>*
119
MultiDimLogit
<
GUM_SCALAR
>::
newFactory
()
const
{
120
return
new
MultiDimLogit
<
GUM_SCALAR
>(
this
->
external_weight__
,
121
this
->
default_weight__
);
122
}
123
124
// returns the name of the implementation
125
template
<
typename
GUM_SCALAR
>
126
INLINE
const
std
::
string
&
MultiDimLogit
<
GUM_SCALAR
>::
name
()
const
{
127
static
const
std
::
string
str
=
"MultiDimLogit"
;
128
return
str
;
129
}
130
131
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669