aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
gum::Dirichlet Class Reference

A class for sampling w.r.t. More...

#include <agrum/base/core/math/Dirichlet.h>

Public Types

using param_type = std::vector< float >
 The parameter type.
using result_type = std::vector< float >
 The type for the samples generated.

Public Member Functions

Constructors / Destructors
 Dirichlet (const param_type &params)
 Default constructor.
 Dirichlet (const Dirichlet &from)
 Copy constructor.
 Dirichlet (Dirichlet &&from)
 Move constructor.
 ~Dirichlet ()
 Class destructor.
Operators
Dirichletoperator= (const Dirichlet &from)
 Copy operator.
Dirichletoperator= (Dirichlet &&from)
 Move operator.
result_type operator() ()
 Returns a sample from the Dirichlet distribution.
result_type operator() (const param_type &p)
 Returns a sample from the Dirichlet distribution.
Accessors / Modifiers
const param_typeparam () const noexcept
 Returns the parameters of the distribution.
void param (const param_type &p)
 Sets the parameters of the distribution.
float min () const noexcept
 Returns the greatest lower bound of the range of values returned by gum::Dirichlet::operator()().
float max () const noexcept
 Returns the lowest higher bound of the range of values returned by gum::Dirichlet::operator()().

Private Attributes

std::gamma_distribution< float > _gamma_
 The gamma distribution used to compute the Dirichlet unnormalized samples.
param_type _params_
 The parameters of the distribution.

Detailed Description

A class for sampling w.r.t.

Dirichlet distributions.

Definition at line 69 of file Dirichlet.h.

Member Typedef Documentation

◆ param_type

using gum::Dirichlet::param_type = std::vector< float >

The parameter type.

Definition at line 72 of file Dirichlet.h.

◆ result_type

using gum::Dirichlet::result_type = std::vector< float >

The type for the samples generated.

Definition at line 75 of file Dirichlet.h.

Constructor & Destructor Documentation

◆ Dirichlet() [1/3]

INLINE gum::Dirichlet::Dirichlet ( const param_type & params)

Default constructor.

Parameters
paramsThe distribution parameters.
seedThe distribution seed.

Definition at line 54 of file Dirichlet_inl.h.

54 : _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 }
Dirichlet(const param_type &params)
Default constructor.
param_type _params_
The parameters of the distribution.
Definition Dirichlet.h:182
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References Dirichlet(), _params_, and GUM_ERROR.

