1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LOG_HPP
28 template <
bool propto,
29 typename T_n,
typename T_N,
30 typename T_size1,
typename T_size2>
31 typename return_type<T_size1, T_size2>::type
35 const T_size2& beta) {
36 static const char*
function(
"stan::math::beta_binomial_log");
53 T_partials_return logp(0.0);
56 "First prior sample size parameter", alpha);
58 "Second prior sample size parameter", beta);
60 "Successes variable", n,
61 "Population size parameter", N,
62 "First prior sample size parameter", alpha,
63 "Second prior sample size parameter", beta);
70 operands_and_partials(alpha, beta);
78 for (
size_t i = 0; i <
size; i++) {
79 if (n_vec[i] < 0 || n_vec[i] > N_vec[i])
88 T_partials_return, T_n, T_N>
89 normalizing_constant(
max_size(N, n));
90 for (
size_t i = 0; i <
max_size(N, n); i++)
92 normalizing_constant[i]
96 T_partials_return, T_n, T_N, T_size1, T_size2>
97 lbeta_numerator(size);
98 for (
size_t i = 0; i <
size; i++)
100 lbeta_numerator[i] =
lbeta(n_vec[i] +
value_of(alpha_vec[i]),
105 T_partials_return, T_size1, T_size2>
106 lbeta_denominator(
max_size(alpha, beta));
107 for (
size_t i = 0; i <
max_size(alpha, beta); i++)
113 T_partials_return, T_n, T_size1>
114 digamma_n_plus_alpha(
max_size(n, alpha));
115 for (
size_t i = 0; i <
max_size(n, alpha); i++)
117 digamma_n_plus_alpha[i]
121 T_partials_return, T_N, T_size1, T_size2>
122 digamma_N_plus_alpha_plus_beta(
max_size(N, alpha, beta));
123 for (
size_t i = 0; i <
max_size(N, alpha, beta); i++)
125 digamma_N_plus_alpha_plus_beta[i]
130 T_partials_return, T_size1, T_size2>
131 digamma_alpha_plus_beta(
max_size(alpha, beta));
132 for (
size_t i = 0; i <
max_size(alpha, beta); i++)
134 digamma_alpha_plus_beta[i]
138 T_partials_return, T_size1> digamma_alpha(
length(alpha));
139 for (
size_t i = 0; i <
length(alpha); i++)
144 T_partials_return, T_size2>
145 digamma_beta(
length(beta));
146 for (
size_t i = 0; i <
length(beta); i++)
150 for (
size_t i = 0; i <
size; i++) {
152 logp += normalizing_constant[i];
154 logp += lbeta_numerator[i] - lbeta_denominator[i];
157 operands_and_partials.
d_x1[i]
158 += digamma_n_plus_alpha[i]
159 - digamma_N_plus_alpha_plus_beta[i]
160 + digamma_alpha_plus_beta[i]
163 operands_and_partials.
d_x2[i]
165 - digamma_N_plus_alpha_plus_beta[i]
166 + digamma_alpha_plus_beta[i]
169 return operands_and_partials.
value(logp);
172 template <
typename T_n,
178 const T_size1& alpha,
const T_size2& beta) {
179 return beta_binomial_log<false>(n, N, alpha, beta);
VectorView< T_return_type, false, true > d_x2
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)
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
size_t length(const std::vector< T > &x)
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
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
This class builds partial derivatives with respect to a set of operands.
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)
VectorBuilder allocates type T1 values to be used as intermediate values.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
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.
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
VectorView is a template expression that is constructed with a container or scalar, which it then allows to be used as an array using operator[].
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.
VectorView< T_return_type, false, true > d_x1
fvar< T > digamma(const fvar< T > &x)