Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_2_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LPMF_HPP
3
16#include <cmath>
17
18namespace stan {
19namespace math {
20
21// NegBinomial(n|mu, phi) [mu >= 0; phi > 0; n >= 0]
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) {
28 using std::log;
29 using T_n_ref = ref_type_t<T_n>;
30 using T_mu_ref = ref_type_t<T_location>;
31 using T_phi_ref = ref_type_t<T_precision>;
32 static constexpr const char* function = "neg_binomial_2_lpmf";
33 check_consistent_sizes(function, "Failures variable", n, "Location parameter",
34 mu, "Precision parameter", phi);
35
36 T_n_ref n_ref = n;
37 T_mu_ref mu_ref = mu;
38 T_phi_ref phi_ref = phi;
39
40 check_nonnegative(function, "Failures variable", n_ref);
41 check_positive_finite(function, "Location parameter", mu_ref);
42 check_positive_finite(function, "Precision parameter", phi_ref);
43
44 if (size_zero(n, mu, phi)) {
45 return 0.0;
46 }
48 return 0.0;
49 }
50
51 T_partials_return logp(0.0);
52 auto ops_partials = make_partials_propagator(mu_ref, phi_ref);
53
54 scalar_seq_view<T_n_ref> n_vec(n_ref);
55 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
56 scalar_seq_view<T_phi_ref> phi_vec(phi_ref);
57 size_t size_mu = stan::math::size(mu);
58 size_t size_phi = stan::math::size(phi);
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);
62
64 for (size_t i = 0; i < size_mu; ++i) {
65 mu_val[i] = mu_vec.val(i);
66 }
67
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]);
73 }
74
76 size_mu_phi);
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]);
82 }
83
85 size_n_phi);
86 for (size_t i = 0; i < size_n_phi; ++i) {
87 n_plus_phi[i] = n_vec[i] + phi_val[i];
88 }
89
90 for (size_t i = 0; i < size_all; i++) {
92 logp += binomial_coefficient_log(n_plus_phi[i] - 1, n_vec[i]);
93 }
95 logp += multiply_log(n_vec[i], mu_val[i]);
96 }
97 logp += -phi_val[i] * (log1p(mu_val[i] / phi_val[i]))
98 - n_vec[i] * log_mu_plus_phi[i];
99
101 partials<0>(ops_partials)[i]
102 += n_vec[i] / mu_val[i] - (n_vec[i] + phi_val[i]) / mu_plus_phi[i];
103 }
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]);
108 } else {
109 log_term = log_phi[i] - log_mu_plus_phi[i];
110 }
111 partials<1>(ops_partials)[i] += (mu_val[i] - n_vec[i]) / mu_plus_phi[i]
112 + log_term - digamma(phi_val[i])
113 + digamma(n_plus_phi[i]);
114 }
115 }
116 return ops_partials.build(logp);
117}
118
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);
123}
124
125} // namespace math
126} // namespace stan
127#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_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>>.
Definition size.hpp:19
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.
Definition size_zero.hpp:19
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
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
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 ...
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...