Automatic Differentiation
 
Loading...
Searching...
No Matches
ordered_logistic_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_ORDERED_LOGISTIC_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_ORDERED_LOGISTIC_LPMF_HPP
3
19#include <vector>
20
21namespace stan {
22namespace math {
23
72template <bool propto, typename T_y, typename T_loc, typename T_cut,
74 T_y, T_loc, T_cut>* = nullptr>
76 const T_loc& lambda,
77 const T_cut& c) {
78 using T_partials_return = partials_return_t<T_loc, T_cut>;
79 using T_cuts_val = partials_return_t<T_cut>;
80 using T_y_ref = ref_type_t<T_y>;
81 using T_lambda_ref = ref_type_if_not_constant_t<T_loc>;
82 using T_cut_ref = ref_type_if_not_constant_t<T_cut>;
83 using Eigen::Array;
84 using Eigen::Dynamic;
85 static constexpr const char* function = "ordered_logistic";
86
87 T_cut_ref c_ref = c;
88 vector_seq_view<T_cut_ref> c_vec(c_ref);
89 int N = math::size(lambda);
90 int C_l = size_mvt(c);
91
92 check_consistent_sizes(function, "Integers", y, "Locations", lambda);
93 if (C_l > 1) {
94 check_size_match(function, "Length of location variables ", N,
95 "Number of cutpoint vectors ", C_l);
96 }
97
98 T_y_ref y_ref = y;
99 T_lambda_ref lambda_ref = lambda;
100
101 scalar_seq_view<T_y_ref> y_seq(y_ref);
102
103 decltype(auto) lambda_val = to_ref(as_value_array_or_scalar(lambda_ref));
104
105 check_finite(function, "Location parameter", lambda_val);
106 if (C_l == 0 || N == 0) {
107 return 0;
108 }
109 int K = c_vec[0].size() + 1;
110 check_bounded(function, "Random variable", y_ref, 1, K);
111
112 for (int i = 0; i < C_l; i++) {
113 check_size_match(function, "One cutpoint set", c_vec[i].size(),
114 "First cutpoint set", K - 1);
115 check_ordered(function, "Cut-points", c_vec[i]);
116 if (K > 1) {
117 if (K > 2) {
118 check_finite(function, "Final cut-point", c_vec[i].coeff(K - 2));
119 }
120 check_finite(function, "First cut-point", c_vec[i].coeff(0));
121 }
122 }
124 return 0.0;
125 }
126
127 scalar_seq_view<decltype(lambda_val)> lam_vec(lambda_val);
128 T_partials_return logp(0.0);
129 Array<T_cuts_val, Dynamic, 1> cuts_y1(N), cuts_y2(N);
130 for (int i = 0; i < N; i++) {
131 int c = y_seq[i];
132 if (c != K) {
133 cuts_y1.coeffRef(i) = value_of(c_vec[i].coeff(c - 1));
134 } else {
135 cuts_y1.coeffRef(i) = INFINITY;
136 }
137 if (c != 1) {
138 cuts_y2.coeffRef(i) = value_of(c_vec[i].coeff(c - 2));
139 } else {
140 cuts_y2.coeffRef(i) = -INFINITY;
141 }
142 }
143
144 Array<T_partials_return, Dynamic, 1> cut2 = lambda_val - cuts_y2;
145 Array<T_partials_return, Dynamic, 1> cut1 = lambda_val - cuts_y1;
146
147 // Not immediately evaluating next two expressions benefits performance
148 auto m_log_1p_exp_cut1
149 = (cut1 > 0.0).select(-cut1, 0) - (-cut1.abs()).exp().log1p();
150 auto m_log_1p_exp_m_cut2
151 = (cut2 <= 0.0).select(cut2, 0) - (-cut2.abs()).exp().log1p();
152
154 Eigen::Map<const Eigen::Matrix<value_type_t<T_y>, Eigen::Dynamic, 1>> y_vec(
155 y_seq.data(), y_seq.size());
156 auto log1m_exp_cuts_diff = log1m_exp(cut1 - cut2);
157 logp = y_vec.cwiseEqual(1)
158 .select(m_log_1p_exp_cut1,
159 y_vec.cwiseEqual(K).select(m_log_1p_exp_m_cut2,
160 m_log_1p_exp_m_cut2
161 + log1m_exp_cuts_diff
162 + m_log_1p_exp_cut1))
163 .sum();
164 } else {
165 if (y_seq[0] == 1) {
166 logp = m_log_1p_exp_cut1.sum();
167 } else if (y_seq[0] == K) {
168 logp = m_log_1p_exp_m_cut2.sum();
169 } else {
170 logp = (m_log_1p_exp_m_cut2 + log1m_exp(cut1 - cut2).array()
171 + m_log_1p_exp_cut1)
172 .sum();
173 }
174 }
175
176 auto ops_partials = make_partials_propagator(lambda_ref, c_ref);
178 Array<T_partials_return, Dynamic, 1> exp_m_cut1 = exp(-cut1);
179 Array<T_partials_return, Dynamic, 1> exp_m_cut2 = exp(-cut2);
180 Array<T_partials_return, Dynamic, 1> exp_cuts_diff = exp(cuts_y2 - cuts_y1);
181 Array<T_partials_return, Dynamic, 1> d1
182 = (cut2 > 0).select(exp_m_cut2 / (1 + exp_m_cut2), 1 / (1 + exp(cut2)))
183 - exp_cuts_diff / (exp_cuts_diff - 1);
184 Array<T_partials_return, Dynamic, 1> d2
185 = 1 / (1 - exp_cuts_diff)
186 - (cut1 > 0).select(exp_m_cut1 / (1 + exp_m_cut1),
187 1 / (1 + exp(cut1)));
189 partials<0>(ops_partials) = d1 - d2;
190 }
192 for (int i = 0; i < N; i++) {
193 int c = y_seq[i];
194 if (c != K) {
195 partials_vec<1>(ops_partials)[i][c - 1] += d2.coeff(i);
196 }
197 if (c != 1) {
198 partials_vec<1>(ops_partials)[i][c - 2] -= d1.coeff(i);
199 }
200 }
201 }
202 }
203 return ops_partials.build(logp);
204}
205
206template <typename T_y, typename T_loc, typename T_cut>
208 const T_loc& lambda,
209 const T_cut& c) {
210 return ordered_logistic_lpmf<false>(y, lambda, c);
211}
212
213} // namespace math
214} // namespace stan
215#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
Definition select.hpp:148
return_type_t< T_y_cl, T_loc_cl, T_cuts_cl > ordered_logistic_lpmf(const T_y_cl &y, const T_loc_cl &lambda, const T_cuts_cl &cuts)
Returns the (natural) log probability of the specified array of integers given the vector of continuo...
int64_t size_mvt(const ScalarT &)
Provides the size of a multivariate argument.
Definition size_mvt.hpp:25
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
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.
fvar< T > log1m_exp(const fvar< T > &x)
Return the natural logarithm of one minus the exponentiation of the specified argument.
Definition log1m_exp.hpp:23
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
void check_ordered(const char *function, const char *name, const T_y &y)
Throw an exception if the specified vector is not sorted into strictly increasing order.
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.
auto as_value_array_or_scalar(T &&v)
Extract the value from an object.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
typename ref_type_if<!is_constant< T >::value, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:62
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
If the input type T is either an eigen matrix with 1 column or 1 row at compile time or a standard ve...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...