1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_GLM_LPMF_HPP
51template <
bool propto,
typename T_y,
typename T_x,
typename T_alpha,
52 typename T_beta, require_matrix_t<T_x>* =
nullptr>
54 const T_y& y,
const T_x& x,
const T_alpha& alpha,
const T_beta&
beta) {
61 constexpr int T_x_rows = T_x::RowsAtCompileTime;
64 typename std::conditional_t<T_x_rows == 1, T_partials_return,
65 Array<T_partials_return, Dynamic, 1>>;
71 const size_t N_attributes = x.cols();
73 static constexpr const char* function =
"bernoulli_logit_glm_lpmf";
82 const auto& y_ref =
to_ref(y);
83 check_bounded(function,
"Vector of dependent variables", y_ref, 0, 1);
90 T_alpha_ref alpha_ref = alpha;
91 T_beta_ref beta_ref =
beta;
94 const auto& x_val = to_ref_if<is_autodiff_v<T_beta>>(
value_of(x_ref));
95 const auto& alpha_val =
value_of(alpha_ref);
96 const auto& beta_val =
value_of(beta_ref);
100 const auto& beta_val_vec
103 auto signs = to_ref_if<is_any_autodiff_v<T_beta, T_x, T_alpha>>(
106 Array<T_partials_return, Dynamic, 1> ytheta(N_instances);
107 if constexpr (T_x_rows == 1) {
108 T_ytheta_tmp ytheta_tmp = (x_val * beta_val_vec)(0, 0);
111 ytheta =
multiply(x_val, beta_val_vec).array();
118 static constexpr double cutoff = 20.0;
119 Eigen::Array<T_partials_return, Dynamic, 1> exp_m_ytheta =
exp(-ytheta);
120 T_partials_return logp =
sum(
123 (ytheta < -cutoff).
select(ytheta, -
log1p(exp_m_ytheta))));
128 check_finite(function,
"Matrix of independent variables", ytheta);
133 if constexpr (is_any_autodiff_v<T_beta, T_x, T_alpha>) {
134 Matrix<T_partials_return, Dynamic, 1> theta_derivative
136 .
select(signs * exp_m_ytheta,
138 .select(signs * T_partials_return(1.0),
139 signs * exp_m_ytheta / (exp_m_ytheta + 1)));
140 if constexpr (is_autodiff_v<T_beta>) {
141 if constexpr (T_x_rows == 1) {
142 edge<2>(ops_partials).partials_ = theta_derivative.sum() * x_val;
143 }
else if constexpr (T_x::ColsAtCompileTime == 1) {
144 partials<2>(ops_partials)[0] =
dot_product(x_val, theta_derivative);
146 partials<2>(ops_partials)
147 =
multiply(x_val.transpose(), theta_derivative);
150 if constexpr (is_autodiff_v<T_x>) {
151 if constexpr (T_x_rows == 1) {
152 edge<0>(ops_partials).partials_ = beta_val_vec * theta_derivative.sum();
154 edge<0>(ops_partials).partials_
155 = (beta_val_vec * theta_derivative.transpose()).
transpose();
158 if constexpr (is_autodiff_v<T_alpha>) {
159 partials<1>(ops_partials) = theta_derivative;
162 return ops_partials.build(logp);
165template <
typename T_y,
typename T_x,
typename T_alpha,
typename T_beta>
167 const T_y& y,
const T_x& x,
const T_alpha& alpha,
const T_beta&
beta) {
168 return bernoulli_logit_glm_lpmf<false>(y, x, alpha,
beta);
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
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.
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
return_type_t< T_x_cl, T_alpha_cl, T_beta_cl > bernoulli_logit_glm_lpmf(const T_y_cl &y, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta)
Returns the log PMF of the Generalized Linear Model (GLM) with Bernoulli distribution and logit link ...
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>>.
T as_array_or_scalar(T &&v)
Returns specified input value.
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 > log1p(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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
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 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...