1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LPMF_HPP
22template <
bool propto,
typename T_n,
typename T_location,
typename T_precision,
24 T_n, T_location, T_precision>* =
nullptr>
26 const T_n& n,
const T_location& mu,
const T_precision& phi) {
32 static constexpr const char* function =
"neg_binomial_2_lpmf";
34 mu,
"Precision parameter", phi);
38 T_phi_ref phi_ref = phi;
51 T_partials_return logp(0.0);
59 size_t size_mu_phi =
max_size(mu, phi);
60 size_t size_n_phi =
max_size(n, phi);
61 size_t size_all =
max_size(n, mu, phi);
64 for (
size_t i = 0; i < size_mu; ++i) {
65 mu_val[i] = mu_vec.val(i);
70 for (
size_t i = 0; i < size_phi; ++i) {
71 phi_val[i] = phi_vec.val(i);
72 log_phi[i] =
log(phi_val[i]);
78 log_mu_plus_phi(size_mu_phi);
79 for (
size_t i = 0; i < size_mu_phi; ++i) {
80 mu_plus_phi[i] = mu_val[i] + phi_val[i];
81 log_mu_plus_phi[i] =
log(mu_plus_phi[i]);
86 for (
size_t i = 0; i < size_n_phi; ++i) {
87 n_plus_phi[i] = n_vec[i] + phi_val[i];
90 for (
size_t i = 0; i < size_all; i++) {
97 logp += -phi_val[i] * (
log1p(mu_val[i] / phi_val[i]))
98 - n_vec[i] * log_mu_plus_phi[i];
101 partials<0>(ops_partials)[i]
102 += n_vec[i] / mu_val[i] - (n_vec[i] + phi_val[i]) / mu_plus_phi[i];
105 T_partials_return log_term;
106 if (mu_val[i] < phi_val[i]) {
107 log_term =
log1p(-mu_val[i] / mu_plus_phi[i]);
109 log_term = log_phi[i] - log_mu_plus_phi[i];
111 partials<1>(ops_partials)[i] += (mu_val[i] - n_vec[i]) / mu_plus_phi[i]
112 + log_term -
digamma(phi_val[i])
116 return ops_partials.build(logp);
119template <
typename T_n,
typename T_location,
typename T_precision>
121 const T_n& n,
const T_location& mu,
const T_precision& phi) {
122 return neg_binomial_2_lpmf<false>(n, mu, 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_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.
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)
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)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log1p(const fvar< T > &x)
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
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 ...
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...