aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
Dirichlet_inl.h
Go to the documentation of this file.
1
/****************************************************************************
2
* This file is part of the aGrUM/pyAgrum library. *
3
* *
4
* Copyright (c) 2005-2026 by *
5
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
6
* - Christophe GONZALES(_at_AMU) *
7
* *
8
* The aGrUM/pyAgrum library is free software; you can redistribute it *
9
* and/or modify it under the terms of either : *
10
* *
11
* - the GNU Lesser General Public License as published by *
12
* the Free Software Foundation, either version 3 of the License, *
13
* or (at your option) any later version, *
14
* - the MIT license (MIT), *
15
* - or both in dual license, as here. *
16
* *
17
* (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18
* *
19
* This aGrUM/pyAgrum library is distributed in the hope that it will be *
20
* useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26
* OTHER DEALINGS IN THE SOFTWARE. *
27
* *
28
* See LICENCES for more details. *
29
* *
30
* SPDX-FileCopyrightText: Copyright 2005-2026 *
31
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
32
* - Christophe GONZALES(_at_AMU) *
33
* SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34
* *
35
* Contact : info_at_agrum_dot_org *
36
* homepage : http://agrum.gitlab.io *
37
* gitlab : https://gitlab.com/agrumery/agrum *
38
* *
39
****************************************************************************/
40
41
#pragma once
42
48
49
#include <
agrum/base/core/math/Dirichlet.h
>
// to ease IDE parser
50
51
namespace
gum
{
52
53
// default constructor
54
INLINE
Dirichlet::Dirichlet
(
const
param_type
& params) :
_params_
(params) {
55
GUM_CONSTRUCTOR(
Dirichlet
);
56
for
(
const
auto
& a: params)
57
if
(a <= 0.0f) {
GUM_ERROR
(
OutOfBounds
,
"Dirichlet: all alpha parameters must be > 0"
) }
58
}
59
60
// copy constructor
61
INLINE
Dirichlet::Dirichlet
(
const
Dirichlet
& from) :
62
_gamma_
(from.
_gamma_
),
_params_
(from.
_params_
) {
63
GUM_CONS_CPY(
Dirichlet
);
64
}
65
66
// move constructor
67
INLINE
Dirichlet::Dirichlet
(
Dirichlet
&& from) :
68
_gamma_
(
std
::move(from.
_gamma_
)),
_params_
(
std
::move(from.
_params_
)) {
69
GUM_CONS_MOV(
Dirichlet
);
70
}
71
72
// destructor
73
INLINE
Dirichlet::~Dirichlet
() { GUM_DESTRUCTOR(
Dirichlet
); }
74
75
// copy operator
76
INLINE
Dirichlet
&
Dirichlet::operator=
(
const
Dirichlet
& from) {
77
if
(&from !=
this
) {
78
_gamma_
= from.
_gamma_
;
79
_params_
= from.
_params_
;
80
}
81
return
*
this
;
82
}
83
84
// move operator
85
INLINE
Dirichlet
&
Dirichlet::operator=
(
Dirichlet
&& from) {
86
if
(&from !=
this
) {
87
_gamma_
= std::move(from._gamma_);
88
_params_
= std::move(from._params_);
89
}
90
return
*
this
;
91
}
92
93
// returns a sample from the Dirichlet distribution
94
INLINE
Dirichlet::result_type
Dirichlet::operator()
() {
95
Size
size =
Size
(
_params_
.size());
96
result_type
res(size);
97
if
(size == 0)
return
res;
98
float
sum = 0.0f;
99
while
(sum == 0.0f) {
100
for
(
Idx
i = 0; i < size; ++i) {
101
_gamma_
.param(std::gamma_distribution< float >::param_type(
_params_
[i], 1));
102
res[i] =
_gamma_
(
gum::randomGenerator
());
103
sum += res[i];
104
}
105
}
106
for
(
Idx
i = 0; i < size; ++i) {
107
res[i] /= sum;
108
}
109
return
res;
110
}
111
112
// returns a sample from the Dirichlet distribution
113
INLINE
Dirichlet::result_type
Dirichlet::operator()
(
const
Dirichlet::param_type
& parm) {
114
Size
size =
Size
(parm.size());
115
result_type
res(size);
116
if
(size == 0)
return
res;
117
float
sum = 0.0f;
118
while
(sum == 0.0f) {
119
for
(
Idx
i = 0; i < size; ++i) {
120
_gamma_
.param(std::gamma_distribution< float >::param_type(parm[i], 1));
121
res[i] =
_gamma_
(
gum::randomGenerator
());
122
sum += res[i];
123
}
124
}
125
for
(
Idx
i = 0; i < size; ++i) {
126
res[i] /= sum;
127
}
128
return
res;
129
}
130
131
// returns the parameters of the distribution
132
INLINE
const
Dirichlet::param_type
&
Dirichlet::param
() const noexcept {
return
_params_
; }
133
134
// sets the parameters of the distribution
135
INLINE
void
Dirichlet::param
(
const
Dirichlet::param_type
& parm) {
136
for
(
const
auto
& a: parm)
137
if
(a <= 0.0f) {
GUM_ERROR
(
OutOfBounds
,
"Dirichlet: all alpha parameters must be > 0"
) }
138
_params_
= parm;
139
}
140
141
// Returns the greatest lower bound of the range of values possibly returned
142
INLINE
float
Dirichlet::min
() const noexcept {
return
0.0f; }
143
144
// Returns the lowest higher bound of the range of values possibly returned
145
INLINE
float
Dirichlet::max
() const noexcept {
return
1.0f; }
146
}
/* namespace gum */
Dirichlet.h
A class for sampling w.r.t.
gum::Dirichlet::~Dirichlet
~Dirichlet()
Class destructor.
Definition
Dirichlet_inl.h:73
gum::Dirichlet::operator=
Dirichlet & operator=(const Dirichlet &from)
Copy operator.
Definition
Dirichlet_inl.h:76
gum::Dirichlet::operator()
result_type operator()()
Returns a sample from the Dirichlet distribution.
Definition
Dirichlet_inl.h:94
gum::Dirichlet::_gamma_
std::gamma_distribution< float > _gamma_
The gamma distribution used to compute the Dirichlet unnormalized samples.
Definition
Dirichlet.h:179
gum::Dirichlet::param_type
std::vector< float > param_type
The parameter type.
Definition
Dirichlet.h:72
gum::Dirichlet::max
float max() const noexcept
Returns the lowest higher bound of the range of values returned by gum::Dirichlet::operator()().
Definition
Dirichlet_inl.h:145
gum::Dirichlet::Dirichlet
Dirichlet(const param_type ¶ms)
Default constructor.
Definition
Dirichlet_inl.h:54
gum::Dirichlet::_params_
param_type _params_
The parameters of the distribution.
Definition
Dirichlet.h:182
gum::Dirichlet::param
const param_type & param() const noexcept
Returns the parameters of the distribution.
Definition
Dirichlet_inl.h:132
gum::Dirichlet::result_type
std::vector< float > result_type
The type for the samples generated.
Definition
Dirichlet.h:75
gum::Dirichlet::min
float min() const noexcept
Returns the greatest lower bound of the range of values returned by gum::Dirichlet::operator()().
Definition
Dirichlet_inl.h:142
OutOfBounds
Exception : out of bound.
GUM_ERROR
#define GUM_ERROR(type, msg)
Definition
exceptions.h:76
gum::Size
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition
types.h:74
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
gum::randomGenerator
GUM_SHARED_PUBLIC std::mt19937 & randomGenerator()
define a random_engine with correct seed
Definition
utils_random_inl.h:98
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
std
STL namespace.
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0