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

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