Stan Math Library  2.11.0
reverse mode automatic differentiation
neg_binomial_2_cdf_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_CDF_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_CDF_LOG_HPP
3 
11 #include <cmath>
12 
13 namespace stan {
14  namespace math {
15 
16  template <typename T_n, typename T_location,
17  typename T_precision>
18  typename return_type<T_location, T_precision>::type
19  neg_binomial_2_cdf_log(const T_n& n,
20  const T_location& mu,
21  const T_precision& phi) {
25  using std::log;
26 
27  // check if any vectors are zero length
28  if (!(stan::length(n)
29  && stan::length(mu)
30  && stan::length(phi)))
31  return 0.0;
32 
33  static const char* function("stan::math::neg_binomial_2_cdf");
34  check_positive_finite(function, "Location parameter", mu);
35  check_positive_finite(function, "Precision parameter", phi);
36  check_not_nan(function, "Random variable", n);
37  check_consistent_sizes(function,
38  "Random variable", n,
39  "Location parameter", mu,
40  "Precision Parameter", phi);
41 
42  VectorView<const T_n> n_vec(n);
44  VectorView<const T_precision> phi_vec(phi);
45 
46  size_t size_phi_mu = max_size(mu, phi);
48  T_location, T_precision> phi_mu(size_phi_mu);
49  for (size_t i = 0; i < size_phi_mu; i++)
50  phi_mu[i] = phi_vec[i] / (phi_vec[i] + mu_vec[i]);
51 
52  size_t size_n = length(n);
54  T_n> np1(size_n);
55  for (size_t i = 0; i < size_n; i++)
56  if (n_vec[i] < 0)
57  return log(0.0);
58  else
59  np1[i] = n_vec[i] + 1.0;
60 
61  return beta_cdf_log(phi_mu.data(), phi, np1.data());
62  }
63 
64  }
65 }
66 #endif
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
return_type< T_y, T_scale_succ, T_scale_fail >::type beta_cdf_log(const T_y &y, const T_scale_succ &alpha, const T_scale_fail &beta)
VectorBuilder allocates type T1 values to be used as intermediate values.
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
VectorBuilderHelper< T1, used, contains_vector< T2, T3, T4, T5, T6, T7 >::value >::type data()
return_type< T_location, T_precision >::type neg_binomial_2_cdf_log(const T_n &n, const T_location &mu, const T_precision &phi)
VectorView is a template expression that is constructed with a container or scalar, which it then allows to be used as an array using operator[].
Definition: VectorView.hpp:48
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.

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