aGrUM 3.1.1
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-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
51namespace 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
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
77 if (&from != this) {
78 _gamma_ = from._gamma_;
79 _params_ = from._params_;
80 }
81 return *this;
82 }
83
84 // move operator
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
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
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 */
A class for sampling w.r.t.
~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()().
Exception : out of bound.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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.