1#ifndef STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_LPMF_HPP
38template <
bool propto,
typename T_n,
typename T_r,
typename T_alpha,
41 T_n, T_r, T_alpha, T_beta>* =
nullptr>
43 const T_n& n,
const T_r& r,
const T_alpha& alpha,
const T_beta&
beta) {
49 static constexpr const char* function =
"beta_neg_binomial_lpmf";
51 function,
"Failures variable", n,
"Number of successes parameter", r,
52 "Prior success parameter", alpha,
"Prior failure parameter",
beta);
59 T_alpha_ref alpha_ref = alpha;
60 T_beta_ref beta_ref =
beta;
76 const size_t max_size_seq_view =
max_size(n, r, alpha,
beta);
77 T_partials_return logp(0.0);
78 for (
size_t i = 0; i < max_size_seq_view; i++) {
80 logp -=
lgamma(n_vec[i] + 1);
82 T_partials_return lbeta_denominator =
lbeta(r_vec.val(i), alpha_vec.val(i));
83 T_partials_return lgamma_numerator =
lgamma(n_vec[i] + beta_vec.val(i));
84 T_partials_return lgamma_denominator =
lgamma(beta_vec.val(i));
85 T_partials_return lbeta_numerator
86 =
lbeta(n_vec[i] + r_vec.val(i), alpha_vec.val(i) + beta_vec.val(i));
87 logp += lbeta_numerator + lgamma_numerator - lbeta_denominator
90 T_partials_return digamma_n_r_alpha_beta =
digamma(
91 n_vec[i] + r_vec.val(i) + alpha_vec.val(i) + beta_vec.val(i));
94 T_partials_return digamma_r_alpha
95 =
digamma(r_vec.val(i) + alpha_vec.val(i));
97 partials<0>(ops_partials)[i]
98 +=
digamma(n_vec[i] + r_vec.val(i)) - digamma_n_r_alpha_beta
99 - (
digamma(r_vec.val(i)) - digamma_r_alpha);
102 partials<1>(ops_partials)[i]
103 += -digamma_n_r_alpha_beta
104 - (
digamma(alpha_vec.val(i)) - digamma_r_alpha);
109 T_partials_return digamma_alpha_beta
110 =
digamma(alpha_vec.val(i) + beta_vec.val(i));
112 partials<2>(ops_partials)[i] += digamma_alpha_beta
113 - digamma_n_r_alpha_beta
114 +
digamma(n_vec[i] + beta_vec.val(i))
118 partials<1>(ops_partials)[i] += digamma_alpha_beta;
123 return ops_partials.build(logp);
126template <
typename T_n,
typename T_r,
typename T_alpha,
typename T_beta>
128 const T_n& n,
const T_r& r,
const T_alpha& alpha,
const T_beta&
beta) {
129 return beta_neg_binomial_lpmf<false>(n, r, alpha,
beta);
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.
return_type_t< T_r, T_alpha, T_beta > beta_neg_binomial_lpmf(const T_n &n, const T_r &r, const T_alpha &alpha, const T_beta &beta)
Returns the log PMF of the Beta Negative Binomial distribution with given number of successes,...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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 > lbeta(const fvar< T > &x1, const fvar< T > &x2)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
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.
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 ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
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...