Referenced by Dirichlet(), Dirichlet(), Dirichlet(), ~Dirichlet(), operator=(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Dirichlet() [2/3]

INLINE gum::Dirichlet::Dirichlet ( const Dirichlet & from)

Copy constructor.

Parameters
fromThe distribution to copy.

Definition at line 61 of file Dirichlet_inl.h.

61 :
62 _gamma_(from._gamma_), _params_(from._params_) {
63 GUM_CONS_CPY(Dirichlet);
64 }
std::gamma_distribution< float > _gamma_
The gamma distribution used to compute the Dirichlet unnormalized samples.
Definition Dirichlet.h:179

References Dirichlet(), _gamma_, and _params_.

Here is the call graph for this function:

◆ Dirichlet() [3/3]

INLINE gum::Dirichlet::Dirichlet ( Dirichlet && from)

Move constructor.

Parameters
fromThe distribution to move.

Definition at line 67 of file Dirichlet_inl.h.

67 :
68 _gamma_(std::move(from._gamma_)), _params_(std::move(from._params_)) {
69 GUM_CONS_MOV(Dirichlet);
70 }

References Dirichlet(), _gamma_, and _params_.

Here is the call graph for this function:

◆ ~Dirichlet()

INLINE gum::Dirichlet::~Dirichlet ( )

Class destructor.

Definition at line 73 of file Dirichlet_inl.h.

73{ GUM_DESTRUCTOR(Dirichlet); }

References Dirichlet().

Here is the call graph for this function:

Member Function Documentation

◆ max()

INLINE float gum::Dirichlet::max ( ) const
noexcept

Returns the lowest higher bound of the range of values returned by gum::Dirichlet::operator()().

Returns
Returns the lowest higher bound of the range of values returned by gum::Dirichlet::operator()().

Definition at line 145 of file Dirichlet_inl.h.

145{ return 1.0f; }

◆ min()

INLINE float gum::Dirichlet::min ( ) const
noexcept

Returns the greatest lower bound of the range of values returned by gum::Dirichlet::operator()().

Returns
Returns the greatest lower bound of the range of values returned by gum::Dirichlet::operator()().

Definition at line 142 of file Dirichlet_inl.h.

142{ return 0.0f; }

◆ operator()() [1/2]

INLINE Dirichlet::result_type gum::Dirichlet::operator() ( )

Returns a sample from the Dirichlet distribution.

Returns
Returns a sample from the Dirichlet distribution.

Definition at line 94 of file Dirichlet_inl.h.

94 {
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 }
std::vector< float > result_type
The type for the samples generated.
Definition Dirichlet.h:75
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

References _gamma_, _params_, and gum::randomGenerator().

Here is the call graph for this function:

◆ operator()() [2/2]

INLINE Dirichlet::result_type gum::Dirichlet::operator() ( const param_type & p)

Returns a sample from the Dirichlet distribution.

Parameters
pAn object representing the distribution's parameters, obtained by a call to gum::Dirichlet::param(const param_type&).

Definition at line 113 of file Dirichlet_inl.h.

113 {
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 }

References _gamma_, and gum::randomGenerator().

Here is the call graph for this function:

◆ operator=() [1/2]

INLINE Dirichlet & gum::Dirichlet::operator= ( const Dirichlet & from)

Copy operator.

Parameters
fromThe distribution to copy.
Returns
Returns this gum::Dirichlet distribution.

Definition at line 76 of file Dirichlet_inl.h.

76 {
77 if (&from != this) {
78 _gamma_ = from._gamma_;
79 _params_ = from._params_;
80 }
81 return *this;
82 }

References Dirichlet(), _gamma_, and _params_.

Here is the call graph for this function:

◆ operator=() [2/2]

INLINE Dirichlet & gum::Dirichlet::operator= ( Dirichlet && from)

Move operator.

Parameters
fromThe distribution to move.
Returns
Returns this gum::Dirichlet distribution.

Definition at line 85 of file Dirichlet_inl.h.

85 {
86 if (&from != this) {
87 _gamma_ = std::move(from._gamma_);
88 _params_ = std::move(from._params_);
89 }
90 return *this;
91 }

References Dirichlet(), _gamma_, and _params_.

Here is the call graph for this function:

◆ param() [1/2]

INLINE const Dirichlet::param_type & gum::Dirichlet::param ( ) const
noexcept

Returns the parameters of the distribution.

Returns
Returns the parameters of the distribution.

Definition at line 132 of file Dirichlet_inl.h.

132{ return _params_; }

References _params_.

◆ param() [2/2]

INLINE void gum::Dirichlet::param ( const param_type & p)

Sets the parameters of the distribution.

Parameters
pAn object representing the distribution's parameters, obtained by a call to member function param.

Definition at line 135 of file Dirichlet_inl.h.

135 {
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 }

References _params_, and GUM_ERROR.

Member Data Documentation

◆ _gamma_

std::gamma_distribution< float > gum::Dirichlet::_gamma_
private

The gamma distribution used to compute the Dirichlet unnormalized samples.

Definition at line 179 of file Dirichlet.h.

Referenced by Dirichlet(), Dirichlet(), operator()(), operator()(), operator=(), and operator=().

◆ _params_

param_type gum::Dirichlet::_params_
private

The parameters of the distribution.

Definition at line 182 of file Dirichlet.h.

Referenced by Dirichlet(), Dirichlet(), Dirichlet(), operator()(), operator=(), operator=(), param(), and param().


The documentation for this class was generated from the following files: