Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_2_log_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LOG_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LOG_LPMF_HPP
3
18#include <cmath>
19
20namespace stan {
21namespace math {
22
23// NegBinomial(n|eta, phi) [phi > 0; n >= 0]
24template <bool propto, typename T_n, typename T_log_location,
25 typename T_precision,
27 T_n, T_log_location, T_precision>* = nullptr>
29 const T_n& n, const T_log_location& eta, const T_precision& phi) {
31 using std::exp;
32 using std::log;
33 using T_n_ref = ref_type_t<T_n>;
34 using T_eta_ref = ref_type_t<T_log_location>;
35 using T_phi_ref = ref_type_t<T_precision>;
36 static constexpr const char* function = "neg_binomial_2_log_lpmf";
37 check_consistent_sizes(function, "Failures variable", n,
38 "Log location parameter", eta, "Precision parameter",
39 phi);
40
41 T_n_ref n_ref = n;
42 T_eta_ref eta_ref = eta;
43 T_phi_ref phi_ref = phi;
44
45 check_nonnegative(function, "Failures variable", n_ref);
46 check_finite(function, "Log location parameter", eta_ref);
47 check_positive_finite(function, "Precision parameter", phi_ref);
48
49 if (size_zero(n, eta, phi)) {
50 return 0.0;
51 }
53 return 0.0;
54 }
55
56 T_partials_return logp(0.0);
57 auto ops_partials = make_partials_propagator(eta_ref, phi_ref);
58
59 scalar_seq_view<T_n> n_vec(n_ref);
60 scalar_seq_view<T_eta_ref> eta_vec(eta_ref);
61 scalar_seq_view<T_phi_ref> phi_vec(phi_ref);
62 size_t size_eta = stan::math::size(eta);
63 size_t size_phi = stan::math::size(phi);
64 size_t size_eta_phi = max_size(eta, phi);
65 size_t size_n_phi = max_size(n, phi);
66 size_t size_all = max_size(n, eta, phi);
67
69 for (size_t i = 0; i < size_eta; ++i) {
70 eta_val[i] = eta_vec.val(i);
71 }
72
75 for (size_t i = 0; i < size_phi; ++i) {
76 phi_val[i] = phi_vec.val(i);
77 log_phi[i] = log(phi_val[i]);
78 }
79
81 T_partials_return, T_log_location>
82 exp_eta(size_eta);
84 for (size_t i = 0; i < size_eta; ++i) {
85 exp_eta[i] = exp(eta_val[i]);
86 }
87 }
88
90 T_partials_return, T_log_location, T_precision>
91 exp_eta_over_exp_eta_phi(size_eta_phi);
93 for (size_t i = 0; i < size_eta_phi; ++i) {
94 exp_eta_over_exp_eta_phi[i] = inv(phi_val[i] / exp_eta[i] + 1);
95 }
96 }
97
99 log1p_exp_eta_m_logphi(size_eta_phi);
100 for (size_t i = 0; i < size_eta_phi; ++i) {
101 log1p_exp_eta_m_logphi[i] = log1p_exp(eta_val[i] - log_phi[i]);
102 }
103
105 size_n_phi);
106 for (size_t i = 0; i < size_n_phi; ++i) {
107 n_plus_phi[i] = n_vec[i] + phi_val[i];
108 }
109
110 for (size_t i = 0; i < size_all; i++) {
112 logp += binomial_coefficient_log(n_plus_phi[i] - 1, n_vec[i]);
113 }
115 logp += n_vec[i] * eta_val[i];
116 }
117 logp += -phi_val[i] * log1p_exp_eta_m_logphi[i]
118 - n_vec[i] * (log_phi[i] + log1p_exp_eta_m_logphi[i]);
119
121 partials<0>(ops_partials)[i]
122 += n_vec[i] - n_plus_phi[i] * exp_eta_over_exp_eta_phi[i];
123 }
125 partials<1>(ops_partials)[i]
126 += exp_eta_over_exp_eta_phi[i] - n_vec[i] / (exp_eta[i] + phi_val[i])
127 - log1p_exp_eta_m_logphi[i]
128 - (digamma(phi_val[i]) - digamma(n_plus_phi[i]));
129 }
130 }
131 return ops_partials.build(logp);
132}
133
134template <typename T_n, typename T_log_location, typename T_precision>
136 const T_n& n, const T_log_location& eta, const T_precision& phi) {
137 return neg_binomial_2_log_lpmf<false>(n, eta, phi);
138}
139
140} // namespace math
141} // namespace stan
142#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_log_location_cl, T_precision_cl > neg_binomial_2_log_lpmf(const T_n_cl &n, const T_log_location_cl &eta, const T_precision_cl &phi)
The log of the log transformed negative binomial density for the specified scalars given the specifie...
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
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:18
fvar< T > log1p_exp(const fvar< T > &x)
Definition log1p_exp.hpp:14
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
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...