Stan Math Library  2.20.0
reverse mode automatic differentiation
neg_binomial_2_lcdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_LCDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NEG_BINOMIAL_2_LCDF_HPP
3 
10 #include <cmath>
11 
12 namespace stan {
13 namespace math {
14 
15 template <typename T_n, typename T_location, typename T_precision>
17  const T_n& n, const T_location& mu, const T_precision& phi) {
18  using std::log;
19 
20  if (size_zero(n, mu, phi))
21  return 0.0;
22 
23  static const char* function = "neg_binomial_2_lcdf";
24  check_positive_finite(function, "Location parameter", mu);
25  check_positive_finite(function, "Precision parameter", phi);
26  check_not_nan(function, "Random variable", n);
27  check_consistent_sizes(function, "Random variable", n, "Location parameter",
28  mu, "Precision Parameter", phi);
29 
30  scalar_seq_view<T_n> n_vec(n);
31  scalar_seq_view<T_location> mu_vec(mu);
32  scalar_seq_view<T_precision> phi_vec(phi);
33 
34  size_t size_phi_mu = max_size(mu, phi);
36  T_location, T_precision>
37  phi_mu(size_phi_mu);
38  for (size_t i = 0; i < size_phi_mu; i++)
39  phi_mu[i] = phi_vec[i] / (phi_vec[i] + mu_vec[i]);
40 
41  size_t size_n = length(n);
43  for (size_t i = 0; i < size_n; i++)
44  if (n_vec[i] < 0)
45  return log(0.0);
46  else
47  np1[i] = n_vec[i] + 1.0;
48 
49  return beta_cdf_log(phi_mu.data(), phi, np1.data());
50 }
51 
52 } // namespace math
53 } // namespace stan
54 #endif
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
return_type< T_location, T_precision >::type neg_binomial_2_lcdf(const T_n &n, const T_location &mu, const T_precision &phi)
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
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.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.

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