Stan  1.0
probability, sampling & optimization
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>
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
var multiply_log(const var &a, const var &b)
Return the value of a*log(b).
var log1m(const stan::agrad::var &a)
The log (1 - x) function for variables.
boost::math::tools::promote_args< T >::type log1m(T x)
Return the natural logarithm of one minus the specified value.
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(T_a a, T_b b)
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
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_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, 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_prob >::type binomial_log(const T_n &n, const T_N &N, const T_prob &theta, const Policy &)
Definition: binomial.hpp:22
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.