All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
categorical_logit_log.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_LOG_HPP
#define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_LOG_HPP
#include <stan/math/prim/scal/err/check_bounded.hpp>
#include <stan/math/prim/scal/err/check_finite.hpp>
#include <stan/math/prim/arr/fun/log_sum_exp.hpp>
#include <stan/math/prim/mat/fun/log_softmax.hpp>
#include <stan/math/prim/mat/fun/log_sum_exp.hpp>
#include <stan/math/prim/mat/fun/sum.hpp>
#include <stan/math/prim/scal/meta/include_summand.hpp>
#include <boost/math/tools/promotion.hpp>
#include <vector>
namespace stan {
namespace math {
// CategoricalLog(n|theta) [0 < n <= N, theta unconstrained], no checking
template <bool propto,
typename T_prob>
typename boost::math::tools::promote_args<T_prob>::type
categorical_logit_log(int n,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
beta) {
static const char* function("stan::math::categorical_logit_log");
using stan::math::check_bounded;
using stan::math::check_finite;
using stan::math::log_sum_exp;
check_bounded(function, "categorical outcome out of support", n,
1, beta.size());
check_finite(function, "log odds parameter", beta);
if (!include_summand<propto, T_prob>::value)
return 0.0;
// FIXME: wasteful vs. creating term (n-1) if not vectorized
return beta(n-1) - log_sum_exp(beta); // == log_softmax(beta)(n-1);
}
template <typename T_prob>
inline
typename boost::math::tools::promote_args<T_prob>::type
categorical_logit_log(int n,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
beta) {
return categorical_logit_log<false>(n, beta);
}
template <bool propto,
typename T_prob>
typename boost::math::tools::promote_args<T_prob>::type
categorical_logit_log(const std::vector<int>& ns,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
beta) {
static const char* function("stan::math::categorical_logit_log");
using stan::math::check_bounded;
using stan::math::check_finite;
using stan::math::log_softmax;
using stan::math::sum;
for (size_t k = 0; k < ns.size(); ++k)
check_bounded(function, "categorical outcome out of support",
ns[k], 1, beta.size());
check_finite(function, "log odds parameter", beta);
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_softmax_beta
= log_softmax(beta);
// FIXME: replace with more efficient sum()
Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
Eigen::Dynamic, 1> results(ns.size());
for (size_t i = 0; i < ns.size(); ++i)
results[i] = log_softmax_beta(ns[i] - 1);
return sum(results);
}
template <typename T_prob>
inline
typename boost::math::tools::promote_args<T_prob>::type
categorical_logit_log(const std::vector<int>& ns,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
beta) {
return categorical_logit_log<false>(ns, beta);
}
}
}
#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
stan::math::categorical_logit_log
boost::math::tools::promote_args< T_prob >::type categorical_logit_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &beta)
Definition: categorical_logit_log.hpp:22
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::log_softmax
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > log_softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: log_softmax.hpp:16
stan::math::log_sum_exp
fvar< T > log_sum_exp(const std::vector< fvar< T > > &v)
Definition: log_sum_exp.hpp:14
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
log_sum_exp.hpp
include_summand.hpp
check_bounded.hpp
stan::math::check_finite
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
Definition: check_finite.hpp:62
log_sum_exp.hpp
check_finite.hpp
sum.hpp
log_softmax.hpp

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