1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_LPMF_HPP 2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_LPMF_HPP 25 #include <boost/math/special_functions/digamma.hpp> 26 #include <boost/random/negative_binomial_distribution.hpp> 27 #include <boost/random/variate_generator.hpp> 34 template <
bool propto,
36 typename T_shape,
typename T_inv_scale>
40 const T_inv_scale& beta) {
45 static const char*
function(
"neg_binomial_lpmf");
52 T_partials_return logp(0.0);
57 "Failures variable", n,
58 "Shape parameter", alpha,
59 "Inverse scale parameter", beta);
73 operands_and_partials(alpha, beta);
75 size_t len_ab =
max_size(alpha, beta);
78 for (
size_t i = 0; i < len_ab; ++i)
83 for (
size_t i = 0; i <
length(beta); ++i)
87 log_beta_m_log1p_beta(
length(beta));
88 for (
size_t i = 0; i <
length(beta); ++i)
89 log_beta_m_log1p_beta[i] =
log(
value_of(beta_vec[i])) - log1p_beta[i];
92 alpha_times_log_beta_over_1p_beta(len_ab);
93 for (
size_t i = 0; i < len_ab; ++i)
94 alpha_times_log_beta_over_1p_beta[i]
100 T_partials_return, T_shape>
101 digamma_alpha(
length(alpha));
103 for (
size_t i = 0; i <
length(alpha); ++i)
108 T_partials_return, T_inv_scale> log_beta(
length(beta));
110 for (
size_t i = 0; i <
length(beta); ++i)
115 T_partials_return, T_shape, T_inv_scale>
116 lambda_m_alpha_over_1p_beta(len_ab);
118 for (
size_t i = 0; i < len_ab; ++i)
119 lambda_m_alpha_over_1p_beta[i] =
125 for (
size_t i = 0; i <
size; i++) {
126 if (alpha_vec[i] > 1e10) {
128 logp -=
lgamma(n_vec[i] + 1.0);
133 operands_and_partials.
d_x1[i]
134 += n_vec[i] /
value_of(alpha_vec[i])
137 operands_and_partials.
d_x2[i]
138 += (lambda[i] - n_vec[i]) /
value_of(beta_vec[i]);
148 alpha_times_log_beta_over_1p_beta[i]
149 - n_vec[i] * log1p_beta[i];
152 operands_and_partials.
d_x1[i]
155 + log_beta_m_log1p_beta[i];
157 operands_and_partials.
d_x2[i]
158 += lambda_m_alpha_over_1p_beta[i]
159 - n_vec[i] / (
value_of(beta_vec[i]) + 1.0);
162 return operands_and_partials.
value(logp);
165 template <
typename T_n,
166 typename T_shape,
typename T_inv_scale>
170 const T_shape& alpha,
171 const T_inv_scale& beta) {
172 return neg_binomial_lpmf<false>(n, alpha, beta);
VectorView< T_return_type, false, true > d_x2
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
size_t length(const std::vector< T > &x)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
return_type< T_shape, T_inv_scale >::type neg_binomial_lpmf(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
This class builds partial derivatives with respect to a set of operands.
size_t max_size(const T1 &x1, const T2 &x2)
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
VectorBuilder allocates type T1 values to be used as intermediate values.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
fvar< T > log1p(const fvar< T > &x)
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
VectorView< T_return_type, false, true > d_x1
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.