1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__GAMMA_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__GAMMA_HPP__
37 template <
bool propto,
38 typename T_y,
typename T_shape,
typename T_inv_scale,
40 typename return_type<T_y,T_shape,T_inv_scale>::type
41 gamma_log(
const T_y& y,
const T_shape& alpha,
const T_inv_scale& beta,
43 static const char*
function =
"stan::prob::gamma_log(%1%)";
63 if (!
check_not_nan(
function, y,
"Random variable", &logp, Policy()))
71 if (!
check_finite(
function, beta,
"Inverse scale parameter",
79 "Random variable",
"Shape parameter",
"Inverse scale parameter",
92 for (
size_t n = 0; n <
length(y); n++) {
93 const double y_dbl =
value_of(y_vec[n]);
103 using boost::math::digamma;
108 for(
size_t n = 0; n <
length(y); n++) {
114 lgamma_alpha(
length(alpha));
116 digamma_alpha(
length(alpha));
117 for (
size_t n = 0; n <
length(alpha); n++) {
121 digamma_alpha[n] = digamma(
value_of(alpha_vec[n]));
127 for (
size_t n = 0; n <
length(beta); n++)
130 for (
size_t n = 0; n < N; n++) {
132 const double y_dbl =
value_of(y_vec[n]);
133 const double alpha_dbl =
value_of(alpha_vec[n]);
134 const double beta_dbl =
value_of(beta_vec[n]);
137 logp -= lgamma_alpha[n];
139 logp += alpha_dbl * log_beta[n];
141 logp += (alpha_dbl-1.0) * log_y[n];
143 logp -= beta_dbl * y_dbl;
147 operands_and_partials.
d_x1[n] += (alpha_dbl-1)/y_dbl - beta_dbl;
149 operands_and_partials.
d_x2[n] += -digamma_alpha[n] + log_beta[n] + log_y[n];
151 operands_and_partials.
d_x3[n] += alpha_dbl / beta_dbl - y_dbl;
153 return operands_and_partials.
to_var(logp);
156 template <
bool propto,
157 typename T_y,
typename T_shape,
typename T_inv_scale>
160 gamma_log(
const T_y& y,
const T_shape& alpha,
const T_inv_scale& beta) {
164 template <
typename T_y,
typename T_shape,
typename T_inv_scale,
168 gamma_log(
const T_y& y,
const T_shape& alpha,
const T_inv_scale& beta,
170 return gamma_log<false>(y,alpha,beta,Policy());
173 template <
typename T_y,
typename T_shape,
typename T_inv_scale>
176 gamma_log(
const T_y& y,
const T_shape& alpha,
const T_inv_scale& beta) {