1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_LOG_HPP
4 #include <boost/math/special_functions/digamma.hpp>
5 #include <boost/random/negative_binomial_distribution.hpp>
6 #include <boost/random/variate_generator.hpp>
35 template <
bool propto,
37 typename T_shape,
typename T_inv_scale>
38 typename return_type<T_shape, T_inv_scale>::type
41 const T_inv_scale& beta) {
46 static const char*
function(
"stan::math::neg_binomial_log");
60 T_partials_return logp(0.0);
65 "Failures variable", n,
66 "Shape parameter", alpha,
67 "Inverse scale parameter", beta);
87 operands_and_partials(alpha, beta);
89 size_t len_ab =
max_size(alpha, beta);
92 for (
size_t i = 0; i < len_ab; ++i)
97 for (
size_t i = 0; i <
length(beta); ++i)
101 log_beta_m_log1p_beta(
length(beta));
102 for (
size_t i = 0; i <
length(beta); ++i)
103 log_beta_m_log1p_beta[i] =
log(
value_of(beta_vec[i])) - log1p_beta[i];
106 alpha_times_log_beta_over_1p_beta(len_ab);
107 for (
size_t i = 0; i < len_ab; ++i)
108 alpha_times_log_beta_over_1p_beta[i]
114 T_partials_return, T_shape>
115 digamma_alpha(
length(alpha));
117 for (
size_t i = 0; i <
length(alpha); ++i)
122 T_partials_return, T_inv_scale> log_beta(
length(beta));
124 for (
size_t i = 0; i <
length(beta); ++i)
129 T_partials_return, T_shape, T_inv_scale>
130 lambda_m_alpha_over_1p_beta(len_ab);
132 for (
size_t i = 0; i < len_ab; ++i)
133 lambda_m_alpha_over_1p_beta[i] =
139 for (
size_t i = 0; i <
size; i++) {
140 if (alpha_vec[i] > 1e10) {
142 logp -=
lgamma(n_vec[i] + 1.0);
147 operands_and_partials.
d_x1[i]
148 += n_vec[i] /
value_of(alpha_vec[i])
151 operands_and_partials.
d_x2[i]
152 += (lambda[i] - n_vec[i]) /
value_of(beta_vec[i]);
162 alpha_times_log_beta_over_1p_beta[i]
163 - n_vec[i] * log1p_beta[i];
166 operands_and_partials.
d_x1[i]
169 + log_beta_m_log1p_beta[i];
171 operands_and_partials.
d_x2[i]
172 += lambda_m_alpha_over_1p_beta[i]
173 - n_vec[i] / (
value_of(beta_vec[i]) + 1.0);
176 return operands_and_partials.
to_var(logp, alpha, beta);
179 template <
typename T_n,
180 typename T_shape,
typename T_inv_scale>
184 const T_shape& alpha,
185 const T_inv_scale& beta) {
186 return neg_binomial_log<false>(n, alpha, beta);
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > lgamma(const fvar< T > &x)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
size_t length(const std::vector< T > &x)
T_return_type to_var(T_partials_return logp, const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
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
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
A variable implementation that stores operands and derivatives with respect to the variable...
size_t max_size(const T1 &x1, const T2 &x2)
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
int size(const std::vector< T > &x)
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
fvar< T > log1p(const fvar< T > &x)
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
return_type< T_shape, T_inv_scale >::type neg_binomial_log(const T_n &n, const T_shape &alpha, const T_inv_scale &beta)
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)