1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__BINOMIAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__BINOMIAL_HPP__
16 template <
bool propto,
21 typename return_type<T_prob>::type
27 static const char*
function =
"stan::prob::binomial_log(%1%)";
48 "Population size parameter",
52 "Probability parameter",
56 "Probability parameter",
62 "Population size parameter",
63 "Probability parameter",
85 for (
size_t i = 0; i < size; ++i)
89 for (
size_t i = 0; i <
length(theta); ++i)
93 for (
size_t i = 0; i < size; ++i)
95 + (N_vec[i] - n_vec[i]) * log1m_theta[i];
100 for (
size_t i = 0; i < size; ++i) {
102 temp2 += N_vec[i] - n_vec[i];
104 operands_and_partials.
d_x1[0]
106 - temp2 / (1.0 -
value_of(theta_vec[0]));
108 for (
size_t i = 0; i < size; ++i)
109 operands_and_partials.
d_x1[i]
110 += n_vec[i] /
value_of(theta_vec[i])
111 - (N_vec[i] - n_vec[i]) / (1.0 -
value_of(theta_vec[i]));
114 return operands_and_partials.
to_var(logp);
117 template <
bool propto,
125 const T_prob& theta) {
130 template <
typename T_n,
140 return binomial_log<false>(n,N,theta,Policy());
144 template <
typename T_n,
151 const T_prob& theta) {
157 template <
bool propto,
168 static const char*
function =
"stan::prob::binomial_logit_log(%1%)";
185 "Successes variable",
189 "Population size parameter",
193 "Probability parameter",
198 "Successes variable",
199 "Population size parameter",
200 "Probability parameter",
212 size_t size =
max_size(n, N, alpha);
221 for (
size_t i = 0; i < size; ++i)
225 for (
size_t i = 0; i <
length(alpha); ++i)
229 for (
size_t i = 0; i <
length(alpha); ++i)
232 for (
size_t i = 0; i < size; ++i)
233 logp += n_vec[i] * log_inv_logit_alpha[i]
234 + (N_vec[i] - n_vec[i]) * log_inv_logit_neg_alpha[i];
239 for (
size_t i = 0; i < size; ++i) {
241 temp2 += N_vec[i] - n_vec[i];
243 operands_and_partials.
d_x1[0]
247 for (
size_t i = 0; i < size; ++i)
248 operands_and_partials.
d_x1[i]
253 return operands_and_partials.
to_var(logp);
256 template <
bool propto,
264 const T_prob& alpha) {
269 template <
typename T_n,
279 return binomial_logit_log<false>(n,N,alpha,Policy());
283 template <
typename T_n,
290 const T_prob& alpha) {
296 template <
bool propto,
typename T_n,
typename T_N,
typename T_prob,
302 static const char*
function =
"stan::prob::binomial_cdf(%1%)";
318 if (!
check_bounded(
function, n, 0, N,
"Successes variable", &P,
326 if (!
check_finite(
function, theta,
"Probability parameter", &P,
331 "Probability parameter", &P, Policy()))
335 "Successes variable",
"Population size parameter",
"Probability parameter",
347 size_t size =
max_size(n, N, theta);
351 using boost::math::ibetac;
352 using boost::math::ibeta_derivative;
358 + operands_and_partials.
nvaris, 0.0);
360 for (
size_t i = 0; i < size; i++) {
362 const double n_dbl =
value_of(n_vec[i]);
363 const double N_dbl =
value_of(N_vec[i]);
364 const double theta_dbl =
value_of(theta_vec[i]);
367 const double Pi = ibetac(n_dbl + 1, N_dbl - n_dbl, theta_dbl);
372 operands_and_partials.
d_x1[i] += - ibeta_derivative(n_dbl + 1, N_dbl - n_dbl, theta_dbl) / Pi;
377 for (
size_t i = 0; i < size; i++) {
379 operands_and_partials.
d_x1[i] *= P;
385 template <
bool propto,
typename T_n,
typename T_N,
typename T_prob>
392 template <
typename T_n,
typename T_N,
typename T_prob,
class Policy>
394 binomial_cdf(
const T_n& n,
const T_N& N,
const T_prob& theta,
const Policy&) {
395 return binomial_cdf<false>(n, N, theta,Policy());
398 template <
typename T_n,
typename T_N,
typename T_prob>