aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
Dirichlet_inl.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
/** @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_
),
_params_
(
from
.
_params_
) {
39
GUM_CONS_CPY
(
Dirichlet
);
40
}
41
42
// move constructor
43
INLINE
Dirichlet
::
Dirichlet
(
Dirichlet
&&
from
) :
44
_generator_
(
std
::
move
(
from
.
_generator_
)),
_gamma_
(
std
::
move
(
from
.
_gamma_
)),
45
_params_
(
std
::
move
(
from
.
_params_
)) {
46
GUM_CONS_MOV
(
Dirichlet
);
47
}
48
49
// destructor
50
INLINE
Dirichlet
::~
Dirichlet
() {
51
GUM_DESTRUCTOR
(
Dirichlet
);
52
;
53
}
54
55
// copy operator
56
INLINE Dirichlet&
Dirichlet
::
operator
=(
const
Dirichlet
&
from
) {
57
if
(&
from
!=
this
) {
58
_generator_
=
from
.
_generator_
;
59
_gamma_
=
from
.
_gamma_
;
60
_params_
=
from
.
_params_
;
61
}
62
return
*
this
;
63
}
64
65
// move operator
66
INLINE
Dirichlet
&
Dirichlet
::
operator
=(
Dirichlet
&&
from
) {
67
if
(&
from
!=
this
) {
68
_generator_
=
std
::
move
(
from
.
_generator_
);
69
_gamma_
=
std
::
move
(
from
.
_gamma_
);
70
_params_
=
std
::
move
(
from
.
_params_
);
71
}
72
return
*
this
;
73
}
74
75
// returns a sample from the Dirichlet distribution
76
INLINE
Dirichlet
::
result_type
Dirichlet
::
operator
()() {
77
Size
size
=
Size
(
_params_
.
size
());
78
result_type
res
(
size
);
79
float
sum
= 0.0f;
80
while
(
sum
== 0.0f) {
81
for
(
Idx
i
= 0;
i
<
size
; ++
i
) {
82
_gamma_
.
param
(
std
::
gamma_distribution
<
float
>::
param_type
(
_params_
[
i
], 1));
83
res
[
i
] =
_gamma_
(
_generator_
);
84
sum
+=
res
[
i
];
85
}
86
}
87
for
(
Idx
i
= 0;
i
<
size
; ++
i
) {
88
res
[
i
] /=
sum
;
89
}
90
return
res
;
91
}
92
93
// returns a sample from the Dirichlet distribution
94
INLINE
Dirichlet
::
result_type
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
{
return
_params_
; }
113
114
// sets the parameters of the distribution
115
INLINE
void
Dirichlet
::
param
(
const
Dirichlet
::
param_type
&
parm
) {
_params_
=
parm
; }
116
117
// Returns the greatest lower bound of the range of values possibly returned
118
INLINE
float
Dirichlet
::
min
()
const
noexcept
{
return
0.0f; }
119
120
// Returns the lowest higher bound of the range of values possibly returned
121
INLINE
float
Dirichlet
::
max
()
const
noexcept
{
return
1.0f; }
122
123
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643