|
| 1 | +#ifndef STAN_MATH_OPENCL_PRIM_ORDERED_LOGISTIC_LPMF_HPP |
| 2 | +#define STAN_MATH_OPENCL_PRIM_ORDERED_LOGISTIC_LPMF_HPP |
| 3 | +#ifdef STAN_OPENCL |
| 4 | + |
| 5 | +#include <stan/math/opencl/kernel_generator.hpp> |
| 6 | +#include <stan/math/opencl/kernels/add.hpp> |
| 7 | +#include <stan/math/opencl/kernels/ordered_logistic_lpmf.hpp> |
| 8 | +#include <stan/math/prim/meta.hpp> |
| 9 | +#include <stan/math/prim/err.hpp> |
| 10 | +#include <stan/math/prim/fun/constants.hpp> |
| 11 | +#include <stan/math/prim/fun/elt_divide.hpp> |
| 12 | +#include <stan/math/prim/fun/elt_multiply.hpp> |
| 13 | +#include <stan/math/prim/functor/operands_and_partials.hpp> |
| 14 | +#include <stan/math/prim/err/constraint_tolerance.hpp> |
| 15 | + |
| 16 | +namespace stan { |
| 17 | +namespace math { |
| 18 | + |
| 19 | +/** \ingroup opencl |
| 20 | + * Returns the (natural) log probability of the specified array |
| 21 | + * of integers given the vector of continuous locations and |
| 22 | + * specified cutpoints in an ordered logistic model. |
| 23 | + * |
| 24 | + * <p>Typically the continuous lambda |
| 25 | + * will be the dot product of a vector of regression coefficients |
| 26 | + * and a vector of predictors for the outcome |
| 27 | + * |
| 28 | + \f[ |
| 29 | + \frac{\partial }{\partial \lambda} = |
| 30 | + \begin{cases}\\ |
| 31 | + -\mathrm{logit}^{-1}(\lambda - c_1) & \mbox{if } k = 1,\\ |
| 32 | + -(((1-e^{c_{k-1}-c_{k-2}})^{-1} - \mathrm{logit}^{-1}(c_{k-2}-\lambda)) + |
| 33 | + ((1-e^{c_{k-2}-c_{k-1}})^{-1} - \mathrm{logit}^{-1}(c_{k-1}-\lambda))) |
| 34 | + & \mathrm{if } 1 < k < K, \mathrm{and}\\ |
| 35 | + \mathrm{logit}^{-1}(c_{K-2}-\lambda) & \mathrm{if } k = K. |
| 36 | + \end{cases} |
| 37 | + \f] |
| 38 | +
|
| 39 | + \f[ |
| 40 | + \frac{\partial }{\partial \lambda} = |
| 41 | + \begin{cases} |
| 42 | + -\mathrm{logit}^{-1}(\lambda - c_1) & \text{if } k = 1,\\ |
| 43 | + -(((1-e^{c_{k-1}-c_{k-2}})^{-1} - \mathrm{logit}^{-1}(c_{k-2}-\lambda)) + |
| 44 | + ((1-e^{c_{k-2}-c_{k-1}})^{-1} - \mathrm{logit}^{-1}(c_{k-1}-\lambda))) |
| 45 | + & \text{if } 1 < k < K, \text{ and}\\ |
| 46 | + \mathrm{logit}^{-1}(c_{K-2}-\lambda) & \text{if } k = K. |
| 47 | + \end{cases} |
| 48 | + \f] |
| 49 | + * |
| 50 | + * @tparam propto True if calculating up to a proportion. |
| 51 | + * @tparam T_y Y variable type (integer or array of integers). |
| 52 | + * @tparam T_loc lambda type. |
| 53 | + * @tparam T_cut Cut-point type. |
| 54 | + * @param y Array of integers |
| 55 | + * @param lambda Vector of continuous lambda variables. |
| 56 | + * @param cuts Positive increasing vector of cutpoints. |
| 57 | + * @return Log probability of outcome given lambda and |
| 58 | + * cutpoints. |
| 59 | + * @throw std::domain_error If the outcome is not between 1 and |
| 60 | + * the number of cutpoints plus 2; if the cutpoint vector is |
| 61 | + * empty; if the cutpoint vector contains a non-positive, |
| 62 | + * non-finite value; or if the cutpoint vector is not sorted in |
| 63 | + * ascending order. |
| 64 | + * @throw std::invalid_argument If y and lambda are different |
| 65 | + * lengths. |
| 66 | + */ |
| 67 | +template <bool propto, typename T_y_cl, typename T_loc_cl, typename T_cuts_cl, |
| 68 | + require_all_prim_or_rev_kernel_expression_t<T_y_cl, T_loc_cl, |
| 69 | + T_cuts_cl>* = nullptr> |
| 70 | +inline return_type_t<T_y_cl, T_loc_cl, T_cuts_cl> ordered_logistic_lpmf( |
| 71 | + const T_y_cl& y, const T_loc_cl& lambda, const T_cuts_cl& cuts) { |
| 72 | + constexpr bool is_y_vector = !is_stan_scalar<T_y_cl>::value; |
| 73 | + static const char* function = "ordered_logistic_lpmf(OpenCL)"; |
| 74 | + |
| 75 | + if (size(y) != 1) { |
| 76 | + check_size_match(function, "Size of ", "y", size(y), "Size of", "lambda", |
| 77 | + size(lambda)); |
| 78 | + } |
| 79 | + |
| 80 | + int N_instances = max_size(y, lambda); |
| 81 | + int N_classes = cuts.rows() + 1; |
| 82 | + int N_cut_sets = cuts.cols(); |
| 83 | + |
| 84 | + if (N_cut_sets > 1) { |
| 85 | + check_size_match(function, "Length of lambda variables ", N_instances, |
| 86 | + "Number of cutpoint vectors ", N_cut_sets); |
| 87 | + } |
| 88 | + if (N_instances == 0 || N_classes == 1) { |
| 89 | + return 0.0; |
| 90 | + } |
| 91 | + const auto& cuts_val = eval(value_of(cuts)); |
| 92 | + if (N_classes >= 2) { |
| 93 | + auto cuts_head |
| 94 | + = block_zero_based(cuts_val, 0, 0, cuts.rows() - 1, N_cut_sets); |
| 95 | + auto cuts_tail |
| 96 | + = block_zero_based(cuts_val, 1, 0, cuts.rows() - 1, N_cut_sets); |
| 97 | + check_cl(function, "Cuts", cuts_head, "ordered and finite") |
| 98 | + = cuts_head < cuts_tail && isfinite(cuts_head) && isfinite(cuts_tail); |
| 99 | + } else if (N_classes == 1) { |
| 100 | + check_cl(function, "Cuts", cuts_val, "finite") = isfinite(cuts_val); |
| 101 | + } |
| 102 | + |
| 103 | + if (!include_summand<propto, T_loc_cl, T_cuts_cl>::value) { |
| 104 | + return 0.0; |
| 105 | + } |
| 106 | + |
| 107 | + const auto& y_val = eval(value_of(y)); |
| 108 | + const auto& lambda_val = eval(value_of(lambda)); |
| 109 | + |
| 110 | + const auto& y_val_cl = to_matrix_cl(y_val); |
| 111 | + |
| 112 | + const int local_size |
| 113 | + = opencl_kernels::ordered_logistic.get_option("LOCAL_SIZE_"); |
| 114 | + const int wgs = (N_instances + local_size - 1) / local_size; |
| 115 | + |
| 116 | + bool need_lambda_derivative = !is_constant_all<T_loc_cl>::value; |
| 117 | + bool need_cuts_derivative = !is_constant_all<T_cuts_cl>::value; |
| 118 | + bool need_broadcasting = N_cut_sets == 1 && N_instances != 1; |
| 119 | + matrix_cl<double> logp_cl(wgs, 1); |
| 120 | + matrix_cl<double> lambda_derivative_cl(N_instances, |
| 121 | + need_lambda_derivative ? 1 : 0); |
| 122 | + matrix_cl<double> cuts_derivative_cl( |
| 123 | + N_classes - 1, |
| 124 | + need_cuts_derivative ? (need_broadcasting ? wgs : N_cut_sets) : 0); |
| 125 | + |
| 126 | + try { |
| 127 | + opencl_kernels::ordered_logistic( |
| 128 | + cl::NDRange(local_size * wgs), cl::NDRange(local_size), logp_cl, |
| 129 | + lambda_derivative_cl, cuts_derivative_cl, y_val_cl, lambda_val, |
| 130 | + cuts_val, N_instances, N_classes, is_y_vector, !need_broadcasting, |
| 131 | + need_lambda_derivative, need_cuts_derivative); |
| 132 | + } catch (const cl::Error& e) { |
| 133 | + check_opencl_error(function, e); |
| 134 | + } |
| 135 | + |
| 136 | + double logp = sum(from_matrix_cl(logp_cl)); |
| 137 | + |
| 138 | + if (!std::isfinite(logp)) { |
| 139 | + results(check_cl(function, "Vector of dependent variables", y_val, |
| 140 | + "between 0 and number of classes"), |
| 141 | + check_cl(function, "lambda vector", lambda_val, "finite")) |
| 142 | + = expressions(y_val >= 1 && y_val <= static_cast<int>(N_classes), |
| 143 | + isfinite(lambda_val)); |
| 144 | + } |
| 145 | + operands_and_partials<T_loc_cl, T_cuts_cl> ops_partials(lambda, cuts); |
| 146 | + |
| 147 | + if (!is_constant_all<T_loc_cl>::value) { |
| 148 | + ops_partials.edge1_.partials_ = lambda_derivative_cl; |
| 149 | + } |
| 150 | + if (!is_constant_all<T_cuts_cl>::value) { |
| 151 | + if (need_broadcasting) { |
| 152 | + ops_partials.edge2_.partials_ = rowwise_sum(cuts_derivative_cl); |
| 153 | + } else { |
| 154 | + ops_partials.edge2_.partials_ = std::move(cuts_derivative_cl); |
| 155 | + } |
| 156 | + } |
| 157 | + return ops_partials.build(logp); |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace math |
| 161 | +} // namespace stan |
| 162 | +#endif |
| 163 | +#endif |
0 commit comments