Stan Math Library  2.20.0
reverse mode automatic differentiation
ordered_logistic_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_ORDERED_LOGISTIC_LPMF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_ORDERED_LOGISTIC_LPMF_HPP
3 
18 #include <vector>
19 
20 namespace stan {
21 namespace math {
22 
71 template <bool propto, typename T_y, typename T_loc, typename T_cut>
73  const T_y& y, const T_loc& lambda, const T_cut& c) {
74  static const char* function = "ordered_logistic";
75 
76  typedef
77  typename stan::partials_return_type<T_loc, T_cut>::type T_partials_return;
78  typedef typename Eigen::Matrix<T_partials_return, -1, 1> T_partials_vec;
79 
80  scalar_seq_view<T_loc> lam_vec(lambda);
81  scalar_seq_view<T_y> y_vec(y);
82  vector_seq_view<T_cut> c_vec(c);
83 
84  int K = c_vec[0].size() + 1;
85  int N = length(lambda);
86  int C_l = length_mvt(c);
87 
88  check_consistent_sizes(function, "Integers", y, "Locations", lambda);
89  if (C_l > 1)
90  check_size_match(function, "Length of location variables ", N,
91  "Number of cutpoint vectors ", C_l);
92 
93  int size_c_old = c_vec[0].size();
94  for (int i = 1; i < C_l; i++) {
95  int size_c_new = c_vec[i].size();
96 
97  check_size_match(function, "Size of one of the vectors of cutpoints ",
98  size_c_new, "Size of another vector of the cutpoints ",
99  size_c_old);
100  }
101 
102  for (int n = 0; n < N; n++) {
103  check_bounded(function, "Random variable", y_vec[n], 1, K);
104  check_finite(function, "Location parameter", lam_vec[n]);
105  }
106 
107  for (int i = 0; i < C_l; i++) {
108  check_ordered(function, "Cut-points", c_vec[i]);
109  check_greater(function, "Size of cut points parameter", c_vec[i].size(), 0);
110  check_finite(function, "Final cut-point", c_vec[i](c_vec[i].size() - 1));
111  check_finite(function, "First cut-point", c_vec[i](0));
112  }
113 
114  operands_and_partials<T_loc, T_cut> ops_partials(lambda, c);
115 
116  T_partials_return logp(0.0);
117  T_partials_vec c_dbl = value_of(c_vec[0]).template cast<T_partials_return>();
118 
119  for (int n = 0; n < N; ++n) {
120  if (C_l > 1)
121  c_dbl = value_of(c_vec[n]).template cast<T_partials_return>();
122  T_partials_return lam_dbl = value_of(lam_vec[n]);
123 
124  if (y_vec[n] == 1) {
125  logp -= log1p_exp(lam_dbl - c_dbl[0]);
126  T_partials_return d = inv_logit(lam_dbl - c_dbl[0]);
127 
129  ops_partials.edge1_.partials_[n] -= d;
130 
132  ops_partials.edge2_.partials_vec_[n](0) += d;
133 
134  } else if (y_vec[n] == K) {
135  logp -= log1p_exp(c_dbl[K - 2] - lam_dbl);
136  T_partials_return d = inv_logit(c_dbl[K - 2] - lam_dbl);
137 
139  ops_partials.edge1_.partials_[n] = d;
140 
142  ops_partials.edge2_.partials_vec_[n](K - 2) -= d;
143 
144  } else {
145  T_partials_return d1
146  = inv(1 - exp(c_dbl[y_vec[n] - 1] - c_dbl[y_vec[n] - 2]))
147  - inv_logit(c_dbl[y_vec[n] - 2] - lam_dbl);
148  T_partials_return d2
149  = inv(1 - exp(c_dbl[y_vec[n] - 2] - c_dbl[y_vec[n] - 1]))
150  - inv_logit(c_dbl[y_vec[n] - 1] - lam_dbl);
151  logp += log_inv_logit_diff(lam_dbl - c_dbl[y_vec[n] - 2],
152  lam_dbl - c_dbl[y_vec[n] - 1]);
153 
155  ops_partials.edge1_.partials_[n] -= d1 + d2;
156 
158  ops_partials.edge2_.partials_vec_[n](y_vec[n] - 2) += d1;
159  ops_partials.edge2_.partials_vec_[n](y_vec[n] - 1) += d2;
160  }
161  }
162  }
163  return ops_partials.build(logp);
164 }
165 
166 template <typename T_y, typename T_loc, typename T_cut>
168  const T_y& y, const T_loc& lambda, const T_cut& c) {
169  return ordered_logistic_lpmf<false>(y, lambda, c);
170 }
171 
172 } // namespace math
173 } // namespace stan
174 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
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.
void check_ordered(const char *function, const char *name, const std::vector< T_y > &y)
Check if the specified vector is sorted into strictly increasing order.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
fvar< T > inv_logit(const fvar< T > &x)
Returns the inverse logit function applied to the argument.
Definition: inv_logit.hpp:20
return_type< T_loc, T_cut >::type ordered_logistic_lpmf(const T_y &y, const T_loc &lambda, const T_cut &c)
Returns the (natural) log probability of the specified array of integers given the vector of continuo...
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
fvar< T > log_inv_logit_diff(const fvar< T > &x, const fvar< T > &y)
Returns fvar with the natural logarithm of the difference of the inverse logits of the specified argu...
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
internal::ops_partials_edge< double, Op2 > edge2_
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
fvar< T > log1p_exp(const fvar< T > &x)
Definition: log1p_exp.hpp:12
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
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.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
size_t length_mvt(const Eigen::Matrix< T, R, C > &)
Definition: length_mvt.hpp:12
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > inv(const fvar< T > &x)
Definition: inv.hpp:12

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