Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
beta_cdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_CDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_CDF_HPP
3 
9 
15 
27 
28 #include <boost/math/special_functions/gamma.hpp>
29 #include <boost/random/gamma_distribution.hpp>
30 #include <boost/random/variate_generator.hpp>
31 
32 #include <cmath>
33 
34 namespace stan {
35 
36  namespace math {
37 
50  template <typename T_y, typename T_scale_succ, typename T_scale_fail>
51  typename return_type<T_y, T_scale_succ, T_scale_fail>::type
52  beta_cdf(const T_y& y, const T_scale_succ& alpha,
53  const T_scale_fail& beta) {
54  typedef typename stan::partials_return_type<T_y, T_scale_succ,
55  T_scale_fail>::type
56  T_partials_return;
57 
58  // Size checks
59  if (!(stan::length(y) && stan::length(alpha)
60  && stan::length(beta)))
61  return 1.0;
62 
63  // Error checks
64  static const char* function("stan::math::beta_cdf");
65  using boost::math::tools::promote_args;
66 
67  T_partials_return P(1.0);
68 
69  check_positive_finite(function, "First shape parameter", alpha);
70  check_positive_finite(function, "Second shape parameter", beta);
71  check_not_nan(function, "Random variable", y);
72  check_consistent_sizes(function,
73  "Random variable", y,
74  "First shape parameter", alpha,
75  "Second shape parameter", beta);
76  check_nonnegative(function, "Random variable", y);
77  check_less_or_equal(function, "Random variable", y, 1);
78 
79  // Wrap arguments in vectors
80  VectorView<const T_y> y_vec(y);
81  VectorView<const T_scale_succ> alpha_vec(alpha);
82  VectorView<const T_scale_fail> beta_vec(beta);
83  size_t N = max_size(y, alpha, beta);
84 
86  operands_and_partials(y, alpha, beta);
87 
88  // Explicit return for extreme values
89  // The gradients are technically ill-defined, but treated as zero
90  for (size_t i = 0; i < stan::length(y); i++) {
91  if (value_of(y_vec[i]) <= 0)
92  return operands_and_partials.to_var(0.0, y, alpha, beta);
93  }
94 
95  // Compute CDF and its gradients
96 
97  // Cache a few expensive function calls if alpha or beta is a parameter
99  T_scale_fail>::value,
100  T_partials_return, T_scale_succ, T_scale_fail>
101  digamma_alpha_vec(max_size(alpha, beta));
102 
104  T_scale_fail>::value,
105  T_partials_return, T_scale_succ, T_scale_fail>
106  digamma_beta_vec(max_size(alpha, beta));
107 
109  T_scale_fail>::value,
110  T_partials_return, T_scale_succ, T_scale_fail>
111  digamma_sum_vec(max_size(alpha, beta));
112 
114  for (size_t n = 0; n < N; n++) {
115  const T_partials_return alpha_dbl = value_of(alpha_vec[n]);
116  const T_partials_return beta_dbl = value_of(beta_vec[n]);
117 
118  digamma_alpha_vec[n] = digamma(alpha_dbl);
119  digamma_beta_vec[n] = digamma(beta_dbl);
120  digamma_sum_vec[n] = digamma(alpha_dbl + beta_dbl);
121  }
122  }
123 
124  // Compute vectorized CDF and gradient
125  for (size_t n = 0; n < N; n++) {
126  // Explicit results for extreme values
127  // The gradients are technically ill-defined, but treated as zero
128  if (value_of(y_vec[n]) >= 1.0) continue;
129 
130  // Pull out values
131  const T_partials_return y_dbl = value_of(y_vec[n]);
132  const T_partials_return alpha_dbl = value_of(alpha_vec[n]);
133  const T_partials_return beta_dbl = value_of(beta_vec[n]);
134 
135  // Compute
136  const T_partials_return Pn = inc_beta(alpha_dbl, beta_dbl, y_dbl);
137 
138  P *= Pn;
139 
141  operands_and_partials.d_x1[n]
142  += inc_beta_ddz(alpha_dbl, beta_dbl, y_dbl) / Pn;
143 
145  operands_and_partials.d_x2[n]
146  += inc_beta_dda(alpha_dbl, beta_dbl, y_dbl,
147  digamma_alpha_vec[n], digamma_sum_vec[n]) / Pn;
149  operands_and_partials.d_x3[n]
150  += inc_beta_ddb(alpha_dbl, beta_dbl, y_dbl,
151  digamma_beta_vec[n], digamma_sum_vec[n]) / Pn;
152  }
153 
155  for (size_t n = 0; n < stan::length(y); ++n)
156  operands_and_partials.d_x1[n] *= P;
157  }
159  for (size_t n = 0; n < stan::length(alpha); ++n)
160  operands_and_partials.d_x2[n] *= P;
161  }
163  for (size_t n = 0; n < stan::length(beta); ++n)
164  operands_and_partials.d_x3[n] *= P;
165  }
166 
167  return operands_and_partials.to_var(P, y, alpha, beta);
168  }
169 
170  }
171 }
172 #endif
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
T inc_beta_dda(T a, T b, T z, T digamma_a, T digamma_ab)
Returns the partial derivative of the regularized incomplete beta function, I_{z}(a, b) with respect to a.
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
T_return_type to_var(T_partials_return logp, const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
T inc_beta_ddb(T a, T b, T z, T digamma_b, T digamma_ab)
Returns the partial derivative of the regularized incomplete beta function, I_{z}(a, b) with respect to b.
T inc_beta_ddz(T a, T b, T z)
Returns the partial derivative of the regularized incomplete beta function, I_{z}(a, b) with respect to z.
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:20
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
return_type< T_y, T_scale_succ, T_scale_fail >::type beta_cdf(const T_y &y, const T_scale_succ &alpha, const T_scale_fail &beta)
Calculates the beta cumulative distribution function for the given variate and scale variables...
Definition: beta_cdf.hpp:52
A variable implementation that stores operands and derivatives with respect to the variable...
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
bool check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Return true if y is less or equal to high.
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.
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
bool check_nonnegative(const char *function, const char *name, const T_y &y)
Return true if y is non-negative.
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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