Stan  1.0
probability, sampling & optimization
neg_binomial.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__NEG_BINOMIAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__NEG_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  // NegBinomial(n|alpha,beta) [alpha > 0; beta > 0; n >= 0]
16  template <bool propto,
17  typename T_n,
18  typename T_shape, typename T_inv_scale,
19  class Policy>
21  neg_binomial_log(const T_n& n,
22  const T_shape& alpha,
23  const T_inv_scale& beta,
24  const Policy&) {
25 
26  static const char* function = "stan::prob::neg_binomial_log(%1%)";
27 
34 
35  // check if any vectors are zero length
36  if (!(stan::length(n)
37  && stan::length(alpha)
38  && stan::length(beta)))
39  return 0.0;
40 
41  typename return_type<T_shape, T_inv_scale>::type logp(0.0);
42  if (!check_nonnegative(function, n, "Failures variable", &logp, Policy()))
43  return logp;
44  if (!check_finite(function, alpha, "Shape parameter", &logp, Policy()))
45  return logp;
46  if (!check_positive(function, alpha, "Shape parameter", &logp, Policy()))
47  return logp;
48  if (!check_finite(function, beta, "Inverse scale parameter",
49  &logp, Policy()))
50  return logp;
51  if (!check_positive(function, beta, "Inverse scale parameter",
52  &logp, Policy()))
53  return logp;
54  if (!(check_consistent_sizes(function,
55  n,alpha,beta,
56  "Failures variable","Shape parameter","Inverse scale parameter",
57  &logp, Policy())))
58  return logp;
59 
60  // check if no variables are involved and prop-to
62  return 0.0;
63 
66 
67  // set up template expressions wrapping scalars into vector views
68  VectorView<const T_n> n_vec(n);
69  VectorView<const T_shape> alpha_vec(alpha);
70  VectorView<const T_inv_scale> beta_vec(beta);
71  size_t size = max_size(n, alpha, beta);
72 
73  for (size_t i = 0; i < size; i++) {
74  // Special case where negative binomial reduces to Poisson
75  if (alpha_vec[i] > 1e10) {
77  logp -= lgamma(n_vec[i] + 1.0);
81  lambda = alpha_vec[i] / beta_vec[i];
82  logp += multiply_log(n_vec[i], lambda) - lambda;
83  }
84  } else {
85  // More typical cases
87  if (n_vec[i] != 0)
88  logp += binomial_coefficient_log<typename scalar_type<T_shape>::type>
89  (n_vec[i] + alpha_vec[i] - 1.0, n_vec[i]);
91  logp += -n_vec[i] * log1p(beta_vec[i])
92  + alpha_vec[i] * log(beta_vec[i] / (1 + beta_vec[i]));
93  }
94  }
95  return logp;
96  }
97 
98  template <bool propto,
99  typename T_n,
100  typename T_shape, typename T_inv_scale>
101  inline
103  neg_binomial_log(const T_n& n,
104  const T_shape& alpha,
105  const T_inv_scale& beta) {
106  return neg_binomial_log<propto>(n,alpha,beta,
108  }
109 
110  template <typename T_n,
111  typename T_shape, typename T_inv_scale,
112  class Policy>
113  inline
115  neg_binomial_log(const T_n& n,
116  const T_shape& alpha,
117  const T_inv_scale& beta,
118  const Policy&) {
119  return neg_binomial_log<false>(n,alpha,beta,Policy());
120  }
121 
122  template <typename T_n,
123  typename T_shape, typename T_inv_scale>
124  inline
126  neg_binomial_log(const T_n& n,
127  const T_shape& alpha,
128  const T_inv_scale& beta) {
129  return neg_binomial_log<false>(n,alpha,beta,
131  }
132 
133 
134  }
135 }
136 #endif
var multiply_log(const var &a, const var &b)
Return the value of a*log(b).
var log1p(const stan::agrad::var &a)
The log (1 + x) function for variables (C99).
var lgamma(const stan::agrad::var &a)
The log gamma function for variables (C99).
var log(const var &a)
Return the natural log of the specified variable (cmath).
Definition: agrad.hpp:1730
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_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_shape, T_inv_scale >::type neg_binomial_log(const T_n &n, const T_shape &alpha, const T_inv_scale &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.