1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LOG_HPP
26 template <
bool propto,
27 typename T_n,
typename T_N,
28 typename T_size1,
typename T_size2>
29 typename return_type<T_size1, T_size2>::type
33 const T_size2& beta) {
34 static const char*
function(
"stan::math::beta_binomial_log");
51 T_partials_return logp(0.0);
54 "First prior sample size parameter", alpha);
56 "Second prior sample size parameter", beta);
58 "Successes variable", n,
59 "Population size parameter", N,
60 "First prior sample size parameter", alpha,
61 "Second prior sample size parameter", beta);
68 operands_and_partials(alpha, beta);
76 for (
size_t i = 0; i <
size; i++) {
77 if (n_vec[i] < 0 || n_vec[i] > N_vec[i])
86 T_partials_return, T_n, T_N>
87 normalizing_constant(
max_size(N, n));
88 for (
size_t i = 0; i <
max_size(N, n); i++)
90 normalizing_constant[i]
94 T_partials_return, T_n, T_N, T_size1, T_size2>
95 lbeta_numerator(size);
96 for (
size_t i = 0; i <
size; i++)
103 T_partials_return, T_size1, T_size2>
104 lbeta_denominator(
max_size(alpha, beta));
105 for (
size_t i = 0; i <
max_size(alpha, beta); i++)
111 T_partials_return, T_n, T_size1>
112 digamma_n_plus_alpha(
max_size(n, alpha));
113 for (
size_t i = 0; i <
max_size(n, alpha); i++)
115 digamma_n_plus_alpha[i]
119 T_partials_return, T_N, T_size1, T_size2>
120 digamma_N_plus_alpha_plus_beta(
max_size(N, alpha, beta));
121 for (
size_t i = 0; i <
max_size(N, alpha, beta); i++)
123 digamma_N_plus_alpha_plus_beta[i]
128 T_partials_return, T_size1, T_size2>
129 digamma_alpha_plus_beta(
max_size(alpha, beta));
130 for (
size_t i = 0; i <
max_size(alpha, beta); i++)
132 digamma_alpha_plus_beta[i]
136 T_partials_return, T_size1> digamma_alpha(
length(alpha));
137 for (
size_t i = 0; i <
length(alpha); i++)
142 T_partials_return, T_size2>
143 digamma_beta(
length(beta));
144 for (
size_t i = 0; i <
length(beta); i++)
148 for (
size_t i = 0; i <
size; i++) {
150 logp += normalizing_constant[i];
152 logp += lbeta_numerator[i] - lbeta_denominator[i];
155 operands_and_partials.
d_x1[i]
156 += digamma_n_plus_alpha[i]
157 - digamma_N_plus_alpha_plus_beta[i]
158 + digamma_alpha_plus_beta[i]
161 operands_and_partials.
d_x2[i]
163 - digamma_N_plus_alpha_plus_beta[i]
164 + digamma_alpha_plus_beta[i]
167 return operands_and_partials.
to_var(logp, alpha, beta);
170 template <
typename T_n,
176 const T_size1& alpha,
const T_size2& beta) {
177 return beta_binomial_log<false>(n, N, alpha, beta);
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
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...
return_type< T_size1, T_size2 >::type beta_binomial_log(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
size_t max_size(const T1 &x1, const T2 &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
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...
boost::math::tools::promote_args< typename partials_type< typename scalar_type< T1 >::type >::type, typename partials_type< typename scalar_type< T2 >::type >::type, typename partials_type< typename scalar_type< T3 >::type >::type, typename partials_type< typename scalar_type< T4 >::type >::type, typename partials_type< typename scalar_type< T5 >::type >::type, typename partials_type< typename scalar_type< T6 >::type >::type >::type type
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)