aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
Dirichlet_inl.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
/** @file
23
* @brief a class for sampling w.r.t. Dirichlet distributions
24
*
25
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26
*/
27
28
namespace
gum
{
29
30
// default constructor
31
INLINE
Dirichlet
::
Dirichlet
(
const
param_type
&
params
,
unsigned
int
seed
) :
32
generator__
(
gum
::
getRandomGenerator
(
seed
)),
params__
(
params
) {
33
GUM_CONSTRUCTOR
(
Dirichlet
);
34
}
35
36
// copy constructor
37
INLINE
Dirichlet
::
Dirichlet
(
const
Dirichlet
&
from
) :
38
generator__
(
from
.
generator__
),
gamma__
(
from
.
gamma__
),
39
params__
(
from
.
params__
) {
40
GUM_CONS_CPY
(
Dirichlet
);
41
}
42
43
// move constructor
44
INLINE
Dirichlet
::
Dirichlet
(
Dirichlet
&&
from
) :
45
generator__
(
std
::
move
(
from
.
generator__
)),
gamma__
(
std
::
move
(
from
.
gamma__
)),
46
params__
(
std
::
move
(
from
.
params__
)) {
47
GUM_CONS_MOV
(
Dirichlet
);
48
}
49
50
// destructor
51
INLINE
Dirichlet
::~
Dirichlet
() {
GUM_DESTRUCTOR
(
Dirichlet
); }
52
53
// copy operator
54
INLINE Dirichlet&
Dirichlet
::
operator
=(
const
Dirichlet
&
from
) {
55
if
(&
from
!=
this
) {
56
generator__
=
from
.
generator__
;
57
gamma__
=
from
.
gamma__
;
58
params__
=
from
.
params__
;
59
}
60
return
*
this
;
61
}
62
63
// move operator
64
INLINE
Dirichlet
&
Dirichlet
::
operator
=(
Dirichlet
&&
from
) {
65
if
(&
from
!=
this
) {
66
generator__
=
std
::
move
(
from
.
generator__
);
67
gamma__
=
std
::
move
(
from
.
gamma__
);
68
params__
=
std
::
move
(
from
.
params__
);
69
}
70
return
*
this
;
71
}
72
73
// returns a sample from the Dirichlet distribution
74
INLINE
Dirichlet
::
result_type
Dirichlet
::
operator
()() {
75
Size
size
=
Size
(
params__
.
size
());
76
result_type
res
(
size
);
77
float
sum
= 0.0f;
78
while
(
sum
== 0.0f) {
79
for
(
Idx
i
= 0;
i
<
size
; ++
i
) {
80
gamma__
.
param
(
81
std
::
gamma_distribution
<
float
>::
param_type
(
params__
[
i
], 1));
82
res
[
i
] =
gamma__
(
generator__
);
83
sum
+=
res
[
i
];
84
}
85
}
86
for
(
Idx
i
= 0;
i
<
size
; ++
i
) {
87
res
[
i
] /=
sum
;
88
}
89
return
res
;
90
}
91
92
// returns a sample from the Dirichlet distribution
93
INLINE
Dirichlet
::
result_type
94
Dirichlet
::
operator
()(
const
Dirichlet
::
param_type
&
parm
) {
95
Size
size
=
Size
(
parm
.
size
());
96
result_type
res
(
size
);
97
float
sum
= 0.0f;
98
while
(
sum
== 0.0f) {
99
for
(
Idx
i
= 0;
i
<
size
; ++
i
) {
100
gamma__
.
param
(
std
::
gamma_distribution
<
float
>::
param_type
(
parm
[
i
], 1));
101
res
[
i
] =
gamma__
(
generator__
);
102
sum
+=
res
[
i
];
103
}
104
}
105
for
(
Idx
i
= 0;
i
<
size
; ++
i
) {
106
res
[
i
] /=
sum
;
107
}
108
return
res
;
109
}
110
111
// returns the parameters of the distribution
112
INLINE
const
Dirichlet
::
param_type
&
Dirichlet
::
param
()
const
noexcept
{
113
return
params__
;
114
}
115
116
// sets the parameters of the distribution
117
INLINE
void
Dirichlet
::
param
(
const
Dirichlet
::
param_type
&
parm
) {
118
params__
=
parm
;
119
}
120
121
// Returns the greatest lower bound of the range of values possibly returned
122
INLINE
float
Dirichlet
::
min
()
const
noexcept
{
return
0.0f; }
123
124
// Returns the lowest higher bound of the range of values possibly returned
125
INLINE
float
Dirichlet
::
max
()
const
noexcept
{
return
1.0f; }
126
127
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669