Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
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>
20  typename return_type<T_shape, T_inv_scale>::type
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

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