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 
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  // Binomial(n|N,theta) [N >= 0; 0 <= n <= N; 0 <= theta <= 1]
16  template <bool propto,
17  typename T_n,
18  typename T_N,
19  typename T_prob,
20  class Policy>
21  typename return_type<T_prob>::type
22  binomial_log(const T_n& n,
23  const T_N& N,
24  const T_prob& theta,
25  const Policy&) {
26 
27  static const char* function = "stan::prob::binomial_log(%1%)";
28 
35 
36  // check if any vectors are zero length
37  if (!(stan::length(n)
38  && stan::length(N)
39  && stan::length(theta)))
40  return 0.0;
41 
42  typename return_type<T_prob>::type logp(0.0);
43  if (!check_bounded(function, n, 0, N,
44  "Successes variable",
45  &logp, Policy()))
46  return logp;
47  if (!check_nonnegative(function, N,
48  "Population size parameter",
49  &logp, Policy()))
50  return logp;
51  if (!check_finite(function, theta,
52  "Probability parameter",
53  &logp, Policy()))
54  return logp;
55  if (!check_bounded(function, theta, 0.0, 1.0,
56  "Probability parameter",
57  &logp, Policy()))
58  return logp;
59  if (!(check_consistent_sizes(function,
60  n,N,theta,
61  "Successes variable","Population size parameter","Probability parameter",
62  &logp, Policy())))
63  return logp;
64 
65 
66  // check if no variables are involved and prop-to
68  return 0.0;
69 
70  // set up template expressions wrapping scalars into vector views
71  VectorView<const T_n> n_vec(n);
72  VectorView<const T_N> N_vec(N);
73  VectorView<const T_prob> theta_vec(theta);
74  size_t size = max_size(n, N, theta);
75  //agrad::OperandsAndPartials<T_prob> operands_and_partials(theta);
76 
79  using stan::math::log1m;
80 
81  for (size_t i = 0; i < size; i++) {
83  logp += binomial_coefficient_log(N_vec[i],n_vec[i]);
85  logp += multiply_log(n_vec[i],theta_vec[i])
86  + (N_vec[i] - n_vec[i]) * log1m(theta_vec[i]);
87  }
88  return logp;
89  }
90 
91  template <bool propto,
92  typename T_n,
93  typename T_N,
94  typename T_prob>
95  inline
97  binomial_log(const T_n& n,
98  const T_N& N,
99  const T_prob& theta) {
100  return binomial_log<propto>(n,N,theta,stan::math::default_policy());
101  }
102 
103 
104  template <typename T_n,
105  typename T_N,
106  typename T_prob,
107  class Policy>
108  inline
110  binomial_log(const T_n& n,
111  const T_N& N,
112  const T_prob& theta,
113  const Policy&) {
114  return binomial_log<false>(n,N,theta,Policy());
115  }
116 
117 
118  template <typename T_n,
119  typename T_N,
120  typename T_prob>
121  inline
123  binomial_log(const T_n& n,
124  const T_N& N,
125  const T_prob& theta) {
126  return binomial_log<false>(n,N,theta,stan::math::default_policy());
127  }
128 
129 
130 
131  }
132 }
133 #endif

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