Stan  1.0
probability, sampling & optimization
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 <stan/prob/traits.hpp>
10 
11 
12 namespace stan {
13 
14  namespace prob {
15 
16 
17  template <typename T>
18  inline T log_inv_logit_diff(const T& alpha, const T& beta) {
19  using std::exp;
20  using stan::math::log1m;
22  return beta + log1m(exp(alpha - beta)) - log1p_exp(alpha) - log1p_exp(beta);
23  }
24 
25  // y in 0,...,K-1; c.size()==K-2, c increasing, lambda finite
52  template <bool propto,
53  typename T_lambda,
54  typename T_cut,
55  class Policy>
56  typename boost::math::tools::promote_args<T_lambda,T_cut>::type
58  const T_lambda& lambda,
59  const Eigen::Matrix<T_cut,Eigen::Dynamic,1>& c,
60  const Policy&) {
61 
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  if (!check_bounded(function, y, 1, K,
82  "Random variable",
83  &lp, Policy()))
84  return lp;
85 
86  if (!check_finite(function, lambda,
87  "Location parameter", &lp, Policy()))
88  return lp;
89 
90  if (!check_greater(function, c.size(), 0,
91  "Size of cut points parameter",
92  &lp, Policy()))
93  return lp;
94 
95 
96  for (int i = 1; i < c.size(); ++i) {
97  if (!check_greater(function, c(i), c(i - 1),
98  "Cut points parameter",
99  &lp, Policy()))
100  return lp;
101  }
102 
103  if (!check_finite(function, c(c.size()-1),
104  "Cut points parameter",
105  &lp, Policy()))
106  return lp;
107 
108  if (!check_finite(function, c(0),
109  "Cut points parameter",
110  &lp, Policy()))
111  return lp;
112 
113  // log(1 - inv_logit(lambda))
114  if (y == 1)
115  return -log1p_exp(lambda - c(0));
116 
117  // log(inv_logit(lambda - c(K-3)));
118  if (y == K) {
119  return -log1p_exp(c(K-2) - lambda);
120  }
121 
122  // if (2 < y < K) { ... }
123  // log(inv_logit(lambda - c(y-2)) - inv_logit(lambda - c(y-1)))
124  return log_inv_logit_diff(c(y-2) - lambda,
125  c(y-1) - lambda);
126 
127  }
128 
129 
130  template <bool propto,
131  typename T_lambda,
132  typename T_cut>
133  typename boost::math::tools::promote_args<T_lambda,T_cut>::type
135  const T_lambda& lambda,
136  const Eigen::Matrix<T_cut,Eigen::Dynamic,1>& c) {
137  return ordered_logistic_log<propto>(y,lambda,c,stan::math::default_policy());
138  }
139 
140 
141  template <typename T_lambda,
142  typename T_cut,
143  class Policy>
144  typename boost::math::tools::promote_args<T_lambda,T_cut>::type
146  const T_lambda& lambda,
147  const Eigen::Matrix<T_cut,Eigen::Dynamic,1>& c,
148  const Policy&) {
149  return ordered_logistic_log<false>(y,lambda,c,Policy());
150  }
151 
152 
153  template <typename T_lambda,
154  typename T_cut>
155  typename boost::math::tools::promote_args<T_lambda,T_cut>::type
157  const T_lambda& lambda,
158  const Eigen::Matrix<T_cut,Eigen::Dynamic,1>& c) {
159  return ordered_logistic_log<false>(y,lambda,c,stan::math::default_policy());
160  }
161 
162  }
163 }
164 
165 #endif
var log1p_exp(const stan::agrad::var &a)
Return the log of 1 plus the exponential of the specified variable.
var log1m(const stan::agrad::var &a)
The log (1 - x) function for variables.
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
Definition: agrad.hpp:1716
boost::math::tools::promote_args< T >::type log1m(T x)
Return the natural logarithm of one minus the specified value.
bool check_less(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result, const Policy &)
boost::math::tools::promote_args< T >::type inv_logit(T a)
Returns the inverse logit function applied to the argument.
double log1p_exp(const double &a)
Calculates the log of 1 plus the exponential of the specified value without overflow.
bool check_greater(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result, const Policy &)
bool check_less_or_equal(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result, const Policy &)
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result, const Policy &)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
T log_inv_logit_diff(const T &alpha, const T &beta)
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, const Policy &)
Returns the (natural) log probability of the specified integer outcome given the continuous location ...
Probability, optimization and sampling library.
Definition: agrad.cpp:6

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