1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__SCALED_INV_CHI_SQUARE_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__SCALED_INV_CHI_SQUARE_HPP
4 #include <boost/random/chi_squared_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
41 template <
bool propto,
42 typename T_y,
typename T_dof,
typename T_scale>
43 typename return_type<T_y,T_dof,T_scale>::type
45 static const char*
function
46 =
"stan::prob::scaled_inv_chi_square_log(%1%)";
67 "Degrees of freedom parameter",
80 for (
size_t n = 0; n < N; n++) {
92 for (
size_t i = 0; i <
length(nu); i++)
94 half_nu[i] = 0.5 *
value_of(nu_vec[i]);
98 for (
size_t i = 0; i <
length(y); i++)
104 for (
size_t i = 0; i <
length(y); i++)
106 inv_y[i] = 1.0 /
value_of(y_vec[i]);
110 for (
size_t i = 0; i <
length(s); i++)
120 for (
size_t i = 0; i <
length(nu); i++) {
122 lgamma_half_nu[i] =
lgamma(half_nu[i]);
124 log_half_nu[i] =
log(half_nu[i]);
126 digamma_half_nu_over_two[i] =
digamma(half_nu[i]) * 0.5;
130 operands_and_partials(y, nu, s);
131 for (
size_t n = 0; n < N; n++) {
132 const double s_dbl =
value_of(s_vec[n]);
133 const double nu_dbl =
value_of(nu_vec[n]);
135 logp += half_nu[n] * log_half_nu[n] - lgamma_half_nu[n];
137 logp += nu_dbl * log_s[n];
139 logp -= (half_nu[n]+1.0) * log_y[n];
141 logp -= half_nu[n] * s_dbl*s_dbl * inv_y[n];
144 operands_and_partials.
d_x1[n]
145 += -(half_nu[n] + 1.0) * inv_y[n]
146 + half_nu[n] * s_dbl*s_dbl * inv_y[n]*inv_y[n];
149 operands_and_partials.
d_x2[n]
150 += 0.5 * log_half_nu[n] + 0.5
151 - digamma_half_nu_over_two[n]
154 - 0.5* s_dbl*s_dbl * inv_y[n];
157 operands_and_partials.
d_x3[n]
158 += nu_dbl / s_dbl - nu_dbl * inv_y[n] * s_dbl;
161 return operands_and_partials.
to_var(logp);
164 template <
typename T_y,
typename T_dof,
typename T_scale>
168 return scaled_inv_chi_square_log<false>(y,nu,s);
185 template <
typename T_y,
typename T_dof,
typename T_scale>
193 static const char*
function
194 =
"stan::prob::scaled_inv_chi_square_cdf(%1%)";
210 "Degrees of freedom parameter",
221 operands_and_partials(y, nu, s);
228 return operands_and_partials.
to_var(0.0);
232 using boost::math::gamma_p_derivative;
245 const double half_nu_dbl = 0.5 *
value_of(nu_vec[i]);
246 gamma_vec[i] =
tgamma(half_nu_dbl);
247 digamma_vec[i] =
digamma(half_nu_dbl);
252 for (
size_t n = 0; n < N; n++) {
256 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
261 const double y_dbl =
value_of(y_vec[n]);
262 const double y_inv_dbl = 1.0 / y_dbl;
263 const double half_nu_dbl = 0.5 *
value_of(nu_vec[n]);
264 const double s_dbl =
value_of(s_vec[n]);
265 const double half_s2_overx_dbl = 0.5 * s_dbl * s_dbl * y_inv_dbl;
266 const double half_nu_s2_overx_dbl
267 = 2.0 * half_nu_dbl * half_s2_overx_dbl;
270 const double Pn =
gamma_q(half_nu_dbl, half_nu_s2_overx_dbl);
275 operands_and_partials.
d_x1[n]
276 += half_nu_s2_overx_dbl * y_inv_dbl
277 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) / Pn;
280 operands_and_partials.
d_x2[n]
282 half_nu_s2_overx_dbl,
283 gamma_vec[n], digamma_vec[n])
285 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) )
289 operands_and_partials.
d_x3[n]
290 += - 2.0 * half_nu_dbl * s_dbl * y_inv_dbl
291 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) / Pn;
297 operands_and_partials.
d_x1[n] *= P;
300 operands_and_partials.
d_x2[n] *= P;
303 operands_and_partials.
d_x3[n] *= P;
305 return operands_and_partials.
to_var(P);
308 template <
typename T_y,
typename T_dof,
typename T_scale>
316 static const char*
function
317 =
"stan::prob::scaled_inv_chi_square_cdf_log(%1%)";
333 "Degrees of freedom parameter",
344 operands_and_partials(y, nu, s);
354 using boost::math::gamma_p_derivative;
367 const double half_nu_dbl = 0.5 *
value_of(nu_vec[i]);
368 gamma_vec[i] =
tgamma(half_nu_dbl);
369 digamma_vec[i] =
digamma(half_nu_dbl);
374 for (
size_t n = 0; n < N; n++) {
378 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
383 const double y_dbl =
value_of(y_vec[n]);
384 const double y_inv_dbl = 1.0 / y_dbl;
385 const double half_nu_dbl = 0.5 *
value_of(nu_vec[n]);
386 const double s_dbl =
value_of(s_vec[n]);
387 const double half_s2_overx_dbl = 0.5 * s_dbl * s_dbl * y_inv_dbl;
388 const double half_nu_s2_overx_dbl
389 = 2.0 * half_nu_dbl * half_s2_overx_dbl;
392 const double Pn =
gamma_q(half_nu_dbl, half_nu_s2_overx_dbl);
397 operands_and_partials.
d_x1[n]
398 += half_nu_s2_overx_dbl * y_inv_dbl
399 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) / Pn;
401 operands_and_partials.
d_x2[n]
403 half_nu_s2_overx_dbl,
404 gamma_vec[n], digamma_vec[n])
406 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) )
409 operands_and_partials.
d_x3[n]
410 += - 2.0 * half_nu_dbl * s_dbl * y_inv_dbl
411 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) / Pn;
414 return operands_and_partials.
to_var(P);
417 template <
typename T_y,
typename T_dof,
typename T_scale>
425 static const char*
function
426 =
"stan::prob::scaled_inv_chi_square_ccdf_log(%1%)";
442 "Degrees of freedom parameter",
453 operands_and_partials(y, nu, s);
459 return operands_and_partials.
to_var(0.0);
463 using boost::math::gamma_p_derivative;
476 const double half_nu_dbl = 0.5 *
value_of(nu_vec[i]);
477 gamma_vec[i] =
tgamma(half_nu_dbl);
478 digamma_vec[i] =
digamma(half_nu_dbl);
483 for (
size_t n = 0; n < N; n++) {
487 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
492 const double y_dbl =
value_of(y_vec[n]);
493 const double y_inv_dbl = 1.0 / y_dbl;
494 const double half_nu_dbl = 0.5 *
value_of(nu_vec[n]);
495 const double s_dbl =
value_of(s_vec[n]);
496 const double half_s2_overx_dbl = 0.5 * s_dbl * s_dbl * y_inv_dbl;
497 const double half_nu_s2_overx_dbl
498 = 2.0 * half_nu_dbl * half_s2_overx_dbl;
501 const double Pn = 1.0 -
gamma_q(half_nu_dbl, half_nu_s2_overx_dbl);
506 operands_and_partials.
d_x1[n]
507 -= half_nu_s2_overx_dbl * y_inv_dbl
508 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) / Pn;
510 operands_and_partials.
d_x2[n]
512 half_nu_s2_overx_dbl,
513 gamma_vec[n], digamma_vec[n])
515 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) )
518 operands_and_partials.
d_x3[n]
519 += 2.0 * half_nu_dbl * s_dbl * y_inv_dbl
520 * gamma_p_derivative(half_nu_dbl, half_nu_s2_overx_dbl) / Pn;
523 return operands_and_partials.
to_var(P);
531 using boost::variate_generator;
532 using boost::random::chi_squared_distribution;
534 static const char*
function
535 =
"stan::prob::scaled_inv_chi_square_rng(%1%)";
543 variate_generator<RNG&, chi_squared_distribution<> >
T square(const T x)
Return the square of the specified argument.
fvar< T > tgamma(const fvar< T > &x)
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(const T_a a, const T_b b)
Calculated the value of the first argument times log of the second argument while behaving properly w...
DoubleVectorView allocates double values to be used as intermediate values.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
return_type< T_y, T_dof, T_scale >::type scaled_inv_chi_square_ccdf_log(const T_y &y, const T_dof &nu, const T_scale &s)
fvar< T > lgamma(const fvar< T > &x)
A variable implementation that stores operands and derivatives with respect to the variable...
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
return_type< T_y, T_dof, T_scale >::type scaled_inv_chi_square_cdf(const T_y &y, const T_dof &nu, const T_scale &s)
The CDF of a scaled inverse chi-squared density for y with the specified degrees of freedom parameter...
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
return_type< T_y, T_dof, T_scale >::type scaled_inv_chi_square_log(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...
double gradRegIncGamma(double a, double z, double g, double dig, double precision=1e-6)
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
size_t max_size(const T1 &x1, const T2 &x2)
return_type< T_y, T_dof, T_scale >::type scaled_inv_chi_square_cdf_log(const T_y &y, const T_dof &nu, const T_scale &s)
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is nan.
fvar< T > digamma(const fvar< T > &x)
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
VectorView< double *, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
fvar< T > log(const fvar< T > &x)
double scaled_inv_chi_square_rng(const double nu, const double s, RNG &rng)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
double negative_infinity()
Return negative infinity.
double chi_square_rng(const double nu, RNG &rng)