Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inv_gamma.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__INV_GAMMA_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__INV_GAMMA_HPP
3 
4 #include <boost/random/gamma_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>
16 
17 namespace stan {
18 
19  namespace prob {
20 
37  template <bool propto,
38  typename T_y, typename T_shape, typename T_scale>
39  typename return_type<T_y,T_shape,T_scale>::type
40  inv_gamma_log(const T_y& y, const T_shape& alpha, const T_scale& beta) {
41  static const char* function = "stan::prob::inv_gamma_log(%1%)";
42 
46  using boost::math::tools::promote_args;
49 
50  // check if any vectors are zero length
51  if (!(stan::length(y)
52  && stan::length(alpha)
53  && stan::length(beta)))
54  return 0.0;
55 
56  // set up return value accumulator
57  double logp(0.0);
58 
59  check_not_nan(function, y, "Random variable", &logp);
60  check_positive_finite(function, alpha, "Shape parameter", &logp);
61  check_positive_finite(function, beta, "Scale parameter", &logp);
62  check_consistent_sizes(function,
63  y,alpha,beta,
64  "Random variable","Shape parameter",
65  "Scale parameter",
66  &logp);
67 
68  // check if no variables are involved and prop-to
70  return 0.0;
71 
72  // set up template expressions wrapping scalars into vector views
73  VectorView<const T_y> y_vec(y);
74  VectorView<const T_shape> alpha_vec(alpha);
75  VectorView<const T_scale> beta_vec(beta);
76 
77  for (size_t n = 0; n < length(y); n++) {
78  const double y_dbl = value_of(y_vec[n]);
79  if (y_dbl <= 0)
80  return LOG_ZERO;
81  }
82 
83  size_t N = max_size(y, alpha, beta);
85  operands_and_partials(y, alpha, beta);
86 
87  using boost::math::lgamma;
90 
93  log_y(length(y));
96  inv_y(length(y));
97  for(size_t n = 0; n < length(y); n++) {
99  if (value_of(y_vec[n]) > 0)
100  log_y[n] = log(value_of(y_vec[n]));
102  inv_y[n] = 1.0 / value_of(y_vec[n]);
103  }
104 
107  lgamma_alpha(length(alpha));
110  digamma_alpha(length(alpha));
111  for (size_t n = 0; n < length(alpha); n++) {
113  lgamma_alpha[n] = lgamma(value_of(alpha_vec[n]));
115  digamma_alpha[n] = digamma(value_of(alpha_vec[n]));
116  }
117 
120  log_beta(length(beta));
122  for (size_t n = 0; n < length(beta); n++)
123  log_beta[n] = log(value_of(beta_vec[n]));
124 
125  for (size_t n = 0; n < N; n++) {
126  // pull out values of arguments
127  const double alpha_dbl = value_of(alpha_vec[n]);
128  const double beta_dbl = value_of(beta_vec[n]);
129 
131  logp -= lgamma_alpha[n];
133  logp += alpha_dbl * log_beta[n];
135  logp -= (alpha_dbl+1.0) * log_y[n];
137  logp -= beta_dbl * inv_y[n];
138 
139  // gradients
140  if (!is_constant<typename is_vector<T_y>::type>::value)
141  operands_and_partials.d_x1[n]
142  += -(alpha_dbl+1) * inv_y[n] + beta_dbl * inv_y[n] * inv_y[n];
143  if (!is_constant<typename is_vector<T_shape>::type>::value)
144  operands_and_partials.d_x2[n]
145  += -digamma_alpha[n] + log_beta[n] - log_y[n];
146  if (!is_constant<typename is_vector<T_scale>::type>::value)
147  operands_and_partials.d_x3[n] += alpha_dbl / beta_dbl - inv_y[n];
148  }
149  return operands_and_partials.to_var(logp);
150  }
151 
152  template <typename T_y, typename T_shape, typename T_scale>
153  inline
155  inv_gamma_log(const T_y& y, const T_shape& alpha, const T_scale& beta) {
156  return inv_gamma_log<false>(y,alpha,beta);
157  }
158 
175  template <typename T_y, typename T_shape, typename T_scale>
177  inv_gamma_cdf(const T_y& y, const T_shape& alpha, const T_scale& beta) {
178 
179  // Size checks
180  if (!(stan::length(y) && stan::length(alpha) && stan::length(beta)))
181  return 1.0;
182 
183  // Error checks
184  static const char* function = "stan::prob::inv_gamma_cdf(%1%)";
185 
192  using stan::math::value_of;
193  using boost::math::tools::promote_args;
194 
195  double P(1.0);
196 
197  check_positive_finite(function, alpha, "Shape parameter", &P);
198  check_positive_finite(function, beta, "Scale parameter", &P);
199  check_not_nan(function, y, "Random variable", &P);
200  check_nonnegative(function, y, "Random variable", &P);
201  check_consistent_sizes(function, y, alpha, beta,
202  "Random variable", "Shape parameter",
203  "Scale Parameter",
204  &P);
205 
206  // Wrap arguments in vectors
207  VectorView<const T_y> y_vec(y);
208  VectorView<const T_shape> alpha_vec(alpha);
209  VectorView<const T_scale> beta_vec(beta);
210  size_t N = max_size(y, alpha, beta);
211 
213  operands_and_partials(y, alpha, beta);
214 
215  // Explicit return for extreme values
216  // The gradients are technically ill-defined, but treated as zero
217 
218  for (size_t i = 0; i < stan::length(y); i++) {
219  if (value_of(y_vec[i]) == 0)
220  return operands_and_partials.to_var(0.0);
221  }
222 
223  // Compute CDF and its gradients
224  using boost::math::gamma_p_derivative;
225  using boost::math::gamma_q;
226  using boost::math::digamma;
227  using boost::math::tgamma;
228 
229  // Cache a few expensive function calls if nu is a parameter
232  gamma_vec(stan::length(alpha));
235  digamma_vec(stan::length(alpha));
236 
238  for (size_t i = 0; i < stan::length(alpha); i++) {
239  const double alpha_dbl = value_of(alpha_vec[i]);
240  gamma_vec[i] = tgamma(alpha_dbl);
241  digamma_vec[i] = digamma(alpha_dbl);
242  }
243  }
244 
245  // Compute vectorized CDF and gradient
246  for (size_t n = 0; n < N; n++) {
247  // Explicit results for extreme values
248  // The gradients are technically ill-defined, but treated as zero
249  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
250  continue;
251 
252  // Pull out values
253  const double y_dbl = value_of(y_vec[n]);
254  const double y_inv_dbl = 1.0 / y_dbl;
255  const double alpha_dbl = value_of(alpha_vec[n]);
256  const double beta_dbl = value_of(beta_vec[n]);
257 
258  // Compute
259  const double Pn = gamma_q(alpha_dbl, beta_dbl * y_inv_dbl);
260 
261  P *= Pn;
262 
264  operands_and_partials.d_x1[n]
265  += beta_dbl * y_inv_dbl * y_inv_dbl
266  * gamma_p_derivative(alpha_dbl, beta_dbl * y_inv_dbl)
267  / Pn;
269  operands_and_partials.d_x2[n]
270  += stan::math::gradRegIncGamma(alpha_dbl, beta_dbl
271  * y_inv_dbl, gamma_vec[n],
272  digamma_vec[n]) / Pn;
274  operands_and_partials.d_x3[n]
275  += - y_inv_dbl * gamma_p_derivative(alpha_dbl,
276  beta_dbl * y_inv_dbl) / Pn;
277  }
278 
280  for (size_t n = 0; n < stan::length(y); ++n)
281  operands_and_partials.d_x1[n] *= P;
283  for (size_t n = 0; n < stan::length(alpha); ++n)
284  operands_and_partials.d_x2[n] *= P;
286  for (size_t n = 0; n < stan::length(beta); ++n)
287  operands_and_partials.d_x3[n] *= P;
288 
289  return operands_and_partials.to_var(P);
290  }
291 
292  template <typename T_y, typename T_shape, typename T_scale>
294  inv_gamma_cdf_log(const T_y& y, const T_shape& alpha, const T_scale& beta) {
295 
296  // Size checks
297  if (!(stan::length(y) && stan::length(alpha) && stan::length(beta)))
298  return 0.0;
299 
300  // Error checks
301  static const char* function = "stan::prob::inv_gamma_cdf_log(%1%)";
302 
309  using stan::math::value_of;
310  using boost::math::tools::promote_args;
311 
312  double P(0.0);
313 
314  check_positive_finite(function, alpha, "Shape parameter", &P);
315  check_positive_finite(function, beta, "Scale parameter", &P);
316  check_not_nan(function, y, "Random variable", &P);
317  check_nonnegative(function, y, "Random variable", &P);
318  check_consistent_sizes(function, y, alpha, beta,
319  "Random variable", "Shape parameter",
320  "Scale Parameter",
321  &P);
322 
323  // Wrap arguments in vectors
324  VectorView<const T_y> y_vec(y);
325  VectorView<const T_shape> alpha_vec(alpha);
326  VectorView<const T_scale> beta_vec(beta);
327  size_t N = max_size(y, alpha, beta);
328 
330  operands_and_partials(y, alpha, beta);
331 
332  // Explicit return for extreme values
333  // The gradients are technically ill-defined, but treated as zero
334 
335  for (size_t i = 0; i < stan::length(y); i++) {
336  if (value_of(y_vec[i]) == 0)
337  return operands_and_partials.to_var(stan::math::negative_infinity());
338  }
339 
340  // Compute cdf_log and its gradients
341  using boost::math::gamma_p_derivative;
342  using boost::math::gamma_q;
343  using boost::math::digamma;
344  using boost::math::tgamma;
345 
346  // Cache a few expensive function calls if nu is a parameter
349  gamma_vec(stan::length(alpha));
352  digamma_vec(stan::length(alpha));
353 
355  for (size_t i = 0; i < stan::length(alpha); i++) {
356  const double alpha_dbl = value_of(alpha_vec[i]);
357  gamma_vec[i] = tgamma(alpha_dbl);
358  digamma_vec[i] = digamma(alpha_dbl);
359  }
360  }
361 
362  // Compute vectorized cdf_log and gradient
363  for (size_t n = 0; n < N; n++) {
364  // Explicit results for extreme values
365  // The gradients are technically ill-defined, but treated as zero
366  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
367  continue;
368 
369  // Pull out values
370  const double y_dbl = value_of(y_vec[n]);
371  const double y_inv_dbl = 1.0 / y_dbl;
372  const double alpha_dbl = value_of(alpha_vec[n]);
373  const double beta_dbl = value_of(beta_vec[n]);
374 
375  // Compute
376  const double Pn = gamma_q(alpha_dbl, beta_dbl * y_inv_dbl);
377 
378  P += log(Pn);
379 
381  operands_and_partials.d_x1[n]
382  += beta_dbl * y_inv_dbl * y_inv_dbl
383  * gamma_p_derivative(alpha_dbl, beta_dbl * y_inv_dbl)
384  / Pn;
386  operands_and_partials.d_x2[n]
387  += stan::math::gradRegIncGamma(alpha_dbl, beta_dbl
388  * y_inv_dbl, gamma_vec[n],
389  digamma_vec[n]) / Pn;
391  operands_and_partials.d_x3[n]
392  += - y_inv_dbl * gamma_p_derivative(alpha_dbl,
393  beta_dbl * y_inv_dbl) / Pn;
394  }
395 
396  return operands_and_partials.to_var(P);
397  }
398 
399  template <typename T_y, typename T_shape, typename T_scale>
401  inv_gamma_ccdf_log(const T_y& y, const T_shape& alpha, const T_scale& beta) {
402 
403  // Size checks
404  if (!(stan::length(y) && stan::length(alpha) && stan::length(beta)))
405  return 0.0;
406 
407  // Error checks
408  static const char* function = "stan::prob::inv_gamma_ccdf_log(%1%)";
409 
416  using stan::math::value_of;
417  using boost::math::tools::promote_args;
418 
419  double P(0.0);
420 
421  check_positive_finite(function, alpha, "Shape parameter", &P);
422  check_positive_finite(function, beta, "Scale parameter", &P);
423  check_not_nan(function, y, "Random variable", &P);
424  check_nonnegative(function, y, "Random variable", &P);
425  check_consistent_sizes(function, y, alpha, beta,
426  "Random variable", "Shape parameter",
427  "Scale Parameter",
428  &P);
429 
430  // Wrap arguments in vectors
431  VectorView<const T_y> y_vec(y);
432  VectorView<const T_shape> alpha_vec(alpha);
433  VectorView<const T_scale> beta_vec(beta);
434  size_t N = max_size(y, alpha, beta);
435 
437  operands_and_partials(y, alpha, beta);
438 
439  // Explicit return for extreme values
440  // The gradients are technically ill-defined, but treated as zero
441 
442  for (size_t i = 0; i < stan::length(y); i++) {
443  if (value_of(y_vec[i]) == 0)
444  return operands_and_partials.to_var(0.0);
445  }
446 
447  // Compute ccdf_log and its gradients
448  using boost::math::gamma_p_derivative;
449  using boost::math::gamma_q;
450  using boost::math::digamma;
451  using boost::math::tgamma;
452 
453  // Cache a few expensive function calls if nu is a parameter
456  gamma_vec(stan::length(alpha));
459  digamma_vec(stan::length(alpha));
460 
462  for (size_t i = 0; i < stan::length(alpha); i++) {
463  const double alpha_dbl = value_of(alpha_vec[i]);
464  gamma_vec[i] = tgamma(alpha_dbl);
465  digamma_vec[i] = digamma(alpha_dbl);
466  }
467  }
468 
469  // Compute vectorized ccdf_log and gradient
470  for (size_t n = 0; n < N; n++) {
471  // Explicit results for extreme values
472  // The gradients are technically ill-defined, but treated as zero
473  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity())
474  return operands_and_partials.to_var(stan::math::negative_infinity());
475 
476  // Pull out values
477  const double y_dbl = value_of(y_vec[n]);
478  const double y_inv_dbl = 1.0 / y_dbl;
479  const double alpha_dbl = value_of(alpha_vec[n]);
480  const double beta_dbl = value_of(beta_vec[n]);
481 
482  // Compute
483  const double Pn = 1.0 - gamma_q(alpha_dbl, beta_dbl * y_inv_dbl);
484 
485  P += log(Pn);
486 
488  operands_and_partials.d_x1[n]
489  -= beta_dbl * y_inv_dbl * y_inv_dbl
490  * gamma_p_derivative(alpha_dbl, beta_dbl * y_inv_dbl)
491  / Pn;
493  operands_and_partials.d_x2[n]
494  -= stan::math::gradRegIncGamma(alpha_dbl, beta_dbl
495  * y_inv_dbl, gamma_vec[n],
496  digamma_vec[n]) / Pn;
498  operands_and_partials.d_x3[n]
499  -= - y_inv_dbl * gamma_p_derivative(alpha_dbl,
500  beta_dbl * y_inv_dbl) / Pn;
501  }
502 
503  return operands_and_partials.to_var(P);
504  }
505 
506  template <class RNG>
507  inline double
508  inv_gamma_rng(const double alpha,
509  const double beta,
510  RNG& rng) {
511  using boost::variate_generator;
512  using boost::random::gamma_distribution;
513 
514  static const char* function = "stan::prob::inv_gamma_rng(%1%)";
515 
517 
518  check_positive_finite(function, alpha, "Shape parameter", (double*)0);
519  check_positive_finite(function, beta, "Scale parameter", (double*)0);
520 
521  variate_generator<RNG&, gamma_distribution<> >
522  gamma_rng(rng, gamma_distribution<>(alpha, 1 / beta));
523  return 1 / gamma_rng();
524  }
525  }
526 }
527 
528 #endif
return_type< T_y, T_shape, T_scale >::type inv_gamma_cdf_log(const T_y &y, const T_shape &alpha, const T_scale &beta)
Definition: inv_gamma.hpp:294
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Definition: traits.hpp:43
fvar< T > tgamma(const fvar< T > &x)
Definition: tgamma.hpp:15
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
return_type< T_y, T_shape, T_scale >::type inv_gamma_cdf(const T_y &y, const T_shape &alpha, const T_scale &beta)
The CDF of an inverse gamma density for y with the specified shape and scale parameters.
Definition: inv_gamma.hpp:177
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
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
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
return_type< T_y, T_shape, T_scale >::type inv_gamma_log(const T_y &y, const T_shape &alpha, const T_scale &beta)
The log of an inverse gamma density for y with the specified shape and scale parameters.
Definition: inv_gamma.hpp:40
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_q.hpp:15
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
double gradRegIncGamma(double a, double z, double g, double dig, double precision=1e-6)
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
double inv_gamma_rng(const double alpha, const double beta, RNG &rng)
Definition: inv_gamma.hpp:508
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
return_type< T_y, T_shape, T_scale >::type inv_gamma_ccdf_log(const T_y &y, const T_shape &alpha, const T_scale &beta)
Definition: inv_gamma.hpp:401
bool check_less_or_equal(const char *function, const T_y &y, const T_high &high, 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.
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16
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)
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
double gamma_rng(const double alpha, const double beta, RNG &rng)
Definition: gamma.hpp:491
bool check_greater_or_equal(const char *function, const T_y &y, const T_low &low, const char *name, T_result *result)
double negative_infinity()
Return negative infinity.
Definition: constants.hpp:123

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