Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_LPMF_HPP
3
13#include <cmath>
14
15namespace stan {
16namespace math {
17
18namespace internal {
19// Exposing to let me use in tests
20// The current tests fail for 1e8 and pass for 1e9, so setting to 1e10
21constexpr double neg_binomial_alpha_cutoff = 1e10;
22} // namespace internal
23
24// NegBinomial(n|alpha, beta) [alpha > 0; beta > 0; n >= 0]
25template <bool propto, typename T_n, typename T_shape, typename T_inv_scale,
27 T_n, T_shape, T_inv_scale>* = nullptr>
29 const T_n& n, const T_shape& alpha, const T_inv_scale& beta) {
30 using T_partials_return = partials_return_t<T_n, T_shape, T_inv_scale>;
31 using std::log;
32 using T_n_ref = ref_type_t<T_n>;
33 using T_alpha_ref = ref_type_t<T_shape>;
34 using T_beta_ref = ref_type_t<T_inv_scale>;
35 static constexpr const char* function = "neg_binomial_lpmf";
36 check_consistent_sizes(function, "Failures variable", n, "Shape parameter",
37 alpha, "Inverse scale parameter", beta);
38 T_n_ref n_ref = n;
39 T_alpha_ref alpha_ref = alpha;
40 T_beta_ref beta_ref = beta;
41 check_nonnegative(function, "Failures variable", n_ref);
42 check_positive_finite(function, "Shape parameter", alpha_ref);
43 check_positive_finite(function, "Inverse scale parameter", beta_ref);
44
45 if (size_zero(n, alpha, beta)) {
46 return 0.0;
47 }
49 return 0.0;
50 }
51
52 T_partials_return logp(0.0);
53 auto ops_partials = make_partials_propagator(alpha_ref, beta_ref);
54
55 scalar_seq_view<T_n_ref> n_vec(n_ref);
56 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
57 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
58 size_t size_alpha = stan::math::size(alpha);
59 size_t size_beta = stan::math::size(beta);
60 size_t size_alpha_beta = max_size(alpha, beta);
61 size_t max_size_seq_view = max_size(n, alpha, beta);
62
63 VectorBuilder<is_autodiff_v<T_shape>, T_partials_return, T_shape>
64 digamma_alpha(size_alpha);
65 if constexpr (is_autodiff_v<T_shape>) {
66 for (size_t i = 0; i < size_alpha; ++i) {
67 digamma_alpha[i] = digamma(alpha_vec.val(i));
68 }
69 }
70
73 for (size_t i = 0; i < size_beta; ++i) {
74 const T_partials_return beta_dbl = beta_vec.val(i);
75 log1p_inv_beta[i] = log1p(inv(beta_dbl));
76 log1p_beta[i] = log1p(beta_dbl);
77 }
78
79 VectorBuilder<is_autodiff_v<T_inv_scale>, T_partials_return, T_shape,
80 T_inv_scale>
81 lambda_m_alpha_over_1p_beta(size_alpha_beta);
82 if constexpr (is_autodiff_v<T_inv_scale>) {
83 for (size_t i = 0; i < size_alpha_beta; ++i) {
84 const T_partials_return alpha_dbl = alpha_vec.val(i);
85 const T_partials_return beta_dbl = beta_vec.val(i);
86 lambda_m_alpha_over_1p_beta[i]
87 = alpha_dbl / beta_dbl - alpha_dbl / (1 + beta_dbl);
88 }
89 }
90
91 for (size_t i = 0; i < max_size_seq_view; i++) {
92 const T_partials_return alpha_dbl = alpha_vec.val(i);
93 const T_partials_return beta_dbl = beta_vec.val(i);
94
96 if (n_vec[i] != 0) {
97 logp += binomial_coefficient_log(n_vec[i] + alpha_dbl - 1.0,
98 alpha_dbl - 1.0);
99 }
100 }
101 logp -= alpha_dbl * log1p_inv_beta[i] + n_vec[i] * log1p_beta[i];
102
103 if constexpr (is_autodiff_v<T_shape>) {
104 partials<0>(ops_partials)[i] += digamma(alpha_dbl + n_vec[i])
105 - digamma_alpha[i] - log1p_inv_beta[i];
106 }
107 if constexpr (is_autodiff_v<T_inv_scale>) {
108 partials<1>(ops_partials)[i]
109 += lambda_m_alpha_over_1p_beta[i] - n_vec[i] / (beta_dbl + 1.0);
110 }
111 }
112
113 return ops_partials.build(logp);
114}
115
116template <typename T_n, typename T_shape, typename T_inv_scale>
118 const T_n& n, const T_shape& alpha, const T_inv_scale& beta) {
119 return neg_binomial_lpmf<false>(n, alpha, beta);
120}
121
122} // namespace math
123} // namespace stan
124#endif
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>>.
Definition size.hpp:19
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.
Definition size_zero.hpp:19
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
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
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:13
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:56
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...