1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP
64template <
bool propto,
typename T_y,
typename T_x,
typename T_alpha,
65 typename T_beta,
typename T_precision,
66 require_matrix_t<T_x>* =
nullptr>
67inline return_type_t<T_x, T_alpha, T_beta, T_precision>
69 const T_beta&
beta,
const T_precision& phi) {
75 constexpr int T_x_rows = T_x::RowsAtCompileTime;
76 using T_partials_return
78 using T_precision_val =
typename std::conditional_t<
80 Eigen::Array<partials_return_t<T_precision>, -1, 1>,
82 using T_sum_val =
typename std::conditional_t<
84 Eigen::Array<partials_return_t<T_y, T_precision>, -1, 1>,
87 typename std::conditional_t<T_x_rows == 1, T_partials_return,
88 Array<T_partials_return, Dynamic, 1>>;
95 const size_t N_attributes = x.cols();
97 static constexpr const char* function =
"neg_binomial_2_log_glm_lpmf";
104 T_alpha_ref alpha_ref = alpha;
105 T_beta_ref beta_ref =
beta;
106 const auto& beta_val =
value_of(beta_ref);
107 const auto& alpha_val =
value_of(alpha_ref);
117 const auto& y_ref =
to_ref(y);
118 T_phi_ref phi_ref = phi;
120 const auto& y_val =
value_of(y_ref);
121 const auto& phi_val =
value_of(phi_ref);
129 T_precision>::value) {
135 const auto& x_val = to_ref_if<is_autodiff_v<T_beta>>(
value_of(x_ref));
140 Array<T_partials_return, Dynamic, 1> theta(N_instances);
141 if constexpr (T_x_rows == 1) {
142 T_theta_tmp theta_tmp = (x_val * beta_val_vec)(0, 0);
145 theta = (x_val * beta_val_vec).array();
148 check_finite(function,
"Matrix of independent variables", theta);
149 T_precision_val log_phi =
log(phi_arr);
150 Array<T_partials_return, Dynamic, 1> logsumexp_theta_logphi
155 T_sum_val y_plus_phi = y_arr + phi_arr;
158 T_partials_return logp(0);
163 logp -=
sum(
lgamma(y_arr + 1.0)) * N_instances;
169 for (
size_t n = 0; n < N_instances; ++n) {
176 logp -=
sum(y_plus_phi * logsumexp_theta_logphi);
179 logp +=
sum(y_arr * theta);
185 logp +=
sum(
lgamma(y_plus_phi)) * N_instances;
192 if constexpr (is_any_autodiff_v<T_x, T_beta, T_alpha, T_precision>) {
193 Array<T_partials_return, Dynamic, 1> theta_exp = theta.exp();
194 if constexpr (is_any_autodiff_v<T_x, T_beta, T_alpha>) {
195 Matrix<T_partials_return, Dynamic, 1> theta_derivative
196 = y_arr - theta_exp * y_plus_phi / (theta_exp + phi_arr);
197 if constexpr (is_autodiff_v<T_beta>) {
198 if constexpr (T_x_rows == 1) {
199 edge<2>(ops_partials).partials_ = theta_derivative.sum() * x_val;
201 edge<2>(ops_partials).partials_
202 = x_val.transpose() * theta_derivative;
205 if constexpr (is_autodiff_v<T_x>) {
206 if constexpr (T_x_rows == 1) {
207 edge<0>(ops_partials).partials_
208 = beta_val_vec * theta_derivative.sum();
210 edge<0>(ops_partials).partials_
211 = (beta_val_vec * theta_derivative.transpose()).
transpose();
214 if constexpr (is_autodiff_v<T_alpha>) {
216 partials<1>(ops_partials) = std::move(theta_derivative);
218 partials<1>(ops_partials)[0] =
sum(theta_derivative);
222 if constexpr (is_autodiff_v<T_precision>) {
224 edge<3>(ops_partials).partials_
225 = 1 - y_plus_phi / (theta_exp + phi_arr) + log_phi
226 - logsumexp_theta_logphi +
digamma(y_plus_phi) -
digamma(phi_arr);
228 partials<3>(ops_partials)[0]
230 +
sum(-y_plus_phi / (theta_exp + phi_arr) + log_phi
231 - logsumexp_theta_logphi +
digamma(y_plus_phi)
236 return ops_partials.build(logp);
239template <
typename T_y,
typename T_x,
typename T_alpha,
typename T_beta,
240 typename T_precision>
243 const T_beta&
beta,
const T_precision& phi) {
244 return neg_binomial_2_log_glm_lpmf<false>(y, x, alpha,
beta, phi);
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
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, T_phi_cl > neg_binomial_2_log_glm_lpmf(const T_y_cl &y, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta, const T_phi_cl &phi)
Returns the log PMF of the Generalized Linear Model (GLM) with Negative-Binomial-2 distribution and l...
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>>.
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
T as_array_or_scalar(T &&v)
Returns specified input value.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
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)
fvar< T > log1p_exp(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.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
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.
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.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
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 ...
If the input type T is either an eigen matrix with 1 column or 1 row at compile time or a standard ve...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...