1#ifndef STAN_MATH_PRIM_PROB_STUDENT_T_LCCDF_HPP
2#define STAN_MATH_PRIM_PROB_STUDENT_T_LCCDF_HPP
23template <
typename T_y,
typename T_dof,
typename T_loc,
typename T_scale>
25 const T_y& y,
const T_dof& nu,
const T_loc& mu,
const T_scale& sigma) {
34 static constexpr const char* function =
"student_t_lccdf";
38 T_sigma_ref sigma_ref = sigma;
48 T_partials_return P(0.0);
55 size_t N =
max_size(y, nu, mu, sigma);
61 return ops_partials.build(0.0);
65 T_partials_return digammaHalf = 0;
78 const T_partials_return nu_dbl = nu_vec.val(i);
80 digammaNu_vec[i] =
digamma(0.5 * nu_dbl);
81 digammaNuPlusHalf_vec[i] =
digamma(0.5 + 0.5 * nu_dbl);
85 for (
size_t n = 0; n < N; n++) {
88 if (y_vec.val(n) ==
INFTY) {
92 const T_partials_return sigma_inv = 1.0 / sigma_vec.val(n);
93 const T_partials_return t = (y_vec.val(n) - mu_vec.val(n)) * sigma_inv;
94 const T_partials_return nu_dbl = nu_vec.val(n);
95 const T_partials_return q = nu_dbl / (t * t);
96 const T_partials_return r = 1.0 / (1.0 + q);
97 const T_partials_return J = 2 * r * r * q / t;
98 const T_partials_return betaNuHalf =
beta(0.5, 0.5 * nu_dbl);
99 T_partials_return zJacobian = t > 0 ? -0.5 : 0.5;
103 =
inc_beta(0.5 * nu_dbl, (T_partials_return)0.5, 1.0 - r);
104 const T_partials_return Pn = t > 0 ? 0.5 * z : 1.0 - 0.5 * z;
105 const T_partials_return d_ibeta
106 =
pow(r, -0.5) *
pow(1.0 - r, 0.5 * nu_dbl - 1) / betaNuHalf;
111 partials<0>(ops_partials)[n]
112 += zJacobian * d_ibeta * J * sigma_inv / Pn;
116 T_partials_return g1 = 0;
117 T_partials_return g2 = 0;
120 digammaNu_vec[n], digammaHalf,
121 digammaNuPlusHalf_vec[n], betaNuHalf);
123 partials<1>(ops_partials)[n]
124 -= zJacobian * (d_ibeta * (r / t) * (r / t) + 0.5 * g1) / Pn;
128 partials<2>(ops_partials)[n]
129 -= zJacobian * d_ibeta * J * sigma_inv / Pn;
132 partials<3>(ops_partials)[n]
133 -= zJacobian * d_ibeta * J * sigma_inv * t / Pn;
138 = 1.0 -
inc_beta((T_partials_return)0.5, 0.5 * nu_dbl, r);
141 const T_partials_return Pn = t > 0 ? 0.5 * z : 1.0 - 0.5 * z;
143 T_partials_return d_ibeta
144 =
pow(1.0 - r, 0.5 * nu_dbl - 1) *
pow(r, -0.5) / betaNuHalf;
149 partials<0>(ops_partials)[n]
150 -= zJacobian * d_ibeta * J * sigma_inv / Pn;
154 T_partials_return g1 = 0;
155 T_partials_return g2 = 0;
158 digammaHalf, digammaNu_vec[n],
159 digammaNuPlusHalf_vec[n], betaNuHalf);
161 partials<1>(ops_partials)[n]
162 -= zJacobian * (-d_ibeta * (r / t) * (r / t) + 0.5 * g2) / Pn;
166 partials<2>(ops_partials)[n]
167 += zJacobian * d_ibeta * J * sigma_inv / Pn;
170 partials<3>(ops_partials)[n]
171 += zJacobian * d_ibeta * J * sigma_inv * t / Pn;
175 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>>.
static constexpr double negative_infinity()
Return negative infinity.
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)
fvar< T > log(const fvar< T > &x)
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.
return_type_t< T_y, T_dof, T_loc, T_scale > student_t_lccdf(const T_y &y, const T_dof &nu, const T_loc &mu, const T_scale &sigma)
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
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 ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...