Automatic Differentiation
 
Loading...
Searching...
No Matches
student_t_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_STUDENT_T_CDF_HPP
2#define STAN_MATH_PRIM_PROB_STUDENT_T_CDF_HPP
3
17#include <cmath>
18
19namespace stan {
20namespace math {
21
22template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
24 const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma) {
25 using T_partials_return = partials_return_t<T_y, T_dof, T_loc, T_scale>;
26 using T_y_ref = ref_type_t<T_y>;
27 using T_nu_ref = ref_type_t<T_dof>;
28 using T_mu_ref = ref_type_t<T_loc>;
29 using T_sigma_ref = ref_type_t<T_scale>;
30 using std::exp;
31 using std::pow;
32 static constexpr const char* function = "student_t_cdf";
33 T_y_ref y_ref = y;
34 T_nu_ref nu_ref = nu;
35 T_mu_ref mu_ref = mu;
36 T_sigma_ref sigma_ref = sigma;
37 check_not_nan(function, "Random variable", y_ref);
38 check_positive_finite(function, "Degrees of freedom parameter", nu_ref);
39 check_finite(function, "Location parameter", mu_ref);
40 check_positive_finite(function, "Scale parameter", sigma_ref);
41
42 if (size_zero(y, nu, mu, sigma)) {
43 return 1.0;
44 }
45
46 T_partials_return P(1.0);
47 auto ops_partials
48 = make_partials_propagator(y_ref, nu_ref, mu_ref, sigma_ref);
49 scalar_seq_view<T_y> y_vec(y_ref);
50 scalar_seq_view<T_nu_ref> nu_vec(nu_ref);
51 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
52 scalar_seq_view<T_sigma_ref> sigma_vec(sigma_ref);
53 size_t N = max_size(y, nu, mu, sigma);
54
55 // Explicit return for extreme values
56 // The gradients are technically ill-defined, but treated as zero
57 for (size_t i = 0; i < stan::math::size(y); i++) {
58 if (y_vec.val(i) == NEGATIVE_INFTY) {
59 return ops_partials.build(0.0);
60 }
61 }
62
63 T_partials_return digammaHalf = 0;
64
65 VectorBuilder<is_autodiff_v<T_dof>, T_partials_return, T_dof> digamma_vec(
66 math::size(nu));
67 VectorBuilder<is_autodiff_v<T_dof>, T_partials_return, T_dof> digammaNu_vec(
68 math::size(nu));
69 VectorBuilder<is_autodiff_v<T_dof>, T_partials_return, T_dof>
70 digammaNuPlusHalf_vec(math::size(nu));
71
72 if constexpr (is_autodiff_v<T_dof>) {
73 digammaHalf = digamma(0.5);
74
75 for (size_t i = 0; i < stan::math::size(nu); i++) {
76 const T_partials_return nu_dbl = nu_vec.val(i);
77
78 digammaNu_vec[i] = digamma(0.5 * nu_dbl);
79 digammaNuPlusHalf_vec[i] = digamma(0.5 + 0.5 * nu_dbl);
80 }
81 }
82
83 for (size_t n = 0; n < N; n++) {
84 // Explicit results for extreme values
85 // The gradients are technically ill-defined, but treated as zero
86 if (y_vec.val(n) == INFTY) {
87 continue;
88 }
89
90 const T_partials_return sigma_inv = 1.0 / sigma_vec.val(n);
91 const T_partials_return t = (y_vec.val(n) - mu_vec.val(n)) * sigma_inv;
92 const T_partials_return nu_dbl = nu_vec.val(n);
93 const T_partials_return q = nu_dbl / (t * t);
94 const T_partials_return r = 1.0 / (1.0 + q);
95 const T_partials_return J = 2 * r * r * q / t;
96 const T_partials_return betaNuHalf = beta(0.5, 0.5 * nu_dbl);
97 double zJacobian = t > 0 ? -0.5 : 0.5;
98
99 if (q < 2) {
100 T_partials_return z
101 = inc_beta(0.5 * nu_dbl, (T_partials_return)0.5, 1.0 - r);
102 const T_partials_return Pn = t > 0 ? 1.0 - 0.5 * z : 0.5 * z;
103 const T_partials_return d_ibeta
104 = pow(r, -0.5) * pow(1.0 - r, 0.5 * nu_dbl - 1) / betaNuHalf;
105
106 P *= Pn;
107
108 if constexpr (is_autodiff_v<T_y>) {
109 partials<0>(ops_partials)[n]
110 += -zJacobian * d_ibeta * J * sigma_inv / Pn;
111 }
112 if constexpr (is_autodiff_v<T_dof>) {
113 T_partials_return g1 = 0;
114 T_partials_return g2 = 0;
115
116 grad_reg_inc_beta(g1, g2, 0.5 * nu_dbl, (T_partials_return)0.5, 1.0 - r,
117 digammaNu_vec[n], digammaHalf,
118 digammaNuPlusHalf_vec[n], betaNuHalf);
119
120 partials<1>(ops_partials)[n]
121 += zJacobian * (d_ibeta * (r / t) * (r / t) + 0.5 * g1) / Pn;
122 }
123
124 if constexpr (is_autodiff_v<T_loc>) {
125 partials<2>(ops_partials)[n]
126 += zJacobian * d_ibeta * J * sigma_inv / Pn;
127 }
128 if constexpr (is_autodiff_v<T_scale>) {
129 partials<3>(ops_partials)[n]
130 += zJacobian * d_ibeta * J * sigma_inv * t / Pn;
131 }
132
133 } else {
134 T_partials_return z
135 = 1.0 - inc_beta((T_partials_return)0.5, 0.5 * nu_dbl, r);
136
137 zJacobian *= -1;
138
139 const T_partials_return Pn = t > 0 ? 1.0 - 0.5 * z : 0.5 * z;
140
141 T_partials_return d_ibeta
142 = pow(1.0 - r, 0.5 * nu_dbl - 1) * pow(r, -0.5) / betaNuHalf;
143
144 P *= Pn;
145
146 if constexpr (is_autodiff_v<T_y>) {
147 partials<0>(ops_partials)[n]
148 += zJacobian * d_ibeta * J * sigma_inv / Pn;
149 }
150 if constexpr (is_autodiff_v<T_dof>) {
151 T_partials_return g1 = 0;
152 T_partials_return g2 = 0;
153
154 grad_reg_inc_beta(g1, g2, (T_partials_return)0.5, 0.5 * nu_dbl, r,
155 digammaHalf, digammaNu_vec[n],
156 digammaNuPlusHalf_vec[n], betaNuHalf);
157
158 partials<1>(ops_partials)[n]
159 += zJacobian * (-d_ibeta * (r / t) * (r / t) + 0.5 * g2) / Pn;
160 }
161 if constexpr (is_autodiff_v<T_loc>) {
162 partials<2>(ops_partials)[n]
163 += -zJacobian * d_ibeta * J * sigma_inv / Pn;
164 }
165 if constexpr (is_autodiff_v<T_scale>) {
166 partials<3>(ops_partials)[n]
167 += -zJacobian * d_ibeta * J * sigma_inv * t / Pn;
168 }
169 }
170 }
171
172 if constexpr (is_autodiff_v<T_y>) {
173 for (size_t n = 0; n < stan::math::size(y); ++n) {
174 partials<0>(ops_partials)[n] *= P;
175 }
176 }
177 if constexpr (is_autodiff_v<T_dof>) {
178 for (size_t n = 0; n < stan::math::size(nu); ++n) {
179 partials<1>(ops_partials)[n] *= P;
180 }
181 }
182 if constexpr (is_autodiff_v<T_loc>) {
183 for (size_t n = 0; n < stan::math::size(mu); ++n) {
184 partials<2>(ops_partials)[n] *= P;
185 }
186 }
187 if constexpr (is_autodiff_v<T_scale>) {
188 for (size_t n = 0; n < stan::math::size(sigma); ++n) {
189 partials<3>(ops_partials)[n] *= P;
190 }
191 }
192 return ops_partials.build(P);
193}
194
195} // namespace math
196} // namespace stan
197#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...
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
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 grad_reg_inc_beta(T &g1, T &g2, const T &a, const T &b, const T &z, const T &digammaA, const T &digammaB, const T &digammaSum, const T &betaAB)
Computes the gradients of the regularized incomplete beta function.
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition inc_beta.hpp:19
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
return_type_t< T_y, T_dof, T_loc, T_scale > student_t_cdf(const T_y &y, const T_dof &nu, const T_loc &mu, const T_scale &sigma)
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
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 ...