1 #ifndef STAN_MATH_PRIM_SCAL_PROB_SCALED_INV_CHI_SQUARE_LPDF_HPP 2 #define STAN_MATH_PRIM_SCAL_PROB_SCALED_INV_CHI_SQUARE_LPDF_HPP 39 template <
bool propto,
typename T_y,
typename T_dof,
typename T_scale>
41 const T_y& y,
const T_dof& nu,
const T_scale& s) {
42 static const char*
function =
"scaled_inv_chi_square_lpdf";
50 "Degrees of freedom parameter", nu,
"Scale parameter",
56 T_partials_return logp(0);
62 for (
size_t n = 0; n < N; n++) {
70 T_partials_return, T_dof>
72 for (
size_t i = 0; i <
length(nu); i++)
74 half_nu[i] = 0.5 *
value_of(nu_vec[i]);
79 for (
size_t i = 0; i <
length(y); i++)
84 T_partials_return, T_y>
86 for (
size_t i = 0; i <
length(y); i++)
91 T_partials_return, T_scale>
93 for (
size_t i = 0; i <
length(s); i++)
100 lgamma_half_nu(
length(nu));
102 digamma_half_nu_over_two(
length(nu));
103 for (
size_t i = 0; i <
length(nu); i++) {
105 lgamma_half_nu[i] =
lgamma(half_nu[i]);
107 log_half_nu[i] =
log(half_nu[i]);
109 digamma_half_nu_over_two[i] =
digamma(half_nu[i]) * 0.5;
113 for (
size_t n = 0; n < N; n++) {
114 const T_partials_return s_dbl =
value_of(s_vec[n]);
115 const T_partials_return nu_dbl =
value_of(nu_vec[n]);
117 logp += half_nu[n] * log_half_nu[n] - lgamma_half_nu[n];
119 logp += nu_dbl * log_s[n];
121 logp -= (half_nu[n] + 1.0) * log_y[n];
123 logp -= half_nu[n] * s_dbl * s_dbl * inv_y[n];
126 ops_partials.
edge1_.partials_[n]
127 += -(half_nu[n] + 1.0) * inv_y[n]
128 + half_nu[n] * s_dbl * s_dbl * inv_y[n] * inv_y[n];
131 ops_partials.
edge2_.partials_[n]
132 += 0.5 * log_half_nu[n] + 0.5 - digamma_half_nu_over_two[n] + log_s[n]
133 - 0.5 * log_y[n] - 0.5 * s_dbl * s_dbl * inv_y[n];
136 ops_partials.
edge3_.partials_[n]
137 += nu_dbl / s_dbl - nu_dbl * inv_y[n] * s_dbl;
140 return ops_partials.
build(logp);
143 template <
typename T_y,
typename T_dof,
typename T_scale>
146 return scaled_inv_chi_square_lpdf<false>(y, nu, s);
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)
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...
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.
return_type< T_y, T_dof, T_scale >::type scaled_inv_chi_square_lpdf(const T_y &y, const T_dof &nu, const T_scale &s)
The log of a scaled inverse chi-squared density for y with the specified degrees of freedom parameter...
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_
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.