1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LOG_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LOG_LPMF_HPP
24template <
bool propto,
typename T_n,
typename T_log_location,
27 T_n, T_log_location, T_precision>* =
nullptr>
29 const T_n& n,
const T_log_location& eta,
const T_precision& phi) {
36 static constexpr const char* function =
"neg_binomial_2_log_lpmf";
38 "Log location parameter", eta,
"Precision parameter",
42 T_eta_ref eta_ref = eta;
43 T_phi_ref phi_ref = phi;
46 check_finite(function,
"Log location parameter", eta_ref);
56 T_partials_return logp(0.0);
64 size_t size_eta_phi =
max_size(eta, phi);
65 size_t size_n_phi =
max_size(n, phi);
66 size_t size_all =
max_size(n, eta, phi);
69 for (
size_t i = 0; i < size_eta; ++i) {
70 eta_val[i] = eta_vec.val(i);
75 for (
size_t i = 0; i < size_phi; ++i) {
76 phi_val[i] = phi_vec.val(i);
77 log_phi[i] =
log(phi_val[i]);
81 T_partials_return, T_log_location>
84 for (
size_t i = 0; i < size_eta; ++i) {
85 exp_eta[i] =
exp(eta_val[i]);
90 T_partials_return, T_log_location, T_precision>
91 exp_eta_over_exp_eta_phi(size_eta_phi);
93 for (
size_t i = 0; i < size_eta_phi; ++i) {
94 exp_eta_over_exp_eta_phi[i] =
inv(phi_val[i] / exp_eta[i] + 1);
99 log1p_exp_eta_m_logphi(size_eta_phi);
100 for (
size_t i = 0; i < size_eta_phi; ++i) {
101 log1p_exp_eta_m_logphi[i] =
log1p_exp(eta_val[i] - log_phi[i]);
106 for (
size_t i = 0; i < size_n_phi; ++i) {
107 n_plus_phi[i] = n_vec[i] + phi_val[i];
110 for (
size_t i = 0; i < size_all; i++) {
115 logp += n_vec[i] * eta_val[i];
117 logp += -phi_val[i] * log1p_exp_eta_m_logphi[i]
118 - n_vec[i] * (log_phi[i] + log1p_exp_eta_m_logphi[i]);
121 partials<0>(ops_partials)[i]
122 += n_vec[i] - n_plus_phi[i] * exp_eta_over_exp_eta_phi[i];
125 partials<1>(ops_partials)[i]
126 += exp_eta_over_exp_eta_phi[i] - n_vec[i] / (exp_eta[i] + phi_val[i])
127 - log1p_exp_eta_m_logphi[i]
131 return ops_partials.build(logp);
134template <
typename T_n,
typename T_log_location,
typename T_precision>
136 const T_n& n,
const T_log_location& eta,
const T_precision& phi) {
137 return neg_binomial_2_log_lpmf<false>(n, eta, phi);
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
return_type_t< T_n_cl, T_log_location_cl, T_precision_cl > neg_binomial_2_log_lpmf(const T_n_cl &n, const T_log_location_cl &eta, const T_precision_cl &phi)
The log of the log transformed negative binomial density for the specified scalars given the specifie...
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>>.
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.
fvar< T > log(const fvar< T > &x)
fvar< T > log1p_exp(const fvar< T > &x)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
fvar< T > inv(const fvar< T > &x)
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.
fvar< T > exp(const fvar< T > &x)
typename ref_type_if< true, T >::type ref_type_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 ...
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...