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>
21 template <
bool propto,
22 typename T_y,
typename T_scale,
typename T_shape>
23 typename return_type<T_y,T_scale,T_shape>::type
24 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%)";
47 "Random variable",
"Scale parameter",
58 size_t N =
max_size(y, y_min, alpha);
60 for (
size_t n = 0; n < N; n++) {
61 if (y_vec[n] < y_min_vec[n])
67 operands_and_partials(y, y_min, alpha);
72 for (
size_t n = 0; n <
length(y); n++)
79 for (
size_t n = 0; n <
length(y); n++)
86 for (
size_t n = 0; n <
length(y_min); n++)
92 for (
size_t n = 0; n <
length(alpha); n++)
97 for (
size_t n = 0; n < N; n++) {
98 const double alpha_dbl =
value_of(alpha_vec[n]);
101 logp += log_alpha[n];
103 logp += alpha_dbl * log_y_min[n];
105 logp -= alpha_dbl * log_y[n] + log_y[n];
109 operands_and_partials.
d_x1[n] -= alpha_dbl * inv_y[n] + inv_y[n];
111 operands_and_partials.
d_x2[n] += alpha_dbl /
value_of(y_min_vec[n]);
113 operands_and_partials.
d_x3[n]
114 += 1 / alpha_dbl + log_y_min[n] - log_y[n];
116 return operands_and_partials.
to_var(logp);
119 template <
typename T_y,
typename T_scale,
typename T_shape>
122 pareto_log(
const T_y& y,
const T_scale& y_min,
const T_shape& alpha) {
123 return pareto_log<false>(y,y_min,alpha);
126 template <
typename T_y,
typename T_scale,
typename T_shape>
128 pareto_cdf(
const T_y& y,
const T_scale& y_min,
const T_shape& alpha) {
136 static const char*
function =
"stan::prob::pareto_cdf(%1%)";
152 "Random variable",
"Scale parameter",
153 "Shape parameter", &P);
159 size_t N =
max_size(y, y_min, alpha);
162 operands_and_partials(y, y_min, alpha);
169 return operands_and_partials.
to_var(0.0);
174 for (
size_t n = 0; n < N; n++) {
178 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
185 const double y_min_inv_dbl = 1.0 /
value_of(y_min_vec[n]);
186 const double alpha_dbl =
value_of(alpha_vec[n]);
189 const double Pn = 1.0 -
exp( alpha_dbl * log_dbl );
194 operands_and_partials.
d_x1[n]
195 += alpha_dbl * y_min_inv_dbl *
exp( (alpha_dbl + 1) * log_dbl )
198 operands_and_partials.
d_x2[n]
199 += - alpha_dbl * y_min_inv_dbl *
exp( alpha_dbl * log_dbl ) / Pn;
201 operands_and_partials.
d_x3[n]
202 += -
exp( alpha_dbl * log_dbl ) * log_dbl / Pn;
207 operands_and_partials.
d_x1[n] *= P;
211 operands_and_partials.
d_x2[n] *= P;
215 operands_and_partials.
d_x3[n] *= P;
218 return operands_and_partials.
to_var(P);
221 template <
typename T_y,
typename T_scale,
typename T_shape>
230 static const char*
function =
"stan::prob::pareto_cdf_log(%1%)";
246 "Random variable",
"Scale parameter",
247 "Shape parameter", &P);
253 size_t N =
max_size(y, y_min, alpha);
256 operands_and_partials(y, y_min, alpha);
268 for (
size_t n = 0; n < N; n++) {
272 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
273 return operands_and_partials.
to_var(0.0);
279 const double y_min_inv_dbl = 1.0 /
value_of(y_min_vec[n]);
280 const double alpha_dbl =
value_of(alpha_vec[n]);
283 const double Pn = 1.0 -
exp(alpha_dbl * log_dbl );
288 operands_and_partials.
d_x1[n]
289 += alpha_dbl * y_min_inv_dbl *
exp((alpha_dbl + 1) * log_dbl) / Pn;
291 operands_and_partials.
d_x2[n]
292 -= alpha_dbl * y_min_inv_dbl *
exp( alpha_dbl * log_dbl ) / Pn;
294 operands_and_partials.
d_x3[n]
295 -=
exp( alpha_dbl * log_dbl ) * log_dbl / Pn;
298 return operands_and_partials.
to_var(P);
301 template <
typename T_y,
typename T_scale,
typename T_shape>
304 const T_shape& alpha) {
311 static const char*
function =
"stan::prob::pareto_ccdf_log(%1%)";
327 "Random variable",
"Scale parameter",
328 "Shape parameter", &P);
334 size_t N =
max_size(y, y_min, alpha);
337 operands_and_partials(y, y_min, alpha);
344 return operands_and_partials.
to_var(0.0);
349 for (
size_t n = 0; n < N; n++) {
353 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
360 const double y_min_inv_dbl = 1.0 /
value_of(y_min_vec[n]);
361 const double alpha_dbl =
value_of(alpha_vec[n]);
363 P += alpha_dbl * log_dbl;
366 operands_and_partials.
d_x1[n] -= alpha_dbl * y_min_inv_dbl
369 operands_and_partials.
d_x2[n] += alpha_dbl * y_min_inv_dbl;
371 operands_and_partials.
d_x3[n] += log_dbl;
374 return operands_and_partials.
to_var(P);
382 using boost::variate_generator;
383 using boost::exponential_distribution;
385 static const char*
function =
"stan::prob::pareto_rng(%1%)";
392 variate_generator<RNG&, exponential_distribution<> >
393 exp_rng(rng, exponential_distribution<>(alpha));
double pareto_rng(const double y_min, const double alpha, RNG &rng)
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.
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_scale, T_shape >::type pareto_ccdf_log(const T_y &y, const T_scale &y_min, const T_shape &alpha)
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_scale, T_shape >::type pareto_cdf_log(const T_y &y, const T_scale &y_min, const T_shape &alpha)
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)
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
return_type< T_y, T_scale, T_shape >::type pareto_log(const T_y &y, const T_scale &y_min, const T_shape &alpha)
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)
bool check_greater_or_equal(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
return_type< T_y, T_scale, T_shape >::type pareto_cdf(const T_y &y, const T_scale &y_min, const T_shape &alpha)
double negative_infinity()
Return negative infinity.