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 
7 #include <stan/prob/traits.hpp>
8 
9 namespace stan {
10 
11  namespace prob {
12 
13  // NegBinomial(n|alpha,beta) [alpha > 0; beta > 0; n >= 0]
14  template <bool propto,
15  typename T_shape, typename T_inv_scale,
16  class Policy>
17  typename boost::math::tools::promote_args<T_shape, T_inv_scale>::type
18  neg_binomial_log(const int n,
19  const T_shape& alpha,
20  const T_inv_scale& beta,
21  const Policy&) {
22 
23  static const char* function = "stan::prob::neg_binomial_log(%1%)";
24 
28  using boost::math::tools::promote_args;
29 
30  typename promote_args<T_shape, T_inv_scale>::type lp;
31  if (!check_nonnegative(function, n, "Failures variable", &lp, Policy()))
32  return lp;
33  if (!check_finite(function, alpha, "Shape parameter", &lp, Policy()))
34  return lp;
35  if (!check_positive(function, alpha, "Shape parameter", &lp, Policy()))
36  return lp;
37  if (!check_finite(function, beta, "Inverse scale parameter",
38  &lp, Policy()))
39  return lp;
40  if (!check_positive(function, beta, "Inverse scale parameter",
41  &lp, Policy()))
42  return lp;
43 
46 
47  lp = 0.0;
48 
49  // Special case where negative binomial reduces to Poisson
50  if (alpha > 1e10) {
52  lp -= lgamma(n + 1.0);
55  typename promote_args<T_shape, T_inv_scale>::type lambda;
56  lambda = alpha / beta;
57  lp += multiply_log(n, lambda) - lambda;
58  return lp;
59  }
60  }
61  // More typical cases
63  if (n != 0)
64  lp += binomial_coefficient_log<T_shape>(n + alpha - 1.0, n);
66  lp += -n * log1p(beta) + alpha * log(beta / (1 + beta));
67  return lp;
68  }
69 
70  template <bool propto,
71  typename T_shape, typename T_inv_scale>
72  inline
73  typename boost::math::tools::promote_args<T_shape, T_inv_scale>::type
74  neg_binomial_log(const int n,
75  const T_shape& alpha,
76  const T_inv_scale& beta) {
77  return neg_binomial_log<propto>(n,alpha,beta,
79  }
80 
81  template <typename T_shape, typename T_inv_scale,
82  class Policy>
83  inline
84  typename boost::math::tools::promote_args<T_shape, T_inv_scale>::type
85  neg_binomial_log(const int n,
86  const T_shape& alpha,
87  const T_inv_scale& beta,
88  const Policy&) {
89  return neg_binomial_log<false>(n,alpha,beta,Policy());
90  }
91 
92  template <typename T_shape, typename T_inv_scale>
93  inline
94  typename boost::math::tools::promote_args<T_shape, T_inv_scale>::type
95  neg_binomial_log(const int n,
96  const T_shape& alpha,
97  const T_inv_scale& beta) {
98  return neg_binomial_log<false>(n,alpha,beta,
100  }
101 
102 
103  }
104 }
105 #endif

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