1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__INV_GAMMA_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__INV_GAMMA_HPP
4 #include <boost/random/gamma_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
37 template <
bool propto,
38 typename T_y,
typename T_shape,
typename T_scale>
39 typename return_type<T_y,T_shape,T_scale>::type
40 inv_gamma_log(
const T_y& y,
const T_shape& alpha,
const T_scale& beta) {
41 static const char*
function =
"stan::prob::inv_gamma_log(%1%)";
46 using boost::math::tools::promote_args;
64 "Random variable",
"Shape parameter",
77 for (
size_t n = 0; n <
length(y); n++) {
78 const double y_dbl =
value_of(y_vec[n]);
85 operands_and_partials(y, alpha, beta);
97 for(
size_t n = 0; n <
length(y); n++) {
102 inv_y[n] = 1.0 /
value_of(y_vec[n]);
107 lgamma_alpha(
length(alpha));
110 digamma_alpha(
length(alpha));
111 for (
size_t n = 0; n <
length(alpha); n++) {
122 for (
size_t n = 0; n <
length(beta); n++)
125 for (
size_t n = 0; n < N; n++) {
127 const double alpha_dbl =
value_of(alpha_vec[n]);
128 const double beta_dbl =
value_of(beta_vec[n]);
131 logp -= lgamma_alpha[n];
133 logp += alpha_dbl * log_beta[n];
135 logp -= (alpha_dbl+1.0) * log_y[n];
137 logp -= beta_dbl * inv_y[n];
141 operands_and_partials.
d_x1[n]
142 += -(alpha_dbl+1) * inv_y[n] + beta_dbl * inv_y[n] * inv_y[n];
144 operands_and_partials.
d_x2[n]
145 += -digamma_alpha[n] + log_beta[n] - log_y[n];
147 operands_and_partials.
d_x3[n] += alpha_dbl / beta_dbl - inv_y[n];
149 return operands_and_partials.
to_var(logp);
152 template <
typename T_y,
typename T_shape,
typename T_scale>
156 return inv_gamma_log<false>(y,alpha,beta);
175 template <
typename T_y,
typename T_shape,
typename T_scale>
184 static const char*
function =
"stan::prob::inv_gamma_cdf(%1%)";
193 using boost::math::tools::promote_args;
202 "Random variable",
"Shape parameter",
210 size_t N =
max_size(y, alpha, beta);
213 operands_and_partials(y, alpha, beta);
220 return operands_and_partials.
to_var(0.0);
224 using boost::math::gamma_p_derivative;
232 gamma_vec(stan::length(alpha));
235 digamma_vec(stan::length(alpha));
239 const double alpha_dbl =
value_of(alpha_vec[i]);
240 gamma_vec[i] =
tgamma(alpha_dbl);
241 digamma_vec[i] =
digamma(alpha_dbl);
246 for (
size_t n = 0; n < N; n++) {
249 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
253 const double y_dbl =
value_of(y_vec[n]);
254 const double y_inv_dbl = 1.0 / y_dbl;
255 const double alpha_dbl =
value_of(alpha_vec[n]);
256 const double beta_dbl =
value_of(beta_vec[n]);
259 const double Pn =
gamma_q(alpha_dbl, beta_dbl * y_inv_dbl);
264 operands_and_partials.
d_x1[n]
265 += beta_dbl * y_inv_dbl * y_inv_dbl
266 * gamma_p_derivative(alpha_dbl, beta_dbl * y_inv_dbl)
269 operands_and_partials.
d_x2[n]
271 * y_inv_dbl, gamma_vec[n],
272 digamma_vec[n]) / Pn;
274 operands_and_partials.
d_x3[n]
275 += - y_inv_dbl * gamma_p_derivative(alpha_dbl,
276 beta_dbl * y_inv_dbl) / Pn;
281 operands_and_partials.
d_x1[n] *= P;
284 operands_and_partials.
d_x2[n] *= P;
287 operands_and_partials.
d_x3[n] *= P;
289 return operands_and_partials.
to_var(P);
292 template <
typename T_y,
typename T_shape,
typename T_scale>
301 static const char*
function =
"stan::prob::inv_gamma_cdf_log(%1%)";
310 using boost::math::tools::promote_args;
319 "Random variable",
"Shape parameter",
327 size_t N =
max_size(y, alpha, beta);
330 operands_and_partials(y, alpha, beta);
341 using boost::math::gamma_p_derivative;
349 gamma_vec(stan::length(alpha));
352 digamma_vec(stan::length(alpha));
356 const double alpha_dbl =
value_of(alpha_vec[i]);
357 gamma_vec[i] =
tgamma(alpha_dbl);
358 digamma_vec[i] =
digamma(alpha_dbl);
363 for (
size_t n = 0; n < N; n++) {
366 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
370 const double y_dbl =
value_of(y_vec[n]);
371 const double y_inv_dbl = 1.0 / y_dbl;
372 const double alpha_dbl =
value_of(alpha_vec[n]);
373 const double beta_dbl =
value_of(beta_vec[n]);
376 const double Pn =
gamma_q(alpha_dbl, beta_dbl * y_inv_dbl);
381 operands_and_partials.
d_x1[n]
382 += beta_dbl * y_inv_dbl * y_inv_dbl
383 * gamma_p_derivative(alpha_dbl, beta_dbl * y_inv_dbl)
386 operands_and_partials.
d_x2[n]
388 * y_inv_dbl, gamma_vec[n],
389 digamma_vec[n]) / Pn;
391 operands_and_partials.
d_x3[n]
392 += - y_inv_dbl * gamma_p_derivative(alpha_dbl,
393 beta_dbl * y_inv_dbl) / Pn;
396 return operands_and_partials.
to_var(P);
399 template <
typename T_y,
typename T_shape,
typename T_scale>
408 static const char*
function =
"stan::prob::inv_gamma_ccdf_log(%1%)";
417 using boost::math::tools::promote_args;
426 "Random variable",
"Shape parameter",
434 size_t N =
max_size(y, alpha, beta);
437 operands_and_partials(y, alpha, beta);
444 return operands_and_partials.
to_var(0.0);
448 using boost::math::gamma_p_derivative;
456 gamma_vec(stan::length(alpha));
459 digamma_vec(stan::length(alpha));
463 const double alpha_dbl =
value_of(alpha_vec[i]);
464 gamma_vec[i] =
tgamma(alpha_dbl);
465 digamma_vec[i] =
digamma(alpha_dbl);
470 for (
size_t n = 0; n < N; n++) {
473 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
477 const double y_dbl =
value_of(y_vec[n]);
478 const double y_inv_dbl = 1.0 / y_dbl;
479 const double alpha_dbl =
value_of(alpha_vec[n]);
480 const double beta_dbl =
value_of(beta_vec[n]);
483 const double Pn = 1.0 -
gamma_q(alpha_dbl, beta_dbl * y_inv_dbl);
488 operands_and_partials.
d_x1[n]
489 -= beta_dbl * y_inv_dbl * y_inv_dbl
490 * gamma_p_derivative(alpha_dbl, beta_dbl * y_inv_dbl)
493 operands_and_partials.
d_x2[n]
495 * y_inv_dbl, gamma_vec[n],
496 digamma_vec[n]) / Pn;
498 operands_and_partials.
d_x3[n]
499 -= - y_inv_dbl * gamma_p_derivative(alpha_dbl,
500 beta_dbl * y_inv_dbl) / Pn;
503 return operands_and_partials.
to_var(P);
511 using boost::variate_generator;
512 using boost::random::gamma_distribution;
514 static const char*
function =
"stan::prob::inv_gamma_rng(%1%)";
521 variate_generator<RNG&, gamma_distribution<> >
522 gamma_rng(rng, gamma_distribution<>(alpha, 1 / beta));
return_type< T_y, T_shape, T_scale >::type inv_gamma_cdf_log(const T_y &y, const T_shape &alpha, const T_scale &beta)
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
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_shape, T_scale >::type inv_gamma_cdf(const T_y &y, const T_shape &alpha, const T_scale &beta)
The CDF of an inverse gamma density for y with the specified shape and scale parameters.
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...
return_type< T_y, T_shape, T_scale >::type inv_gamma_log(const T_y &y, const T_shape &alpha, const T_scale &beta)
The log of an inverse gamma density for y with the specified shape and scale parameters.
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
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)
double inv_gamma_rng(const double alpha, const double beta, RNG &rng)
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_shape, T_scale >::type inv_gamma_ccdf_log(const T_y &y, const T_shape &alpha, const T_scale &beta)
bool check_less_or_equal(const char *function, const T_y &y, const T_high &high, const char *name, T_result *result)
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)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
double gamma_rng(const double alpha, const double beta, RNG &rng)
bool check_greater_or_equal(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
double negative_infinity()
Return negative infinity.