1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__GUMBEL_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__GUMBEL_HPP
4 #include <boost/random/uniform_01.hpp>
5 #include <boost/random/variate_generator.hpp>
19 template <
bool propto,
typename T_y,
typename T_loc,
typename T_scale>
20 typename return_type<T_y,T_loc,T_scale>::type
21 gumbel_log(
const T_y& y,
const T_loc& mu,
const T_scale& beta) {
22 static const char*
function =
"stan::prob::gumbel_log(%1%)";
49 "Random variable",
"Location parameter",
59 operands_and_partials(y, mu, beta);
69 for (
size_t i = 0; i <
length(beta); i++) {
70 inv_beta[i] = 1.0 /
value_of(beta_vec[i]);
75 for (
size_t n = 0; n < N; n++) {
77 const double y_dbl =
value_of(y_vec[n]);
78 const double mu_dbl =
value_of(mu_vec[n]);
81 const double y_minus_mu_over_beta
82 = (y_dbl - mu_dbl) * inv_beta[n];
88 logp += -y_minus_mu_over_beta -
exp(-y_minus_mu_over_beta);
91 double scaled_diff = inv_beta[n] *
exp(-y_minus_mu_over_beta);
93 operands_and_partials.
d_x1[n] -= inv_beta[n] - scaled_diff;
95 operands_and_partials.
d_x2[n] += inv_beta[n] - scaled_diff;
97 operands_and_partials.
d_x3[n]
98 += -inv_beta[n] + y_minus_mu_over_beta * inv_beta[n]
99 - scaled_diff * y_minus_mu_over_beta;
101 return operands_and_partials.
to_var(logp);
104 template <
typename T_y,
typename T_loc,
typename T_scale>
107 gumbel_log(
const T_y& y,
const T_loc& mu,
const T_scale& beta) {
108 return gumbel_log<false>(y,mu,beta);
111 template <
typename T_y,
typename T_loc,
typename T_scale>
113 gumbel_cdf(
const T_y& y,
const T_loc& mu,
const T_scale& beta) {
114 static const char*
function =
"stan::prob::gumbel_cdf(%1%)";
134 "Random variable",
"Location parameter",
135 "Scale parameter", &cdf);
138 operands_and_partials(y, mu, beta);
145 for (
size_t n = 0; n < N; n++) {
146 const double y_dbl =
value_of(y_vec[n]);
147 const double mu_dbl =
value_of(mu_vec[n]);
148 const double beta_dbl =
value_of(beta_vec[n]);
149 const double scaled_diff = (y_dbl - mu_dbl) / beta_dbl;
150 const double rep_deriv =
exp(-scaled_diff -
exp(-scaled_diff))
152 const double cdf_ =
exp(-
exp(-scaled_diff));
156 operands_and_partials.
d_x1[n] += rep_deriv / cdf_;
158 operands_and_partials.
d_x2[n] -= rep_deriv / cdf_;
160 operands_and_partials.
d_x3[n] -= rep_deriv * scaled_diff / cdf_;
165 operands_and_partials.
d_x1[n] *= cdf;
169 operands_and_partials.
d_x2[n] *= cdf;
173 operands_and_partials.
d_x3[n] *= cdf;
176 return operands_and_partials.
to_var(cdf);
179 template <
typename T_y,
typename T_loc,
typename T_scale>
182 static const char*
function =
"stan::prob::gumbel_cdf_log(%1%)";
198 check_finite(
function, mu,
"Location parameter", &cdf_log);
202 "Random variable",
"Location parameter",
203 "Scale parameter", &cdf_log);
206 operands_and_partials(y, mu, beta);
213 for (
size_t n = 0; n < N; n++) {
214 const double y_dbl =
value_of(y_vec[n]);
215 const double mu_dbl =
value_of(mu_vec[n]);
216 const double beta_dbl =
value_of(beta_vec[n]);
217 const double scaled_diff = (y_dbl - mu_dbl) / beta_dbl;
218 const double rep_deriv =
exp(-scaled_diff) / beta_dbl;
219 cdf_log -=
exp(-scaled_diff);
222 operands_and_partials.
d_x1[n] += rep_deriv;
224 operands_and_partials.
d_x2[n] -= rep_deriv;
226 operands_and_partials.
d_x3[n] -= rep_deriv * scaled_diff;
229 return operands_and_partials.
to_var(cdf_log);
232 template <
typename T_y,
typename T_loc,
typename T_scale>
235 static const char*
function =
"stan::prob::gumbel_ccdf_log(%1%)";
243 double ccdf_log(0.0);
251 check_finite(
function, mu,
"Location parameter", &ccdf_log);
255 "Random variable",
"Location parameter",
256 "Scale parameter", &ccdf_log);
259 operands_and_partials(y, mu, beta);
266 for (
size_t n = 0; n < N; n++) {
267 const double y_dbl =
value_of(y_vec[n]);
268 const double mu_dbl =
value_of(mu_vec[n]);
269 const double beta_dbl =
value_of(beta_vec[n]);
270 const double scaled_diff = (y_dbl - mu_dbl) / beta_dbl;
271 const double rep_deriv =
exp(-scaled_diff -
exp(-scaled_diff))
273 const double ccdf_log_ = 1.0 -
exp(-
exp(-scaled_diff));
274 ccdf_log +=
log(ccdf_log_);
277 operands_and_partials.
d_x1[n] -= rep_deriv / ccdf_log_;
279 operands_and_partials.
d_x2[n] += rep_deriv / ccdf_log_;
281 operands_and_partials.
d_x3[n] += rep_deriv * scaled_diff / ccdf_log_;
284 return operands_and_partials.
to_var(ccdf_log);
292 using boost::variate_generator;
293 using boost::uniform_01;
295 static const char*
function =
"stan::prob::gumbel_rng(%1%)";
301 check_finite(
function, mu,
"Location parameter", (
double*)0);
304 variate_generator<RNG&, uniform_01<> >
305 uniform01_rng(rng, uniform_01<>());
return_type< T_y, T_loc, T_scale >::type gumbel_log(const T_y &y, const T_loc &mu, const T_scale &beta)
T_return_type to_var(double logp)
DoubleVectorView allocates double values to be used as intermediate values.
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
return_type< T_y, T_loc, T_scale >::type gumbel_ccdf_log(const T_y &y, const T_loc &mu, const T_scale &beta)
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
return_type< T_y, T_loc, T_scale >::type gumbel_cdf(const T_y &y, const T_loc &mu, const T_scale &beta)
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_loc, T_scale >::type gumbel_cdf_log(const T_y &y, const T_loc &mu, const T_scale &beta)
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)
bool check_positive(const char *function, const T_y &y, 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.
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...
fvar< T > exp(const fvar< T > &x)
double gumbel_rng(const double mu, const double beta, RNG &rng)