Automatic Differentiation
 
Loading...
Searching...
No Matches
inv_gamma_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_INV_GAMMA_CDF_HPP
2#define STAN_MATH_PRIM_PROB_INV_GAMMA_CDF_HPP
3
18#include <cmath>
19
20namespace stan {
21namespace math {
22
39template <typename T_y, typename T_shape, typename T_scale>
41 const T_shape& alpha,
42 const T_scale& beta) {
43 using T_partials_return = partials_return_t<T_y, T_shape, T_scale>;
44 using T_y_ref = ref_type_t<T_y>;
45 using T_alpha_ref = ref_type_t<T_shape>;
46 using T_beta_ref = ref_type_t<T_scale>;
47 using std::exp;
48 using std::pow;
49 static constexpr const char* function = "inv_gamma_cdf";
50 check_consistent_sizes(function, "Random variable", y, "Shape parameter",
51 alpha, "Scale Parameter", beta);
52
53 T_y_ref y_ref = y;
54 T_alpha_ref alpha_ref = alpha;
55 T_beta_ref beta_ref = beta;
56
57 check_positive_finite(function, "Shape parameter", alpha_ref);
58 check_positive_finite(function, "Scale parameter", beta_ref);
59 check_nonnegative(function, "Random variable", y_ref);
60
61 if (size_zero(y, alpha, beta)) {
62 return 1.0;
63 }
64
65 T_partials_return P(1.0);
66 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, beta_ref);
67
68 scalar_seq_view<T_y_ref> y_vec(y_ref);
69 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
70 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
71 size_t N = max_size(y, alpha, beta);
72
73 // Explicit return for extreme values
74 // The gradients are technically ill-defined, but treated as zero
75 for (size_t i = 0; i < stan::math::size(y); i++) {
76 if (y_vec.val(i) == 0) {
77 return ops_partials.build(0.0);
78 }
79 }
80
81 VectorBuilder<!is_constant_all<T_shape>::value, T_partials_return, T_shape>
82 gamma_vec(math::size(alpha));
83 VectorBuilder<!is_constant_all<T_shape>::value, T_partials_return, T_shape>
84 digamma_vec(math::size(alpha));
85
87 for (size_t i = 0; i < stan::math::size(alpha); i++) {
88 const T_partials_return alpha_dbl = alpha_vec.val(i);
89 gamma_vec[i] = tgamma(alpha_dbl);
90 digamma_vec[i] = digamma(alpha_dbl);
91 }
92 }
93
94 for (size_t n = 0; n < N; n++) {
95 // Explicit results for extreme values
96 // The gradients are technically ill-defined, but treated as zero
97 if (y_vec.val(n) == INFTY) {
98 continue;
99 }
100
101 const T_partials_return y_dbl = y_vec.val(n);
102 const T_partials_return y_inv_dbl = 1.0 / y_dbl;
103 const T_partials_return alpha_dbl = alpha_vec.val(n);
104 const T_partials_return beta_dbl = beta_vec.val(n);
105
106 const T_partials_return Pn = gamma_q(alpha_dbl, beta_dbl * y_inv_dbl);
107
108 P *= Pn;
109
111 partials<0>(ops_partials)[n] += beta_dbl * y_inv_dbl * y_inv_dbl
112 * exp(-beta_dbl * y_inv_dbl)
113 * pow(beta_dbl * y_inv_dbl, alpha_dbl - 1)
114 / tgamma(alpha_dbl) / Pn;
115 }
117 partials<1>(ops_partials)[n]
118 += grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_inv_dbl, gamma_vec[n],
119 digamma_vec[n])
120 / Pn;
121 }
123 partials<2>(ops_partials)[n] += -y_inv_dbl * exp(-beta_dbl * y_inv_dbl)
124 * pow(beta_dbl * y_inv_dbl, alpha_dbl - 1)
125 / tgamma(alpha_dbl) / Pn;
126 }
127 }
128
130 for (size_t n = 0; n < stan::math::size(y); ++n) {
131 partials<0>(ops_partials)[n] *= P;
132 }
133 }
135 for (size_t n = 0; n < stan::math::size(alpha); ++n) {
136 partials<1>(ops_partials)[n] *= P;
137 }
138 }
140 for (size_t n = 0; n < stan::math::size(beta); ++n) {
141 partials<2>(ops_partials)[n] *= P;
142 }
143 }
144 return ops_partials.build(P);
145}
146
147} // namespace math
148} // namespace stan
149#endif
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_shape, T_scale > inv_gamma_cdf(const T_y &y, const T_shape &alpha, const T_scale &beta)
The CDF of an inverse gamma density for y with the specified shape and scale parameters.
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
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.
Definition size_zero.hpp:19
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
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.
Definition tgamma.hpp:21
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
Definition gamma_q.hpp:14
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
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.
Definition constants.hpp:46
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
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...