1 #ifndef __STAN__PROB__DISTRIBUTIONS__PARETO_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__PARETO_HPP__
4 #include <boost/random/exponential_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
19 template <
bool propto,
20 typename T_y,
typename T_scale,
typename T_shape,
22 typename return_type<T_y,T_scale,T_shape>::type
23 pareto_log(
const T_y& y,
const T_scale& y_min,
const T_shape& alpha,
25 static const char*
function =
"stan::prob::pareto_log(%1%)";
44 if (!
check_not_nan(
function, y,
"Random variable", &logp, Policy()))
60 "Random variable",
"Scale parameter",
"Shape parameter",
71 size_t N =
max_size(y, y_min, alpha);
73 for (
size_t n = 0; n < N; n++) {
74 if (y_vec[n] < y_min_vec[n])
83 for (
size_t n = 0; n <
length(y); n++)
87 for (
size_t n = 0; n <
length(y); n++)
92 for (
size_t n = 0; n <
length(y_min); n++)
96 for (
size_t n = 0; n <
length(alpha); n++)
101 for (
size_t n = 0; n <
length(alpha); n++)
102 inv_alpha[n] = 1 /
value_of(alpha_vec[n]);
106 for (
size_t n = 0; n < N; n++) {
107 const double alpha_dbl =
value_of(alpha_vec[n]);
110 logp += log_alpha[n];
112 logp += alpha_dbl * log_y_min[n];
114 logp -= alpha_dbl * log_y[n] + log_y[n];
118 operands_and_partials.
d_x1[n] -= alpha_dbl * inv_y[n] + inv_y[n];
120 operands_and_partials.
d_x2[n] += alpha_dbl /
value_of(y_min_vec[n]);
122 operands_and_partials.
d_x3[n] += 1 / alpha_dbl + log_y_min[n] - log_y[n];
124 return operands_and_partials.
to_var(logp);
128 template <
bool propto,
129 typename T_y,
typename T_scale,
typename T_shape>
132 pareto_log(
const T_y& y,
const T_scale& y_min,
const T_shape& alpha) {
136 template <
typename T_y,
typename T_scale,
typename T_shape,
140 pareto_log(
const T_y& y,
const T_scale& y_min,
const T_shape& alpha,
142 return pareto_log<false>(y,y_min,alpha,Policy());
145 template <
typename T_y,
typename T_scale,
typename T_shape>
148 pareto_log(
const T_y& y,
const T_scale& y_min,
const T_shape& alpha) {
152 template <
typename T_y,
typename T_scale,
typename T_shape,
class Policy>
154 pareto_cdf(
const T_y& y,
const T_scale& y_min,
const T_shape& alpha,
const Policy&) {
161 static const char*
function =
"stan::prob::pareto_cdf(%1%)";
174 if (!
check_not_nan(
function, y,
"Random variable", &P, Policy()))
180 if (!
check_finite(
function, y_min,
"Scale parameter", &P, Policy()))
183 if (!
check_positive(
function, y_min,
"Scale parameter", &P, Policy()))
186 if (!
check_finite(
function, alpha,
"Shape parameter", &P, Policy()))
189 if (!
check_positive(
function, alpha,
"Shape parameter", &P, Policy()))
193 "Random variable",
"Scale parameter",
"Shape parameter",
201 size_t N =
max_size(y, y_min, alpha);
213 return operands_and_partials.
to_var(0.0);
218 for (
size_t n = 0; n < N; n++) {
222 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
228 const double y_min_inv_dbl = 1.0 /
value_of(y_min_vec[n]);
229 const double alpha_dbl =
value_of(alpha_vec[n]);
232 const double Pn = 1.0 -
exp( alpha_dbl * log_dbl );
237 operands_and_partials.
d_x1[n]
238 += alpha_dbl * y_min_inv_dbl *
exp( (alpha_dbl + 1) * log_dbl ) / Pn;
241 operands_and_partials.
d_x2[n]
242 += - alpha_dbl * y_min_inv_dbl *
exp( alpha_dbl * log_dbl ) / Pn;
245 operands_and_partials.
d_x3[n]
246 += -
exp( alpha_dbl * log_dbl ) * log_dbl / Pn;
252 for(
size_t n = 0; n <
stan::length(y); ++n) operands_and_partials.
d_x1[n] *= P;
256 for(
size_t n = 0; n <
stan::length(y_min); ++n) operands_and_partials.
d_x2[n] *= P;
260 for(
size_t n = 0; n <
stan::length(alpha); ++n) operands_and_partials.
d_x3[n] *= P;
263 return operands_and_partials.
to_var(P);
267 template <
typename T_y,
typename T_scale,
typename T_shape>
269 pareto_cdf(
const T_y& y,
const T_scale& y_min,
const T_shape& alpha) {
278 using boost::variate_generator;
279 using boost::exponential_distribution;
280 variate_generator<RNG&, exponential_distribution<> >
281 exp_rng(rng, exponential_distribution<>(alpha));