1#ifndef STAN_MATH_PRIM_PROB_CATEGORICAL_LOGIT_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_CATEGORICAL_LOGIT_GLM_LPMF_HPP
45template <
bool propto,
typename T_y,
typename T_x,
typename T_alpha,
46 typename T_beta, require_matrix_t<T_x>* =
nullptr,
47 require_col_vector_t<T_alpha>* =
nullptr,
48 require_matrix_t<T_beta>* =
nullptr>
50 const T_y& y,
const T_x& x,
const T_alpha& alpha,
const T_beta&
beta) {
63 constexpr int T_x_rows = T_x::RowsAtCompileTime;
66 const size_t N_attributes = x.cols();
67 const size_t N_classes =
beta.cols();
69 static constexpr const char* function =
"categorical_logit_glm_lpmf";
79 check_bounded(function,
"categorical outcome out of support", y_ref, 1,
87 T_alpha_ref alpha_ref = alpha;
88 T_beta_ref beta_ref =
beta;
90 const auto& x_val = to_ref_if<is_autodiff_v<T_beta>>(
value_of(x_ref));
91 const auto& alpha_val =
value_of(alpha_ref);
92 const auto& beta_val = to_ref_if<is_autodiff_v<T_x>>(
value_of(beta_ref));
96 Array<T_partials_return, T_x_rows, Dynamic> lin;
97 if constexpr (T_x_rows == 1 && T_beta::ColsAtCompileTime == 1) {
98 lin =
dot_product(x_val, beta_val) + alpha_val_vec.array();
100 lin =
multiply(x_val, beta_val).rowwise() + alpha_val_vec;
102 Array<T_partials_return, T_x_rows, 1> lin_max
103 = lin.rowwise().maxCoeff();
106 Array<T_partials_return, T_x_rows, Dynamic> exp_lin
107 =
exp(lin.colwise() - lin_max);
108 Array<T_partials_return, T_x_rows, 1> inv_sum_exp_lin
109 = 1 / exp_lin.rowwise().sum();
111 T_partials_return logp =
log(inv_sum_exp_lin).sum() - lin_max.sum();
112 if constexpr (T_x_rows == 1) {
116 for (
int i = 0; i < N_instances; i++) {
117 if constexpr (T_x_rows == 1) {
118 logp += lin(0, y_seq[i] - 1);
120 logp += lin(i, y_seq[i] - 1);
130 check_finite(function,
"Matrix of independent variables", x_ref);
136 if constexpr (is_autodiff_v<T_x>) {
137 if constexpr (T_x_rows == 1) {
138 Array<T_beta_partials, 1, Dynamic> beta_y = beta_val.col(y_seq[0] - 1);
139 for (
int i = 1; i < N_instances; i++) {
140 beta_y += beta_val.col(y_seq[i] - 1).array();
142 if constexpr (T_beta::RowsAtCompileTime == 1) {
143 edge<0>(ops_partials).partials_
145 -
dot_product(exp_lin.matrix(), beta_val) * inv_sum_exp_lin
148 edge<0>(ops_partials).partials_
150 -
multiply(exp_lin.matrix(), beta_val.transpose())
153 * inv_sum_exp_lin * N_instances;
156 Array<T_beta_partials, Dynamic, Dynamic> beta_y(N_instances,
158 for (
int i = 0; i < N_instances; i++) {
159 beta_y.row(i) = beta_val.col(y_seq[i] - 1);
161 edge<0>(ops_partials).partials_
163 -
multiply(exp_lin.matrix(), beta_val.transpose()).array().colwise()
171 if constexpr (is_any_autodiff_v<T_alpha, T_beta>) {
172 Array<T_partials_return, T_x_rows, Dynamic> neg_softmax_lin
173 = exp_lin.colwise() * -inv_sum_exp_lin;
174 if constexpr (is_autodiff_v<T_alpha>) {
175 if constexpr (T_x_rows == 1) {
176 edge<1>(ops_partials).partials_
177 = neg_softmax_lin.colwise().sum() * N_instances;
179 partials<1>(ops_partials) = neg_softmax_lin.colwise().sum();
181 for (
int i = 0; i < N_instances; i++) {
182 partials<1>(ops_partials)[y_seq[i] - 1] += 1;
185 if constexpr (is_autodiff_v<T_beta>) {
186 Matrix<T_partials_return, Dynamic, Dynamic> beta_derivative
187 =
multiply(x_val.transpose(), neg_softmax_lin.matrix());
188 if constexpr (T_x_rows == 1) {
189 beta_derivative *= N_instances;
192 for (
int i = 0; i < N_instances; i++) {
193 if constexpr (T_x_rows == 1) {
194 beta_derivative.col(y_seq[i] - 1) += x_val;
196 beta_derivative.col(y_seq[i] - 1) += x_val.row(i);
204 partials<2>(ops_partials) = std::move(beta_derivative);
207 return ops_partials.build(logp);
210template <
typename T_y,
typename T_x,
typename T_alpha,
typename T_beta>
212 const T_y& y,
const T_x& x,
const T_alpha& alpha,
const T_beta&
beta) {
213 return categorical_logit_glm_lpmf<false>(y, x, alpha,
beta);
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
return_type_t< T_x, T_alpha, T_beta > categorical_logit_glm_lpmf(const T_y &y, const T_x &x, const T_alpha &alpha, const T_beta &beta)
Returns the log PMF of the Generalized Linear Model (GLM) with categorical distribution and logit (so...
typename partials_type< T >::type partials_type_t
Helper alias for accessing the partial type.
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>>.
auto multiply(Mat1 &&m1, Mat2 &&m2)
Return the product of the specified matrices.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
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_size(const char *function, const char *name, const T &x, size_t expected_size)
Check if x is consistent with size expected_size.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
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 dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
typename ref_type_if< is_autodiff_v< T >, T >::type ref_type_if_not_constant_t
typename ref_type_if< true, T >::type ref_type_t
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 ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...