1 #ifndef STAN_MATH_PRIM_SCAL_PROB_STUDENT_T_LPDF_HPP 2 #define STAN_MATH_PRIM_SCAL_PROB_STUDENT_T_LPDF_HPP 48 template <
bool propto,
typename T_y,
typename T_dof,
typename T_loc,
51 const T_y& y,
const T_dof& nu,
const T_loc& mu,
const T_scale& sigma) {
52 static const char*
function =
"student_t_lpdf";
59 T_partials_return logp(0.0);
66 "Degrees of freedom parameter", nu,
67 "Location parameter", mu,
"Scale parameter", sigma);
76 size_t N =
max_size(y, nu, mu, sigma);
81 T_partials_return, T_dof>
83 for (
size_t i = 0; i <
length(nu); i++)
85 half_nu[i] = 0.5 *
value_of(nu_vec[i]);
88 lgamma_half_nu(
length(nu));
90 lgamma_half_nu_plus_half(
length(nu));
92 for (
size_t i = 0; i <
length(nu); i++) {
93 lgamma_half_nu[i] =
lgamma(half_nu[i]);
94 lgamma_half_nu_plus_half[i] =
lgamma(half_nu[i] + 0.5);
99 digamma_half_nu(
length(nu));
101 digamma_half_nu_plus_half(
length(nu));
103 for (
size_t i = 0; i <
length(nu); i++) {
104 digamma_half_nu[i] =
digamma(half_nu[i]);
105 digamma_half_nu_plus_half[i] =
digamma(half_nu[i] + 0.5);
111 for (
size_t i = 0; i <
length(nu); i++)
118 for (
size_t i = 0; i <
length(sigma); i++)
123 T_partials_return, T_y, T_dof, T_loc, T_scale>
124 square_y_minus_mu_over_sigma__over_nu(N);
127 T_partials_return, T_y, T_dof, T_loc, T_scale>
130 for (
size_t i = 0; i < N; i++)
132 const T_partials_return y_dbl =
value_of(y_vec[i]);
133 const T_partials_return mu_dbl =
value_of(mu_vec[i]);
134 const T_partials_return sigma_dbl =
value_of(sigma_vec[i]);
135 const T_partials_return nu_dbl =
value_of(nu_vec[i]);
136 square_y_minus_mu_over_sigma__over_nu[i]
137 =
square((y_dbl - mu_dbl) / sigma_dbl) / nu_dbl;
138 log1p_exp[i] =
log1p(square_y_minus_mu_over_sigma__over_nu[i]);
143 for (
size_t n = 0; n < N; n++) {
144 const T_partials_return y_dbl =
value_of(y_vec[n]);
145 const T_partials_return mu_dbl =
value_of(mu_vec[n]);
146 const T_partials_return sigma_dbl =
value_of(sigma_vec[n]);
147 const T_partials_return nu_dbl =
value_of(nu_vec[n]);
151 logp += lgamma_half_nu_plus_half[n] - lgamma_half_nu[n] - 0.5 * log_nu[n];
153 logp -= log_sigma[n];
155 logp -= (half_nu[n] + 0.5) * log1p_exp[n];
158 ops_partials.
edge1_.partials_[n]
159 += -(half_nu[n] + 0.5) * 1.0
160 / (1.0 + square_y_minus_mu_over_sigma__over_nu[n])
161 * (2.0 * (y_dbl - mu_dbl) /
square(sigma_dbl) / nu_dbl);
164 const T_partials_return inv_nu = 1.0 / nu_dbl;
165 ops_partials.
edge2_.partials_[n]
166 += 0.5 * digamma_half_nu_plus_half[n] - 0.5 * digamma_half_nu[n]
167 - 0.5 * inv_nu - 0.5 * log1p_exp[n]
169 * (1.0 / (1.0 + square_y_minus_mu_over_sigma__over_nu[n])
170 * square_y_minus_mu_over_sigma__over_nu[n] * inv_nu);
173 ops_partials.
edge3_.partials_[n]
174 -= (half_nu[n] + 0.5)
175 / (1.0 + square_y_minus_mu_over_sigma__over_nu[n])
176 * (2.0 * (mu_dbl - y_dbl) / (sigma_dbl * sigma_dbl * nu_dbl));
179 const T_partials_return inv_sigma = 1.0 / sigma_dbl;
180 ops_partials.
edge4_.partials_[n]
182 + (nu_dbl + 1.0) / (1.0 + square_y_minus_mu_over_sigma__over_nu[n])
183 * (square_y_minus_mu_over_sigma__over_nu[n] * inv_sigma);
186 return ops_partials.
build(logp);
189 template <
typename T_y,
typename T_dof,
typename T_loc,
typename T_scale>
191 const T_y& y,
const T_dof& nu,
const T_loc& mu,
const T_scale& sigma) {
192 return student_t_lpdf<false>(y, nu, mu, sigma);
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
fvar< T > log(const fvar< T > &x)
internal::ops_partials_edge< double, Op4 > edge4_
const double NEG_LOG_SQRT_PI
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
fvar< T > square(const fvar< T > &x)
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
size_t max_size(const T1 &x1, const T2 &x2)
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
VectorBuilder allocates type T1 values to be used as intermediate values.
internal::ops_partials_edge< double, Op2 > edge2_
fvar< T > log1p_exp(const fvar< T > &x)
return_type< T_y, T_dof, T_loc, T_scale >::type student_t_lpdf(const T_y &y, const T_dof &nu, const T_loc &mu, const T_scale &sigma)
The log of the Student-t density for the given y, nu, mean, and scale parameter.
fvar< T > log1p(const fvar< T > &x)
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.