1 #ifndef STAN_MATH_PRIM_PROB_STUDENT_T_CDF_HPP
2 #define STAN_MATH_PRIM_PROB_STUDENT_T_CDF_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/beta.hpp>
7 #include <stan/math/prim/fun/constants.hpp>
8 #include <stan/math/prim/fun/digamma.hpp>
9 #include <stan/math/prim/fun/grad_reg_inc_beta.hpp>
10 #include <stan/math/prim/fun/inc_beta.hpp>
11 #include <stan/math/prim/fun/max_size.hpp>
12 #include <stan/math/prim/fun/scalar_seq_view.hpp>
13 #include <stan/math/prim/fun/size.hpp>
14 #include <stan/math/prim/fun/size_zero.hpp>
15 #include <stan/math/prim/fun/value_of.hpp>
16 #include <stan/math/prim/functor/operands_and_partials.hpp>
17 #include <cmath>
18 
19 namespace stan {
20 namespace math {
21 
22 template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
student_t_cdf(const T_y & y,const T_dof & nu,const T_loc & mu,const T_scale & sigma)23 return_type_t<T_y, T_dof, T_loc, T_scale> student_t_cdf(const T_y& y,
24                                                         const T_dof& nu,
25                                                         const T_loc& mu,
26                                                         const T_scale& sigma) {
27   using T_partials_return = partials_return_t<T_y, T_dof, T_loc, T_scale>;
28   using T_y_ref = ref_type_t<T_y>;
29   using T_nu_ref = ref_type_t<T_dof>;
30   using T_mu_ref = ref_type_t<T_loc>;
31   using T_sigma_ref = ref_type_t<T_scale>;
32   using std::exp;
33   using std::pow;
34   static const char* function = "student_t_cdf";
35   T_y_ref y_ref = y;
36   T_nu_ref nu_ref = nu;
37   T_mu_ref mu_ref = mu;
38   T_sigma_ref sigma_ref = sigma;
39   check_not_nan(function, "Random variable", y_ref);
40   check_positive_finite(function, "Degrees of freedom parameter", nu_ref);
41   check_finite(function, "Location parameter", mu_ref);
42   check_positive_finite(function, "Scale parameter", sigma_ref);
43 
44   if (size_zero(y, nu, mu, sigma)) {
45     return 1.0;
46   }
47 
48   T_partials_return P(1.0);
49   operands_and_partials<T_y_ref, T_nu_ref, T_mu_ref, T_sigma_ref> ops_partials(
50       y_ref, nu_ref, mu_ref, sigma_ref);
51   scalar_seq_view<T_y> y_vec(y_ref);
52   scalar_seq_view<T_nu_ref> nu_vec(nu_ref);
53   scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
54   scalar_seq_view<T_sigma_ref> sigma_vec(sigma_ref);
55   size_t N = max_size(y, nu, mu, sigma);
56 
57   // Explicit return for extreme values
58   // The gradients are technically ill-defined, but treated as zero
59   for (size_t i = 0; i < stan::math::size(y); i++) {
60     if (y_vec.val(i) == NEGATIVE_INFTY) {
61       return ops_partials.build(0.0);
62     }
63   }
64 
65   T_partials_return digammaHalf = 0;
66 
67   VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
68       digamma_vec(size(nu));
69   VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
70       digammaNu_vec(size(nu));
71   VectorBuilder<!is_constant_all<T_dof>::value, T_partials_return, T_dof>
72       digammaNuPlusHalf_vec(size(nu));
73 
74   if (!is_constant_all<T_dof>::value) {
75     digammaHalf = digamma(0.5);
76 
77     for (size_t i = 0; i < stan::math::size(nu); i++) {
78       const T_partials_return nu_dbl = nu_vec.val(i);
79 
80       digammaNu_vec[i] = digamma(0.5 * nu_dbl);
81       digammaNuPlusHalf_vec[i] = digamma(0.5 + 0.5 * nu_dbl);
82     }
83   }
84 
85   for (size_t n = 0; n < N; n++) {
86     // Explicit results for extreme values
87     // The gradients are technically ill-defined, but treated as zero
88     if (y_vec.val(n) == INFTY) {
89       continue;
90     }
91 
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     double zJacobian = t > 0 ? -0.5 : 0.5;
100 
101     if (q < 2) {
102       T_partials_return z
103           = inc_beta(0.5 * nu_dbl, (T_partials_return)0.5, 1.0 - r);
104       const T_partials_return Pn = t > 0 ? 1.0 - 0.5 * z : 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;
107 
108       P *= Pn;
109 
110       if (!is_constant_all<T_y>::value) {
111         ops_partials.edge1_.partials_[n]
112             += -zJacobian * d_ibeta * J * sigma_inv / Pn;
113       }
114       if (!is_constant_all<T_dof>::value) {
115         T_partials_return g1 = 0;
116         T_partials_return g2 = 0;
117 
118         grad_reg_inc_beta(g1, g2, 0.5 * nu_dbl, (T_partials_return)0.5, 1.0 - r,
119                           digammaNu_vec[n], digammaHalf,
120                           digammaNuPlusHalf_vec[n], betaNuHalf);
121 
122         ops_partials.edge2_.partials_[n]
123             += zJacobian * (d_ibeta * (r / t) * (r / t) + 0.5 * g1) / Pn;
124       }
125 
126       if (!is_constant_all<T_loc>::value) {
127         ops_partials.edge3_.partials_[n]
128             += zJacobian * d_ibeta * J * sigma_inv / Pn;
129       }
130       if (!is_constant_all<T_scale>::value) {
131         ops_partials.edge4_.partials_[n]
132             += zJacobian * d_ibeta * J * sigma_inv * t / Pn;
133       }
134 
135     } else {
136       T_partials_return z
137           = 1.0 - inc_beta((T_partials_return)0.5, 0.5 * nu_dbl, r);
138 
139       zJacobian *= -1;
140 
141       const T_partials_return Pn = t > 0 ? 1.0 - 0.5 * z : 0.5 * z;
142 
143       T_partials_return d_ibeta
144           = pow(1.0 - r, 0.5 * nu_dbl - 1) * pow(r, -0.5) / betaNuHalf;
145 
146       P *= Pn;
147 
148       if (!is_constant_all<T_y>::value) {
149         ops_partials.edge1_.partials_[n]
150             += zJacobian * d_ibeta * J * sigma_inv / Pn;
151       }
152       if (!is_constant_all<T_dof>::value) {
153         T_partials_return g1 = 0;
154         T_partials_return g2 = 0;
155 
156         grad_reg_inc_beta(g1, g2, (T_partials_return)0.5, 0.5 * nu_dbl, r,
157                           digammaHalf, digammaNu_vec[n],
158                           digammaNuPlusHalf_vec[n], betaNuHalf);
159 
160         ops_partials.edge2_.partials_[n]
161             += zJacobian * (-d_ibeta * (r / t) * (r / t) + 0.5 * g2) / Pn;
162       }
163       if (!is_constant_all<T_loc>::value) {
164         ops_partials.edge3_.partials_[n]
165             += -zJacobian * d_ibeta * J * sigma_inv / Pn;
166       }
167       if (!is_constant_all<T_scale>::value) {
168         ops_partials.edge4_.partials_[n]
169             += -zJacobian * d_ibeta * J * sigma_inv * t / Pn;
170       }
171     }
172   }
173 
174   if (!is_constant_all<T_y>::value) {
175     for (size_t n = 0; n < stan::math::size(y); ++n) {
176       ops_partials.edge1_.partials_[n] *= P;
177     }
178   }
179   if (!is_constant_all<T_dof>::value) {
180     for (size_t n = 0; n < stan::math::size(nu); ++n) {
181       ops_partials.edge2_.partials_[n] *= P;
182     }
183   }
184   if (!is_constant_all<T_loc>::value) {
185     for (size_t n = 0; n < stan::math::size(mu); ++n) {
186       ops_partials.edge3_.partials_[n] *= P;
187     }
188   }
189   if (!is_constant_all<T_scale>::value) {
190     for (size_t n = 0; n < stan::math::size(sigma); ++n) {
191       ops_partials.edge4_.partials_[n] *= P;
192     }
193   }
194   return ops_partials.build(P);
195 }
196 
197 }  // namespace math
198 }  // namespace stan
199 #endif
200