Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
beta_binomial.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__BETA_BINOMIAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__BETA_BINOMIAL_HPP__
3 
7 #include <stan/prob/traits.hpp>
8 
9 namespace stan {
10  namespace prob {
11 
12  // BetaBinomial(n|alpha,beta) [alpha > 0; beta > 0; n >= 0]
13  template <bool propto,
14  typename T_size1,
15  typename T_size2,
16  class Policy>
17  typename boost::math::tools::promote_args<T_size1,T_size2>::type
18  beta_binomial_log(const int n,
19  const int N,
20  const T_size1& alpha,
21  const T_size2& beta,
22  const Policy&) {
23  static const char* function = "stan::prob::beta_binomial_log(%1%)";
24 
28  using boost::math::tools::promote_args;
29 
30  typename promote_args<T_size1,T_size2>::type lp;
31  if (!check_nonnegative(function, N, "Population size parameter", &lp, Policy()))
32  return lp;
33  if (!check_finite(function, alpha, "First prior sample size parameter", &lp,
34  Policy()))
35  return lp;
36  if (!check_positive(function, alpha, "First prior sample size parameter",
37  &lp, Policy()))
38  return lp;
39  if (!check_finite(function, beta, "Second prior sample size parameter",
40  &lp, Policy()))
41  return lp;
42  if (!check_positive(function, beta, "Second prior sample size parameter",
43  &lp, Policy()))
44  return lp;
45 
46  if (n < 0 || n > N)
47  return LOG_ZERO;
48 
49  using stan::math::lbeta;
51 
52  lp = 0.0;
54  lp += binomial_coefficient_log(N,n);
56  lp += lbeta(n + alpha, N - n + beta) - lbeta(alpha,beta);
57  return lp;
58  }
59 
60  template <bool propto,
61  typename T_size1,
62  typename T_size2>
63  typename boost::math::tools::promote_args<T_size1,T_size2>::type
64  beta_binomial_log(const int n, const int N,
65  const T_size1& alpha, const T_size2& beta) {
66  return beta_binomial_log<propto>(n,N,alpha,beta,
68  }
69 
70  template <typename T_size1,
71  typename T_size2,
72  class Policy>
73  typename boost::math::tools::promote_args<T_size1,T_size2>::type
74  inline
75  beta_binomial_log(const int n, const int N,
76  const T_size1& alpha, const T_size2& beta,
77  const Policy&) {
78  return beta_binomial_log<false>(n,N,alpha,beta,Policy());
79  }
80 
81  template <typename T_size1,
82  typename T_size2>
83  typename boost::math::tools::promote_args<T_size1,T_size2>::type
84  beta_binomial_log(const int n, const int N,
85  const T_size1& alpha, const T_size2& beta) {
86  return beta_binomial_log<false>(n,N,alpha,beta,
88  }
89 
90  }
91 }
92 #endif

     [ Stan Home Page ] © 2011–2012, Stan Development Team.