1#ifndef STAN_MATH_PRIM_PROB_BINOMIAL_LOGIT_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BINOMIAL_LOGIT_GLM_LPMF_HPP
56template <
bool propto,
typename T_n,
typename T_N,
typename T_x,
57 typename T_alpha,
typename T_beta, require_matrix_t<T_x>* =
nullptr>
59 const T_n& n,
const T_N& N,
const T_x& x,
const T_alpha& alpha,
61 constexpr int T_x_rows = T_x::RowsAtCompileTime;
72 T_alpha_ref alpha_ref = alpha;
73 T_beta_ref beta_ref =
beta;
83 const size_t N_instances =
max_size(n, N, x.col(0), alpha);
84 const size_t N_attributes = x.cols();
86 static const char* function =
"binomial_logit_glm_lpmf";
88 "Population size parameter", N);
97 check_bounded(function,
"Successes variable", n_val, 0, N_val);
103 Eigen::Array<T_partials_return, -1, 1> theta(N_instances);
104 if constexpr (T_x_rows == 1) {
105 theta = (x_val * beta_val)(0, 0) + alpha_val;
107 theta =
multiply(x_val, beta_val).array() + alpha_val;
110 constexpr bool gradients_calc = is_any_autodiff_v<T_beta, T_x, T_alpha>;
111 auto&& log_inv_logit_theta = to_ref_if<gradients_calc>(
log_inv_logit(theta));
113 T_partials_return logp =
sum(n_val * log_inv_logit_theta
120 check_finite(function,
"Matrix of independent variables", x);
124 size_t broadcast_n =
max_size(N, n) == N_instances ? 1 : N_instances;
129 if constexpr (gradients_calc) {
130 Eigen::Matrix<T_partials_return, -1, 1> theta_derivative
131 = n_val - N_val *
exp(log_inv_logit_theta);
133 if constexpr (is_autodiff_v<T_beta>) {
134 if constexpr (T_x_rows == 1) {
135 edge<2>(ops_partials).partials_ = theta_derivative.sum() * x_val;
136 }
else if constexpr (T_x::ColsAtCompileTime == 1) {
137 partials<2>(ops_partials)[0] =
dot_product(x_val, theta_derivative);
139 partials<2>(ops_partials)
140 =
multiply(x_val.transpose(), theta_derivative);
144 if constexpr (is_autodiff_v<T_x>) {
145 if constexpr (T_x_rows == 1) {
146 edge<0>(ops_partials).partials_ = beta_val * theta_derivative.sum();
148 edge<0>(ops_partials).partials_
149 = (beta_val * theta_derivative.transpose()).
transpose();
152 if constexpr (is_autodiff_v<T_alpha>) {
153 partials<1>(ops_partials) = theta_derivative;
156 return ops_partials.build(logp);
159template <
typename T_n,
typename T_N,
typename T_x,
typename T_alpha,
162 const T_n& n,
const T_N& N,
const T_x& x,
const T_alpha& alpha,
163 const T_beta&
beta) {
164 return binomial_logit_glm_lpmf<false>(n, N, x, alpha,
beta);
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
auto transpose(Arg &&a)
Transposes a kernel generator expression.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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_inv_logit(const fvar< T > &x)
auto as_value_column_array_or_scalar(T &&a)
Extract the value from an object and for eigen vectors and std::vectors convert to an eigen column ar...
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
return_type_t< T_x_cl, T_alpha_cl, T_beta_cl > binomial_logit_glm_lpmf(const T_n_cl &n, const T_N_cl &N, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta)
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.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
auto as_value_column_vector_or_scalar(T &&a)
Extract values from input argument and transform to a column vector.
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.
fvar< T > log1m_inv_logit(const fvar< T > &x)
Return the natural logarithm of one minus the inverse logit of the specified argument.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
typename ref_type_if< Condition, T >::type ref_type_if_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...