Automatic Differentiation
 
Loading...
Searching...
No Matches
scaled_inv_chi_square_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_SCALED_INV_CHI_SQUARE_CDF_HPP
2#define STAN_MATH_PRIM_PROB_SCALED_INV_CHI_SQUARE_CDF_HPP
3
19#include <cmath>
20
21namespace stan {
22namespace math {
23
37template <typename T_y, typename T_dof, typename T_scale>
39 const T_y& y, const T_dof& nu, const T_scale& s) {
40 using T_partials_return = partials_return_t<T_y, T_dof, T_scale>;
41 using std::exp;
42 using std::pow;
43 using T_y_ref = ref_type_t<T_y>;
44 using T_nu_ref = ref_type_t<T_dof>;
45 using T_s_ref = ref_type_t<T_scale>;
46 static constexpr const char* function = "scaled_inv_chi_square_cdf";
47 check_consistent_sizes(function, "Random variable", y,
48 "Degrees of freedom parameter", nu, "Scale parameter",
49 s);
50 T_y_ref y_ref = y;
51 T_nu_ref nu_ref = nu;
52 T_s_ref s_ref = s;
53
54 check_nonnegative(function, "Random variable", y_ref);
55 check_positive_finite(function, "Degrees of freedom parameter", nu_ref);
56 check_positive_finite(function, "Scale parameter", s_ref);
57
58 if (size_zero(y, nu, s)) {
59 return 1.0;
60 }
61
62 T_partials_return P(1.0);
63 auto ops_partials = make_partials_propagator(y_ref, nu_ref, s_ref);
64
65 scalar_seq_view<T_y_ref> y_vec(y_ref);
66 scalar_seq_view<T_nu_ref> nu_vec(nu_ref);
67 scalar_seq_view<T_s_ref> s_vec(s_ref);
68 size_t N = max_size(y, nu, s);
69
70 // Explicit return for extreme values
71 // The gradients are technically ill-defined, but treated as zero
72 for (size_t i = 0; i < stan::math::size(y); i++) {
73 if (y_vec.val(i) == 0) {
74 return ops_partials.build(0.0);
75 }
76 }
77
78 VectorBuilder<is_autodiff_v<T_dof>, T_partials_return, T_dof> gamma_vec(
79 math::size(nu));
80 VectorBuilder<is_autodiff_v<T_dof>, T_partials_return, T_dof> digamma_vec(
81 math::size(nu));
82
83 if constexpr (is_autodiff_v<T_dof>) {
84 for (size_t i = 0; i < stan::math::size(nu); i++) {
85 const T_partials_return half_nu_dbl = 0.5 * nu_vec.val(i);
86 gamma_vec[i] = tgamma(half_nu_dbl);
87 digamma_vec[i] = digamma(half_nu_dbl);
88 }
89 }
90
91 for (size_t n = 0; n < N; n++) {
92 // Explicit results for extreme values
93 // The gradients are technically ill-defined, but treated as zero
94 if (y_vec.val(n) == INFTY) {
95 continue;
96 }
97
98 const T_partials_return y_dbl = y_vec.val(n);
99 const T_partials_return y_inv_dbl = 1.0 / y_dbl;
100 const T_partials_return half_nu_dbl = 0.5 * nu_vec.val(n);
101 const T_partials_return s_dbl = s_vec.val(n);
102 const T_partials_return half_s2_overx_dbl = 0.5 * s_dbl * s_dbl * y_inv_dbl;
103 const T_partials_return half_nu_s2_overx_dbl
104 = 2.0 * half_nu_dbl * half_s2_overx_dbl;
105
106 const T_partials_return Pn = gamma_q(half_nu_dbl, half_nu_s2_overx_dbl);
107 const T_partials_return gamma_p_deriv
108 = exp(-half_nu_s2_overx_dbl)
109 * pow(half_nu_s2_overx_dbl, half_nu_dbl - 1) / tgamma(half_nu_dbl);
110
111 P *= Pn;
112
113 if constexpr (is_autodiff_v<T_y>) {
114 partials<0>(ops_partials)[n]
115 += half_nu_s2_overx_dbl * y_inv_dbl * gamma_p_deriv / Pn;
116 }
117
118 if constexpr (is_autodiff_v<T_dof>) {
119 partials<1>(ops_partials)[n]
120 += (0.5
121 * grad_reg_inc_gamma(half_nu_dbl, half_nu_s2_overx_dbl,
122 gamma_vec[n], digamma_vec[n])
123 - half_s2_overx_dbl * gamma_p_deriv)
124 / Pn;
125 }
126
127 if constexpr (is_autodiff_v<T_scale>) {
128 partials<2>(ops_partials)[n]
129 += -2.0 * half_nu_dbl * s_dbl * y_inv_dbl * gamma_p_deriv / Pn;
130 }
131 }
132
133 if constexpr (is_autodiff_v<T_y>) {
134 for (size_t n = 0; n < stan::math::size(y); ++n) {
135 partials<0>(ops_partials)[n] *= P;
136 }
137 }
138 if constexpr (is_autodiff_v<T_dof>) {
139 for (size_t n = 0; n < stan::math::size(nu); ++n) {
140 partials<1>(ops_partials)[n] *= P;
141 }
142 }
143 if constexpr (is_autodiff_v<T_scale>) {
144 for (size_t n = 0; n < stan::math::size(s); ++n) {
145 partials<2>(ops_partials)[n] *= P;
146 }
147 }
148 return ops_partials.build(P);
149}
150
151} // namespace math
152} // namespace stan
153#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_dof, T_scale > scaled_inv_chi_square_cdf(const T_y &y, const T_dof &nu, const T_scale &s)
The CDF of a scaled inverse chi-squared density for y with the specified degrees of freedom parameter...
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:19
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: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 ...