1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_LPMF_HPP
30template <
bool propto,
typename T_n,
typename T_shape,
typename T_inv_scale,
32 T_n, T_shape, T_inv_scale>* =
nullptr>
35 const T_inv_scale&
beta) {
41 static constexpr const char* function =
"neg_binomial_lpmf";
43 alpha,
"Inverse scale parameter",
beta);
45 T_alpha_ref alpha_ref = alpha;
46 T_beta_ref beta_ref =
beta;
58 T_partials_return logp(0.0);
70 digamma_alpha(size_alpha);
72 for (
size_t i = 0; i < size_alpha; ++i) {
73 digamma_alpha[i] =
digamma(alpha_vec.val(i));
79 for (
size_t i = 0; i < size_beta; ++i) {
80 const T_partials_return beta_dbl = beta_vec.val(i);
81 log1p_inv_beta[i] =
log1p(
inv(beta_dbl));
82 log1p_beta[i] =
log1p(beta_dbl);
87 lambda_m_alpha_over_1p_beta(size_alpha_beta);
89 for (
size_t i = 0; i < size_alpha_beta; ++i) {
90 const T_partials_return alpha_dbl = alpha_vec.val(i);
91 const T_partials_return beta_dbl = beta_vec.val(i);
92 lambda_m_alpha_over_1p_beta[i]
93 = alpha_dbl / beta_dbl - alpha_dbl / (1 + beta_dbl);
97 for (
size_t i = 0; i < max_size_seq_view; i++) {
98 const T_partials_return alpha_dbl = alpha_vec.val(i);
99 const T_partials_return beta_dbl = beta_vec.val(i);
107 logp -= alpha_dbl * log1p_inv_beta[i] + n_vec[i] * log1p_beta[i];
110 partials<0>(ops_partials)[i] +=
digamma(alpha_dbl + n_vec[i])
111 - digamma_alpha[i] - log1p_inv_beta[i];
114 partials<1>(ops_partials)[i]
115 += lambda_m_alpha_over_1p_beta[i] - n_vec[i] / (beta_dbl + 1.0);
119 return ops_partials.build(logp);
122template <
typename T_n,
typename T_shape,
typename T_inv_scale>
124 const T_n& n,
const T_shape& alpha,
const T_inv_scale&
beta) {
125 return neg_binomial_lpmf<false>(n, alpha,
beta);
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_shape_cl, T_inv_scale_cl > neg_binomial_lpmf(const T_n_cl &n, const T_shape_cl &alpha, const T_inv_scale_cl &beta)
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>>.
constexpr double neg_binomial_alpha_cutoff
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_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.
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.
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.
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...