aGrUM
0.21.0
a C++ library for (probabilistic) graphical models
multiDimLogit_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by 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
(
const
MultiDimLogit
<
GUM_SCALAR
>&
from
) :
44
MultiDimICIModel
<
GUM_SCALAR
>(
from
) {
45
GUM_CONS_CPY
(
MultiDimLogit
);
46
}
47
48
// Copy constructor using a bijection to replace variables from source.
49
template
<
typename
GUM_SCALAR
>
50
INLINE
MultiDimLogit
<
GUM_SCALAR
>::
MultiDimLogit
(
51
const
Bijection
<
const
DiscreteVariable
*,
const
DiscreteVariable
* >&
bij
,
52
const
MultiDimLogit
<
GUM_SCALAR
>&
from
) :
53
MultiDimICIModel
<
GUM_SCALAR
>(
bij
,
from
) {
54
GUM_CONSTRUCTOR
(
MultiDimLogit
);
55
}
56
57
// destructor
58
template
<
typename
GUM_SCALAR
>
59
INLINE
MultiDimLogit
<
GUM_SCALAR
>::~
MultiDimLogit
() {
60
GUM_DESTRUCTOR
(
MultiDimLogit
);
61
}
62
63
template
<
typename
GUM_SCALAR
>
64
GUM_SCALAR
MultiDimLogit
<
GUM_SCALAR
>::
get
(
const
Instantiation
&
i
)
const
{
65
if
(
this
->
nbrDim
() < 1) {
GUM_ERROR
(
OperationNotAllowed
,
"Not enough variable for a Logit"
) }
66
67
const
DiscreteVariable
&
C
=
this
->
variable
((
Idx
)0);
68
69
if
(
i
.
val
(
C
) > 1)
return
(
GUM_SCALAR
)0.0;
70
71
GUM_SCALAR
fact
=
this
->
externalWeight
();
72
73
for
(
Idx
j
= 1;
j
<
this
->
nbrDim
();
j
++) {
74
const
DiscreteVariable
&
v
=
this
->
variable
(
j
);
75
fact
+=
GUM_SCALAR
(
this
->
causalWeight
(
v
) *
this
->
variable
(
j
).
numerical
(
i
.
val
(
v
)));
76
}
77
78
fact
= 1 / (1 +
std
::
exp
(-
fact
));
// or std::exp(fact)/(1+std::exp(fact))
79
auto
res
= (
i
.
val
(
C
) == 1) ?
fact
: (
GUM_SCALAR
)1.0 -
fact
;
80
81
return
res
;
82
}
83
84
template
<
typename
GUM_SCALAR
>
85
std
::
string
MultiDimLogit
<
GUM_SCALAR
>::
toString
()
const
{
86
std
::
stringstream
s
;
87
s
<<
this
->
variable
(0) <<
"=logit("
<<
this
->
externalWeight
();
88
89
for
(
Idx
i
= 1;
i
<
this
->
nbrDim
();
i
++) {
90
GUM_SCALAR
c
=
this
->
causalWeight
(
this
->
variable
(
i
));
91
92
if
(
c
!=
GUM_SCALAR
(0)) {
93
s
<<
" "
;
94
95
if
(
c
> 0)
s
<<
"+"
;
96
97
s
<<
this
->
causalWeight
(
this
->
variable
(
i
)) <<
"*"
<<
this
->
variable
(
i
);
98
}
99
}
100
101
s
<<
")"
;
102
103
return
s
.
str
();
104
}
105
106
// For friendly displaying the content of the variable.
107
template
<
typename
GUM_SCALAR
>
108
INLINE
std
::
ostream
&
operator
<<(
std
::
ostream
&
s
,
const
MultiDimLogit
<
GUM_SCALAR
>&
ag
) {
109
return
s
<<
ag
.
toString
();
110
}
111
112
template
<
typename
GUM_SCALAR
>
113
INLINE
MultiDimContainer
<
GUM_SCALAR
>*
MultiDimLogit
<
GUM_SCALAR
>::
newFactory
()
const
{
114
return
new
MultiDimLogit
<
GUM_SCALAR
>(
this
->
_external_weight_
,
this
->
_default_weight_
);
115
}
116
117
// returns the name of the implementation
118
template
<
typename
GUM_SCALAR
>
119
INLINE
const
std
::
string
&
MultiDimLogit
<
GUM_SCALAR
>::
name
()
const
{
120
static
const
std
::
string
str
=
"MultiDimLogit"
;
121
return
str
;
122
}
123
124
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643