Automatic Differentiation
 
Loading...
Searching...
No Matches
inv_chi_square_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_INV_CHI_SQUARE_CDF_HPP
2#define STAN_MATH_PRIM_PROB_INV_CHI_SQUARE_CDF_HPP
3
18#include <cmath>
19
20namespace stan {
21namespace math {
22
36template <typename T_y, typename T_dof>
37return_type_t<T_y, T_dof> inv_chi_square_cdf(const T_y& y, const T_dof& nu) {
38 using T_partials_return = partials_return_t<T_y, T_dof>;
39 using std::exp;
40 using std::pow;
41 using T_y_ref = ref_type_t<T_y>;
42 using T_nu_ref = ref_type_t<T_dof>;
43 static constexpr const char* function = "inv_chi_square_cdf";
44 check_consistent_sizes(function, "Random variable", y,
45 "Degrees of freedom parameter", nu);
46
47 T_y_ref y_ref = y;
48 T_nu_ref nu_ref = nu;
49 check_positive_finite(function, "Degrees of freedom parameter", nu_ref);
50 check_nonnegative(function, "Random variable", y_ref);
51
52 if (size_zero(y, nu)) {
53 return 1.0;
54 }
55
56 T_partials_return P(1.0);
57 auto ops_partials = make_partials_propagator(y_ref, nu_ref);
58
59 scalar_seq_view<T_y_ref> y_vec(y_ref);
60 scalar_seq_view<T_nu_ref> nu_vec(nu_ref);
61 size_t N = max_size(y, nu);
62
63 // Explicit return for extreme values
64 // The gradients are technically ill-defined, but treated as zero
65 for (size_t i = 0; i < stan::math::size(y); i++) {
66 if (y_vec.val(i) == 0) {
67 return ops_partials.build(0.0);
68 }
69 }
70
71 VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
72 gamma_vec(math::size(nu));
73 VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
74 digamma_vec(math::size(nu));
75
77 for (size_t i = 0; i < stan::math::size(nu); i++) {
78 const T_partials_return nu_dbl = nu_vec.val(i);
79 gamma_vec[i] = tgamma(0.5 * nu_dbl);
80 digamma_vec[i] = digamma(0.5 * nu_dbl);
81 }
82 }
83
84 for (size_t n = 0; n < N; n++) {
85 // Explicit results for extreme values
86 // The gradients are technically ill-defined, but treated as zero
87 if (y_vec.val(n) == INFTY) {
88 continue;
89 }
90
91 const T_partials_return y_dbl = y_vec.val(n);
92 const T_partials_return y_inv_dbl = 1.0 / y_dbl;
93 const T_partials_return nu_dbl = nu_vec.val(n);
94
95 const T_partials_return Pn = gamma_q(0.5 * nu_dbl, 0.5 * y_inv_dbl);
96
97 P *= Pn;
98
100 partials<0>(ops_partials)[n] += 0.5 * y_inv_dbl * y_inv_dbl
101 * exp(-0.5 * y_inv_dbl)
102 * pow(0.5 * y_inv_dbl, 0.5 * nu_dbl - 1)
103 / tgamma(0.5 * nu_dbl) / Pn;
104 }
106 partials<1>(ops_partials)[n]
107 += 0.5
108 * grad_reg_inc_gamma(0.5 * nu_dbl, 0.5 * y_inv_dbl, gamma_vec[n],
109 digamma_vec[n])
110 / Pn;
111 }
112 }
113
115 for (size_t n = 0; n < stan::math::size(y); ++n) {
116 partials<0>(ops_partials)[n] *= P;
117 }
118 }
120 for (size_t n = 0; n < stan::math::size(nu); ++n) {
121 partials<1>(ops_partials)[n] *= P;
122 }
123 }
124 return ops_partials.build(P);
125}
126
127} // namespace math
128} // namespace stan
129#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 > inv_chi_square_cdf(const T_y &y, const T_dof &nu)
Returns the inverse chi square cumulative distribution function for the given variate and degrees of ...
size_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:18
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_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
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 > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition pow.hpp:19
fvar< T > tgamma(const fvar< T > &x)
Return the result of applying the gamma function to the specified argument.
Definition tgamma.hpp:21
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
Definition gamma_q.hpp:14
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 ...
Definition fvar.hpp:9
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...