1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__GAMMA_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__GAMMA_HPP
4 #include <boost/random/gamma_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
43 template <
bool propto,
44 typename T_y,
typename T_shape,
typename T_inv_scale>
45 typename return_type<T_y,T_shape,T_inv_scale>::type
46 gamma_log(
const T_y& y,
const T_shape& alpha,
const T_inv_scale& beta) {
47 static const char*
function =
"stan::prob::gamma_log(%1%)";
71 "Random variable",
"Shape parameter",
72 "Inverse scale parameter",
84 for (
size_t n = 0; n <
length(y); n++) {
85 const double y_dbl =
value_of(y_vec[n]);
100 for(
size_t n = 0; n <
length(y); n++) {
106 lgamma_alpha(
length(alpha));
108 digamma_alpha(
length(alpha));
109 for (
size_t n = 0; n <
length(alpha); n++) {
119 for (
size_t n = 0; n <
length(beta); n++)
122 for (
size_t n = 0; n < N; n++) {
124 const double y_dbl =
value_of(y_vec[n]);
125 const double alpha_dbl =
value_of(alpha_vec[n]);
126 const double beta_dbl =
value_of(beta_vec[n]);
129 logp -= lgamma_alpha[n];
131 logp += alpha_dbl * log_beta[n];
133 logp += (alpha_dbl-1.0) * log_y[n];
135 logp -= beta_dbl * y_dbl;
139 operands_and_partials.
d_x1[n] += (alpha_dbl-1)/y_dbl - beta_dbl;
141 operands_and_partials.
d_x2[n] += -digamma_alpha[n] + log_beta[n] + log_y[n];
143 operands_and_partials.
d_x3[n] += alpha_dbl / beta_dbl - y_dbl;
145 return operands_and_partials.
to_var(logp);
148 template <
typename T_y,
typename T_shape,
typename T_inv_scale>
151 gamma_log(
const T_y& y,
const T_shape& alpha,
const T_inv_scale& beta) {
152 return gamma_log<false>(y,alpha,beta);
170 template <
typename T_y,
typename T_shape,
typename T_inv_scale>
172 gamma_cdf(
const T_y& y,
const T_shape& alpha,
const T_inv_scale& beta) {
178 static const char*
function =
"stan::prob::gamma_cdf(%1%)";
187 using boost::math::tools::promote_args;
196 "Random variable",
"Shape parameter",
204 size_t N =
max_size(y, alpha, beta);
207 operands_and_partials(y, alpha, beta);
214 return operands_and_partials.
to_var(0.0);
218 using boost::math::gamma_p_derivative;
226 gamma_vec(stan::length(alpha));
229 digamma_vec(stan::length(alpha));
233 const double alpha_dbl =
value_of(alpha_vec[i]);
234 gamma_vec[i] =
tgamma(alpha_dbl);
235 digamma_vec[i] =
digamma(alpha_dbl);
240 for (
size_t n = 0; n < N; n++) {
243 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
247 const double y_dbl =
value_of(y_vec[n]);
248 const double alpha_dbl =
value_of(alpha_vec[n]);
249 const double beta_dbl =
value_of(beta_vec[n]);
252 const double Pn =
gamma_p(alpha_dbl, beta_dbl * y_dbl);
257 operands_and_partials.
d_x1[n]
258 += beta_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl)
261 operands_and_partials.
d_x2[n]
263 * y_dbl, gamma_vec[n],
264 digamma_vec[n]) / Pn;
266 operands_and_partials.
d_x3[n]
267 += y_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl) / Pn;
272 operands_and_partials.
d_x1[n] *= P;
275 operands_and_partials.
d_x2[n] *= P;
278 operands_and_partials.
d_x3[n] *= P;
280 return operands_and_partials.
to_var(P);
283 template <
typename T_y,
typename T_shape,
typename T_inv_scale>
285 gamma_cdf_log(
const T_y& y,
const T_shape& alpha,
const T_inv_scale& beta) {
291 static const char*
function =
"stan::prob::gamma_cdf_log(%1%)";
300 using boost::math::tools::promote_args;
309 "Random variable",
"Shape parameter",
317 size_t N =
max_size(y, alpha, beta);
320 operands_and_partials(y, alpha, beta);
331 using boost::math::gamma_p_derivative;
339 gamma_vec(stan::length(alpha));
342 digamma_vec(stan::length(alpha));
346 const double alpha_dbl =
value_of(alpha_vec[i]);
347 gamma_vec[i] =
tgamma(alpha_dbl);
348 digamma_vec[i] =
digamma(alpha_dbl);
353 for (
size_t n = 0; n < N; n++) {
356 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
357 return operands_and_partials.
to_var(0.0);
360 const double y_dbl =
value_of(y_vec[n]);
361 const double alpha_dbl =
value_of(alpha_vec[n]);
362 const double beta_dbl =
value_of(beta_vec[n]);
365 const double Pn =
gamma_p(alpha_dbl, beta_dbl * y_dbl);
370 operands_and_partials.
d_x1[n]
371 += beta_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl)
374 operands_and_partials.
d_x2[n]
376 * y_dbl, gamma_vec[n],
377 digamma_vec[n]) / Pn;
379 operands_and_partials.
d_x3[n]
380 += y_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl) / Pn;
383 return operands_and_partials.
to_var(P);
386 template <
typename T_y,
typename T_shape,
typename T_inv_scale>
394 static const char*
function =
"stan::prob::gamma_ccdf_log(%1%)";
403 using boost::math::tools::promote_args;
412 "Random variable",
"Shape parameter",
420 size_t N =
max_size(y, alpha, beta);
423 operands_and_partials(y, alpha, beta);
430 return operands_and_partials.
to_var(0.0);
434 using boost::math::gamma_p_derivative;
442 gamma_vec(stan::length(alpha));
445 digamma_vec(stan::length(alpha));
449 const double alpha_dbl =
value_of(alpha_vec[i]);
450 gamma_vec[i] =
tgamma(alpha_dbl);
451 digamma_vec[i] =
digamma(alpha_dbl);
456 for (
size_t n = 0; n < N; n++) {
459 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
463 const double y_dbl =
value_of(y_vec[n]);
464 const double alpha_dbl =
value_of(alpha_vec[n]);
465 const double beta_dbl =
value_of(beta_vec[n]);
468 const double Pn = 1.0 -
gamma_p(alpha_dbl, beta_dbl * y_dbl);
473 operands_and_partials.
d_x1[n]
474 -= beta_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl)
477 operands_and_partials.
d_x2[n]
479 * y_dbl, gamma_vec[n],
480 digamma_vec[n]) / Pn;
482 operands_and_partials.
d_x3[n]
483 -= y_dbl * gamma_p_derivative(alpha_dbl, beta_dbl * y_dbl) / Pn;
486 return operands_and_partials.
to_var(P);
494 using boost::variate_generator;
495 using boost::gamma_distribution;
497 static const char*
function =
"stan::prob::gamma_rng(%1%)";
510 variate_generator<RNG&, gamma_distribution<> >
511 gamma_rng(rng, gamma_distribution<>(alpha, 1.0 / beta));
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.
return_type< T_y, T_shape, T_inv_scale >::type gamma_cdf_log(const T_y &y, const T_shape &alpha, const T_inv_scale &beta)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
return_type< T_y, T_shape, T_inv_scale >::type gamma_cdf(const T_y &y, const T_shape &alpha, const T_inv_scale &beta)
The cumulative density function for a gamma distribution for y with the specified shape and inverse 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...
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)
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_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)
return_type< T_y, T_shape, T_inv_scale >::type gamma_ccdf_log(const T_y &y, const T_shape &alpha, const T_inv_scale &beta)
return_type< T_y, T_shape, T_inv_scale >::type gamma_log(const T_y &y, const T_shape &alpha, const T_inv_scale &beta)
The log of a gamma density for y with the specified shape and inverse scale parameters.
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.