aGrUM  0.16.0
Dirichlet_inl.h
Go to the documentation of this file.
1 
29 namespace gum {
30 
31  // default constructor
32  INLINE Dirichlet::Dirichlet(const param_type& params, unsigned int seed) :
33  __generator(gum::getRandomGenerator(seed)), __params(params) {
34  GUM_CONSTRUCTOR(Dirichlet);
35  }
36 
37  // copy constructor
38  INLINE Dirichlet::Dirichlet(const Dirichlet& from) :
40  __params(from.__params) {
41  GUM_CONS_CPY(Dirichlet);
42  }
43 
44  // move constructor
46  __generator(std::move(from.__generator)), __gamma(std::move(from.__gamma)),
47  __params(std::move(from.__params)) {
48  GUM_CONS_MOV(Dirichlet);
49  }
50 
51  // destructor
52  INLINE Dirichlet::~Dirichlet() { GUM_DESTRUCTOR(Dirichlet); }
53 
54  // copy operator
55  INLINE Dirichlet& Dirichlet::operator=(const Dirichlet& from) {
56  if (&from != this) {
57  __generator = from.__generator;
58  __gamma = from.__gamma;
59  __params = from.__params;
60  }
61  return *this;
62  }
63 
64  // move operator
66  if (&from != this) {
67  __generator = std::move(from.__generator);
68  __gamma = std::move(from.__gamma);
69  __params = std::move(from.__params);
70  }
71  return *this;
72  }
73 
74  // returns a sample from the Dirichlet distribution
76  Size size = Size(__params.size());
77  result_type res(size);
78  float sum = 0.0f;
79  while (sum == 0.0f) {
80  for (Idx i = 0; i < size; ++i) {
81  __gamma.param(
82  std::gamma_distribution< float >::param_type(__params[i], 1));
83  res[i] = __gamma(__generator);
84  sum += res[i];
85  }
86  }
87  for (Idx i = 0; i < size; ++i) {
88  res[i] /= sum;
89  }
90  return res;
91  }
92 
93  // returns a sample from the Dirichlet distribution
96  Size size = Size(parm.size());
97  result_type res(size);
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(parm[i], 1));
102  res[i] = __gamma(__generator);
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 the parameters of the distribution
113  INLINE const Dirichlet::param_type& Dirichlet::param() const noexcept {
114  return __params;
115  }
116 
117  // sets the parameters of the distribution
118  INLINE void Dirichlet::param(const Dirichlet::param_type& parm) {
119  __params = parm;
120  }
121 
122  // Returns the greatest lower bound of the range of values possibly returned
123  INLINE float Dirichlet::min() const noexcept { return 0.0f; }
124 
125  // Returns the lowest higher bound of the range of values possibly returned
126  INLINE float Dirichlet::max() const noexcept { return 1.0f; }
127 
128 } /* namespace gum */
A class for sampling w.r.t.
Definition: Dirichlet.h:50
STL namespace.
const param_type & param() const noexcept
Returns the parameters of the distribution.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Dirichlet(const param_type &params, unsigned int seed=GUM_RANDOMSEED)
Default constructor.
Definition: Dirichlet_inl.h:32
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:175
std::vector< float > param_type
The parameter type.
Definition: Dirichlet.h:53
std::vector< float > result_type
The type for the samples generated.
Definition: Dirichlet.h:56
std::default_random_engine __generator
The random engine used by the unform random distribution.
Definition: Dirichlet.h:171
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:52
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:75
param_type __params
The parameters of the distribution.
Definition: Dirichlet.h:178
Size Idx
Type for indexes.
Definition: types.h:53
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Dirichlet & operator=(const Dirichlet &from)
Copy operator.
Definition: Dirichlet_inl.h:55