Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_2_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LCDF_HPP
3
12#include <cmath>
13
14namespace stan {
15namespace math {
16
17template <typename T_n, typename T_location, typename T_precision>
19 const T_n& n, const T_location& mu, const T_precision& phi) {
20 using std::log;
21 using T_n_ref = ref_type_t<T_n>;
22 using T_mu_ref = ref_type_t<T_location>;
23 using T_phi_ref = ref_type_t<T_precision>;
24 static constexpr const char* function = "neg_binomial_2_lcdf";
25 check_consistent_sizes(function, "Random variable", n, "Location parameter",
26 mu, "Precision Parameter", phi);
27
28 T_n_ref n_ref = n;
29 T_mu_ref mu_ref = mu;
30 T_phi_ref phi_ref = phi;
31
32 check_positive_finite(function, "Location parameter", mu_ref);
33 check_positive_finite(function, "Precision parameter", phi_ref);
34 check_not_nan(function, "Random variable", n_ref);
35
36 if (size_zero(n, mu, phi)) {
37 return 0;
38 }
39
40 scalar_seq_view<T_n_ref> n_vec(n_ref);
41 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
42 scalar_seq_view<T_phi_ref> phi_vec(phi_ref);
43 size_t size_n = stan::math::size(n);
44 size_t size_phi_mu = max_size(mu, phi);
45
46 for (size_t i = 0; i < size_n; i++) {
47 if (n_vec[i] < 0) {
48 return LOG_ZERO;
49 }
50 }
51
53 T_precision>
54 phi_mu(size_phi_mu);
55 for (size_t i = 0; i < size_phi_mu; i++) {
56 phi_mu[i] = phi_vec[i] / (phi_vec[i] + mu_vec[i]);
57 }
58
60 for (size_t i = 0; i < size_n; i++) {
61 np1[i] = n_vec[i] + 1.0;
62 }
63
64 return beta_cdf_log(phi_mu.data(), phi_ref, np1.data());
65}
66
67} // namespace math
68} // namespace stan
69#endif
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
return_type_t< T_y, T_scale_succ, T_scale_fail > beta_cdf_log(const T_y &y, const T_scale_succ &alpha, const T_scale_fail &beta)
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
static constexpr double LOG_ZERO
The natural logarithm of 0, .
Definition constants.hpp:68
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
return_type_t< T_location, T_precision > neg_binomial_2_lcdf(const T_n &n, const T_location &mu, const T_precision &phi)
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...