Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pareto.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__PARETO_HPP
2 #define STAN__PROB__DISTRIBUTIONS__PARETO_HPP
3 
4 #include <boost/random/exponential_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
6 
12 #include <stan/meta/traits.hpp>
13 #include <stan/prob/constants.hpp>
14 #include <stan/prob/traits.hpp>
15 
16 
17 namespace stan {
18  namespace prob {
19 
20  // Pareto(y|y_m,alpha) [y > y_m; y_m > 0; alpha > 0]
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%)";
26 
31 
32  // check if any vectors are zero length
33  if (!(stan::length(y)
34  && stan::length(y_min)
35  && stan::length(alpha)))
36  return 0.0;
37 
38  // set up return value accumulator
39  double logp(0.0);
40 
41  // validate args (here done over var, which should be OK)
42  check_not_nan(function, y, "Random variable", &logp);
43  check_positive_finite(function, y_min, "Scale parameter", &logp);
44  check_positive_finite(function, alpha, "Shape parameter", &logp);
45  check_consistent_sizes(function,
46  y,y_min,alpha,
47  "Random variable","Scale parameter",
48  "Shape parameter",
49  &logp);
50 
51  // check if no variables are involved and prop-to
53  return 0.0;
54 
55  VectorView<const T_y> y_vec(y);
56  VectorView<const T_scale> y_min_vec(y_min);
57  VectorView<const T_shape> alpha_vec(alpha);
58  size_t N = max_size(y, y_min, alpha);
59 
60  for (size_t n = 0; n < N; n++) {
61  if (y_vec[n] < y_min_vec[n])
62  return LOG_ZERO;
63  }
64 
65  // set up template expressions wrapping scalars into vector views
67  operands_and_partials(y, y_min, alpha);
68 
70  is_vector<T_y>::value> log_y(length(y));
72  for (size_t n = 0; n < length(y); n++)
73  log_y[n] = log(value_of(y_vec[n]));
74 
77  is_vector<T_y>::value> inv_y(length(y));
79  for (size_t n = 0; n < length(y); n++)
80  inv_y[n] = 1 / value_of(y_vec[n]);
81 
84  log_y_min(length(y_min));
86  for (size_t n = 0; n < length(y_min); n++)
87  log_y_min[n] = log(value_of(y_min_vec[n]));
88 
90  is_vector<T_shape>::value> log_alpha(length(alpha));
92  for (size_t n = 0; n < length(alpha); n++)
93  log_alpha[n] = log(value_of(alpha_vec[n]));
94 
96 
97  for (size_t n = 0; n < N; n++) {
98  const double alpha_dbl = value_of(alpha_vec[n]);
99  // log probability
101  logp += log_alpha[n];
103  logp += alpha_dbl * log_y_min[n];
105  logp -= alpha_dbl * log_y[n] + log_y[n];
106 
107  // gradients
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];
115  }
116  return operands_and_partials.to_var(logp);
117  }
118 
119  template <typename T_y, typename T_scale, typename T_shape>
120  inline
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);
124  }
125 
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) {
129 
130  // Check sizes
131  // Size checks
132  if ( !( stan::length(y) && stan::length(y_min) && stan::length(alpha) ) )
133  return 1.0;
134 
135  // Check errors
136  static const char* function = "stan::prob::pareto_cdf(%1%)";
137 
143  using stan::math::value_of;
144 
145  double P(1.0);
146 
147  check_not_nan(function, y, "Random variable", &P);
148  check_nonnegative(function, y, "Random variable", &P);
149  check_positive_finite(function, y_min, "Scale parameter", &P);
150  check_positive_finite(function, alpha, "Shape parameter", &P);
151  check_consistent_sizes(function, y, y_min, alpha,
152  "Random variable", "Scale parameter",
153  "Shape parameter", &P);
154 
155  // Wrap arguments in vectors
156  VectorView<const T_y> y_vec(y);
157  VectorView<const T_scale> y_min_vec(y_min);
158  VectorView<const T_shape> alpha_vec(alpha);
159  size_t N = max_size(y, y_min, alpha);
160 
162  operands_and_partials(y, y_min, alpha);
163 
164  // Explicit return for extreme values
165  // The gradients are technically ill-defined, but treated as zero
166 
167  for (size_t i = 0; i < stan::length(y); i++) {
168  if (value_of(y_vec[i]) < value_of(y_min_vec[i]))
169  return operands_and_partials.to_var(0.0);
170  }
171 
172  // Compute vectorized CDF and its gradients
173 
174  for (size_t n = 0; n < N; n++) {
175 
176  // Explicit results for extreme values
177  // The gradients are technically ill-defined, but treated as zero
178  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
179  continue;
180  }
181 
182  // Pull out values
183  const double log_dbl = log( value_of(y_min_vec[n])
184  / value_of(y_vec[n]) );
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]);
187 
188  // Compute
189  const double Pn = 1.0 - exp( alpha_dbl * log_dbl );
190 
191  P *= Pn;
192 
194  operands_and_partials.d_x1[n]
195  += alpha_dbl * y_min_inv_dbl * exp( (alpha_dbl + 1) * log_dbl )
196  / Pn;
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;
203  }
204 
206  for(size_t n = 0; n < stan::length(y); ++n)
207  operands_and_partials.d_x1[n] *= P;
208  }
210  for(size_t n = 0; n < stan::length(y_min); ++n)
211  operands_and_partials.d_x2[n] *= P;
212  }
214  for(size_t n = 0; n < stan::length(alpha); ++n)
215  operands_and_partials.d_x3[n] *= P;
216  }
217 
218  return operands_and_partials.to_var(P);
219  }
220 
221  template <typename T_y, typename T_scale, typename T_shape>
223  pareto_cdf_log(const T_y& y, const T_scale& y_min, const T_shape& alpha) {
224 
225  // Size checks
226  if ( !( stan::length(y) && stan::length(y_min) && stan::length(alpha) ) )
227  return 0.0;
228 
229  // Check errors
230  static const char* function = "stan::prob::pareto_cdf_log(%1%)";
231 
237  using stan::math::value_of;
238 
239  double P(0.0);
240 
241  check_not_nan(function, y, "Random variable", &P);
242  check_nonnegative(function, y, "Random variable", &P);
243  check_positive_finite(function, y_min, "Scale parameter", &P);
244  check_positive_finite(function, alpha, "Shape parameter", &P);
245  check_consistent_sizes(function, y, y_min, alpha,
246  "Random variable", "Scale parameter",
247  "Shape parameter", &P);
248 
249  // Wrap arguments in vectors
250  VectorView<const T_y> y_vec(y);
251  VectorView<const T_scale> y_min_vec(y_min);
252  VectorView<const T_shape> alpha_vec(alpha);
253  size_t N = max_size(y, y_min, alpha);
254 
256  operands_and_partials(y, y_min, alpha);
257 
258  // Explicit return for extreme values
259  // The gradients are technically ill-defined, but treated as zero
260 
261  for (size_t i = 0; i < stan::length(y); i++) {
262  if (value_of(y_vec[i]) < value_of(y_min_vec[i]))
263  return operands_and_partials.to_var(stan::math::negative_infinity());
264  }
265 
266  // Compute vectorized cdf_log and its gradients
267 
268  for (size_t n = 0; n < N; n++) {
269 
270  // Explicit results for extreme values
271  // The gradients are technically ill-defined, but treated as zero
272  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
273  return operands_and_partials.to_var(0.0);
274  }
275 
276  // Pull out values
277  const double log_dbl = log( value_of(y_min_vec[n])
278  / value_of(y_vec[n]) );
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]);
281 
282  // Compute
283  const double Pn = 1.0 - exp(alpha_dbl * log_dbl );
284 
285  P += log(Pn);
286 
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;
296  }
297 
298  return operands_and_partials.to_var(P);
299  }
300 
301  template <typename T_y, typename T_scale, typename T_shape>
303  pareto_ccdf_log(const T_y& y, const T_scale& y_min,
304  const T_shape& alpha) {
305 
306  // Size checks
307  if ( !( stan::length(y) && stan::length(y_min) && stan::length(alpha) ) )
308  return 0.0;
309 
310  // Check errors
311  static const char* function = "stan::prob::pareto_ccdf_log(%1%)";
312 
318  using stan::math::value_of;
319 
320  double P(0.0);
321 
322  check_not_nan(function, y, "Random variable", &P);
323  check_nonnegative(function, y, "Random variable", &P);
324  check_positive_finite(function, y_min, "Scale parameter", &P);
325  check_positive_finite(function, alpha, "Shape parameter", &P);
326  check_consistent_sizes(function, y, y_min, alpha,
327  "Random variable", "Scale parameter",
328  "Shape parameter", &P);
329 
330  // Wrap arguments in vectors
331  VectorView<const T_y> y_vec(y);
332  VectorView<const T_scale> y_min_vec(y_min);
333  VectorView<const T_shape> alpha_vec(alpha);
334  size_t N = max_size(y, y_min, alpha);
335 
337  operands_and_partials(y, y_min, alpha);
338 
339  // Explicit return for extreme values
340  // The gradients are technically ill-defined, but treated as zero
341 
342  for (size_t i = 0; i < stan::length(y); i++) {
343  if (value_of(y_vec[i]) < value_of(y_min_vec[i]))
344  return operands_and_partials.to_var(0.0);
345  }
346 
347  // Compute vectorized cdf_log and its gradients
348 
349  for (size_t n = 0; n < N; n++) {
350 
351  // Explicit results for extreme values
352  // The gradients are technically ill-defined, but treated as zero
353  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
354  return operands_and_partials.to_var(stan::math::negative_infinity());
355  }
356 
357  // Pull out values
358  const double log_dbl = log( value_of(y_min_vec[n])
359  / value_of(y_vec[n]) );
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]);
362 
363  P += alpha_dbl * log_dbl;
364 
366  operands_and_partials.d_x1[n] -= alpha_dbl * y_min_inv_dbl
367  * exp(log_dbl);
369  operands_and_partials.d_x2[n] += alpha_dbl * y_min_inv_dbl;
371  operands_and_partials.d_x3[n] += log_dbl;
372  }
373 
374  return operands_and_partials.to_var(P);
375  }
376 
377  template <class RNG>
378  inline double
379  pareto_rng(const double y_min,
380  const double alpha,
381  RNG& rng) {
382  using boost::variate_generator;
383  using boost::exponential_distribution;
384 
385  static const char* function = "stan::prob::pareto_rng(%1%)";
386 
388 
389  check_positive_finite(function, y_min, "Scale parameter", (double*)0);
390  check_positive_finite(function, alpha, "Shape parameter", (double*)0);
391 
392  variate_generator<RNG&, exponential_distribution<> >
393  exp_rng(rng, exponential_distribution<>(alpha));
394  return y_min * std::exp(exp_rng());
395  }
396  }
397 }
398 #endif
double pareto_rng(const double y_min, const double alpha, RNG &rng)
Definition: pareto.hpp:379
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...
size_t length(const T &)
Definition: traits.hpp:159
DoubleVectorView allocates double values to be used as intermediate values.
Definition: traits.hpp:358
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
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
Definition: traits.hpp:406
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)
Definition: pareto.hpp:303
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: traits.hpp:57
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Definition: value_of.hpp:24
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
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)
Definition: pareto.hpp:223
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)
Definition: traits.hpp:191
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)
Definition: pareto.hpp:24
VectorView< double *, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: traits.hpp:275
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:16
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)
Definition: pareto.hpp:128
double negative_infinity()
Return negative infinity.
Definition: constants.hpp:123

     [ Stan Home Page ] © 2011–2014, Stan Development Team.