Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_neg_binomial_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_NEG_BINOMIAL_LPMF_HPP
3
16
17namespace stan {
18namespace math {
19
38template <bool propto, typename T_n, typename T_r, typename T_alpha,
39 typename T_beta,
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) {
44 using T_partials_return = partials_return_t<T_n, T_r, T_alpha, T_beta>;
45 using T_n_ref = ref_type_t<T_n>;
46 using T_r_ref = ref_type_t<T_r>;
47 using T_alpha_ref = ref_type_t<T_alpha>;
48 using T_beta_ref = ref_type_t<T_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);
53 if (size_zero(n, r, alpha, beta)) {
54 return 0.0;
55 }
56
57 T_n_ref n_ref = n;
58 T_r_ref r_ref = r;
59 T_alpha_ref alpha_ref = alpha;
60 T_beta_ref beta_ref = beta;
61 check_nonnegative(function, "Failures variable", n_ref);
62 check_positive_finite(function, "Number of successes parameter", r_ref);
63 check_positive_finite(function, "Prior success parameter", alpha_ref);
64 check_positive_finite(function, "Prior failure parameter", beta_ref);
65
67 return 0.0;
68 }
69
70 auto ops_partials = make_partials_propagator(r_ref, alpha_ref, beta_ref);
71
72 scalar_seq_view<T_n> n_vec(n);
73 scalar_seq_view<T_r_ref> r_vec(r_ref);
74 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
75 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
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++) {
79 if constexpr (include_summand<propto>::value) {
80 logp -= lgamma(n_vec[i] + 1);
81 }
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
88 - lgamma_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));
92
94 T_partials_return digamma_r_alpha
95 = digamma(r_vec.val(i) + alpha_vec.val(i));
96 if constexpr (!is_constant_all<T_r>::value) {
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);
100 }
101 if constexpr (!is_constant_all<T_alpha>::value) {
102 partials<1>(ops_partials)[i]
103 += -digamma_n_r_alpha_beta
104 - (digamma(alpha_vec.val(i)) - digamma_r_alpha);
105 }
106 }
107 if constexpr (!is_constant<T_beta>::value
109 T_partials_return digamma_alpha_beta
110 = digamma(alpha_vec.val(i) + beta_vec.val(i));
111 if constexpr (!is_constant_all<T_beta>::value) {
112 partials<2>(ops_partials)[i] += digamma_alpha_beta
113 - digamma_n_r_alpha_beta
114 + digamma(n_vec[i] + beta_vec.val(i))
115 - digamma(beta_vec.val(i));
116 }
117 if constexpr (!is_constant_all<T_alpha>::value) {
118 partials<1>(ops_partials)[i] += digamma_alpha_beta;
119 }
120 }
121 }
122 }
123 return ops_partials.build(logp);
124}
125
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);
130}
131
132} // namespace math
133} // namespace stan
134#endif
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.
Definition size_zero.hpp:19
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition lbeta.hpp:14
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.
Definition lgamma.hpp:21
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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.
Definition beta.hpp:51
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.
Definition digamma.hpp:23
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
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...