1#ifndef STAN_MATH_PRIM_PROB_STUDENT_T_CDF_HPP
2#define STAN_MATH_PRIM_PROB_STUDENT_T_CDF_HPP
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) {
32 static constexpr const char* function =
"student_t_cdf";
36 T_sigma_ref sigma_ref = sigma;
46 T_partials_return P(1.0);
53 size_t N =
max_size(y, nu, mu, sigma);
59 return ops_partials.build(0.0);
63 T_partials_return digammaHalf = 0;
72 if constexpr (is_autodiff_v<T_dof>) {
76 const T_partials_return nu_dbl = nu_vec.val(i);
78 digammaNu_vec[i] =
digamma(0.5 * nu_dbl);
79 digammaNuPlusHalf_vec[i] =
digamma(0.5 + 0.5 * nu_dbl);
83 for (
size_t n = 0; n < N; n++) {
86 if (y_vec.val(n) ==
INFTY) {
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;
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;
108 if constexpr (is_autodiff_v<T_y>) {
109 partials<0>(ops_partials)[n]
110 += -zJacobian * d_ibeta * J * sigma_inv / Pn;
112 if constexpr (is_autodiff_v<T_dof>) {
113 T_partials_return g1 = 0;
114 T_partials_return g2 = 0;
117 digammaNu_vec[n], digammaHalf,
118 digammaNuPlusHalf_vec[n], betaNuHalf);
120 partials<1>(ops_partials)[n]
121 += zJacobian * (d_ibeta * (r / t) * (r / t) + 0.5 * g1) / Pn;
124 if constexpr (is_autodiff_v<T_loc>) {
125 partials<2>(ops_partials)[n]
126 += zJacobian * d_ibeta * J * sigma_inv / Pn;
128 if constexpr (is_autodiff_v<T_scale>) {
129 partials<3>(ops_partials)[n]
130 += zJacobian * d_ibeta * J * sigma_inv * t / Pn;
135 = 1.0 -
inc_beta((T_partials_return)0.5, 0.5 * nu_dbl, r);
139 const T_partials_return Pn = t > 0 ? 1.0 - 0.5 * z : 0.5 * z;
141 T_partials_return d_ibeta
142 =
pow(1.0 - r, 0.5 * nu_dbl - 1) *
pow(r, -0.5) / betaNuHalf;
146 if constexpr (is_autodiff_v<T_y>) {
147 partials<0>(ops_partials)[n]
148 += zJacobian * d_ibeta * J * sigma_inv / Pn;
150 if constexpr (is_autodiff_v<T_dof>) {
151 T_partials_return g1 = 0;
152 T_partials_return g2 = 0;
155 digammaHalf, digammaNu_vec[n],
156 digammaNuPlusHalf_vec[n], betaNuHalf);
158 partials<1>(ops_partials)[n]
159 += zJacobian * (-d_ibeta * (r / t) * (r / t) + 0.5 * g2) / Pn;
161 if constexpr (is_autodiff_v<T_loc>) {
162 partials<2>(ops_partials)[n]
163 += -zJacobian * d_ibeta * J * sigma_inv / Pn;
165 if constexpr (is_autodiff_v<T_scale>) {
166 partials<3>(ops_partials)[n]
167 += -zJacobian * d_ibeta * J * sigma_inv * t / Pn;
172 if constexpr (is_autodiff_v<T_y>) {
174 partials<0>(ops_partials)[n] *= P;
177 if constexpr (is_autodiff_v<T_dof>) {
179 partials<1>(ops_partials)[n] *= P;
182 if constexpr (is_autodiff_v<T_loc>) {
184 partials<2>(ops_partials)[n] *= P;
187 if constexpr (is_autodiff_v<T_scale>) {
189 partials<3>(ops_partials)[n] *= P;
192 return ops_partials.build(P);
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>>.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
auto pow(const T1 &x1, const T2 &x2)
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.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
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.
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.
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.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
typename ref_type_if< true, T >::type ref_type_t
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 ...