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) :
34  }
35 
36  // copy constructor
41  }
42 
43  // move constructor
48  }
49 
50  // destructor
52 
53  // copy operator
54  INLINE Dirichlet& Dirichlet::operator=(const Dirichlet& from) {
55  if (&from != this) {
59  }
60  return *this;
61  }
62 
63  // move operator
65  if (&from != this) {
69  }
70  return *this;
71  }
72 
73  // returns a sample from the Dirichlet distribution
75  Size size = Size(params__.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));
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
95  Size size = Size(parm.size());
97  float sum = 0.0f;
98  while (sum == 0.0f) {
99  for (Idx i = 0; i < size; ++i) {
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
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 */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669