Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_LCDF_HPP
3
18
19#include <cmath>
20
21namespace stan {
22namespace math {
23
31template <typename T_y, typename T_scale_succ, typename T_scale_fail>
33 const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta_param) {
35 using T_y_ref = ref_type_t<T_y>;
36 using T_alpha_ref = ref_type_t<T_scale_succ>;
37 using T_beta_ref = ref_type_t<T_scale_fail>;
38 static constexpr const char* function = "beta_lcdf";
39
40 check_consistent_sizes(function, "Random variable", y,
41 "First shape parameter", alpha,
42 "Second shape parameter", beta_param);
43 if (size_zero(y, alpha, beta_param)) {
44 return 0;
45 }
46
47 T_y_ref y_ref = y;
48 T_alpha_ref alpha_ref = alpha;
49 T_beta_ref beta_ref = beta_param;
50 check_positive_finite(function, "First shape parameter", alpha_ref);
51 check_positive_finite(function, "Second shape parameter", beta_ref);
52 check_bounded(function, "Random variable", value_of(y_ref), 0, 1);
53
54 T_partials_return cdf_log(0.0);
55 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, beta_ref);
56
57 scalar_seq_view<T_y_ref> y_vec(y_ref);
58 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
59 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
60
61 const size_t size_alpha = stan::math::size(alpha);
62 const size_t size_beta = stan::math::size(beta_param);
63 const size_t size_alpha_beta = max_size(alpha, beta_param);
64 const size_t N = max_size(y, alpha, beta_param);
65
66 // Allocate digamma buffers only if alpha/beta contain any autodiff scalars.
67 constexpr bool need_digamma = is_any_autodiff_v<T_scale_succ, T_scale_fail>;
69 size_alpha);
71 size_beta);
73 digamma_sum(size_alpha_beta);
74
75 if constexpr (is_any_autodiff_v<T_scale_succ, T_scale_fail>) {
76 for (size_t i = 0; i < size_alpha; ++i) {
77 digamma_alpha[i] = digamma(alpha_vec.val(i));
78 }
79 for (size_t i = 0; i < size_beta; ++i) {
80 digamma_beta[i] = digamma(beta_vec.val(i));
81 }
82 for (size_t i = 0; i < size_alpha_beta; ++i) {
83 digamma_sum[i] = digamma(alpha_vec.val(i) + beta_vec.val(i));
84 }
85 }
86
87 for (size_t n = 0; n < N; ++n) {
88 const T_partials_return y_dbl = y_vec.val(n);
89 const T_partials_return alpha_dbl = alpha_vec.val(n);
90 const T_partials_return beta_dbl = beta_vec.val(n);
91 const T_partials_return betafunc_dbl = beta(alpha_dbl, beta_dbl);
92 const T_partials_return Pn = inc_beta(alpha_dbl, beta_dbl, y_dbl);
93
94 const T_partials_return inv_Pn
95 = is_any_autodiff_v<T_y, T_scale_succ, T_scale_fail> ? inv(Pn) : 0;
96
97 cdf_log += log(Pn);
98
99 if constexpr (is_any_autodiff_v<T_y>) {
100 partials<0>(ops_partials)[n] += pow(1 - y_dbl, beta_dbl - 1)
101 * pow(y_dbl, alpha_dbl - 1) * inv_Pn
102 / betafunc_dbl;
103 }
104
105 if constexpr (is_any_autodiff_v<T_scale_succ, T_scale_fail>) {
106 T_partials_return g1 = 0;
107 T_partials_return g2 = 0;
108 grad_reg_inc_beta(g1, g2, alpha_dbl, beta_dbl, y_dbl, digamma_alpha[n],
109 digamma_beta[n], digamma_sum[n], betafunc_dbl);
110 if constexpr (is_any_autodiff_v<T_scale_succ>) {
111 partials<1>(ops_partials)[n] += g1 * inv_Pn;
112 }
113 if constexpr (is_any_autodiff_v<T_scale_fail>) {
114 partials<2>(ops_partials)[n] += g2 * inv_Pn;
115 }
116 }
117 }
118
119 return ops_partials.build(cdf_log);
120}
121
122} // namespace math
123} // namespace stan
124#endif // STAN_MATH_PRIM_PROB_BETA_LCDF_HPP
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
void grad_reg_inc_beta(T &g1, T &g2, const T &a, const T &b, const T &z, const T &digammaA, const T &digammaB, const T &digammaSum, const T &betaAB)
Computes the gradients of the regularized incomplete beta function.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition inc_beta.hpp:19
return_type_t< T_y, T_scale_succ, T_scale_fail > beta_lcdf(const T_y &y, const T_scale_succ &alpha, const T_scale_fail &beta_param)
Beta log CDF.
Definition beta_lcdf.hpp:32
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:13
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:56
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...