1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__BINOMIAL_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__BINOMIAL_HPP
4 #include <boost/random/binomial_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
26 template <
bool propto,
30 typename return_type<T_prob>::type
33 const T_prob& theta) {
35 static const char*
function =
"stan::prob::binomial_log(%1%)";
55 "Population size parameter",
58 "Probability parameter",
61 "Probability parameter",
66 "Population size parameter",
67 "Probability parameter",
88 for (
size_t i = 0; i <
size; ++i)
92 for (
size_t i = 0; i <
length(theta); ++i)
96 for (
size_t i = 0; i <
size; ++i)
98 + (N_vec[i] - n_vec[i]) * log1m_theta[i];
103 for (
size_t i = 0; i <
size; ++i) {
105 temp2 += N_vec[i] - n_vec[i];
108 operands_and_partials.
d_x1[0]
110 - temp2 / (1.0 -
value_of(theta_vec[0]));
114 for (
size_t i = 0; i <
size; ++i)
115 operands_and_partials.
d_x1[i]
116 += n_vec[i] /
value_of(theta_vec[i])
117 - (N_vec[i] - n_vec[i]) / (1.0 -
value_of(theta_vec[i]));
121 return operands_and_partials.
to_var(logp);
124 template <
typename T_n,
131 const T_prob& theta) {
132 return binomial_log<false>(n,N,theta);
137 template <
bool propto,
144 const T_prob& alpha) {
146 static const char*
function =
"stan::prob::binomial_logit_log(%1%)";
163 "Successes variable",
166 "Population size parameter",
169 "Probability parameter",
173 "Successes variable",
174 "Population size parameter",
175 "Probability parameter",
195 for (
size_t i = 0; i <
size; ++i)
199 for (
size_t i = 0; i <
length(alpha); ++i)
203 for (
size_t i = 0; i <
length(alpha); ++i)
206 for (
size_t i = 0; i <
size; ++i)
207 logp += n_vec[i] * log_inv_logit_alpha[i]
208 + (N_vec[i] - n_vec[i]) * log_inv_logit_neg_alpha[i];
213 for (
size_t i = 0; i <
size; ++i) {
215 temp2 += N_vec[i] - n_vec[i];
218 operands_and_partials.
d_x1[0]
224 for (
size_t i = 0; i <
size; ++i)
225 operands_and_partials.
d_x1[i]
231 return operands_and_partials.
to_var(logp);
234 template <
typename T_n,
241 const T_prob& alpha) {
242 return binomial_logit_log<false>(n,N,alpha);
247 template <
typename T_n,
typename T_N,
typename T_prob>
251 static const char*
function =
"stan::prob::binomial_cdf(%1%)";
268 check_finite(
function, theta,
"Probability parameter", &P);
270 "Probability parameter", &P);
272 "Successes variable",
"Population size parameter",
273 "Probability parameter",
285 using boost::math::ibeta_derivative;
293 return operands_and_partials.
to_var(0.0);
296 for (
size_t i = 0; i <
size; i++) {
304 const double n_dbl =
value_of(n_vec[i]);
305 const double N_dbl =
value_of(N_vec[i]);
306 const double theta_dbl =
value_of(theta_vec[i]);
308 const double Pi =
ibeta(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl);
313 operands_and_partials.
d_x1[i]
314 += - ibeta_derivative(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl) / Pi;
320 for(
size_t i = 0; i <
stan::length(theta); ++i) operands_and_partials.
d_x1[i] *= P;
323 return operands_and_partials.
to_var(P);
327 template <
typename T_n,
typename T_N,
typename T_prob>
331 static const char*
function =
"stan::prob::binomial_cdf_log(%1%)";
348 check_finite(
function, theta,
"Probability parameter", &P);
350 "Probability parameter", &P);
352 "Successes variable",
"Population size parameter",
353 "Probability parameter",
365 using boost::math::ibeta_derivative;
376 for (
size_t i = 0; i <
size; i++) {
382 const double n_dbl =
value_of(n_vec[i]);
383 const double N_dbl =
value_of(N_vec[i]);
384 const double theta_dbl =
value_of(theta_vec[i]);
385 const double Pi =
ibeta(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl);
390 operands_and_partials.
d_x1[i]
391 += - ibeta_derivative(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl) / Pi;
394 return operands_and_partials.
to_var(P);
397 template <
typename T_n,
typename T_N,
typename T_prob>
401 static const char*
function =
"stan::prob::binomial_ccdf_log(%1%)";
418 check_finite(
function, theta,
"Probability parameter", &P);
420 "Probability parameter", &P);
422 "Successes variable",
"Population size parameter",
423 "Probability parameter",
435 using boost::math::ibeta_derivative;
443 return operands_and_partials.
to_var(0.0);
446 for (
size_t i = 0; i <
size; i++) {
452 const double n_dbl =
value_of(n_vec[i]);
453 const double N_dbl =
value_of(N_vec[i]);
454 const double theta_dbl =
value_of(theta_vec[i]);
455 const double Pi = 1.0 -
ibeta(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl);
460 operands_and_partials.
d_x1[i]
461 += ibeta_derivative(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl) / Pi;
464 return operands_and_partials.
to_var(P);
473 using boost::variate_generator;
474 using boost::binomial_distribution;
476 static const char*
function =
"stan::prob::binomial_rng(%1%)";
484 "Population size parameter", (
double*)0);
486 "Probability parameter", (
double*)0);
488 "Probability parameter", (
double*)0);
490 "Probability parameter", (
double*)0);
492 variate_generator<RNG&, binomial_distribution<> >
fvar< T > log1m(const fvar< T > &x)
bool check_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, const char *name, T_result *result)
T_return_type to_var(double logp)
return_type< T_prob >::type binomial_cdf_log(const T_n &n, const T_N &N, const T_prob &theta)
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 >::type log_inv_logit(const T &u)
Returns the natural logarithm of the inverse logit of the specified argument.
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.
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
return_type< T_prob >::type binomial_log(const T_n &n, const T_N &N, const T_prob &theta)
boost::math::tools::promote_args< T >::type inv_logit(const T a)
Returns the inverse logit function applied to the argument.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
int binomial_rng(const int N, const double theta, RNG &rng)
A variable implementation that stores operands and derivatives with respect to the variable...
fvar< T > inv_logit(const fvar< T > &x)
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...
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_prob >::type binomial_ccdf_log(const T_n &n, const T_N &N, const T_prob &theta)
bool check_less_or_equal(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result)
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
int size(const std::vector< T > &x)
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > log_inv_logit(const fvar< T > &x)
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
return_type< T_prob >::type binomial_cdf(const T_n &n, const T_N &N, const T_prob &theta)
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...
bool check_greater_or_equal(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
boost::math::tools::promote_args< T >::type log1m(T x)
Return the natural logarithm of one minus the specified value.
return_type< T_prob >::type binomial_logit_log(const T_n &n, const T_N &N, const T_prob &alpha)
double negative_infinity()
Return negative infinity.