1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LPMF_HPP 2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LPMF_HPP 27 template <
bool propto,
28 typename T_n,
typename T_N,
29 typename T_size1,
typename T_size2>
34 const T_size2& beta) {
35 static const char*
function(
"beta_binomial_lpmf");
45 T_partials_return logp(0.0);
48 "First prior sample size parameter", alpha);
50 "Second prior sample size parameter", beta);
52 "Successes variable", n,
53 "Population size parameter", N,
54 "First prior sample size parameter", alpha,
55 "Second prior sample size parameter", beta);
61 operands_and_partials(alpha, beta);
69 for (
size_t i = 0; i <
size; i++) {
70 if (n_vec[i] < 0 || n_vec[i] > N_vec[i])
75 T_partials_return, T_n, T_N>
76 normalizing_constant(
max_size(N, n));
77 for (
size_t i = 0; i <
max_size(N, n); i++)
79 normalizing_constant[i]
83 T_partials_return, T_n, T_N, T_size1, T_size2>
84 lbeta_numerator(size);
85 for (
size_t i = 0; i <
size; i++)
92 T_partials_return, T_size1, T_size2>
93 lbeta_denominator(
max_size(alpha, beta));
94 for (
size_t i = 0; i <
max_size(alpha, beta); i++)
100 T_partials_return, T_n, T_size1>
101 digamma_n_plus_alpha(
max_size(n, alpha));
102 for (
size_t i = 0; i <
max_size(n, alpha); i++)
104 digamma_n_plus_alpha[i]
108 T_partials_return, T_N, T_size1, T_size2>
109 digamma_N_plus_alpha_plus_beta(
max_size(N, alpha, beta));
110 for (
size_t i = 0; i <
max_size(N, alpha, beta); i++)
112 digamma_N_plus_alpha_plus_beta[i]
117 T_partials_return, T_size1, T_size2>
118 digamma_alpha_plus_beta(
max_size(alpha, beta));
119 for (
size_t i = 0; i <
max_size(alpha, beta); i++)
121 digamma_alpha_plus_beta[i]
125 T_partials_return, T_size1> digamma_alpha(
length(alpha));
126 for (
size_t i = 0; i <
length(alpha); i++)
131 T_partials_return, T_size2>
132 digamma_beta(
length(beta));
133 for (
size_t i = 0; i <
length(beta); i++)
137 for (
size_t i = 0; i <
size; i++) {
139 logp += normalizing_constant[i];
141 logp += lbeta_numerator[i] - lbeta_denominator[i];
144 operands_and_partials.
d_x1[i]
145 += digamma_n_plus_alpha[i]
146 - digamma_N_plus_alpha_plus_beta[i]
147 + digamma_alpha_plus_beta[i]
150 operands_and_partials.
d_x2[i]
152 - digamma_N_plus_alpha_plus_beta[i]
153 + digamma_alpha_plus_beta[i]
156 return operands_and_partials.
value(logp);
159 template <
typename T_n,
165 const T_size1& alpha,
const T_size2& beta) {
166 return beta_binomial_lpmf<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...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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
return_type< T_size1, T_size2 >::type beta_binomial_lpmf(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
This class builds partial derivatives with respect to a set of operands.
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.
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[].
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
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
VectorView< T_return_type, false, true > d_x1
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.