1 #ifndef STAN_MATH_PRIM_MAT_PROB_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP 2 #define STAN_MATH_PRIM_MAT_PROB_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP 54 template <
bool propto,
typename T_y,
typename T_x,
typename T_alpha,
55 typename T_beta,
typename T_precision>
58 const T_beta&
beta,
const T_precision& phi) {
59 static const char*
function =
"neg_binomial_2_log_glm_lpmf";
62 T_precision>::type T_partials_return;
63 typedef typename std::conditional<
65 Eigen::Array<typename partials_return_type<T_precision>::type, -1, 1>,
67 typedef typename std::conditional<
69 Eigen::Array<typename partials_return_type<T_y, T_precision>::type, -1,
79 const size_t N = x.rows();
80 const size_t M = x.cols();
88 if (is_vector<T_precision>::value)
90 "Vector of dependent variables", y);
93 "Vector of dependent variables", y);
101 T_partials_return logp(0);
116 Array<T_partials_return, Dynamic, 1> theta =
value_of(x) * beta_val_vec;
118 check_finite(
function,
"Matrix of independent variables", theta);
119 T_precision_val log_phi =
log(phi_arr);
120 Array<T_partials_return, Dynamic, 1> logsumexp_theta_logphi
122 .select(theta +
log1p(
exp(log_phi - theta)),
123 log_phi +
log1p(
exp(theta - log_phi)));
125 T_sum_val y_plus_phi = y_arr + phi_arr;
132 if (is_vector<T_precision>::value) {
134 for (
size_t n = 0; n < N; ++n)
143 logp -=
sum(y_plus_phi * logsumexp_theta_logphi);
145 logp +=
sum(y_arr * theta);
152 x, alpha, beta, phi);
154 Array<T_partials_return, Dynamic, 1> theta_exp = theta.exp();
156 Matrix<T_partials_return, Dynamic, 1> theta_derivative
157 = y_arr - theta_exp * y_plus_phi / (theta_exp + phi_arr);
159 ops_partials.
edge3_.partials_ = x_val.transpose() * theta_derivative;
162 ops_partials.
edge1_.partials_
163 = (beta_val_vec * theta_derivative.transpose()).
transpose();
167 ops_partials.
edge2_.partials_ = theta_derivative;
169 ops_partials.
edge2_.partials_[0] =
sum(theta_derivative);
173 if (is_vector<T_precision>::value) {
174 ops_partials.
edge4_.partials_
175 = 1 - y_plus_phi / (theta_exp + phi_arr) + log_phi
179 ops_partials.
edge4_.partials_[0]
181 +
sum(-y_plus_phi / (theta_exp + phi_arr) + log_phi
182 - logsumexp_theta_logphi
188 return ops_partials.
build(logp);
191 template <
typename T_y,
typename T_x,
typename T_alpha,
typename T_beta,
192 typename T_precision>
195 const T_beta&
beta,
const T_precision& phi) {
196 return neg_binomial_2_log_glm_lpmf<false>(y, x, alpha,
beta, phi);
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
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.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
T value_of(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)
internal::ops_partials_edge< double, Op4 > edge4_
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.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
return_type< T_x, T_alpha, T_beta, T_precision >::type neg_binomial_2_log_glm_lpmf(const T_y &y, const T_x &x, const T_alpha &alpha, const T_beta &beta, const T_precision &phi)
Returns the log PMF of the Generalized Linear Model (GLM) with Negative-Binomial-2 distribution and l...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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...
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
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)
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
double as_scalar(const std::vector< T > &a)
Converts input to a scalar.
matrix_cl transpose(const matrix_cl &src)
Takes the transpose of the matrix on the OpenCL device.
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
internal::ops_partials_edge< double, Op2 > edge2_
Eigen::ArrayWrapper< const Eigen::Matrix< T, R, C > > as_array_or_scalar(const Eigen::Matrix< T, R, C > &v)
Converts a matrix type to an array.
fvar< T > log1p(const fvar< T > &x)
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.