aGrUM  0.13.2
Dirichlet_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 namespace gum {
27 
28  // default constructor
29  INLINE Dirichlet::Dirichlet(const param_type& params, unsigned int seed) :
30  __generator(gum::getRandomGenerator(seed)), __params(params) {
31  GUM_CONSTRUCTOR(Dirichlet);
32  }
33 
34  // copy constructor
35  INLINE Dirichlet::Dirichlet(const Dirichlet& from) :
37  __params(from.__params) {
38  GUM_CONS_CPY(Dirichlet);
39  }
40 
41  // move constructor
43  __generator(std::move(from.__generator)), __gamma(std::move(from.__gamma)),
44  __params(std::move(from.__params)) {
45  GUM_CONS_MOV(Dirichlet);
46  }
47 
48  // destructor
49  INLINE Dirichlet::~Dirichlet() { GUM_DESTRUCTOR(Dirichlet); }
50 
51  // copy operator
52  INLINE Dirichlet& Dirichlet::operator=(const Dirichlet& from) {
53  if (&from != this) {
54  __generator = from.__generator;
55  __gamma = from.__gamma;
56  __params = from.__params;
57  }
58  return *this;
59  }
60 
61  // move operator
63  if (&from != this) {
64  __generator = std::move(from.__generator);
65  __gamma = std::move(from.__gamma);
66  __params = std::move(from.__params);
67  }
68  return *this;
69  }
70 
71  // returns a sample from the Dirichlet distribution
73  Size size = Size(__params.size());
74  result_type res(size);
75  float sum = 0.0f;
76  while (sum == 0.0f) {
77  for (Idx i = 0; i < size; ++i) {
78  __gamma.param(
79  std::gamma_distribution< float >::param_type(__params[i], 1));
80  res[i] = __gamma(__generator);
81  sum += res[i];
82  }
83  }
84  for (Idx i = 0; i < size; ++i) {
85  res[i] /= sum;
86  }
87  return res;
88  }
89 
90  // returns a sample from the Dirichlet distribution
93  Size size = Size(parm.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(parm[i], 1));
99  res[i] = __gamma(__generator);
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 the parameters of the distribution
110  INLINE const Dirichlet::param_type& Dirichlet::param() const noexcept {
111  return __params;
112  }
113 
114  // sets the parameters of the distribution
115  INLINE void Dirichlet::param(const Dirichlet::param_type& parm) {
116  __params = parm;
117  }
118 
119  // Returns the greatest lower bound of the range of values possibly returned
120  INLINE float Dirichlet::min() const noexcept { return 0.0f; }
121 
122  // Returns the lowest higher bound of the range of values possibly returned
123  INLINE float Dirichlet::max() const noexcept { return 1.0f; }
124 
125 } /* namespace gum */
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
A class for sampling w.r.t.
Definition: Dirichlet.h:47
STL namespace.
const param_type & param() const noexcept
Returns the parameters of the distribution.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Dirichlet(const param_type &params, unsigned int seed=GUM_RANDOMSEED)
Default constructor.
Definition: Dirichlet_inl.h:29
float min() const noexcept
Returns the greatest lower bound of the range of values returned by gum::Dirichlet::operator()().
std::gamma_distribution< float > __gamma
The gamma distribution used to compute the Dirichlet unnormalized samples.
Definition: Dirichlet.h:172
std::vector< float > param_type
The parameter type.
Definition: Dirichlet.h:50
std::vector< float > result_type
The type for the samples generated.
Definition: Dirichlet.h:53
std::default_random_engine __generator
The random engine used by the unform random distribution.
Definition: Dirichlet.h:168
float max() const noexcept
Returns the lowest higher bound of the range of values returned by gum::Dirichlet::operator()().
~Dirichlet()
Class destructor.
Definition: Dirichlet_inl.h:49
std::default_random_engine getRandomGenerator(unsigned int seed)
define a random_engine with correct seed
result_type operator()()
Returns a sample from the Dirichlet distribution.
Definition: Dirichlet_inl.h:72
param_type __params
The parameters of the distribution.
Definition: Dirichlet.h:175
unsigned long Idx
Type for indexes.
Definition: types.h:43
Dirichlet & operator=(const Dirichlet &from)
Copy operator.
Definition: Dirichlet_inl.h:52