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