Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inv_chi_square.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DIST__UNI__CONTINUOUS__INV_CHI_SQUARE_HPP
2 #define STAN__PROB__DIST__UNI__CONTINUOUS__INV_CHI_SQUARE_HPP
3 
4 #include <boost/random/chi_squared_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 
40  template <bool propto,
41  typename T_y, typename T_dof>
42  typename return_type<T_y,T_dof>::type
43  inv_chi_square_log(const T_y& y, const T_dof& nu) {
44  static const char* function = "stan::prob::inv_chi_square_log(%1%)";
45 
46  // check if any vectors are zero length
47  if (!(stan::length(y)
48  && stan::length(nu)))
49  return 0.0;
50 
55 
56  double logp(0.0);
57  check_positive_finite(function, nu, "Degrees of freedom parameter",
58  &logp);
59  check_not_nan(function, y, "Random variable", &logp);
60  check_consistent_sizes(function,
61  y,nu,
62  "Random variable","Degrees of freedom parameter",
63  &logp);
64 
65 
66  // set up template expressions wrapping scalars into vector views
67  VectorView<const T_y> y_vec(y);
68  VectorView<const T_dof> nu_vec(nu);
69  size_t N = max_size(y, nu);
70 
71  for (size_t n = 0; n < length(y); n++)
72  if (value_of(y_vec[n]) <= 0)
73  return LOG_ZERO;
74 
76  using boost::math::lgamma;
78 
80  is_vector<T_y>::value> log_y(length(y));
81  for (size_t i = 0; i < length(y); i++)
83  log_y[i] = log(value_of(y_vec[i]));
84 
86  is_vector<T_y>::value> inv_y(length(y));
87  for (size_t i = 0; i < length(y); i++)
89  inv_y[i] = 1.0 / value_of(y_vec[i]);
90 
92  is_vector<T_dof>::value> lgamma_half_nu(length(nu));
94  is_vector<T_dof>::value> digamma_half_nu_over_two(length(nu));
95  for (size_t i = 0; i < length(nu); i++) {
96  double half_nu = 0.5 * value_of(nu_vec[i]);
98  lgamma_half_nu[i] = lgamma(half_nu);
100  digamma_half_nu_over_two[i] = digamma(half_nu) * 0.5;
101  }
102 
103  agrad::OperandsAndPartials<T_y, T_dof> operands_and_partials(y, nu);
104  for (size_t n = 0; n < N; n++) {
105  const double nu_dbl = value_of(nu_vec[n]);
106  const double half_nu = 0.5 * nu_dbl;
107 
109  logp += nu_dbl * NEG_LOG_TWO_OVER_TWO - lgamma_half_nu[n];
111  logp -= (half_nu+1.0) * log_y[n];
113  logp -= 0.5 * inv_y[n];
114 
116  operands_and_partials.d_x1[n]
117  += -(half_nu+1.0) * inv_y[n] + 0.5 * inv_y[n] * inv_y[n];
118  }
120  operands_and_partials.d_x2[n]
121  += NEG_LOG_TWO_OVER_TWO - digamma_half_nu_over_two[n]
122  - 0.5*log_y[n];
123  }
124  }
125  return operands_and_partials.to_var(logp);
126  }
127 
128  template <typename T_y, typename T_dof>
129  inline
131  inv_chi_square_log(const T_y& y, const T_dof& nu) {
132  return inv_chi_square_log<false>(y,nu);
133  }
134 
135  template <typename T_y, typename T_dof>
137  inv_chi_square_cdf(const T_y& y, const T_dof& nu) {
138 
139  // Size checks
140  if ( !( stan::length(y) && stan::length(nu) ) ) return 1.0;
141 
142  // Error checks
143  static const char* function = "stan::prob::inv_chi_square_cdf(%1%)";
144 
149  using boost::math::tools::promote_args;
150  using stan::math::value_of;
151 
152  double P(1.0);
153 
154  check_positive_finite(function, nu, "Degrees of freedom parameter", &P);
155  check_not_nan(function, y, "Random variable", &P);
156  check_nonnegative(function, y, "Random variable", &P);
157  check_consistent_sizes(function, y, nu,
158  "Random variable",
159  "Degrees of freedom parameter",
160  &P);
161 
162  // Wrap arguments in vectors
163  VectorView<const T_y> y_vec(y);
164  VectorView<const T_dof> nu_vec(nu);
165  size_t N = max_size(y, nu);
166 
167  agrad::OperandsAndPartials<T_y, T_dof> operands_and_partials(y, nu);
168 
169  // Explicit return for extreme values
170  // The gradients are technically ill-defined, but treated as zero
171 
172  for (size_t i = 0; i < stan::length(y); i++)
173  if (value_of(y_vec[i]) == 0)
174  return operands_and_partials.to_var(0.0);
175 
176  // Compute CDF and its gradients
177  using boost::math::gamma_p_derivative;
178  using boost::math::gamma_q;
179  using boost::math::tgamma;
180  using boost::math::digamma;
181 
182  // Cache a few expensive function calls if nu is a parameter
184  is_vector<T_dof>::value> gamma_vec(stan::length(nu));
186  is_vector<T_dof>::value> digamma_vec(stan::length(nu));
187 
189  for (size_t i = 0; i < stan::length(nu); i++) {
190  const double nu_dbl = value_of(nu_vec[i]);
191  gamma_vec[i] = tgamma(0.5 * nu_dbl);
192  digamma_vec[i] = digamma(0.5 * nu_dbl);
193  }
194  }
195 
196  // Compute vectorized CDF and gradient
197  for (size_t n = 0; n < N; n++) {
198 
199  // Explicit results for extreme values
200  // The gradients are technically ill-defined, but treated as zero
201  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
202  continue;
203  }
204 
205  // Pull out values
206  const double y_dbl = value_of(y_vec[n]);
207  const double y_inv_dbl = 1.0 / y_dbl;
208  const double nu_dbl = value_of(nu_vec[n]);
209 
210  // Compute
211  const double Pn = gamma_q(0.5 * nu_dbl, 0.5 * y_inv_dbl);
212 
213  P *= Pn;
214 
216  operands_and_partials.d_x1[n]
217  += 0.5 * y_inv_dbl * y_inv_dbl
218  * gamma_p_derivative(0.5 * nu_dbl, 0.5 * y_inv_dbl) / Pn;
220  operands_and_partials.d_x2[n]
221  += 0.5 * stan::math::gradRegIncGamma(0.5 * nu_dbl,
222  0.5 * y_inv_dbl,
223  gamma_vec[n],
224  digamma_vec[n]) / Pn;
225  }
226 
228  for (size_t n = 0; n < stan::length(y); ++n)
229  operands_and_partials.d_x1[n] *= P;
231  for (size_t n = 0; n < stan::length(nu); ++n)
232  operands_and_partials.d_x2[n] *= P;
233 
234  return operands_and_partials.to_var(P);
235  }
236 
237  template <typename T_y, typename T_dof>
239  inv_chi_square_cdf_log(const T_y& y, const T_dof& nu) {
240 
241  // Size checks
242  if ( !( stan::length(y) && stan::length(nu) ) ) return 0.0;
243 
244  // Error checks
245  static const char* function = "stan::prob::inv_chi_square_cdf_log(%1%)";
246 
251  using boost::math::tools::promote_args;
252  using stan::math::value_of;
253 
254  double P(0.0);
255 
256  check_positive_finite(function, nu, "Degrees of freedom parameter", &P);
257  check_not_nan(function, y, "Random variable", &P);
258  check_nonnegative(function, y, "Random variable", &P);
259  check_consistent_sizes(function, y, nu,
260  "Random variable",
261  "Degrees of freedom parameter", &P);
262 
263  // Wrap arguments in vectors
264  VectorView<const T_y> y_vec(y);
265  VectorView<const T_dof> nu_vec(nu);
266  size_t N = max_size(y, nu);
267 
268  agrad::OperandsAndPartials<T_y, T_dof> operands_and_partials(y, nu);
269 
270  // Explicit return for extreme values
271  // The gradients are technically ill-defined, but treated as zero
272 
273  for (size_t i = 0; i < stan::length(y); i++)
274  if (value_of(y_vec[i]) == 0)
275  return operands_and_partials.to_var(stan::math::negative_infinity());
276 
277  // Compute cdf_log and its gradients
278  using boost::math::gamma_p_derivative;
279  using boost::math::gamma_q;
280  using boost::math::tgamma;
281  using boost::math::digamma;
282 
283  // Cache a few expensive function calls if nu is a parameter
285  is_vector<T_dof>::value> gamma_vec(stan::length(nu));
287  is_vector<T_dof>::value> digamma_vec(stan::length(nu));
288 
290  for (size_t i = 0; i < stan::length(nu); i++) {
291  const double nu_dbl = value_of(nu_vec[i]);
292  gamma_vec[i] = tgamma(0.5 * nu_dbl);
293  digamma_vec[i] = digamma(0.5 * nu_dbl);
294  }
295  }
296 
297  // Compute vectorized cdf_log and gradient
298  for (size_t n = 0; n < N; n++) {
299 
300  // Explicit results for extreme values
301  // The gradients are technically ill-defined, but treated as zero
302  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
303  continue;
304  }
305 
306  // Pull out values
307  const double y_dbl = value_of(y_vec[n]);
308  const double y_inv_dbl = 1.0 / y_dbl;
309  const double nu_dbl = value_of(nu_vec[n]);
310 
311  // Compute
312  const double Pn = gamma_q(0.5 * nu_dbl, 0.5 * y_inv_dbl);
313 
314  P += log(Pn);
315 
317  operands_and_partials.d_x1[n] += 0.5 * y_inv_dbl * y_inv_dbl
318  * gamma_p_derivative(0.5 * nu_dbl, 0.5 * y_inv_dbl) / Pn;
320  operands_and_partials.d_x2[n]
321  += 0.5 * stan::math::gradRegIncGamma(0.5 * nu_dbl,
322  0.5 * y_inv_dbl,
323  gamma_vec[n],
324  digamma_vec[n]) / Pn;
325  }
326 
327  return operands_and_partials.to_var(P);
328  }
329 
330  template <typename T_y, typename T_dof>
332  inv_chi_square_ccdf_log(const T_y& y, const T_dof& nu) {
333 
334  // Size checks
335  if ( !( stan::length(y) && stan::length(nu) ) ) return 0.0;
336 
337  // Error checks
338  static const char* function = "stan::prob::inv_chi_square_ccdf_log(%1%)";
339 
344  using boost::math::tools::promote_args;
345  using stan::math::value_of;
346 
347  double P(0.0);
348 
349  check_positive_finite(function, nu, "Degrees of freedom parameter", &P);
350  check_not_nan(function, y, "Random variable", &P);
351  check_nonnegative(function, y, "Random variable", &P);
352  check_consistent_sizes(function, y, nu,
353  "Random variable",
354  "Degrees of freedom parameter", &P);
355 
356  // Wrap arguments in vectors
357  VectorView<const T_y> y_vec(y);
358  VectorView<const T_dof> nu_vec(nu);
359  size_t N = max_size(y, nu);
360 
361  agrad::OperandsAndPartials<T_y, T_dof> operands_and_partials(y, nu);
362 
363  // Explicit return for extreme values
364  // The gradients are technically ill-defined, but treated as zero
365 
366  for (size_t i = 0; i < stan::length(y); i++)
367  if (value_of(y_vec[i]) == 0)
368  return operands_and_partials.to_var(0.0);
369 
370  // Compute ccdf_log and its gradients
371  using boost::math::gamma_p_derivative;
372  using boost::math::gamma_q;
373  using boost::math::tgamma;
374  using boost::math::digamma;
375 
376  // Cache a few expensive function calls if nu is a parameter
378  is_vector<T_dof>::value> gamma_vec(stan::length(nu));
380  is_vector<T_dof>::value> digamma_vec(stan::length(nu));
381 
383  for (size_t i = 0; i < stan::length(nu); i++) {
384  const double nu_dbl = value_of(nu_vec[i]);
385  gamma_vec[i] = tgamma(0.5 * nu_dbl);
386  digamma_vec[i] = digamma(0.5 * nu_dbl);
387  }
388  }
389 
390  // Compute vectorized ccdf_log and gradient
391  for (size_t n = 0; n < N; n++) {
392 
393  // Explicit results for extreme values
394  // The gradients are technically ill-defined, but treated as zero
395  if (value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
396  return operands_and_partials.to_var(stan::math::negative_infinity());
397  }
398 
399  // Pull out values
400  const double y_dbl = value_of(y_vec[n]);
401  const double y_inv_dbl = 1.0 / y_dbl;
402  const double nu_dbl = value_of(nu_vec[n]);
403 
404  // Compute
405  const double Pn = 1.0 - gamma_q(0.5 * nu_dbl, 0.5 * y_inv_dbl);
406 
407  P += log(Pn);
408 
410  operands_and_partials.d_x1[n] -= 0.5 * y_inv_dbl * y_inv_dbl
411  * gamma_p_derivative(0.5 * nu_dbl, 0.5 * y_inv_dbl) / Pn;
413  operands_and_partials.d_x2[n]
414  -= 0.5 * stan::math::gradRegIncGamma(0.5 * nu_dbl,
415  0.5 * y_inv_dbl,
416  gamma_vec[n],
417  digamma_vec[n]) / Pn;
418  }
419 
420  return operands_and_partials.to_var(P);
421  }
422 
423  template <class RNG>
424  inline double
425  inv_chi_square_rng(const double nu,
426  RNG& rng) {
427  using boost::variate_generator;
428  using boost::random::chi_squared_distribution;
429 
430  static const char* function = "stan::prob::inv_chi_square_rng(%1%)";
431 
433 
434  check_positive_finite(function, nu, "Degrees of freedom parameter",
435  (double*)0);
436 
437  variate_generator<RNG&, chi_squared_distribution<> >
438  chi_square_rng(rng, chi_squared_distribution<>(nu));
439  return 1 / chi_square_rng();
440  }
441 
442  }
443 }
444 
445 #endif
446 
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)
return_type< T_y, T_dof >::type inv_chi_square_cdf(const T_y &y, const T_dof &nu)
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
return_type< T_y, T_dof >::type inv_chi_square_log(const T_y &y, const T_dof &nu)
The log of an inverse chi-squared density for y with the specified degrees of freedom parameter...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
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
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)
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_dof >::type inv_chi_square_ccdf_log(const T_y &y, const T_dof &nu)
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
double inv_chi_square_rng(const double nu, RNG &rng)
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 negative_infinity()
Return negative infinity.
Definition: constants.hpp:123
return_type< T_y, T_dof >::type inv_chi_square_cdf_log(const T_y &y, const T_dof &nu)
double chi_square_rng(const double nu, RNG &rng)
Definition: chi_square.hpp:442

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