Automatic Differentiation
 
Loading...
Searching...
No Matches
ordered_probit_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_ORDERED_PROBIT_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_ORDERED_PROBIT_LPMF_HPP
3
15#include <vector>
16#include <cmath>
17
18namespace stan {
19namespace math {
20
47template <bool propto, typename T_y, typename T_loc, typename T_cut>
49 const T_loc& lambda,
50 const T_cut& c) {
51 using std::exp;
52 using std::log;
53 using T_lambda_ref = ref_type_t<T_loc>;
54 static constexpr const char* function = "ordered_probit";
55
56 check_nonzero_size(function, "Cut-points", c);
57 int N = math::size(lambda);
58 int C_l = size_mvt(c);
60 int K = c_vec[0].size() + 1;
61
62 check_consistent_sizes(function, "Integers", y, "Locations", lambda);
63 if (C_l > 1) {
64 check_size_match(function, "Length of location variables ", N,
65 "Number of cutpoint vectors ", C_l);
66 }
67
68 check_bounded(function, "Random variable", y, 1, K);
69 scalar_seq_view<T_y> y_vec(y);
70 check_nonzero_size(function, "First cutpoint set", c_vec[0]);
71 for (int i = 0; i < size_mvt(c); ++i) {
72 check_size_match(function, "One cutpoint set", K - 1, "First cutpoint set",
73 c_vec[i].size());
74 check_ordered(function, "Cut-points", c_vec[i]);
75 if (K > 2) {
76 check_finite(function, "Final cut point", c_vec[i].coeff(K - 2));
77 }
78 check_finite(function, "First cut point", c_vec[i].coeff(0));
79 }
80
81 T_lambda_ref lambda_ref = lambda;
82 check_finite(function, "Location parameter", lambda_ref);
83 scalar_seq_view<T_lambda_ref> lambda_vec(lambda_ref);
84
86
87 for (int i = 0; i < N; ++i) {
88 int K = c_vec[i].size() + 1;
89
90 if (y_vec[i] == 1) {
91 logp_n += std_normal_lcdf(c_vec[i].coeff(0) - lambda_vec[i]);
92 } else if (y_vec[i] == K) {
93 logp_n += std_normal_lcdf(lambda_vec[i] - c_vec[i].coeff(K - 2));
94 } else {
95 logp_n += log_diff_exp(
96 std_normal_lcdf(c_vec[i].coeff(y_vec[i] - 1) - lambda_vec[i]),
97 std_normal_lcdf(c_vec[i].coeff(y_vec[i] - 2) - lambda_vec[i]));
98 }
99 }
100 return logp_n;
101}
102
103template <typename T_y, typename T_loc, typename T_cut>
105 const T_loc& lambda,
106 const T_cut& c) {
107 return ordered_probit_lpmf<false>(y, lambda, c);
108}
109} // namespace math
110} // namespace stan
111#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...
return_type_t< T_loc, T_cut > ordered_probit_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...
return_type_t< T_y_cl > std_normal_lcdf(const T_y_cl &y)
Returns the log standard normal complementary cumulative distribution function.
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.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
fvar< T > log_diff_exp(const fvar< T > &x1, const fvar< T > &x2)
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
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.
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...