1 #ifndef STAN_MATH_PRIM_MAT_PROB_ORDERED_LOGISTIC_LPMF_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_ORDERED_LOGISTIC_LPMF_HPP 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";
78 typedef typename Eigen::Matrix<T_partials_return, -1, 1> T_partials_vec;
84 int K = c_vec[0].size() + 1;
91 "Number of cutpoint vectors ", C_l);
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();
98 size_c_new,
"Size of another vector of the cutpoints ",
102 for (
int n = 0; n < N; n++) {
104 check_finite(
function,
"Location parameter", lam_vec[n]);
107 for (
int i = 0; i < C_l; i++) {
116 T_partials_return logp(0.0);
117 T_partials_vec c_dbl =
value_of(c_vec[0]).template cast<T_partials_return>();
119 for (
int n = 0; n < N; ++n) {
121 c_dbl =
value_of(c_vec[n]).template cast<T_partials_return>();
122 T_partials_return lam_dbl =
value_of(lam_vec[n]);
126 T_partials_return d =
inv_logit(lam_dbl - c_dbl[0]);
129 ops_partials.
edge1_.partials_[n] -= d;
132 ops_partials.
edge2_.partials_vec_[n](0) += d;
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);
139 ops_partials.
edge1_.partials_[n] = d;
142 ops_partials.
edge2_.partials_vec_[n](K - 2) -= d;
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);
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);
152 lam_dbl - c_dbl[y_vec[n] - 1]);
155 ops_partials.
edge1_.partials_[n] -= d1 + d2;
158 ops_partials.
edge2_.partials_vec_[n](y_vec[n] - 2) += d1;
159 ops_partials.
edge2_.partials_vec_[n](y_vec[n] - 1) += d2;
163 return ops_partials.
build(logp);
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);
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.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
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.
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.
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
fvar< T > exp(const fvar< T > &x)
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.
fvar< T > log1p_exp(const fvar< T > &x)
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 > &)
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > inv(const fvar< T > &x)