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
17#include <cmath>
18
19namespace stan {
20namespace math {
21
22// NegBinomial(n|mu, phi) [mu >= 0; phi > 0; n >= 0]
23template <bool propto, typename T_n, typename T_location, typename T_precision,
25 T_n, T_location, T_precision>* = nullptr>
27 const T_n& n, const T_location& mu, const T_precision& phi) {
29 using std::log;
30 using T_n_ref = ref_type_t<T_n>;
31 using T_mu_ref = ref_type_t<T_location>;
32 using T_phi_ref = ref_type_t<T_precision>;
33 static constexpr const char* function = "neg_binomial_2_lpmf";
34 check_consistent_sizes(function, "Failures variable", n, "Location parameter",
35 mu, "Precision parameter", phi);
36
37 T_n_ref n_ref = n;
38 T_mu_ref mu_ref = mu;
39 T_phi_ref phi_ref = phi;
40
41 check_nonnegative(function, "Failures variable", n_ref);
42 check_positive_finite(function, "Location parameter", mu_ref);
43 check_positive_finite(function, "Precision parameter", phi_ref);
44
45 if (size_zero(n, mu, phi)) {
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(mu_ref, phi_ref);
54
57 auto phi_vec = as_array_or_scalar(as_column_vector_or_scalar(phi_ref));
58 decltype(auto) mu_val = value_of(mu_vec);
59 decltype(auto) phi_val = value_of(phi_vec);
60 auto log_phi = log(phi_val);
61 auto mu_plus_phi = mu_val + phi_val;
62 auto log_mu_plus_phi = log(mu_plus_phi);
63 auto n_plus_phi = value_of(n_vec) + phi_val;
64 constexpr bool include_precision
66 constexpr bool include_location = include_summand<propto, T_location>::value;
67 auto logp_calc = [&]() {
68 return -phi_val * (log1p(mu_val / phi_val))
69 - value_of(n_vec) * log_mu_plus_phi;
70 };
71 if constexpr (include_precision || include_location) {
72 if constexpr (include_precision && include_location) {
73 logp += sum(binomial_coefficient_log(n_plus_phi - 1, n_vec)
74 + multiply_log(n_vec, mu_val) + logp_calc());
75 } else if constexpr (include_precision) {
76 logp
77 += sum(binomial_coefficient_log(n_plus_phi - 1, n_vec) + logp_calc());
78 } else if constexpr (include_location) {
79 logp += sum(multiply_log(n_vec, mu_val) + logp_calc());
80 }
81 }
82 if constexpr (is_autodiff_v<T_location>) {
83 partials<0>(ops_partials)
84 = n_vec / mu_val - (n_vec + phi_val) / mu_plus_phi;
85 }
86 if constexpr (is_autodiff_v<T_precision>) {
87 auto log_term = select(mu_val < phi_val, log1p(-mu_val / mu_plus_phi),
88 log_phi - log_mu_plus_phi);
89 partials<1>(ops_partials) = (mu_val - value_of(n_vec)) / mu_plus_phi
90 + log_term - digamma(phi_val)
91 + digamma(n_plus_phi);
92 }
93 return ops_partials.build(logp);
94}
95
96template <typename T_n, typename T_location, typename T_precision>
98 const T_n& n, const T_location& mu, const T_precision& phi) {
99 return neg_binomial_2_lpmf<false>(n, mu, phi);
100}
101
102} // namespace math
103} // namespace stan
104#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.
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
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
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...