Stan Math Library  2.14.0
reverse mode automatic differentiation
ordered_logistic_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_ORDERED_LOGISTIC_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_ORDERED_LOGISTIC_LOG_HPP
3 
4 #include <boost/random/uniform_01.hpp>
5 #include <boost/random/variate_generator.hpp>
21 
22 namespace stan {
23  namespace math {
24 
49  template <bool propto, typename T_lambda, typename T_cut>
50  typename boost::math::tools::promote_args<T_lambda, T_cut>::type
51  ordered_logistic_log(int y, const T_lambda& lambda,
52  const Eigen::Matrix<T_cut, Eigen::Dynamic, 1>& c) {
53  using std::exp;
54  using std::log;
55 
56  static const char* function("ordered_logistic");
57 
58  int K = c.size() + 1;
59 
60  check_bounded(function, "Random variable", y, 1, K);
61  check_finite(function, "Location parameter", lambda);
62  check_greater(function, "Size of cut points parameter", c.size(), 0);
63  for (int i = 1; i < c.size(); ++i)
64  check_greater(function, "Cut points parameter", c(i), c(i - 1));
65 
66  check_finite(function, "Cut points parameter", c(c.size()-1));
67  check_finite(function, "Cut points parameter", c(0));
68 
69  // log(1 - inv_logit(lambda))
70  if (y == 1)
71  return -log1p_exp(lambda - c(0));
72 
73  // log(inv_logit(lambda - c(K-3)));
74  if (y == K) {
75  return -log1p_exp(c(K-2) - lambda);
76  }
77 
78  // if (2 < y < K) { ... }
79  // log(inv_logit(lambda - c(y-2)) - inv_logit(lambda - c(y-1)))
80  return log_inv_logit_diff(c(y-2) - lambda,
81  c(y-1) - lambda);
82  }
83 
84  template <typename T_lambda, typename T_cut>
85  typename boost::math::tools::promote_args<T_lambda, T_cut>::type
86  ordered_logistic_log(int y, const T_lambda& lambda,
87  const Eigen::Matrix<T_cut, Eigen::Dynamic, 1>& c) {
88  return ordered_logistic_log<false>(y, lambda, c);
89  }
90 
91  }
92 }
93 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
T log_inv_logit_diff(const T &alpha, const T &beta)
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
fvar< T > log1p_exp(const fvar< T > &x)
Definition: log1p_exp.hpp:13
void check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Check if y is strictly greater than low.
boost::math::tools::promote_args< T_lambda, T_cut >::type ordered_logistic_log(int y, const T_lambda &lambda, const Eigen::Matrix< T_cut, Eigen::Dynamic, 1 > &c)
Returns the (natural) log probability of the specified integer outcome given the continuous location ...

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