Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
17 #include <cmath>
18 
19 namespace stan {
20 namespace math {
21 
34 template <typename T_y, typename T_scale_succ, typename T_scale_fail>
36  const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
37  typedef
39  T_partials_return;
40 
41  if (size_zero(y, alpha, beta))
42  return 1.0;
43 
44  static const char* function = "beta_cdf";
45 
46  T_partials_return P(1.0);
47 
48  check_positive_finite(function, "First shape parameter", alpha);
49  check_positive_finite(function, "Second shape parameter", beta);
50  check_not_nan(function, "Random variable", y);
51  check_consistent_sizes(function, "Random variable", y,
52  "First shape parameter", alpha,
53  "Second shape parameter", beta);
54  check_nonnegative(function, "Random variable", y);
55  check_less_or_equal(function, "Random variable", y, 1);
56 
57  scalar_seq_view<T_y> y_vec(y);
58  scalar_seq_view<T_scale_succ> alpha_vec(alpha);
59  scalar_seq_view<T_scale_fail> beta_vec(beta);
60  size_t N = max_size(y, alpha, beta);
61 
63  beta);
64 
65  // Explicit return for extreme values
66  // The gradients are technically ill-defined, but treated as zero
67  for (size_t i = 0; i < stan::length(y); i++) {
68  if (value_of(y_vec[i]) <= 0)
69  return ops_partials.build(0.0);
70  }
71 
73  T_partials_return, T_scale_succ, T_scale_fail>
74  digamma_alpha_vec(max_size(alpha, beta));
75 
77  T_partials_return, T_scale_succ, T_scale_fail>
78  digamma_beta_vec(max_size(alpha, beta));
79 
81  T_partials_return, T_scale_succ, T_scale_fail>
82  digamma_sum_vec(max_size(alpha, beta));
83 
85  for (size_t n = 0; n < N; n++) {
86  const T_partials_return alpha_dbl = value_of(alpha_vec[n]);
87  const T_partials_return beta_dbl = value_of(beta_vec[n]);
88 
89  digamma_alpha_vec[n] = digamma(alpha_dbl);
90  digamma_beta_vec[n] = digamma(beta_dbl);
91  digamma_sum_vec[n] = digamma(alpha_dbl + beta_dbl);
92  }
93  }
94 
95  for (size_t n = 0; n < N; n++) {
96  // Explicit results for extreme values
97  // The gradients are technically ill-defined, but treated as zero
98  if (value_of(y_vec[n]) >= 1.0)
99  continue;
100 
101  const T_partials_return y_dbl = value_of(y_vec[n]);
102  const T_partials_return alpha_dbl = value_of(alpha_vec[n]);
103  const T_partials_return beta_dbl = value_of(beta_vec[n]);
104 
105  const T_partials_return Pn = inc_beta(alpha_dbl, beta_dbl, y_dbl);
106 
107  P *= Pn;
108 
110  ops_partials.edge1_.partials_[n]
111  += inc_beta_ddz(alpha_dbl, beta_dbl, y_dbl) / Pn;
112 
114  ops_partials.edge2_.partials_[n]
115  += inc_beta_dda(alpha_dbl, beta_dbl, y_dbl, digamma_alpha_vec[n],
116  digamma_sum_vec[n])
117  / Pn;
119  ops_partials.edge3_.partials_[n]
120  += inc_beta_ddb(alpha_dbl, beta_dbl, y_dbl, digamma_beta_vec[n],
121  digamma_sum_vec[n])
122  / Pn;
123  }
124 
126  for (size_t n = 0; n < stan::length(y); ++n)
127  ops_partials.edge1_.partials_[n] *= P;
128  }
130  for (size_t n = 0; n < stan::length(alpha); ++n)
131  ops_partials.edge2_.partials_[n] *= P;
132  }
134  for (size_t n = 0; n < stan::length(beta); ++n)
135  ops_partials.edge3_.partials_[n] *= P;
136  }
137 
138  return ops_partials.build(P);
139 }
140 
141 } // namespace math
142 } // namespace stan
143 #endif
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Check if y is less or equal to high.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
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.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
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.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:18
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition: beta.hpp:51
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
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:35
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
VectorBuilder allocates type T1 values to be used as intermediate values.
internal::ops_partials_edge< double, Op2 > edge2_
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition: digamma.hpp:23

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