| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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
4 #include <stan/math/prim/scal/meta/OperandsAndPartials.hpp>
5 #include <stan/math/prim/scal/err/check_consistent_sizes.hpp>
6 #include <stan/math/prim/scal/err/check_positive_finite.hpp>
7 #include <stan/math/prim/scal/err/check_nonnegative.hpp>
8 #include <stan/math/prim/scal/err/check_less.hpp>
9 #include <stan/math/prim/scal/fun/constants.hpp>
10 #include <stan/math/prim/scal/fun/binomial_coefficient_log.hpp>
11 #include <stan/math/prim/scal/fun/multiply_log.hpp>
12 #include <stan/math/prim/scal/fun/digamma.hpp>
13 #include <stan/math/prim/scal/fun/lgamma.hpp>
14 #include <stan/math/prim/scal/fun/value_of.hpp>
15 #include <stan/math/prim/scal/meta/include_summand.hpp>
16 #include <stan/math/prim/scal/prob/beta_cdf_log.hpp>
17 #include <stan/math/prim/scal/fun/grad_reg_inc_beta.hpp>
18 #include <boost/math/special_functions/digamma.hpp>
19 #include <boost/random/negative_binomial_distribution.hpp>
20 #include <boost/random/variate_generator.hpp>
21 #include <cmath>
22 #include <vector>
23
24 namespace stan {
25
26 namespace math {
27
28 template <typename T_n, typename T_location,
29 typename T_precision>
30 typename return_type<T_location, T_precision>::type
31 neg_binomial_2_cdf_log(const T_n& n,
32 const T_location& mu,
33 const T_precision& phi) {
34 // Size checks
35 if ( !( stan::length(n) && stan::length(mu)
36 && stan::length(phi) ) )
37 return 0.0;
38
39 using stan::math::check_nonnegative;
40 using stan::math::check_positive_finite;
41 using stan::math::check_not_nan;
42 using stan::math::check_consistent_sizes;
43 using stan::math::check_less;
44 using std::log;
45 using std::log;
46
47 static const char* function("stan::math::neg_binomial_2_cdf");
48 check_positive_finite(function, "Location parameter", mu);
49 check_positive_finite(function, "Precision parameter", phi);
50 check_not_nan(function, "Random variable", n);
51 check_consistent_sizes(function,
52 "Random variable", n,
53 "Location parameter", mu,
54 "Precision Parameter", phi);
55
56 VectorView<const T_n> n_vec(n);
57 VectorView<const T_location> mu_vec(mu);
58 VectorView<const T_precision> phi_vec(phi);
59
60 size_t size_phi_mu = max_size(mu, phi);
61 size_t size_n = length(n);
62
63 std::vector<typename return_type<T_location, T_precision>::type>
64 phi_mu(size_phi_mu);
65 std::vector<typename return_type<T_n>::type> np1(size_n);
66
67 for (size_t i = 0; i < size_phi_mu; i++)
68 phi_mu[i] = phi_vec[i]/(phi_vec[i]+mu_vec[i]);
69
70 for (size_t i = 0; i < size_n; i++)
71 if (n_vec[i] < 0)
72 return log(0.0);
73 else
74 np1[i] = n_vec[i] + 1.0;
75
76 if (size_n == 1) {
77 if (size_phi_mu == 1)
78 return beta_cdf_log(phi_mu[0], phi, np1[0]);
79 else
80 return beta_cdf_log(phi_mu, phi, np1[0]);
81 } else {
82 if (size_phi_mu == 1)
83 return beta_cdf_log(phi_mu[0], phi, np1);
84 else
85 return beta_cdf_log(phi_mu, phi, np1);
86 }
87 }
88 }
89 }
90 #endif
stan::math::check_less
bool check_less(const char *function, const char *name, const T_y &y, const T_high &high)
Return true if y is strictly less than high.
Definition: check_less.hpp:81
lgamma.hpp
stan::math::check_not_nan
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Definition: check_not_nan.hpp:63
check_positive_finite.hpp
beta_cdf_log.hpp
digamma.hpp
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
binomial_coefficient_log.hpp
include_summand.hpp
multiply_log.hpp
check_nonnegative.hpp
OperandsAndPartials.hpp
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
stan::math::beta_cdf_log
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)
Definition: beta_cdf_log.hpp:33
grad_reg_inc_beta.hpp
constants.hpp
stan::math::check_consistent_sizes
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.
Definition: check_consistent_sizes.hpp:31
stan::math::check_nonnegative
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
Definition: check_nonnegative.hpp:66
value_of.hpp
stan::math::neg_binomial_2_cdf_log
return_type< T_location, T_precision >::type neg_binomial_2_cdf_log(const T_n &n, const T_location &mu, const T_precision &phi)
Definition: neg_binomial_2_cdf_log.hpp:31
stan::VectorView
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
check_less.hpp
check_consistent_sizes.hpp
stan::math::check_positive_finite
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
Definition: check_positive_finite.hpp:28

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