1 #ifndef __STAN__PROB__DIST__UNI__CONTINUOUS__INV_CHI_SQUARE_HPP__
2 #define __STAN__PROB__DIST__UNI__CONTINUOUS__INV_CHI_SQUARE_HPP__
35 template <
bool propto,
36 typename T_y,
typename T_dof,
38 typename return_type<T_y,T_dof>::type
41 static const char*
function =
"stan::prob::inv_chi_square_log(%1%)";
55 if (!
check_finite(
function, nu,
"Degrees of freedom parameter", &logp, Policy()))
57 if (!
check_positive(
function, nu,
"Degrees of freedom parameter", &logp, Policy()))
59 if (!
check_not_nan(
function, y,
"Random variable", &logp, Policy()))
64 "Random variable",
"Degrees of freedom parameter",
74 for (
size_t n = 0; n <
length(y); n++)
80 for (
size_t n = 0; n < N; n++) {
82 logp += nu_vec[n] * NEG_LOG_TWO_OVER_TWO -
lgamma(0.5 * nu_vec[n]);
86 logp -= 0.5 / y_vec[n];
91 template <
bool propto,
92 typename T_y,
typename T_dof>
99 template <
typename T_y,
typename T_dof,
105 return inv_chi_square_log<false>(y,nu,Policy());
109 template <
typename T_y,
typename T_dof>
116 template <
typename T_y,
typename T_dof,
class Policy>
124 static const char*
function =
"stan::prob::inv_chi_square_cdf(%1%)";
131 using boost::math::tools::promote_args;
135 if (!
check_finite(
function, nu,
"Degrees of freedom parameter", &P, Policy()))
138 if (!
check_positive(
function, nu,
"Degrees of freedom parameter", &P, Policy()))
141 if (!
check_not_nan(
function, y,
"Random variable", &P, Policy()))
144 if (!
check_positive(
function, y,
"Random variable", &P, Policy()))
148 "Random variable",
"Degrees of freedom parameter",
164 using boost::math::gamma_p_derivative;
165 using boost::math::gamma_q;
167 using boost::math::digamma;
176 const double nu_dbl =
value_of(nu_vec[i]);
177 gamma_vec[i] =
tgamma(nu_dbl);
178 digamma_vec[i] = digamma(nu_dbl);
184 for (
size_t n = 0; n < N; n++) {
187 const double y_dbl =
value_of(y_vec[n]);
188 const double y_inv_dbl = 1.0 / y_dbl;
189 const double nu_dbl =
value_of(nu_vec[n]);
192 const double Pn = gamma_q(0.5 * nu_dbl, 0.5 * y_inv_dbl);
197 operands_and_partials.
d_x1[n]
198 += 0.5 * y_inv_dbl * y_inv_dbl * gamma_p_derivative(0.5 * nu_dbl, 0.5 * y_inv_dbl) / Pn;
201 operands_and_partials.
d_x2[n]
206 for (
size_t n = 0; n < N; n++) {
209 operands_and_partials.
d_x1[n] *= P;
212 operands_and_partials.
d_x2[n] *= P;
216 return operands_and_partials.
to_var(P);
220 template <
typename T_y,
typename T_dof>