1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__NEG_BINOMIAL_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__NEG_BINOMIAL_HPP
4 #include <boost/random/negative_binomial_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
7 #include <boost/math/special_functions/digamma.hpp>
26 template <
bool propto,
28 typename T_shape,
typename T_inv_scale>
29 typename return_type<T_shape, T_inv_scale>::type
32 const T_inv_scale& beta) {
34 static const char*
function =
"stan::prob::neg_binomial_log(%1%)";
55 "Shape parameter",
"Inverse scale parameter",
74 operands_and_partials(alpha,beta);
76 size_t len_ab =
max_size(alpha,beta);
80 for (
size_t i = 0; i < len_ab; ++i)
85 for (
size_t i = 0; i <
length(beta); ++i)
89 log_beta_m_log1p_beta(
length(beta));
90 for (
size_t i = 0; i <
length(beta); ++i)
91 log_beta_m_log1p_beta[i] =
log(
value_of(beta_vec[i])) - log1p_beta[i];
95 alpha_times_log_beta_over_1p_beta(len_ab);
96 for (
size_t i = 0; i < len_ab; ++i)
97 alpha_times_log_beta_over_1p_beta[i]
104 digamma_alpha(
length(alpha));
106 for (
size_t i = 0; i <
length(alpha); ++i)
113 for (
size_t i = 0; i <
length(beta); ++i)
119 lambda_m_alpha_over_1p_beta(len_ab);
121 for (
size_t i = 0; i < len_ab; ++i)
122 lambda_m_alpha_over_1p_beta[i] =
127 for (
size_t i = 0; i <
size; i++) {
128 if (alpha_vec[i] > 1e10) {
130 logp -=
lgamma(n_vec[i] + 1.0);
135 operands_and_partials.
d_x1[i]
136 += n_vec[i] /
value_of(alpha_vec[i])
139 operands_and_partials.
d_x2[i]
140 += (lambda[i] - n_vec[i]) /
value_of(beta_vec[i]) ;
144 logp += binomial_coefficient_log<double>(n_vec[i]
150 alpha_times_log_beta_over_1p_beta[i]
151 - n_vec[i] * log1p_beta[i];
154 operands_and_partials.
d_x1[i]
157 + log_beta_m_log1p_beta[i];
159 operands_and_partials.
d_x2[i]
160 += lambda_m_alpha_over_1p_beta[i]
161 - n_vec[i] / (
value_of(beta_vec[i]) + 1.0);
164 return operands_and_partials.
to_var(logp);
167 template <
typename T_n,
168 typename T_shape,
typename T_inv_scale>
172 const T_shape& alpha,
173 const T_inv_scale& beta) {
174 return neg_binomial_log<false>(n,alpha,beta);
178 template <
typename T_n,
typename T_shape,
179 typename T_inv_scale>
182 const T_inv_scale& beta) {
183 static const char*
function =
"stan::prob::neg_binomial_cdf(%1%)";
203 "Inverse scale parameter",
215 using boost::math::ibeta_derivative;
220 operands_and_partials(alpha, beta);
226 return operands_and_partials.
to_var(0.0);
232 digammaN_vec(stan::length(alpha));
235 digammaAlpha_vec(stan::length(alpha));
238 digammaSum_vec(stan::length(alpha));
241 betaFunc_vec(stan::length(alpha));
246 const double n_dbl =
value_of(n_vec[i]);
247 const double alpha_dbl =
value_of(alpha_vec[i]);
249 digammaN_vec[i] =
digamma(n_dbl + 1);
250 digammaAlpha_vec[i] =
digamma(alpha_dbl);
251 digammaSum_vec[i] =
digamma(n_dbl + alpha_dbl + 1);
252 betaFunc_vec[i] = boost::math::beta(n_dbl + 1, alpha_dbl);
256 for (
size_t i = 0; i <
size; i++) {
261 == std::numeric_limits<double>::infinity())
264 const double n_dbl =
value_of(n_vec[i]);
265 const double alpha_dbl =
value_of(alpha_vec[i]);
266 const double beta_dbl =
value_of(beta_vec[i]);
268 const double p_dbl = beta_dbl / (1.0 + beta_dbl);
269 const double d_dbl = 1.0 / ( (1.0 + beta_dbl)
270 * (1.0 + beta_dbl) );
272 const double Pi =
ibeta(alpha_dbl, n_dbl + 1.0, p_dbl);
288 operands_and_partials.
d_x1[i]
293 operands_and_partials.
d_x2[i]
294 += d_dbl * ibeta_derivative(alpha_dbl, n_dbl + 1, p_dbl)
301 operands_and_partials.
d_x1[i] *= P;
305 operands_and_partials.
d_x2[i] *= P;
307 return operands_and_partials.
to_var(P);
311 template <
typename T_n,
typename T_shape,
312 typename T_inv_scale>
315 const T_inv_scale& beta) {
316 static const char*
function =
"stan::prob::neg_binomial_cdf_log(%1%)";
336 "Inverse scale parameter",
348 using boost::math::ibeta_derivative;
353 operands_and_partials(alpha, beta);
365 digammaN_vec(stan::length(alpha));
368 digammaAlpha_vec(stan::length(alpha));
371 digammaSum_vec(stan::length(alpha));
374 betaFunc_vec(stan::length(alpha));
378 const double n_dbl =
value_of(n_vec[i]);
379 const double alpha_dbl =
value_of(alpha_vec[i]);
381 digammaN_vec[i] =
digamma(n_dbl + 1);
382 digammaAlpha_vec[i] =
digamma(alpha_dbl);
383 digammaSum_vec[i] =
digamma(n_dbl + alpha_dbl + 1);
384 betaFunc_vec[i] = boost::math::beta(n_dbl + 1, alpha_dbl);
388 for (
size_t i = 0; i <
size; i++) {
392 == std::numeric_limits<double>::infinity())
395 const double n_dbl =
value_of(n_vec[i]);
396 const double alpha_dbl =
value_of(alpha_vec[i]);
397 const double beta_dbl =
value_of(beta_vec[i]);
398 const double p_dbl = beta_dbl / (1.0 + beta_dbl);
399 const double d_dbl = 1.0 / ( (1.0 + beta_dbl)
400 * (1.0 + beta_dbl) );
401 const double Pi =
ibeta(alpha_dbl, n_dbl + 1.0, p_dbl);
415 operands_and_partials.
d_x1[i] += g1 / Pi;
418 operands_and_partials.
d_x2[i]
419 += d_dbl * ibeta_derivative(alpha_dbl, n_dbl + 1, p_dbl) / Pi;
422 return operands_and_partials.
to_var(P);
425 template <
typename T_n,
typename T_shape,
426 typename T_inv_scale>
429 const T_inv_scale& beta) {
430 static const char*
function =
"stan::prob::neg_binomial_ccdf_log(%1%)";
450 "Inverse scale parameter",
462 using boost::math::ibeta_derivative;
467 operands_and_partials(alpha, beta);
473 return operands_and_partials.
to_var(0.0);
479 digammaN_vec(stan::length(alpha));
482 digammaAlpha_vec(stan::length(alpha));
485 digammaSum_vec(stan::length(alpha));
488 betaFunc_vec(stan::length(alpha));
492 const double n_dbl =
value_of(n_vec[i]);
493 const double alpha_dbl =
value_of(alpha_vec[i]);
495 digammaN_vec[i] =
digamma(n_dbl + 1);
496 digammaAlpha_vec[i] =
digamma(alpha_dbl);
497 digammaSum_vec[i] =
digamma(n_dbl + alpha_dbl + 1);
498 betaFunc_vec[i] = boost::math::beta(n_dbl + 1, alpha_dbl);
502 for (
size_t i = 0; i <
size; i++) {
506 == std::numeric_limits<double>::infinity())
509 const double n_dbl =
value_of(n_vec[i]);
510 const double alpha_dbl =
value_of(alpha_vec[i]);
511 const double beta_dbl =
value_of(beta_vec[i]);
512 const double p_dbl = beta_dbl / (1.0 + beta_dbl);
513 const double d_dbl = 1.0 / ( (1.0 + beta_dbl)
514 * (1.0 + beta_dbl) );
515 const double Pi = 1.0 -
ibeta(alpha_dbl, n_dbl + 1.0, p_dbl);
529 operands_and_partials.
d_x1[i] -= g1 / Pi;
532 operands_and_partials.
d_x2[i]
533 -= d_dbl * ibeta_derivative(alpha_dbl, n_dbl + 1, p_dbl) / Pi;
536 return operands_and_partials.
to_var(P);
544 using boost::variate_generator;
545 using boost::random::negative_binomial_distribution;
547 static const char*
function =
"stan::prob::neg_binomial_rng(%1%)";
void gradRegIncBeta(double &g1, double &g2, double a, double b, double z, double digammaA, double digammaB, double digammaSum, double betaAB)
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(const T_a a, const T_b b)
Calculated the value of the first argument times log of the second argument while behaving properly w...
boost::math::tools::promote_args< T_N, T_n >::type binomial_coefficient_log(const T_N N, const T_n n)
Return the log of the binomial coefficient for the specified arguments.
DoubleVectorView allocates double values to be used as intermediate values.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > lgamma(const fvar< T > &x)
A variable implementation that stores operands and derivatives with respect to the variable...
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...
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
int poisson_rng(const double lambda, RNG &rng)
return_type< T_shape, T_inv_scale >::type neg_binomial_ccdf_log(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
var ibeta(const var &a, const var &b, const var &x)
The normalized incomplete beta function of a, b, and x.
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
size_t max_size(const T1 &x1, const T2 &x2)
return_type< T_shape, T_inv_scale >::type neg_binomial_log(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
return_type< T_shape, T_inv_scale >::type neg_binomial_cdf(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
int size(const std::vector< T > &x)
return_type< T_shape, T_inv_scale >::type neg_binomial_cdf_log(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
fvar< T > digamma(const fvar< T > &x)
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
int neg_binomial_rng(const double alpha, const double beta, RNG &rng)
fvar< T > log(const fvar< T > &x)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
double gamma_rng(const double alpha, const double beta, RNG &rng)
fvar< T > log1p(const fvar< T > &x)
double negative_infinity()
Return negative infinity.