1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LPMF_HPP 2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_LPMF_HPP 38 template <
bool propto,
typename T_n,
typename T_N,
typename T_size1,
41 const T_n& n,
const T_N& N,
const T_size1& alpha,
const T_size2&
beta) {
42 static const char*
function =
"beta_binomial_lpmf";
49 T_partials_return logp(0.0);
54 "Population size parameter", N,
55 "First prior sample size parameter", alpha,
56 "Second prior sample size parameter", beta);
69 for (
size_t i = 0; i <
size; i++) {
70 if (n_vec[i] < 0 || n_vec[i] > N_vec[i])
75 normalizing_constant(
max_size(N, n));
76 for (
size_t i = 0; i <
max_size(N, n); i++)
81 T_partials_return, T_n, T_N, T_size1, T_size2>
82 lbeta_numerator(size);
83 for (
size_t i = 0; i <
size; i++)
86 N_vec[i] - n_vec[i] +
value_of(beta_vec[i]));
89 T_partials_return, T_size1, T_size2>
90 lbeta_denominator(
max_size(alpha, beta));
91 for (
size_t i = 0; i <
max_size(alpha, beta); i++)
98 digamma_n_plus_alpha(
max_size(n, alpha));
99 for (
size_t i = 0; i <
max_size(n, alpha); i++)
104 T_N, T_size1, T_size2>
105 digamma_N_plus_alpha_plus_beta(
max_size(N, alpha, beta));
106 for (
size_t i = 0; i <
max_size(N, alpha, beta); i++)
108 digamma_N_plus_alpha_plus_beta[i]
113 digamma_alpha_plus_beta(
max_size(alpha, beta));
114 for (
size_t i = 0; i <
max_size(alpha, beta); i++)
116 digamma_alpha_plus_beta[i]
120 digamma_alpha(
length(alpha));
121 for (
size_t i = 0; i <
length(alpha); i++)
126 digamma_beta(
length(beta));
127 for (
size_t i = 0; i <
length(beta); i++)
131 for (
size_t i = 0; i <
size; i++) {
133 logp += normalizing_constant[i];
135 logp += lbeta_numerator[i] - lbeta_denominator[i];
138 ops_partials.
edge1_.partials_[i]
139 += digamma_n_plus_alpha[i] - digamma_N_plus_alpha_plus_beta[i]
140 + digamma_alpha_plus_beta[i] - digamma_alpha[i];
142 ops_partials.
edge2_.partials_[i]
144 - digamma_N_plus_alpha_plus_beta[i] + digamma_alpha_plus_beta[i]
147 return ops_partials.
build(logp);
150 template <
typename T_n,
typename T_N,
typename T_size1,
typename T_size2>
152 const T_n& n,
const T_N& N,
const T_size1& alpha,
const T_size2&
beta) {
153 return beta_binomial_lpmf<false>(n, N, alpha,
beta);
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
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)
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
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.
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)
Returns the log PMF of the Beta-Binomial distribution with given population size, prior success...
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
size_t max_size(const T1 &x1, const T2 &x2)
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
VectorBuilder allocates type T1 values to be used as intermediate values.
internal::ops_partials_edge< double, Op2 > edge2_
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
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.
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.