All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
categorical_log.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOG_HPP
#define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOG_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>
#include <stan/math/prim/scal/meta/is_constant_struct.hpp>
#include <cmath>
#include <vector>
namespace stan {
namespace math {
// Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
template <bool propto,
typename T_prob>
typename boost::math::tools::promote_args<T_prob>::type
categorical_log(int n,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
static const char* function("stan::math::categorical_log");
using stan::math::check_bounded;
using stan::math::check_simplex;
using boost::math::tools::promote_args;
using stan::math::value_of;
using std::log;
int lb = 1;
T_prob lp = 0.0;
check_bounded(function, "Number of categories", n, lb, theta.size());
if (!stan::is_constant_struct<T_prob>::value) {
if (!check_simplex(function, "Probabilities parameter", theta))
return lp;
} else {
if (!check_simplex(function, "Probabilities parameter", theta))
return lp;
}
if (include_summand<propto, T_prob>::value)
return log(theta(n-1));
return 0.0;
}
template <typename T_prob>
inline
typename boost::math::tools::promote_args<T_prob>::type
categorical_log(const typename
math::index_type<Eigen::Matrix<T_prob,
Eigen::Dynamic, 1> >::type n,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
return categorical_log<false>(n, theta);
}
// Categorical(n|theta) [0 < n <= N; 0 <= theta[n] <= 1; SUM theta = 1]
template <bool propto,
typename T_prob>
typename boost::math::tools::promote_args<T_prob>::type
categorical_log(const std::vector<int>& ns,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
static const char* function("stan::math::categorical_log");
using boost::math::tools::promote_args;
using stan::math::check_bounded;
using stan::math::check_simplex;
using stan::math::sum;
using stan::math::value_of;
using std::log;
int lb = 1;
T_prob lp = 0.0;
for (size_t i = 0; i < ns.size(); ++i)
check_bounded(function, "element of outcome array", ns[i],
lb, theta.size());
if (!stan::is_constant_struct<T_prob>::value) {
if (!check_simplex(function, "Probabilities parameter", theta))
return lp;
} else {
if (!check_simplex(function, "Probabilities parameter", theta))
return lp;
}
if (!include_summand<propto, T_prob>::value)
return 0.0;
if (ns.size() == 0)
return 0.0;
Eigen::Matrix<T_prob, Eigen::Dynamic, 1> log_theta(theta.size());
for (int i = 0; i < theta.size(); ++i)
log_theta(i) = log(theta(i));
Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
Eigen::Dynamic, 1> log_theta_ns(ns.size());
for (size_t i = 0; i < ns.size(); ++i)
log_theta_ns(i) = log_theta(ns[i] - 1);
return sum(log_theta_ns);
}
template <typename T_prob>
inline
typename boost::math::tools::promote_args<T_prob>::type
categorical_log(const std::vector<int>& ns,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
return categorical_log<false>(ns, theta);
}
}
}
#endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
check_simplex.hpp
stan::math::value_of
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::check_bounded
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true if the value is between the low and high values, inclusively.
Definition: check_bounded.hpp:95
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:36
include_summand.hpp
stan::is_constant_struct
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: is_constant_struct.hpp:16
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
index_type.hpp
stan::math::categorical_log
boost::math::tools::promote_args< T_prob >::type categorical_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Definition: categorical_log.hpp:25
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
is_constant_struct.hpp
value_of.hpp

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