1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LPMF_HPP
23template <
bool propto,
typename T_n,
typename T_location,
typename T_precision,
25 T_n, T_location, T_precision>* =
nullptr>
27 const T_n& n,
const T_location& mu,
const T_precision& phi) {
33 static constexpr const char* function =
"neg_binomial_2_lpmf";
35 mu,
"Precision parameter", phi);
39 T_phi_ref phi_ref = phi;
52 T_partials_return logp(0.0);
58 decltype(
auto) mu_val =
value_of(mu_vec);
59 decltype(
auto) phi_val =
value_of(phi_vec);
60 auto log_phi =
log(phi_val);
61 auto mu_plus_phi = mu_val + phi_val;
62 auto log_mu_plus_phi =
log(mu_plus_phi);
63 auto n_plus_phi =
value_of(n_vec) + phi_val;
64 constexpr bool include_precision
67 auto logp_calc = [&]() {
68 return -phi_val * (
log1p(mu_val / phi_val))
71 if constexpr (include_precision || include_location) {
72 if constexpr (include_precision && include_location) {
75 }
else if constexpr (include_precision) {
78 }
else if constexpr (include_location) {
82 if constexpr (is_autodiff_v<T_location>) {
83 partials<0>(ops_partials)
84 = n_vec / mu_val - (n_vec + phi_val) / mu_plus_phi;
86 if constexpr (is_autodiff_v<T_precision>) {
87 auto log_term =
select(mu_val < phi_val,
log1p(-mu_val / mu_plus_phi),
88 log_phi - log_mu_plus_phi);
89 partials<1>(ops_partials) = (mu_val -
value_of(n_vec)) / mu_plus_phi
93 return ops_partials.build(logp);
96template <
typename T_n,
typename T_location,
typename T_precision>
98 const T_n& n,
const T_location& mu,
const T_precision& phi) {
99 return neg_binomial_2_lpmf<false>(n, mu, phi);
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.
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.
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
return_type_t< T_n_cl, T_location_cl, T_precision_cl > neg_binomial_2_lpmf(const T_n_cl &n, const T_location_cl &mu, const T_precision_cl &phi)
The log of the negative binomial density for the specified scalars given the specified mean(s) and de...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log1p(const fvar< T > &x)
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
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< 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 ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...