ssmkit  master-68aed98
categorical.hpp
Go to the documentation of this file.
1 
7 #ifndef SSMPACK_DISTRIBUTION_CATEGORICAL_HPP
8 #define SSMPACK_DISTRIBUTION_CATEGORICAL_HPP
9 
11 
12 #include <armadillo>
13 
14 namespace ssmkit {
15 namespace distribution {
16 
22 class Categorical {
24  using TParameterVar = arma::vec;
26  using TValueType = unsigned int;
27 
28  private:
30  TParameterVar param_;
32  TParameterVar cdf_;
34  std::uniform_real_distribution<double> uniform_;
36  TValueType max_;
37 
38  public:
40  Categorical() : Categorical(arma::ones<arma::vec>(1)) {}
45  Categorical(TParameterVar parameters)
46  : param_(std::move(parameters)) {calcCDF(); calcMax();}
48  TValueType random() {
49  double rv = uniform_(random::Generator::get().getGenerator());
50  for (TValueType i = 0; i < max_; ++i)
51  if (rv < cdf_(i))
52  return i;
53 
54  return 0; // this line never get reached
55  }
57  double likelihood(const TValueType &rv) {
58  return param_(rv);
59  }
60 
65  Categorical &parameterize(const TParameterVar & param){
66  param_ = param;
67  calcCDF();
68  calcMax();
69  return *this;
70  }
71 
72  private:
73  void calcCDF() { cdf_ = arma::cumsum(param_); }
74  void calcMax() { max_ = param_.n_rows; }
75 };
76 
77 } // namespace ssmkit
78 } // namespace distribution
79 
80 #endif //SSMPACK_DISTRIBUTION_CATEGORICAL_HPP
Categorical & parameterize(const TParameterVar &param)
Change parameters of the distribution.
Definition: categorical.hpp:65
TValueType random()
Return a random variable from the distribution.
Definition: categorical.hpp:48
static Generator & get()
Returns a reference to singleton instance.
Definition: generator.hpp:38
Categorical()
Default constructor .
Definition: categorical.hpp:40
Categorical (multinomial) distribution.
Definition: categorical.hpp:22
double likelihood(const TValueType &rv)
Return likelihood of the given random variable.
Definition: categorical.hpp:57
Categorical(TParameterVar parameters)
Constructor.
Definition: categorical.hpp:45