Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
categorical_logit.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__CATEGORICAL_LOGIT_LOG_HPP
2 #define STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__DISCRETE__CATEGORICAL_LOGIT_LOG_HPP
3 
4 #include <vector>
5 #include <boost/math/tools/promotion.hpp>
10 #include <stan/math/matrix/sum.hpp>
11 #include <stan/prob/traits.hpp>
12 
13 namespace stan {
14 
15  namespace prob {
16 
17  // CategoricalLog(n|theta) [0 < n <= N, theta unconstrained], no checking
18  template <bool propto,
19  typename T_prob>
20  typename boost::math::tools::promote_args<T_prob>::type
22  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& beta) {
23  static const char* function = "stan::prob::categorical_logit_log(%1%)";
24 
28 
29  double lp = 0.0;
30  check_bounded(function, n, 1, beta.size(),
31  "categorical outcome out of support",
32  &lp);
33 
34  check_finite(function, beta, "log odds parameter", &lp);
35 
37  return 0.0;
38 
39  // FIXME: wasteful vs. creating term (n-1) if not vectorized
40  return beta(n-1) - log_sum_exp(beta); // == log_softmax(beta)(n-1);
41  }
42 
43  template <typename T_prob>
44  inline
45  typename boost::math::tools::promote_args<T_prob>::type
47  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& beta) {
48  return categorical_logit_log<false>(n,beta);
49  }
50 
51  template <bool propto,
52  typename T_prob>
53  typename boost::math::tools::promote_args<T_prob>::type
54  categorical_logit_log(const std::vector<int>& ns,
55  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& beta) {
56  static const char* function = "stan::prob::categorical_logit_log(%1%)";
57 
61  using stan::math::sum;
62 
63  double lp = 0.0;
64  for (size_t k = 0; k < ns.size(); ++k)
65  check_bounded(function, ns[k], 1, beta.size(),
66  "categorical outcome out of support",
67  &lp);
68 
69  check_finite(function, beta, "log odds parameter", &lp);
70 
72  return 0.0;
73 
74  if (ns.size() == 0)
75  return 0.0;
76 
77  Eigen::Matrix<T_prob,Eigen::Dynamic,1> log_softmax_beta
78  = log_softmax(beta);
79 
80  // FIXME: replace with more efficient sum()
81  Eigen::Matrix<typename boost::math::tools::promote_args<T_prob>::type,
82  Eigen::Dynamic,1> results(ns.size());
83  for (size_t i = 0; i < ns.size(); ++i)
84  results[i] = log_softmax_beta(ns[i] - 1);
85  return sum(results);
86  }
87 
88  template <typename T_prob>
89  inline
90  typename boost::math::tools::promote_args<T_prob>::type
91  categorical_logit_log(const std::vector<int>& ns,
92  const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& beta) {
93  return categorical_logit_log<false>(ns,beta);
94  }
95 
96 
97  }
98 }
99 #endif
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result)
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
Definition: log_sum_exp.hpp:15
boost::math::tools::promote_args< T1, T2 >::type log_sum_exp(const T2 &a, const T1 &b)
Calculates the log sum of exponetials without overflow.
Definition: log_sum_exp.hpp:49
boost::math::tools::promote_args< T_prob >::type categorical_logit_log(int n, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &beta)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > log_softmax(const Eigen::Matrix< fvar< T >, Eigen::Dynamic, 1 > &alpha)
Definition: log_softmax.hpp:16
fvar< T > sum(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: sum.hpp:14
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
Eigen::Matrix< T, Eigen::Dynamic, 1 > log_softmax(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v)
Return the natural logarithm of the softmax of the specified vector.
Definition: log_softmax.hpp:44
double sum(std::vector< double > &x)
Definition: sum.hpp:10

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