Automatic Differentiation
 
Loading...
Searching...
No Matches
gamma_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_GAMMA_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_GAMMA_LCCDF_HPP
3
24#include <cmath>
25#include <optional>
26
27namespace stan {
28namespace math {
29namespace internal {
30
34template <bool any_fvar, bool partials_fvar, typename T_shape, typename T1,
35 typename T2>
36inline std::optional<std::pair<return_type_t<T1, T2>, return_type_t<T1, T2>>>
37eval_q_cf(const T1& alpha, const T2& beta_y) {
38 using scalar_t = return_type_t<T1, T2>;
39 using ret_t = std::pair<scalar_t, scalar_t>;
40 if constexpr (!any_fvar && is_autodiff_v<T_shape>) {
41 std::pair<double, double> log_q_result
42 = log_gamma_q_dgamma(value_of(alpha), value_of(beta_y));
43 if (likely(std::isfinite(log_q_result.first))) {
44 return std::optional{log_q_result};
45 } else {
46 return std::optional<ret_t>{std::nullopt};
47 }
48 } else {
49 ret_t out{internal::log_q_gamma_cf(alpha, beta_y), 0.0};
50 if (unlikely(!std::isfinite(value_of_rec(out.first)))) {
51 return std::optional<ret_t>{std::nullopt};
52 }
53 if constexpr (is_autodiff_v<T_shape>) {
54 if constexpr (!partials_fvar) {
55 out.second
56 = grad_reg_inc_gamma(alpha, beta_y, tgamma(alpha), digamma(alpha))
57 / exp(out.first);
58 } else {
59 auto alpha_unit = alpha;
60 alpha_unit.d_ = 1;
61 auto beta_y_unit = beta_y;
62 beta_y_unit.d_ = 0;
63 auto log_Q_fvar = internal::log_q_gamma_cf(alpha_unit, beta_y_unit);
64 out.second = log_Q_fvar.d_;
65 }
66 }
67 return std::optional{out};
68 }
69}
70
74template <bool partials_fvar, typename T_shape, typename T1, typename T2>
75inline std::optional<std::pair<return_type_t<T1, T2>, return_type_t<T1, T2>>>
76eval_q_log1m(const T1& alpha, const T2& beta_y) {
77 using scalar_t = return_type_t<T1, T2>;
78 using ret_t = std::pair<scalar_t, scalar_t>;
79 ret_t out{log1m(gamma_p(alpha, beta_y)), 0.0};
80 if (unlikely(!std::isfinite(value_of_rec(out.first)))) {
81 return std::optional<ret_t>{std::nullopt};
82 }
83 if constexpr (is_autodiff_v<T_shape>) {
84 if constexpr (partials_fvar) {
85 auto alpha_unit = alpha;
86 alpha_unit.d_ = 1;
87 auto beta_unit = beta_y;
88 beta_unit.d_ = 0;
89 auto log_Q_fvar = log1m(gamma_p(alpha_unit, beta_unit));
90 out.second = log_Q_fvar.d_;
91 } else {
92 out.second = -grad_reg_lower_inc_gamma(alpha, beta_y) / exp(out.first);
93 }
94 }
95 return std::optional{out};
96}
97} // namespace internal
98
99template <typename T_y, typename T_shape, typename T_inv_scale>
101 const T_y& y, const T_shape& alpha, const T_inv_scale& beta) {
102 using std::exp;
103 using std::log;
104 using T_partials_return = partials_return_t<T_y, T_shape, T_inv_scale>;
105 using T_y_ref = ref_type_t<T_y>;
106 using T_alpha_ref = ref_type_t<T_shape>;
107 using T_beta_ref = ref_type_t<T_inv_scale>;
108 static constexpr const char* function = "gamma_lccdf";
109 check_consistent_sizes(function, "Random variable", y, "Shape parameter",
110 alpha, "Inverse scale parameter", beta);
111 T_y_ref y_ref = y;
112 T_alpha_ref alpha_ref = alpha;
113 T_beta_ref beta_ref = beta;
114 check_positive_finite(function, "Shape parameter", alpha_ref);
115 check_positive_finite(function, "Inverse scale parameter", beta_ref);
116 check_nonnegative(function, "Random variable", y_ref);
117
118 if (size_zero(y, alpha, beta)) {
119 return 0;
120 }
121
122 T_partials_return P(0.0);
123 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, beta_ref);
124
125 scalar_seq_view<T_y_ref> y_vec(y_ref);
126 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
127 scalar_seq_view<T_beta_ref> beta_vec(beta_ref);
128 const size_t N = max_size(y, alpha, beta);
129
130 constexpr bool is_y_fvar = is_fvar_v<scalar_type_t<T_y>>;
131 constexpr bool is_shape_fvar = is_fvar_v<scalar_type_t<T_shape>>;
132 constexpr bool is_beta_fvar = is_fvar_v<scalar_type_t<T_inv_scale>>;
133 constexpr bool any_fvar = is_y_fvar || is_shape_fvar || is_beta_fvar;
134 constexpr bool partials_fvar = is_fvar_v<T_partials_return>;
135
136 for (size_t n = 0; n < N; n++) {
137 // Explicit results for extreme values
138 // The gradients are technically ill-defined, but treated as zero
139 const T_partials_return y_val = y_vec.val(n);
140 if (y_val == 0.0) {
141 continue;
142 }
143 if (y_val == INFTY) {
144 return ops_partials.build(negative_infinity());
145 }
146
147 const T_partials_return alpha_val = alpha_vec.val(n);
148 const T_partials_return beta_val = beta_vec.val(n);
149
150 const T_partials_return beta_y = beta_val * y_val;
151 if (beta_y == INFTY) {
152 return ops_partials.build(negative_infinity());
153 }
154 std::optional<std::pair<T_partials_return, T_partials_return>> result;
155 if (beta_y > alpha_val + 1.0) {
156 result = internal::eval_q_cf<any_fvar, partials_fvar, T_shape>(alpha_val,
157 beta_y);
158 } else {
159 result
160 = internal::eval_q_log1m<partials_fvar, T_shape>(alpha_val, beta_y);
161 if (!result && beta_y > 0.0) {
162 // Fallback to continued fraction if log1m fails
163 result = internal::eval_q_cf<any_fvar, partials_fvar, T_shape>(
164 alpha_val, beta_y);
165 }
166 }
167 if (unlikely(!result)) {
168 return ops_partials.build(negative_infinity());
169 }
170
171 P += result->first;
172
173 if constexpr (is_autodiff_v<T_y> || is_autodiff_v<T_inv_scale>) {
174 const T_partials_return log_y = log(y_val);
175 const T_partials_return alpha_minus_one = fma(alpha_val, log_y, -log_y);
176
177 const T_partials_return log_pdf = alpha_val * log(beta_val)
178 - lgamma(alpha_val) + alpha_minus_one
179 - beta_y;
180
181 const T_partials_return hazard = exp(log_pdf - result->first); // f/Q
182
183 if constexpr (is_autodiff_v<T_y>) {
184 partials<0>(ops_partials)[n] -= hazard;
185 }
186 if constexpr (is_autodiff_v<T_inv_scale>) {
187 partials<2>(ops_partials)[n] -= (y_val / beta_val) * hazard;
188 }
189 }
190 if constexpr (is_autodiff_v<T_shape>) {
191 partials<1>(ops_partials)[n] += result->second;
192 }
193 }
194 return ops_partials.build(P);
195}
196
197} // namespace math
198} // namespace stan
199
200#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
#define likely(x)
#define unlikely(x)
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
std::optional< std::pair< return_type_t< T1, T2 >, return_type_t< T1, T2 > > > eval_q_log1m(const T1 &alpha, const T2 &beta_y)
Computes log q and d(log q) / d(alpha) using log1m.
return_type_t< T_a, T_z > log_q_gamma_cf(const T_a &a, const T_z &z, double precision=LOG_Q_GAMMA_CF_PRECISION, int max_steps=250)
Compute log(Q(a,z)) using continued fraction expansion for upper incomplete gamma function.
std::optional< std::pair< return_type_t< T1, T2 >, return_type_t< T1, T2 > > > eval_q_cf(const T1 &alpha, const T2 &beta_y)
Computes log q and d(log q) / d(alpha) using continued fraction.
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
static constexpr double negative_infinity()
Return negative infinity.
std::pair< return_type_t< T_a, T_z >, return_type_t< T_a, T_z > > log_gamma_q_dgamma(const T_a &a, const T_z &z, double precision=internal::LOG_Q_GAMMA_CF_PRECISION, int max_steps=250)
Compute log(Q(a,z)) and its gradient with respect to a using continued fraction expansion,...
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
return_type_t< T_y, T_shape, T_inv_scale > gamma_lccdf(const T_y &y, const T_shape &alpha, const T_inv_scale &beta)
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 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 > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition gamma_p.hpp:18
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 > log1m(const fvar< T > &x)
Definition log1m.hpp:12
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
return_type_t< T1, T2 > grad_reg_lower_inc_gamma(const T1 &a, const T2 &z, double precision=1e-10, int max_steps=1e5)
Computes the gradient of the lower regularized incomplete gamma function.
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< return_type_t< T1, T2, T3 > > fma(const fvar< T1 > &x1, const fvar< T2 > &x2, const fvar< T3 > &x3)
The fused multiply-add operation (C99).
Definition fma.hpp:60
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:15
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 ...