All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
categorical_rng.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_RNG_HPP
#define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_RNG_HPP
#include <boost/random/uniform_01.hpp>
#include <boost/random/variate_generator.hpp>
#include <stan/math/prim/mat/err/check_simplex.hpp>
#include <stan/math/prim/scal/err/check_bounded.hpp>
#include <stan/math/prim/scal/fun/value_of.hpp>
#include <stan/math/prim/mat/fun/sum.hpp>
#include <stan/math/prim/mat/meta/index_type.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/meta/include_summand.hpp>
namespace stan {
namespace math {
template <class RNG>
inline int
categorical_rng(const Eigen::Matrix<double, Eigen::Dynamic, 1>& theta,
RNG& rng) {
using boost::variate_generator;
using boost::uniform_01;
using stan::math::check_simplex;
static const char* function("stan::math::categorical_rng");
check_simplex(function, "Probabilities parameter", theta);
variate_generator<RNG&, uniform_01<> >
uniform01_rng(rng, uniform_01<>());
Eigen::VectorXd index(theta.rows());
index.setZero();
for (int i = 0; i < theta.rows(); i++) {
for (int j = i; j < theta.rows(); j++)
index(j) += theta(i, 0);
}
double c = uniform01_rng();
int b = 0;
while (c > index(b, 0))
b++;
return b + 1;
}
}
}
#endif
check_simplex.hpp
include_summand.hpp
stan::math::categorical_rng
int categorical_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &theta, RNG &rng)
Definition: categorical_rng.hpp:20
index_type.hpp
check_bounded.hpp
constants.hpp
sum.hpp
stan::math::check_simplex
bool check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Return true if the specified vector is simplex.
Definition: check_simplex.hpp:41
value_of.hpp

[ Stan Home Page ] © 2011–2015, Stan Development Team.