Stan Math Library  2.10.0
reverse mode automatic differentiation
beta_binomial_cdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_CDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_CDF_HPP
3 
21 #include <cmath>
22 
23 namespace stan {
24 
25  namespace math {
26 
27  // Beta-Binomial CDF
28  template <typename T_n, typename T_N,
29  typename T_size1, typename T_size2>
30  typename return_type<T_size1, T_size2>::type
31  beta_binomial_cdf(const T_n& n, const T_N& N, const T_size1& alpha,
32  const T_size2& beta) {
33  static const char* function("stan::math::beta_binomial_cdf");
34  typedef typename stan::partials_return_type<T_n, T_N, T_size1,
35  T_size2>::type
36  T_partials_return;
37 
43 
44  // Ensure non-zero argument lengths
45  if (!(stan::length(n) && stan::length(N) && stan::length(alpha)
46  && stan::length(beta)))
47  return 1.0;
48 
49  T_partials_return P(1.0);
50 
51  // Validate arguments
52  check_nonnegative(function, "Population size parameter", N);
53  check_positive_finite(function,
54  "First prior sample size parameter", alpha);
55  check_positive_finite(function,
56  "Second prior sample size parameter", beta);
57  check_consistent_sizes(function,
58  "Successes variable", n,
59  "Population size parameter", N,
60  "First prior sample size parameter", alpha,
61  "Second prior sample size parameter", beta);
62 
63  // Wrap arguments in vector views
64  VectorView<const T_n> n_vec(n);
65  VectorView<const T_N> N_vec(N);
66  VectorView<const T_size1> alpha_vec(alpha);
67  VectorView<const T_size2> beta_vec(beta);
68  size_t size = max_size(n, N, alpha, beta);
69 
70  // Compute vectorized CDF and gradient
71  using stan::math::lgamma;
72  using stan::math::lbeta;
73  using stan::math::digamma;
74  using std::exp;
75  using std::exp;
76 
78  operands_and_partials(alpha, beta);
79 
80  // Explicit return for extreme values
81  // The gradients are technically ill-defined, but treated as zero
82  for (size_t i = 0; i < stan::length(n); i++) {
83  if (value_of(n_vec[i]) <= 0)
84  return operands_and_partials.value(0.0);
85  }
86 
87  for (size_t i = 0; i < size; i++) {
88  // Explicit results for extreme values
89  // The gradients are technically ill-defined, but treated as zero
90  if (value_of(n_vec[i]) >= value_of(N_vec[i])) {
91  continue;
92  }
93 
94  const T_partials_return n_dbl = value_of(n_vec[i]);
95  const T_partials_return N_dbl = value_of(N_vec[i]);
96  const T_partials_return alpha_dbl = value_of(alpha_vec[i]);
97  const T_partials_return beta_dbl = value_of(beta_vec[i]);
98 
99  const T_partials_return mu = alpha_dbl + n_dbl + 1;
100  const T_partials_return nu = beta_dbl + N_dbl - n_dbl - 1;
101 
102  const T_partials_return F = stan::math::F32((T_partials_return)1, mu,
103  -N_dbl + n_dbl + 1,
104  n_dbl + 2, 1 - nu,
105  (T_partials_return)1);
106 
107  T_partials_return C = lgamma(nu) - lgamma(N_dbl - n_dbl);
108  C += lgamma(mu) - lgamma(n_dbl + 2);
109  C += lgamma(N_dbl + 2) - lgamma(N_dbl + alpha_dbl + beta_dbl);
110  C = exp(C);
111 
112  C *= F / exp(lbeta(alpha_dbl, beta_dbl));
113  C /= N_dbl + 1;
114 
115  const T_partials_return Pi = 1 - C;
116 
117  P *= Pi;
118 
119  T_partials_return dF[6];
120  T_partials_return digammaOne = 0;
121  T_partials_return digammaTwo = 0;
122 
124  digammaOne = digamma(mu + nu);
125  digammaTwo = digamma(alpha_dbl + beta_dbl);
126  stan::math::grad_F32(dF, (T_partials_return)1, mu, -N_dbl + n_dbl + 1,
127  n_dbl + 2,
128  1 - nu, (T_partials_return)1);
129  }
131  const T_partials_return g
132  = - C * (digamma(mu) - digammaOne + dF[1] / F
133  - digamma(alpha_dbl) + digammaTwo);
134  operands_and_partials.d_x1[i]
135  += g / Pi;
136  }
138  const T_partials_return g
139  = - C * (digamma(nu) - digammaOne - dF[4] / F - digamma(beta_dbl)
140  + digammaTwo);
141  operands_and_partials.d_x2[i]
142  += g / Pi;
143  }
144  }
145 
147  for (size_t i = 0; i < stan::length(alpha); ++i)
148  operands_and_partials.d_x1[i] *= P;
149  }
151  for (size_t i = 0; i < stan::length(beta); ++i)
152  operands_and_partials.d_x2[i] *= P;
153  }
154 
155  return operands_and_partials.value(P);
156  }
157 
158  }
159 }
160 #endif
VectorView< T_return_type, false, true > d_x2
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
fvar< T > lbeta(const fvar< T > &x1, const fvar< T > &x2)
Definition: lbeta.hpp:16
T_return_type value(double value)
Returns a T_return_type with the value specified with the partial derivatves.
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
return_type< T_size1, T_size2 >::type beta_binomial_cdf(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
This class builds partial derivatives with respect to a set of operands.
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
void grad_F32(T *g, T a, T b, T c, T d, T e, T z, T precision=1e-6)
Definition: grad_F32.hpp:11
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
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.
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
T F32(T a, T b, T c, T d, T e, T z, T precision=1e-6)
Definition: F32.hpp:11
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.
VectorView< T_return_type, false, true > d_x1
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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