All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
multinomial_log.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_PROB_MULTINOMIAL_LOG_HPP
#define STAN_MATH_PRIM_MAT_PROB_MULTINOMIAL_LOG_HPP
#include <boost/math/special_functions/gamma.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_size_match.hpp>
#include <stan/math/prim/scal/err/check_nonnegative.hpp>
#include <stan/math/prim/scal/err/check_positive.hpp>
#include <stan/math/prim/scal/fun/multiply_log.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/meta/include_summand.hpp>
#include <vector>
namespace stan {
namespace math {
// Multinomial(ns|N, theta) [0 <= n <= N; SUM ns = N;
// 0 <= theta[n] <= 1; SUM theta = 1]
template <bool propto,
typename T_prob>
typename boost::math::tools::promote_args<T_prob>::type
multinomial_log(const std::vector<int>& ns,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
static const char* function("stan::math::multinomial_log");
using stan::math::check_nonnegative;
using stan::math::check_simplex;
using stan::math::check_size_match;
using boost::math::tools::promote_args;
using boost::math::lgamma;
typename promote_args<T_prob>::type lp(0.0);
check_nonnegative(function, "Number of trials variable", ns);
check_simplex(function, "Probabilites parameter", theta);
check_size_match(function,
"Size of number of trials variable", ns.size(),
"rows of probabilities parameter", theta.rows());
using stan::math::multiply_log;
if (include_summand<propto>::value) {
double sum = 1.0;
for (unsigned int i = 0; i < ns.size(); ++i)
sum += ns[i];
lp += lgamma(sum);
for (unsigned int i = 0; i < ns.size(); ++i)
lp -= lgamma(ns[i] + 1.0);
}
if (include_summand<propto, T_prob>::value) {
for (unsigned int i = 0; i < ns.size(); ++i)
lp += multiply_log(ns[i], theta[i]);
}
return lp;
}
template <typename T_prob>
typename boost::math::tools::promote_args<T_prob>::type
multinomial_log(const std::vector<int>& ns,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
return multinomial_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::lgamma
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
check_size_match.hpp
check_positive.hpp
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
multiply_log.hpp
check_nonnegative.hpp
stan::math::check_size_match
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
Definition: check_size_match.hpp:30
stan::math::multiply_log
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
Definition: multiply_log.hpp:15
constants.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
stan::math::multinomial_log
boost::math::tools::promote_args< T_prob >::type multinomial_log(const std::vector< int > &ns, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Definition: multinomial_log.hpp:24
stan::math::check_nonnegative
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
Definition: check_nonnegative.hpp:66

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