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
13#include <cmath>
14
15namespace stan {
16namespace math {
17
18// NegBinomial(n|mu, phi) [mu >= 0; phi > 0; n >= 0]
19template <bool propto, typename T_n, typename T_location, typename T_precision,
21 T_n, T_location, T_precision>* = nullptr>
23 const T_n& n, const T_location& mu, const T_precision& phi) {
25 using std::log;
26 using T_n_ref = ref_type_t<T_n>;
27 using T_mu_ref = ref_type_t<T_location>;
28 using T_phi_ref = ref_type_t<T_precision>;
29 static constexpr const char* function = "neg_binomial_2_lpmf";
30 check_consistent_sizes(function, "Failures variable", n, "Location parameter",
31 mu, "Precision parameter", phi);
32
33 T_n_ref n_ref = n;
34 T_mu_ref mu_ref = mu;
35 T_phi_ref phi_ref = phi;
36
37 check_nonnegative(function, "Failures variable", n_ref);
38 check_positive_finite(function, "Location parameter", mu_ref);
39 check_positive_finite(function, "Precision parameter", phi_ref);
40
41 if (size_zero(n, mu, phi)) {
42 return 0.0;
43 }
45 return 0.0;
46 }
47
48 T_partials_return logp(0.0);
49 auto ops_partials = make_partials_propagator(mu_ref, phi_ref);
50
53 auto phi_vec = as_array_or_scalar(as_column_vector_or_scalar(phi_ref));
54 decltype(auto) mu_val = value_of(mu_vec);
55 decltype(auto) phi_val = value_of(phi_vec);
56 auto log_phi = log(phi_val);
57 auto mu_plus_phi = mu_val + phi_val;
58 auto log_mu_plus_phi = log(mu_plus_phi);
59 auto n_plus_phi = value_of(n_vec) + phi_val;
60 constexpr bool include_precision
62 constexpr bool include_location = include_summand<propto, T_location>::value;
63 auto logp_calc = [&]() {
64 return -phi_val * (log1p(mu_val / phi_val))
65 - value_of(n_vec) * log_mu_plus_phi;
66 };
67 if constexpr (include_precision || include_location) {
68 if constexpr (include_precision && include_location) {
69 logp += sum(binomial_coefficient_log(n_plus_phi - 1, n_vec)
70 + lmultiply(n_vec, mu_val) + logp_calc());
71 } else if constexpr (include_precision) {
72 logp
73 += sum(binomial_coefficient_log(n_plus_phi - 1, n_vec) + logp_calc());
74 } else if constexpr (include_location) {
75 logp += sum(lmultiply(n_vec, mu_val) + logp_calc());
76 }
77 }
78 if constexpr (is_autodiff_v<T_location>) {
79 partials<0>(ops_partials)
80 = n_vec / mu_val - (n_vec + phi_val) / mu_plus_phi;
81 }
82 if constexpr (is_autodiff_v<T_precision>) {
83 auto log_term = select(mu_val < phi_val, log1p(-mu_val / mu_plus_phi),
84 log_phi - log_mu_plus_phi);
85 partials<1>(ops_partials) = (mu_val - value_of(n_vec)) / mu_plus_phi
86 + log_term - digamma(phi_val)
87 + digamma(n_plus_phi);
88 }
89 return ops_partials.build(logp);
90}
91
92template <typename T_n, typename T_location, typename T_precision>
94 const T_n& n, const T_location& mu, const T_precision& phi) {
95 return neg_binomial_2_lpmf<false>(n, mu, phi);
96}
97
98} // namespace math
99} // namespace stan
100#endif
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.
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
Definition select.hpp:148
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
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.
T as_array_or_scalar(T &&v)
Returns specified input value.
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
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
fvar< T > lmultiply(const fvar< T > &x1, const fvar< T > &x2)
Definition lmultiply.hpp:14
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
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
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...