1#ifndef STAN_MATH_PRIM_PROB_BETA_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_LCDF_HPP
40template <
typename T_y,
typename T_scale_succ,
typename T_scale_fail>
42 const T_y& y,
const T_scale_succ& alpha,
const T_scale_fail& beta_param) {
50 static constexpr const char* function =
"beta_lcdf";
52 "First shape parameter", alpha,
53 "Second shape parameter", beta_param);
59 T_alpha_ref alpha_ref = alpha;
60 T_beta_ref beta_ref = beta_param;
65 T_partials_return cdf_log(0.0);
72 size_t size_alpha_beta =
max_size(alpha, beta_param);
73 size_t N =
max_size(y, alpha, beta_param);
76 T_partials_return, T_scale_succ>
77 digamma_alpha(size_alpha);
79 T_partials_return, T_scale_fail>
80 digamma_beta(size_beta);
82 T_partials_return, T_scale_succ, T_scale_fail>
83 digamma_sum(size_alpha_beta);
86 for (
size_t i = 0; i < size_alpha; i++) {
87 digamma_alpha[i] =
digamma(alpha_vec.val(i));
89 for (
size_t i = 0; i < size_beta; i++) {
90 digamma_beta[i] =
digamma(beta_vec.val(i));
92 for (
size_t i = 0; i < size_alpha_beta; i++) {
93 digamma_sum[i] =
digamma(alpha_vec.val(i) + beta_vec.val(i));
97 for (
size_t n = 0; n < N; n++) {
98 const T_partials_return y_dbl = y_vec.val(n);
99 const T_partials_return alpha_dbl = alpha_vec.val(n);
100 const T_partials_return beta_dbl = beta_vec.val(n);
101 const T_partials_return betafunc_dbl =
beta(alpha_dbl, beta_dbl);
102 const T_partials_return Pn =
inc_beta(alpha_dbl, beta_dbl, y_dbl);
103 const T_partials_return inv_Pn
109 partials<0>(ops_partials)[n] +=
pow(1 - y_dbl, beta_dbl - 1)
110 *
pow(y_dbl, alpha_dbl - 1) * inv_Pn
114 T_partials_return g1 = 0;
115 T_partials_return g2 = 0;
119 digamma_beta[n], digamma_sum[n], betafunc_dbl);
122 partials<1>(ops_partials)[n] += g1 * inv_Pn;
125 partials<2>(ops_partials)[n] += g2 * inv_Pn;
129 return ops_partials.build(cdf_log);
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...
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)
Returns the beta log cumulative distribution function for the given probability, success,...
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>>.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
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)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
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)
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
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.
fvar< T > inv(const fvar< T > &x)
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.
typename ref_type_if< true, T >::type ref_type_t
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 ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...