Stan Math Library  2.11.0
reverse mode automatic differentiation
beta_binomial_cdf_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_CDF_LOG_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_BINOMIAL_CDF_LOG_HPP
3 
21 #include <cmath>
22 
23 namespace stan {
24 
25  namespace math {
26 
27  template <typename T_n, typename T_N,
28  typename T_size1, typename T_size2>
29  typename return_type<T_size1, T_size2>::type
30  beta_binomial_cdf_log(const T_n& n, const T_N& N, const T_size1& alpha,
31  const T_size2& beta) {
32  static const char* function("stan::math::beta_binomial_cdf_log");
33  typedef typename stan::partials_return_type<T_n, T_N, T_size1,
34  T_size2>::type
35  T_partials_return;
36 
42 
43  // Ensure non-zero argument lengths
44  if (!(stan::length(n) && stan::length(N) && stan::length(alpha)
45  && stan::length(beta)))
46  return 0.0;
47 
48  T_partials_return P(0.0);
49 
50  // Validate arguments
51  check_nonnegative(function, "Population size parameter", N);
52  check_positive_finite(function,
53  "First prior sample size parameter", alpha);
54  check_positive_finite(function,
55  "Second prior sample size parameter", beta);
56  check_consistent_sizes(function,
57  "Successes variable", n,
58  "Population size parameter", N,
59  "First prior sample size parameter", alpha,
60  "Second prior sample size parameter", beta);
61 
62  // Wrap arguments in vector views
63  VectorView<const T_n> n_vec(n);
64  VectorView<const T_N> N_vec(N);
65  VectorView<const T_size1> alpha_vec(alpha);
66  VectorView<const T_size2> beta_vec(beta);
67  size_t size = max_size(n, N, alpha, beta);
68 
69  // Compute vectorized cdf_log and gradient
70  using stan::math::lgamma;
71  using stan::math::digamma;
72  using stan::math::lbeta;
73  using std::exp;
74  using std::log;
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 neg infinity
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(stan::math::negative_infinity());
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 += log(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, 1 - nu, (T_partials_return)1);
128  }
130  const T_partials_return g
131  = - C * (digamma(mu) - digammaOne + dF[1] / F
132  - digamma(alpha_dbl) + digammaTwo);
133  operands_and_partials.d_x1[i] += g / Pi;
134  }
136  const T_partials_return g
137  = - C * (digamma(nu) - digammaOne - dF[4] / F - digamma(beta_dbl)
138  + digammaTwo);
139  operands_and_partials.d_x2[i] += g / Pi;
140  }
141  }
142 
143  return operands_and_partials.value(P);
144  }
145 
146  }
147 }
148 #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
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
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
return_type< T_size1, T_size2 >::type beta_binomial_cdf_log(const T_n &n, const T_N &N, const T_size1 &alpha, const T_size2 &beta)
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...
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
double negative_infinity()
Return negative infinity.
Definition: constants.hpp:132
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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