1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__CHI_SQUARE_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__CHI_SQUARE_HPP
4 #include <boost/random/chi_squared_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
40 template <
bool propto,
41 typename T_y,
typename T_dof>
42 typename return_type<T_y,T_dof>::type
44 static const char*
function =
"stan::prob::chi_square_log(%1%)";
64 "Random variable",
"Degrees of freedom parameter",
73 for (
size_t n = 0; n <
length(y); n++)
87 for (
size_t i = 0; i <
length(y); i++)
93 for (
size_t i = 0; i <
length(y); i++)
102 for (
size_t i = 0; i <
length(nu); i++) {
103 double half_nu = 0.5 *
value_of(nu_vec[i]);
105 lgamma_half_nu[i] =
lgamma(half_nu);
107 digamma_half_nu_over_two[i] =
digamma(half_nu) * 0.5;
113 for (
size_t n = 0; n < N; n++) {
114 const double y_dbl =
value_of(y_vec[n]);
115 const double half_y = 0.5 * y_dbl;
116 const double nu_dbl =
value_of(nu_vec[n]);
117 const double half_nu = 0.5 * nu_dbl;
119 logp += nu_dbl * NEG_LOG_TWO_OVER_TWO - lgamma_half_nu[n];
121 logp += (half_nu-1.0) * log_y[n];
126 operands_and_partials.
d_x1[n] += (half_nu-1.0)*inv_y[n] - 0.5;
129 operands_and_partials.
d_x2[n]
130 += NEG_LOG_TWO_OVER_TWO - digamma_half_nu_over_two[n] + log_y[n]*0.5;
133 return operands_and_partials.
to_var(logp);
136 template <
typename T_y,
typename T_dof>
140 return chi_square_log<false>(y,nu);
152 template <
typename T_y,
typename T_dof>
155 static const char*
function =
"stan::prob::chi_square_cdf(%1%)";
174 "Random variable",
"Degrees of freedom parameter",
183 operands_and_partials(y, nu);
189 return operands_and_partials.
to_var(0.0);
193 using boost::math::gamma_p_derivative;
201 gamma_vec(stan::length(nu));
204 digamma_vec(stan::length(nu));
208 const double alpha_dbl =
value_of(nu_vec[i]) * 0.5;
209 gamma_vec[i] =
tgamma(alpha_dbl);
210 digamma_vec[i] =
digamma(alpha_dbl);
215 for (
size_t n = 0; n < N; n++) {
218 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
222 const double y_dbl =
value_of(y_vec[n]);
223 const double alpha_dbl =
value_of(nu_vec[n]) * 0.5;
224 const double beta_dbl = 0.5;
227 const double Pn =
gamma_p(alpha_dbl, beta_dbl * y_dbl);
232 operands_and_partials.
d_x1[n]
233 += beta_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl)
236 operands_and_partials.
d_x2[n]
238 * y_dbl, gamma_vec[n],
239 digamma_vec[n]) / Pn;
244 operands_and_partials.
d_x1[n] *= cdf;
247 operands_and_partials.
d_x2[n] *= cdf;
249 return operands_and_partials.
to_var(cdf);
252 template <
typename T_y,
typename T_dof>
255 static const char*
function =
"stan::prob::chi_square_cdf_log(%1%)";
275 "Random variable",
"Degrees of freedom parameter",
284 operands_and_partials(y, nu);
294 using boost::math::gamma_p_derivative;
302 gamma_vec(stan::length(nu));
305 digamma_vec(stan::length(nu));
309 const double alpha_dbl =
value_of(nu_vec[i]) * 0.5;
310 gamma_vec[i] =
tgamma(alpha_dbl);
311 digamma_vec[i] =
digamma(alpha_dbl);
316 for (
size_t n = 0; n < N; n++) {
319 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
320 return operands_and_partials.
to_var(0.0);
323 const double y_dbl =
value_of(y_vec[n]);
324 const double alpha_dbl =
value_of(nu_vec[n]) * 0.5;
325 const double beta_dbl = 0.5;
328 const double Pn =
gamma_p(alpha_dbl, beta_dbl * y_dbl);
333 operands_and_partials.
d_x1[n]
334 += beta_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl)
337 operands_and_partials.
d_x2[n]
339 * y_dbl, gamma_vec[n],
340 digamma_vec[n]) / Pn;
343 return operands_and_partials.
to_var(cdf_log);
346 template <
typename T_y,
typename T_dof>
349 static const char*
function =
"stan::prob::chi_square_ccdf_log(%1%)";
357 double ccdf_log(0.0);
369 "Random variable",
"Degrees of freedom parameter",
378 operands_and_partials(y, nu);
384 return operands_and_partials.
to_var(0.0);
388 using boost::math::gamma_p_derivative;
396 gamma_vec(stan::length(nu));
399 digamma_vec(stan::length(nu));
403 const double alpha_dbl =
value_of(nu_vec[i]) * 0.5;
404 gamma_vec[i] =
tgamma(alpha_dbl);
405 digamma_vec[i] =
digamma(alpha_dbl);
410 for (
size_t n = 0; n < N; n++) {
413 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
417 const double y_dbl =
value_of(y_vec[n]);
418 const double alpha_dbl =
value_of(nu_vec[n]) * 0.5;
419 const double beta_dbl = 0.5;
422 const double Pn = 1.0 -
gamma_p(alpha_dbl, beta_dbl * y_dbl);
427 operands_and_partials.
d_x1[n]
428 -= beta_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl)
431 operands_and_partials.
d_x2[n]
433 * y_dbl, gamma_vec[n],
434 digamma_vec[n]) / Pn;
437 return operands_and_partials.
to_var(ccdf_log);
444 using boost::variate_generator;
445 using boost::random::chi_squared_distribution;
447 static const char*
function =
"stan::prob::chi_square_rng(%1%)";
455 variate_generator<RNG&, chi_squared_distribution<> >
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 >::type chi_square_log(const T_y &y, const T_dof &nu)
The log of a chi-squared density for y with the specified degrees of freedom parameter.
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...
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
return_type< T_y, T_dof >::type chi_square_cdf_log(const T_y &y, const T_dof &nu)
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)
return_type< T_y, T_dof >::type chi_square_ccdf_log(const T_y &y, const T_dof &nu)
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)
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
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
fvar< T > log(const fvar< T > &x)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
return_type< T_y, T_dof >::type chi_square_cdf(const T_y &y, const T_dof &nu)
Calculates the chi square cumulative distribution function for the given variate and degrees of freed...
double negative_infinity()
Return negative infinity.
double chi_square_rng(const double nu, RNG &rng)