Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ordered_logistic.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__ORDERED_LOGISTIC_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__ORDERED_LOGISTIC_HPP
3 
4 #include <boost/random/uniform_01.hpp>
5 #include <boost/random/variate_generator.hpp>
7 
8 #include <stan/prob/traits.hpp>
16 #include <stan/prob/constants.hpp>
17 
18 
19 namespace stan {
20 
21  namespace prob {
22 
23  template <typename T>
24  inline T log_inv_logit_diff(const T& alpha, const T& beta) {
25  using std::exp;
28  return beta + log1m_exp(alpha - beta) - log1p_exp(alpha) - log1p_exp(beta);
29  }
30 
31  // y in 0,...,K-1; c.size()==K-2, c increasing, lambda finite
58  template <bool propto, typename T_lambda, typename T_cut>
59  typename boost::math::tools::promote_args<T_lambda,T_cut>::type
60  ordered_logistic_log(int y, const T_lambda& lambda,
61  const Eigen::Matrix<T_cut,Eigen::Dynamic,1>& c) {
62  using std::exp;
63  using std::log;
65  using stan::math::log1m;
67 
68  static const char* function = "stan::prob::ordered_logistic(%1%)";
69 
77 
78  int K = c.size() + 1;
79 
80  typename boost::math::tools::promote_args<T_lambda,T_cut>::type lp(0.0);
81  check_bounded(function, y, 1, K, "Random variable", &lp);
82  check_finite(function, lambda, "Location parameter", &lp);
83  check_greater(function, c.size(), 0, "Size of cut points parameter", &lp);
84  for (int i = 1; i < c.size(); ++i)
85  check_greater(function, c(i), c(i - 1), "Cut points parameter", &lp);
86 
87  check_finite(function, c(c.size()-1), "Cut points parameter", &lp);
88  check_finite(function, c(0), "Cut points parameter", &lp);
89 
90  // log(1 - inv_logit(lambda))
91  if (y == 1)
92  return -log1p_exp(lambda - c(0));
93 
94  // log(inv_logit(lambda - c(K-3)));
95  if (y == K) {
96  return -log1p_exp(c(K-2) - lambda);
97  }
98 
99  // if (2 < y < K) { ... }
100  // log(inv_logit(lambda - c(y-2)) - inv_logit(lambda - c(y-1)))
101  return log_inv_logit_diff(c(y-2) - lambda,
102  c(y-1) - lambda);
103 
104  }
105 
106  template <typename T_lambda, typename T_cut>
107  typename boost::math::tools::promote_args<T_lambda,T_cut>::type
108  ordered_logistic_log(int y, const T_lambda& lambda,
109  const Eigen::Matrix<T_cut,Eigen::Dynamic,1>& c) {
110  return ordered_logistic_log<false>(y,lambda,c);
111  }
112 
113  template <class RNG>
114  inline int
115  ordered_logistic_rng(const double eta,
116  const Eigen::Matrix<double,Eigen::Dynamic,1>& c,
117  RNG& rng) {
118  using boost::variate_generator;
119  using stan::math::inv_logit;
120 
121  static const char* function = "stan::prob::ordered_logistic(%1%)";
122 
130 
131  check_finite(function, eta, "Location parameter", (double*)0);
132  check_greater(function, c.size(), 0, "Size of cut points parameter",
133  (double*)0);
134  for (int i = 1; i < c.size(); ++i) {
135  check_greater(function, c(i), c(i - 1),
136  "Cut points parameter", (double*)0);
137  }
138  check_finite(function, c(c.size()-1),
139  "Cut points parameter", (double*)0);
140  check_finite(function, c(0),
141  "Cut points parameter", (double*)0);
142 
143  Eigen::VectorXd cut(c.rows()+1);
144  cut(0) = 1 - inv_logit(eta - c(0));
145  for(int j = 1; j < c.rows(); j++)
146  cut(j) = inv_logit(eta - c(j - 1)) - inv_logit(eta - c(j));
147  cut(c.rows()) = inv_logit(eta - c(c.rows() - 1));
148 
149  return stan::prob::categorical_rng(cut, rng);
150  }
151  }
152 }
153 
154 #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 > log1p_exp(const fvar< T > &x)
Definition: log1p_exp.hpp:15
boost::math::tools::promote_args< T >::type log1p_exp(const T a)
Calculates the log of 1 plus the exponential of the specified value without overflow.
Definition: log1p_exp.hpp:44
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
boost::math::tools::promote_args< T >::type inv_logit(const T a)
Returns the inverse logit function applied to the argument.
Definition: inv_logit.hpp:52
int categorical_rng(const Eigen::Matrix< double, Eigen::Dynamic, 1 > &theta, RNG &rng)
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 ...
fvar< T > inv_logit(const fvar< T > &x)
Definition: inv_logit.hpp:15
T log_inv_logit_diff(const T &alpha, const T &beta)
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
fvar< T > log1m_exp(const fvar< T > &x)
Definition: log1m_exp.hpp:17
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result)
bool check_less_or_equal(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result)
boost::math::tools::promote_args< T >::type log1m_exp(const T a)
Calculates the log of 1 minus the exponential of the specified value without overflow log1m_exp(x) = ...
Definition: log1m_exp.hpp:40
bool check_less(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result)
Definition: check_less.hpp:52
int ordered_logistic_rng(const double eta, const Eigen::Matrix< double, Eigen::Dynamic, 1 > &c, RNG &rng)
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:16
boost::math::tools::promote_args< T >::type log1m(T x)
Return the natural logarithm of one minus the specified value.
Definition: log1m.hpp:40

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