1 #ifndef STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_GLM_LPMF_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_CATEGORICAL_LOGIT_GLM_LPMF_HPP 36 template <
bool propto,
typename T_y,
typename T_x_scalar,
int T_x_rows,
37 typename T_alpha_scalar,
typename T_beta_scalar>
40 const T_y& y,
const Eigen::Matrix<T_x_scalar, T_x_rows, Eigen::Dynamic>& x,
41 const Eigen::Matrix<T_alpha_scalar, Eigen::Dynamic, 1>& alpha,
42 const Eigen::Matrix<T_beta_scalar, Eigen::Dynamic, Eigen::Dynamic>&
beta) {
44 T_x_scalar, T_alpha_scalar, T_beta_scalar>::type T_partials_return;
45 static const char*
function =
"categorical_logit_glm_lpmf";
53 const size_t N_instances = T_x_rows == 1 ?
length(y) : x.rows();
54 const size_t N_attributes = x.cols();
55 const size_t N_classes = beta.cols();
64 check_bounded(
function,
"categorical outcome out of support", y, 1,
71 T_beta_scalar>::value)
80 Array<T_partials_return, T_x_rows, Dynamic> lin
81 = (x_val * beta_val).rowwise() + alpha_val_vec;
82 Array<T_partials_return, T_x_rows, 1> lin_max
87 Array<T_partials_return, T_x_rows, Dynamic> exp_lin
88 =
exp(lin.colwise() - lin_max);
89 Array<T_partials_return, T_x_rows, 1> inv_sum_exp_lin
90 = 1 / exp_lin.rowwise().sum();
92 T_partials_return logp =
log(inv_sum_exp_lin).sum() - lin_max.sum();
97 for (
int i = 0; i < N_instances; i++) {
99 logp += lin(0, y_seq[i] - 1);
101 logp += lin(i, y_seq[i] - 1);
111 check_finite(
function,
"Matrix of independent variables", x);
116 Matrix<T_alpha_scalar, Dynamic, 1>,
117 Matrix<T_beta_scalar, Dynamic, Dynamic>>
118 ops_partials(x, alpha, beta);
122 Array<double, 1, Dynamic> beta_y = beta_val.col(y_seq[0] - 1);
123 for (
int i = 1; i < N_instances; i++) {
124 beta_y += beta_val.col(y_seq[i] - 1).array();
128 - (exp_lin.matrix() * beta_val.transpose()).array().colwise()
129 * inv_sum_exp_lin * N_instances;
131 Array<double, Dynamic, Dynamic> beta_y(N_instances, N_attributes);
132 for (
int i = 0; i < N_instances; i++) {
133 beta_y.row(i) = beta_val.col(y_seq[i] - 1);
137 - (exp_lin.matrix() * beta_val.transpose()).array().colwise()
147 Array<T_partials_return, T_x_rows, Dynamic> neg_softmax_lin
148 = exp_lin.colwise() * -inv_sum_exp_lin;
152 = neg_softmax_lin.colwise().sum() * N_instances;
156 for (
int i = 0; i < N_instances; i++) {
161 Matrix<T_partials_return, Dynamic, Dynamic> beta_derivative;
164 = x_val.transpose() * neg_softmax_lin.matrix() * N_instances;
166 beta_derivative = x_val.transpose() * neg_softmax_lin.matrix();
169 for (
int i = 0; i < N_instances; i++) {
171 beta_derivative.col(y_seq[i] - 1) += x_val;
173 beta_derivative.col(y_seq[i] - 1) += x_val.row(i);
183 return ops_partials.
build(logp);
186 template <
typename T_y,
typename T_x_scalar,
int T_x_rows,
187 typename T_alpha_scalar,
typename T_beta_scalar>
190 const T_y& y,
const Eigen::Matrix<T_x_scalar, T_x_rows, Eigen::Dynamic>& x,
191 const Eigen::Matrix<T_alpha_scalar, Eigen::Dynamic, 1>& alpha,
192 const Eigen::Matrix<T_beta_scalar, Eigen::Dynamic, Eigen::Dynamic>&
beta) {
193 return categorical_logit_glm_lpmf<false>(y, x, alpha,
beta);
const Eigen::Matrix< T, Eigen::Dynamic, 1 > & as_column_vector_or_scalar(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &a)
Converts input argument to a column vector or a scalar.
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
bool isfinite(const stan::math::var &v)
Checks if the given number has finite value.
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.
double value_of_rec(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...
fvar< T > log(const fvar< T > &x)
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.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
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.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
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.
void check_consistent_size(const char *function, const char *name, const T &x, size_t expected_size)
Check if the dimension of x is consistent, which is defined to be expected_size if x is a vector or 1...
Template metaprogram to calculate the partial derivative type resulting from promoting all the scalar...
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)
empty_broadcast_array< ViewElt, Op > partials_
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
internal::ops_partials_edge< double, Op2 > edge2_
return_type< T_x_scalar, T_alpha_scalar, T_beta_scalar >::type categorical_logit_glm_lpmf(const T_y &y, const Eigen::Matrix< T_x_scalar, T_x_rows, Eigen::Dynamic > &x, const Eigen::Matrix< T_alpha_scalar, Eigen::Dynamic, 1 > &alpha, const Eigen::Matrix< T_beta_scalar, Eigen::Dynamic, Eigen::Dynamic > &beta)
Returns the log PMF of the Generalized Linear Model (GLM) with categorical distribution and logit (so...
internal::ops_partials_edge< double, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_