| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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>
6 #include <stan/math/prim/scal/fun/inv_logit.hpp>
7 #include <stan/math/prim/scal/fun/log1m.hpp>
8 #include <stan/math/prim/scal/fun/log1m_exp.hpp>
9 #include <stan/math/prim/scal/fun/log1p_exp.hpp>
10 #include <stan/math/prim/scal/err/check_bounded.hpp>
11 #include <stan/math/prim/scal/err/check_finite.hpp>
12 #include <stan/math/prim/scal/err/check_greater.hpp>
13 #include <stan/math/prim/scal/err/check_less.hpp>
14 #include <stan/math/prim/scal/err/check_less_or_equal.hpp>
15 #include <stan/math/prim/scal/err/check_nonnegative.hpp>
16 #include <stan/math/prim/scal/err/check_positive.hpp>
17 #include <stan/math/prim/scal/fun/constants.hpp>
18 #include <stan/math/prim/mat/prob/categorical_rng.hpp>
19 #include <stan/math/prim/scal/meta/include_summand.hpp>
20
21 namespace stan {
22
23 namespace math {
24
25 template <typename T>
26 inline T log_inv_logit_diff(const T& alpha, const T& beta) {
27 using std::exp;
28 using stan::math::log1m_exp;
29 using stan::math::log1p_exp;
30 return beta + log1m_exp(alpha - beta) - log1p_exp(alpha)
31 - log1p_exp(beta);
32 }
33
34 // y in 0, ..., K-1; c.size()==K-2, c increasing, lambda finite
59 template <bool propto, typename T_lambda, typename T_cut>
60 typename boost::math::tools::promote_args<T_lambda, T_cut>::type
61 ordered_logistic_log(int y, const T_lambda& lambda,
62 const Eigen::Matrix<T_cut, Eigen::Dynamic, 1>& c) {
63 using std::exp;
64 using std::log;
65 using stan::math::inv_logit;
66 using stan::math::log1m;
67 using stan::math::log1p_exp;
68
69 static const char* function("stan::math::ordered_logistic");
70
71 using stan::math::check_finite;
72 using stan::math::check_positive;
73 using stan::math::check_nonnegative;
74 using stan::math::check_less;
75 using stan::math::check_less_or_equal;
76 using stan::math::check_greater;
77 using stan::math::check_bounded;
78
79 int K = c.size() + 1;
80
81 check_bounded(function, "Random variable", y, 1, K);
82 check_finite(function, "Location parameter", lambda);
83 check_greater(function, "Size of cut points parameter", c.size(), 0);
84 for (int i = 1; i < c.size(); ++i)
85 check_greater(function, "Cut points parameter", c(i), c(i - 1));
86
87 check_finite(function, "Cut points parameter", c(c.size()-1));
88 check_finite(function, "Cut points parameter", c(0));
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 template <typename T_lambda, typename T_cut>
106 typename boost::math::tools::promote_args<T_lambda, T_cut>::type
107 ordered_logistic_log(int y, const T_lambda& lambda,
108 const Eigen::Matrix<T_cut, Eigen::Dynamic, 1>& c) {
109 return ordered_logistic_log<false>(y, lambda, c);
110 }
111 }
112 }
113
114 #endif
stan::math::check_less
bool check_less(const char *function, const char *name, const T_y &y, const T_high &high)
Return true if y is strictly less than high.
Definition: check_less.hpp:81
check_positive.hpp
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::inv_logit
fvar< T > inv_logit(const fvar< T > &x)
Definition: inv_logit.hpp:15
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_inv_logit_diff
T log_inv_logit_diff(const T &alpha, const T &beta)
Definition: ordered_logistic_log.hpp:26
include_summand.hpp
check_greater.hpp
check_nonnegative.hpp
check_less_or_equal.hpp
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::log1m_exp
fvar< T > log1m_exp(const fvar< T > &x)
Definition: log1m_exp.hpp:16
log1m_exp.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
log1m.hpp
stan::math::check_less_or_equal
bool check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Return true if y is less or equal to high.
Definition: check_less_or_equal.hpp:81
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
log1p_exp.hpp
constants.hpp
stan::math::log1p_exp
fvar< T > log1p_exp(const fvar< T > &x)
Definition: log1p_exp.hpp:13
check_finite.hpp
stan::math::check_nonnegative
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
Definition: check_nonnegative.hpp:66
categorical_rng.hpp
inv_logit.hpp
check_less.hpp
stan::math::log1m
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:16
stan::math::ordered_logistic_log
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 ...
Definition: ordered_logistic_log.hpp:61
stan::math::check_greater
bool check_greater(const char *function, const char *name, const T_y &y, const T_low &low)
Return true if y is strictly greater than low.
Definition: check_greater.hpp:84

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