Automatic Differentiation
 
Loading...
Searching...
No Matches
chi_square_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_CHI_SQUARE_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_CHI_SQUARE_LCCDF_HPP
3
19#include <cmath>
20
21namespace stan {
22namespace math {
23
37template <typename T_y, typename T_dof>
38return_type_t<T_y, T_dof> chi_square_lccdf(const T_y& y, const T_dof& nu) {
39 using T_partials_return = partials_return_t<T_y, T_dof>;
40 using std::exp;
41 using std::log;
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 static constexpr const char* function = "chi_square_lccdf";
46 check_consistent_sizes(function, "Random variable", y,
47 "Degrees of freedom parameter", nu);
48 T_y_ref y_ref = y;
49 T_nu_ref nu_ref = nu;
50 check_not_nan(function, "Random variable", y_ref);
51 check_nonnegative(function, "Random variable", y_ref);
52 check_positive_finite(function, "Degrees of freedom parameter", nu_ref);
53
54 if (size_zero(y, nu)) {
55 return 0;
56 }
57
58 T_partials_return ccdf_log(0.0);
59 auto ops_partials = make_partials_propagator(y_ref, nu_ref);
60
61 scalar_seq_view<T_y_ref> y_vec(y_ref);
62 scalar_seq_view<T_nu_ref> nu_vec(nu_ref);
63 size_t N = max_size(y, nu);
64
65 // Explicit return for extreme values
66 // The gradients are technically ill-defined, but treated as zero
67 for (size_t i = 0; i < stan::math::size(y); i++) {
68 if (y_vec.val(i) == 0) {
69 return ops_partials.build(0.0);
70 }
71 }
72
73 VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
74 gamma_vec(math::size(nu));
75 VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
76 digamma_vec(math::size(nu));
77
79 for (size_t i = 0; i < stan::math::size(nu); i++) {
80 const T_partials_return alpha_dbl = nu_vec.val(i) * 0.5;
81 gamma_vec[i] = tgamma(alpha_dbl);
82 digamma_vec[i] = digamma(alpha_dbl);
83 }
84 }
85
86 for (size_t n = 0; n < N; n++) {
87 // Explicit results for extreme values
88 // The gradients are technically ill-defined, but treated as zero
89 if (y_vec.val(n) == INFTY) {
90 return ops_partials.build(negative_infinity());
91 }
92
93 const T_partials_return y_dbl = y_vec.val(n);
94 const T_partials_return alpha_dbl = nu_vec.val(n) * 0.5;
95 const T_partials_return beta_dbl = 0.5;
96
97 const T_partials_return Pn = gamma_q(alpha_dbl, beta_dbl * y_dbl);
98
99 ccdf_log += log(Pn);
100
102 partials<0>(ops_partials)[n] -= beta_dbl * exp(-beta_dbl * y_dbl)
103 * pow(beta_dbl * y_dbl, alpha_dbl - 1)
104 / tgamma(alpha_dbl) / Pn;
105 }
107 partials<1>(ops_partials)[n]
108 += 0.5
109 * grad_reg_inc_gamma(alpha_dbl, beta_dbl * y_dbl, gamma_vec[n],
110 digamma_vec[n])
111 / Pn;
112 }
113 }
114 return ops_partials.build(ccdf_log);
115}
116
117} // namespace math
118} // namespace stan
119#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 > chi_square_lccdf(const T_y &y, const T_dof &nu)
Returns the chi square log complementary cumulative distribution function for the given variate and d...
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
static constexpr double negative_infinity()
Return negative infinity.
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
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)
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
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: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...