1 #ifndef STAN_MATH_PRIM_PROB_STUDENT_T_LPDF_HPP
2 #define STAN_MATH_PRIM_PROB_STUDENT_T_LPDF_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/as_column_vector_or_scalar.hpp>
7 #include <stan/math/prim/fun/as_array_or_scalar.hpp>
8 #include <stan/math/prim/fun/as_value_column_array_or_scalar.hpp>
9 #include <stan/math/prim/fun/constants.hpp>
10 #include <stan/math/prim/fun/digamma.hpp>
11 #include <stan/math/prim/fun/lgamma.hpp>
12 #include <stan/math/prim/fun/log.hpp>
13 #include <stan/math/prim/fun/log1p.hpp>
14 #include <stan/math/prim/fun/max_size.hpp>
15 #include <stan/math/prim/fun/size.hpp>
16 #include <stan/math/prim/fun/size_zero.hpp>
17 #include <stan/math/prim/fun/square.hpp>
18 #include <stan/math/prim/fun/to_ref.hpp>
19 #include <stan/math/prim/fun/value_of.hpp>
20 #include <stan/math/prim/functor/operands_and_partials.hpp>
21 #include <cmath>
22 
23 namespace stan {
24 namespace math {
25 
26 /** \ingroup prob_dists
27  * The log of the Student-t density for the given y, nu, mean, and
28  * scale parameter.  The scale parameter must be greater
29  * than 0.
30  *
31  * \f{eqnarray*}{
32  y &\sim& t_{\nu} (\mu, \sigma^2) \\
33  \log (p (y \, |\, \nu, \mu, \sigma) ) &=& \log \left( \frac{\Gamma((\nu + 1)
34  /2)}
35  {\Gamma(\nu/2)\sqrt{\nu \pi} \sigma} \left( 1 + \frac{1}{\nu} (\frac{y -
36  \mu}{\sigma})^2 \right)^{-(\nu + 1)/2} \right) \\
37  &=& \log( \Gamma( (\nu+1)/2 )) - \log (\Gamma (\nu/2) - \frac{1}{2} \log(\nu
38  \pi) - \log(\sigma)
39  -\frac{\nu + 1}{2} \log (1 + \frac{1}{\nu} (\frac{y - \mu}{\sigma})^2)
40  \f}
41  *
42  * @tparam T_y type of scalar
43  * @tparam T_dof type of degrees of freedom
44  * @tparam T_loc type of location
45  * @tparam T_scale type of scale
46  *
47  * @param y A scalar variable.
48  * @param nu Degrees of freedom.
49  * @param mu The mean of the Student-t distribution.
50  * @param sigma The scale parameter of the Student-t distribution.
51  * @return The log of the Student-t density at y.
52  * @throw std::domain_error if sigma is not greater than 0.
53  * @throw std::domain_error if nu is not greater than 0.
54  */
55 template <bool propto, typename T_y, typename T_dof, typename T_loc,
56           typename T_scale,
57           require_all_not_nonscalar_prim_or_rev_kernel_expression_t<
58               T_y, T_dof, T_loc, T_scale>* = nullptr>
student_t_lpdf(const T_y & y,const T_dof & nu,const T_loc & mu,const T_scale & sigma)59 return_type_t<T_y, T_dof, T_loc, T_scale> student_t_lpdf(const T_y& y,
60                                                          const T_dof& nu,
61                                                          const T_loc& mu,
62                                                          const T_scale& sigma) {
63   using T_partials_return = partials_return_t<T_y, T_dof, T_loc, T_scale>;
64   using T_y_ref = ref_type_if_t<!is_constant<T_y>::value, T_y>;
65   using T_nu_ref = ref_type_if_t<!is_constant<T_dof>::value, T_dof>;
66   using T_mu_ref = ref_type_if_t<!is_constant<T_loc>::value, T_loc>;
67   using T_sigma_ref = ref_type_if_t<!is_constant<T_scale>::value, T_scale>;
68   static const char* function = "student_t_lpdf";
69   check_consistent_sizes(function, "Random variable", y,
70                          "Degrees of freedom parameter", nu,
71                          "Location parameter", mu, "Scale parameter", sigma);
72   T_y_ref y_ref = y;
73   T_mu_ref mu_ref = mu;
74   T_nu_ref nu_ref = nu;
75   T_sigma_ref sigma_ref = sigma;
76 
77   decltype(auto) y_val = to_ref(as_value_column_array_or_scalar(y_ref));
78   decltype(auto) nu_val = to_ref(as_value_column_array_or_scalar(nu_ref));
79   decltype(auto) mu_val = to_ref(as_value_column_array_or_scalar(mu_ref));
80   decltype(auto) sigma_val = to_ref(as_value_column_array_or_scalar(sigma_ref));
81 
82   check_not_nan(function, "Random variable", y_val);
83   check_positive_finite(function, "Degrees of freedom parameter", nu_val);
84   check_finite(function, "Location parameter", mu_val);
85   check_positive_finite(function, "Scale parameter", sigma_val);
86 
87   if (size_zero(y, nu, mu, sigma)) {
88     return 0.0;
89   }
90   if (!include_summand<propto, T_y, T_dof, T_loc, T_scale>::value) {
91     return 0.0;
92   }
93 
94   operands_and_partials<T_y_ref, T_nu_ref, T_mu_ref, T_sigma_ref> ops_partials(
95       y_ref, nu_ref, mu_ref, sigma_ref);
96 
97   const auto& half_nu
98       = to_ref_if<include_summand<propto, T_dof>::value>(0.5 * nu_val);
99   const auto& square_y_scaled = square((y_val - mu_val) / sigma_val);
100   const auto& square_y_scaled_over_nu
101       = to_ref_if<!is_constant_all<T_y, T_dof, T_loc, T_scale>::value>(
102           square_y_scaled / nu_val);
103   const auto& log1p_val = to_ref_if<!is_constant_all<T_dof>::value>(
104       log1p(square_y_scaled_over_nu));
105 
106   size_t N = max_size(y, nu, mu, sigma);
107   T_partials_return logp = -sum((half_nu + 0.5) * log1p_val);
108   if (include_summand<propto>::value) {
109     logp -= LOG_SQRT_PI * N;
110   }
111   if (include_summand<propto, T_dof>::value) {
112     logp += (sum(lgamma(half_nu + 0.5)) - sum(lgamma(half_nu))
113              - 0.5 * sum(log(nu_val)))
114             * N / size(nu);
115   }
116   if (include_summand<propto, T_scale>::value) {
117     logp -= sum(log(sigma_val)) * N / size(sigma);
118   }
119 
120   if (!is_constant_all<T_y, T_loc>::value) {
121     const auto& square_sigma = square(sigma_val);
122     auto deriv_y_mu = to_ref_if<(!is_constant_all<T_y>::value
123                                  && !is_constant_all<T_loc>::value)>(
124         (nu_val + 1) * (y_val - mu_val)
125         / ((1 + square_y_scaled_over_nu) * square_sigma * nu_val));
126     if (!is_constant_all<T_y>::value) {
127       ops_partials.edge1_.partials_ = -deriv_y_mu;
128     }
129     if (!is_constant_all<T_loc>::value) {
130       ops_partials.edge3_.partials_ = std::move(deriv_y_mu);
131     }
132   }
133   if (!is_constant_all<T_dof, T_scale>::value) {
134     const auto& rep_deriv = to_ref_if<(!is_constant_all<T_dof>::value
135                                        && !is_constant_all<T_scale>::value)>(
136         (nu_val + 1) * square_y_scaled_over_nu / (1 + square_y_scaled_over_nu)
137         - 1);
138     if (!is_constant_all<T_dof>::value) {
139       const auto& digamma_half_nu_plus_half = digamma(half_nu + 0.5);
140       const auto& digamma_half_nu = digamma(half_nu);
141       ops_partials.edge2_.partials_
142           = 0.5
143             * (digamma_half_nu_plus_half - digamma_half_nu - log1p_val
144                + rep_deriv / nu_val);
145     }
146     if (!is_constant_all<T_scale>::value) {
147       ops_partials.edge4_.partials_ = rep_deriv / sigma_val;
148     }
149   }
150   return ops_partials.build(logp);
151 }
152 
153 template <typename T_y, typename T_dof, typename T_loc, typename T_scale>
student_t_lpdf(const T_y & y,const T_dof & nu,const T_loc & mu,const T_scale & sigma)154 inline return_type_t<T_y, T_dof, T_loc, T_scale> student_t_lpdf(
155     const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma) {
156   return student_t_lpdf<false>(y, nu, mu, sigma);
157 }
158 
159 }  // namespace math
160 }  // namespace stan
161 #endif
162