Stan  1.0
probability, sampling & optimization
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 
4 #include <stan/agrad.hpp>
7 #include <stan/meta/traits.hpp>
8 #include <stan/prob/traits.hpp>
10 
11 namespace stan {
12 
13  namespace prob {
14 
15  // BetaBinomial(n|alpha,beta) [alpha > 0; beta > 0; n >= 0]
16  template <bool propto,
17  typename T_n,
18  typename T_N,
19  typename T_size1,
20  typename T_size2,
21  class Policy>
23  beta_binomial_log(const T_n& n,
24  const T_N& N,
25  const T_size1& alpha,
26  const T_size2& beta,
27  const Policy&) {
28  static const char* function = "stan::prob::beta_binomial_log(%1%)";
29 
36 
37  // check if any vectors are zero length
38  if (!(stan::length(n)
39  && stan::length(N)
40  && stan::length(alpha)
41  && stan::length(beta)))
42  return 0.0;
43 
44  typename return_type<T_size1,T_size2>::type logp(0.0);
45  if (!check_nonnegative(function, N, "Population size parameter", &logp, Policy()))
46  return logp;
47  if (!check_finite(function, alpha, "First prior sample size parameter", &logp,
48  Policy()))
49  return logp;
50  if (!check_positive(function, alpha, "First prior sample size parameter",
51  &logp, Policy()))
52  return logp;
53  if (!check_finite(function, beta, "Second prior sample size parameter",
54  &logp, Policy()))
55  return logp;
56  if (!check_positive(function, beta, "Second prior sample size parameter",
57  &logp, Policy()))
58  return logp;
59  if (!(check_consistent_sizes(function,
60  n,N,alpha,beta,
61  "Successes variable","Population size parameter","First prior sample size parameter","Second prior sample size parameter",
62  &logp, Policy())))
63  return logp;
64 
65  // check if no variables are involved and prop-to
67  return 0.0;
68 
69  VectorView<const T_n> n_vec(n);
70  VectorView<const T_N> N_vec(N);
71  VectorView<const T_size1> alpha_vec(alpha);
72  VectorView<const T_size2> beta_vec(beta);
73  size_t size = max_size(n, N, alpha, beta);
74 
75  for (size_t i = 0; i < size; i++) {
76  if (n_vec[i] < 0 || n_vec[i] > N_vec[i])
77  return LOG_ZERO;
78  }
79 
80  using stan::math::lbeta;
82 
83  for (size_t i = 0; i < size; i++) {
85  logp += binomial_coefficient_log(N_vec[i],n_vec[i]);
87  logp += lbeta(n_vec[i] + alpha_vec[i], N_vec[i] - n_vec[i] + beta_vec[i])
88  - lbeta(alpha_vec[i],beta_vec[i]);
89  }
90  return logp;
91  }
92 
93  template <bool propto,
94  typename T_n,
95  typename T_N,
96  typename T_size1,
97  typename T_size2>
99  beta_binomial_log(const T_n& n, const T_N& N,
100  const T_size1& alpha, const T_size2& beta) {
101  return beta_binomial_log<propto>(n,N,alpha,beta,
103  }
104 
105  template <typename T_n,
106  typename T_N,
107  typename T_size1,
108  typename T_size2,
109  class Policy>
111  inline
112  beta_binomial_log(const T_n& n, const T_N& N,
113  const T_size1& alpha, const T_size2& beta,
114  const Policy&) {
115  return beta_binomial_log<false>(n,N,alpha,beta,Policy());
116  }
117 
118  template <typename T_n,
119  typename T_N,
120  typename T_size1,
121  typename T_size2>
123  beta_binomial_log(const T_n& n, const T_N& N,
124  const T_size1& alpha, const T_size2& beta) {
125  return beta_binomial_log<false>(n,N,alpha,beta,
127  }
128 
129  }
130 }
131 #endif
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
boost::math::tools::promote_args< T1, T2 >::type lbeta(T1 a, T2 b)
Return the log of the beta function applied to the specified arguments.
boost::math::tools::promote_args< T_N, T_n >::type binomial_coefficient_log(T_N N, T_n n)
Return the log of the binomial coefficient for the specified arguments.
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result, const Policy &)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
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, const Policy &)
Probability, optimization and sampling library.
Definition: agrad.cpp:6
size_t length(const T &x)
Definition: traits.hpp:111
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:148
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33
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
Definition: traits.hpp:368

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