1#ifndef STAN_MATH_PRIM_PROB_POISSON_LOG_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_POISSON_LOG_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>
62 constexpr int T_x_rows = T_x::RowsAtCompileTime;
66 typename std::conditional_t<T_x_rows == 1, T_partials_return,
67 Array<T_partials_return, Dynamic, 1>>;
69 typename std::conditional_t<T_x_rows == 1, T_xbeta_partials,
70 Array<T_xbeta_partials, Dynamic, 1>>;
76 const size_t N_attributes = x.cols();
78 static constexpr const char* function =
"poisson_log_glm_lpmf";
83 const auto& y_ref =
to_ref(y);
94 T_alpha_ref alpha_ref = alpha;
95 T_beta_ref beta_ref =
beta;
98 const auto& x_val = to_ref_if<!is_constant<T_beta>::value>(
value_of(x_ref));
99 const auto& alpha_val =
value_of(alpha_ref);
100 const auto& beta_val =
value_of(beta_ref);
104 const auto& beta_val_vec = to_ref_if<!is_constant<T_x>::value>(
107 Array<T_partials_return, Dynamic, 1> theta(N_instances);
109 T_theta_tmp theta_tmp
110 = forward_as<T_xbeta_tmp>((x_val * beta_val_vec).coeff(0, 0));
113 theta = x_val * beta_val_vec;
117 Matrix<T_partials_return, Dynamic, 1> theta_derivative
119 T_partials_return theta_derivative_sum =
sum(theta_derivative);
120 if (!
isfinite(theta_derivative_sum)) {
123 check_finite(function,
"Matrix of independent variables", theta);
126 T_partials_return logp(0);
132 -
exp(theta.array()));
138 edge<2>(ops_partials).partials_
139 = forward_as<Matrix<T_partials_return, 1, Dynamic>>(
140 theta_derivative.sum() * x_val);
142 partials<2>(ops_partials) = x_val.transpose() * theta_derivative;
147 edge<0>(ops_partials).partials_
148 = forward_as<Array<T_partials_return, Dynamic, T_x_rows>>(
149 beta_val_vec * theta_derivative.sum());
151 edge<0>(ops_partials).partials_
152 = (beta_val_vec * theta_derivative.transpose()).
transpose();
157 partials<1>(ops_partials) = theta_derivative;
159 partials<1>(ops_partials)[0] = theta_derivative_sum;
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 poisson_log_glm_lpmf<false>(y, x, alpha,
beta);
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
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 > poisson_log_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 Poisson distribution and log link func...
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.
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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
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.
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_constant< T >::value, 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...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...