1#ifndef STAN_MATH_PRIM_PROB_INV_GAMMA_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_INV_GAMMA_LCDF_HPP
24template <
typename T_y,
typename T_shape,
typename T_scale>
26 const T_y& y,
const T_shape& alpha,
const T_scale&
beta) {
34 static constexpr const char* function =
"inv_gamma_lcdf";
36 alpha,
"Scale Parameter",
beta);
39 T_alpha_ref alpha_ref = alpha;
40 T_beta_ref beta_ref =
beta;
50 T_partials_return P(0.0);
61 if (y_vec.val(i) == 0) {
71 if constexpr (is_autodiff_v<T_shape>) {
73 const T_partials_return alpha_dbl = alpha_vec.val(i);
74 gamma_vec[i] =
tgamma(alpha_dbl);
75 digamma_vec[i] =
digamma(alpha_dbl);
79 for (
size_t n = 0; n < N; n++) {
82 if (y_vec.val(n) ==
INFTY) {
86 const T_partials_return y_dbl = y_vec.val(n);
87 const T_partials_return y_inv_dbl = 1.0 / y_dbl;
88 const T_partials_return alpha_dbl = alpha_vec.val(n);
89 const T_partials_return beta_dbl = beta_vec.val(n);
91 const T_partials_return Pn =
gamma_q(alpha_dbl, beta_dbl * y_inv_dbl);
95 if constexpr (is_autodiff_v<T_y>) {
96 partials<0>(ops_partials)[n] += beta_dbl * y_inv_dbl * y_inv_dbl
97 *
exp(-beta_dbl * y_inv_dbl)
98 *
pow(beta_dbl * y_inv_dbl, alpha_dbl - 1)
101 if constexpr (is_autodiff_v<T_shape>) {
102 partials<1>(ops_partials)[n]
107 if constexpr (is_autodiff_v<T_scale>) {
108 partials<2>(ops_partials)[n] += -y_inv_dbl *
exp(-beta_dbl * y_inv_dbl)
109 *
pow(beta_dbl * y_inv_dbl, alpha_dbl - 1)
113 return ops_partials.build(P);
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>>.
static constexpr double negative_infinity()
Return negative infinity.
return_type_t< T_y, T_shape, T_scale > inv_gamma_lcdf(const T_y &y, const T_shape &alpha, const T_scale &beta)
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
auto pow(const T1 &x1, const T2 &x2)
fvar< T > log(const fvar< T > &x)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
return_type_t< T1, T2 > grad_reg_inc_gamma(T1 a, T2 z, T1 g, T1 dig, double precision=1e-6, int max_steps=1e5)
Gradient of the regularized incomplete gamma functions igamma(a, z)
fvar< T > tgamma(const fvar< T > &x)
Return the result of applying the gamma function to the specified argument.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
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.
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.
static constexpr double INFTY
Positive infinity.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
fvar< T > exp(const fvar< T > &x)
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 ...