aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
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-2025 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-2025 *
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#pragma once
41
47
48namespace gum {
49
50 // default constructor
51 INLINE Dirichlet::Dirichlet(const param_type& params) : _params_(params) {
52 GUM_CONSTRUCTOR(Dirichlet);
53 }
54
55 // copy constructor
56 INLINE Dirichlet::Dirichlet(const Dirichlet& from) :
57 _gamma_(from._gamma_), _params_(from._params_) {
58 GUM_CONS_CPY(Dirichlet);
59 }
60
61 // move constructor
63 _gamma_(std::move(from._gamma_)), _params_(std::move(from._params_)) {
64 GUM_CONS_MOV(Dirichlet);
65 }
66
67 // destructor
69 GUM_DESTRUCTOR(Dirichlet);
70 ;
71 }
72
73 // copy operator
75 if (&from != this) {
76 _gamma_ = from._gamma_;
77 _params_ = from._params_;
78 }
79 return *this;
80 }
81
82 // move operator
84 if (&from != this) {
85 _gamma_ = std::move(from._gamma_);
86 _params_ = std::move(from._params_);
87 }
88 return *this;
89 }
90
91 // returns a sample from the Dirichlet distribution
93 Size size = Size(_params_.size());
94 result_type res(size);
95 float sum = 0.0f;
96 while (sum == 0.0f) {
97 for (Idx i = 0; i < size; ++i) {
98 _gamma_.param(std::gamma_distribution< float >::param_type(_params_[i], 1));
99 res[i] = _gamma_(gum::randomGenerator());
100 sum += res[i];
101 }
102 }
103 for (Idx i = 0; i < size; ++i) {
104 res[i] /= sum;
105 }
106 return res;
107 }
108
109 // returns a sample from the Dirichlet distribution
111 Size size = Size(parm.size());
112 result_type res(size);
113 float sum = 0.0f;
114 while (sum == 0.0f) {
115 for (Idx i = 0; i < size; ++i) {
116 _gamma_.param(std::gamma_distribution< float >::param_type(parm[i], 1));
117 res[i] = _gamma_(gum::randomGenerator());
118 sum += res[i];
119 }
120 }
121 for (Idx i = 0; i < size; ++i) {
122 res[i] /= sum;
123 }
124 return res;
125 }
126
127 // returns the parameters of the distribution
128 INLINE const Dirichlet::param_type& Dirichlet::param() const noexcept { return _params_; }
129
130 // sets the parameters of the distribution
131 INLINE void Dirichlet::param(const Dirichlet::param_type& parm) { _params_ = parm; }
132
133 // Returns the greatest lower bound of the range of values possibly returned
134 INLINE float Dirichlet::min() const noexcept { return 0.0f; }
135
136 // Returns the lowest higher bound of the range of values possibly returned
137 INLINE float Dirichlet::max() const noexcept { return 1.0f; }
138} /* namespace gum */
~Dirichlet()
Class destructor.
Dirichlet & operator=(const Dirichlet &from)
Copy operator.
result_type operator()()
Returns a sample from the Dirichlet distribution.
std::gamma_distribution< float > _gamma_
The gamma distribution used to compute the Dirichlet unnormalized samples.
Definition Dirichlet.h:179
std::vector< float > param_type
The parameter type.
Definition Dirichlet.h:72
float max() const noexcept
Returns the lowest higher bound of the range of values returned by gum::Dirichlet::operator()().
Dirichlet(const param_type &params)
Default constructor.
param_type _params_
The parameters of the distribution.
Definition Dirichlet.h:182
const param_type & param() const noexcept
Returns the parameters of the distribution.
std::vector< float > result_type
The type for the samples generated.
Definition Dirichlet.h:75
float min() const noexcept
Returns the greatest lower bound of the range of values returned by gum::Dirichlet::operator()().
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
std::mt19937 & randomGenerator()
define a random_engine with correct seed
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.