1 // Copyright John Maddock 2010.
2 // Copyright Paul A. Bristow 2010.
4 // Use, modification and distribution are subject to the
5 // Boost Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_STATS_INVERSE_GAUSSIAN_HPP
9 #define BOOST_STATS_INVERSE_GAUSSIAN_HPP
12 #pragma warning(disable: 4512) // assignment operator could not be generated
15 // http://en.wikipedia.org/wiki/Normal-inverse_Gaussian_distribution
16 // http://mathworld.wolfram.com/InverseGaussianDistribution.html
18 // The normal-inverse Gaussian distribution
19 // also called the Wald distribution (some sources limit this to when mean = 1).
21 // It is the continuous probability distribution
22 // that is defined as the normal variance-mean mixture where the mixing density is the
23 // inverse Gaussian distribution. The tails of the distribution decrease more slowly
24 // than the normal distribution. It is therefore suitable to model phenomena
25 // where numerically large values are more probable than is the case for the normal distribution.
27 // The Inverse Gaussian distribution was first studied in relationship to Brownian motion.
28 // In 1956 M.C.K. Tweedie used the name 'Inverse Gaussian' because there is an inverse
29 // relationship between the time to cover a unit distance and distance covered in unit time.
31 // Examples are returns from financial assets and turbulent wind speeds.
32 // The normal-inverse Gaussian distributions form
33 // a subclass of the generalised hyperbolic distributions.
37 // http://en.wikipedia.org/wiki/Normal_distribution
38 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
40 // Weisstein, Eric W. "Normal Distribution."
41 // From MathWorld--A Wolfram Web Resource.
42 // http://mathworld.wolfram.com/NormalDistribution.html
44 // http://www.jstatsoft.org/v26/i04/paper General class of inverse Gaussian distributions.
45 // ig package - withdrawn but at http://cran.r-project.org/src/contrib/Archive/ig/
47 // http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/SuppDists/html/inverse_gaussian.html
48 // R package for dinverse_gaussian, ...
50 // http://www.statsci.org/s/inverse_gaussian.s and http://www.statsci.org/s/inverse_gaussian.html
52 //#include <boost/math/distributions/fwd.hpp>
53 #include <boost/math/special_functions/erf.hpp> // for erf/erfc.
54 #include <boost/math/distributions/complement.hpp>
55 #include <boost/math/distributions/detail/common_error_handling.hpp>
56 #include <boost/math/distributions/normal.hpp>
57 #include <boost/math/distributions/gamma.hpp> // for gamma function
58 // using boost::math::gamma_p;
60 #include <boost/math/tools/tuple.hpp>
61 //using std::tr1::tuple;
62 //using std::tr1::make_tuple;
63 #include <boost/math/tools/roots.hpp>
64 //using boost::math::tools::newton_raphson_iterate;
68 namespace boost{ namespace math{
70 template <class RealType = double, class Policy = policies::policy<> >
71 class inverse_gaussian_distribution
74 typedef RealType value_type;
75 typedef Policy policy_type;
77 inverse_gaussian_distribution(RealType l_mean = 1, RealType l_scale = 1)
78 : m_mean(l_mean), m_scale(l_scale)
79 { // Default is a 1,1 inverse_gaussian distribution.
80 static const char* function = "boost::math::inverse_gaussian_distribution<%1%>::inverse_gaussian_distribution";
83 detail::check_scale(function, l_scale, &result, Policy());
84 detail::check_location(function, l_mean, &result, Policy());
85 detail::check_x_gt0(function, l_mean, &result, Policy());
89 { // alias for location.
90 return m_mean; // aka mu
93 // Synonyms, provided to allow generic use of find_location and find_scale.
94 RealType location()const
95 { // location, aka mu.
99 { // scale, aka lambda.
103 RealType shape()const
104 { // shape, aka phi = lambda/mu.
105 return m_scale / m_mean;
112 RealType m_mean; // distribution mean or location, aka mu.
113 RealType m_scale; // distribution standard deviation or scale, aka lambda.
114 }; // class normal_distribution
116 typedef inverse_gaussian_distribution<double> inverse_gaussian;
118 template <class RealType, class Policy>
119 inline const std::pair<RealType, RealType> range(const inverse_gaussian_distribution<RealType, Policy>& /*dist*/)
120 { // Range of permissible values for random variable x, zero to max.
121 using boost::math::tools::max_value;
122 return std::pair<RealType, RealType>(static_cast<RealType>(0.), max_value<RealType>()); // - to + max value.
125 template <class RealType, class Policy>
126 inline const std::pair<RealType, RealType> support(const inverse_gaussian_distribution<RealType, Policy>& /*dist*/)
127 { // Range of supported values for random variable x, zero to max.
128 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
129 using boost::math::tools::max_value;
130 return std::pair<RealType, RealType>(static_cast<RealType>(0.), max_value<RealType>()); // - to + max value.
133 template <class RealType, class Policy>
134 inline RealType pdf(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& x)
135 { // Probability Density Function
136 BOOST_MATH_STD_USING // for ADL of std functions
138 RealType scale = dist.scale();
139 RealType mean = dist.mean();
141 static const char* function = "boost::math::pdf(const inverse_gaussian_distribution<%1%>&, %1%)";
142 if(false == detail::check_scale(function, scale, &result, Policy()))
146 if(false == detail::check_location(function, mean, &result, Policy()))
150 if(false == detail::check_x_gt0(function, mean, &result, Policy()))
154 if(false == detail::check_positive_x(function, x, &result, Policy()))
161 return 0; // Convenient, even if not defined mathematically.
165 sqrt(scale / (constants::two_pi<RealType>() * x * x * x))
166 * exp(-scale * (x - mean) * (x - mean) / (2 * x * mean * mean));
170 template <class RealType, class Policy>
171 inline RealType cdf(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& x)
172 { // Cumulative Density Function.
173 BOOST_MATH_STD_USING // for ADL of std functions.
175 RealType scale = dist.scale();
176 RealType mean = dist.mean();
177 static const char* function = "boost::math::cdf(const inverse_gaussian_distribution<%1%>&, %1%)";
179 if(false == detail::check_scale(function, scale, &result, Policy()))
183 if(false == detail::check_location(function, mean, &result, Policy()))
187 if (false == detail::check_x_gt0(function, mean, &result, Policy()))
191 if(false == detail::check_positive_x(function, x, &result, Policy()))
197 return 0; // Convenient, even if not defined mathematically.
199 // Problem with this formula for large scale > 1000 or small x,
200 //result = 0.5 * (erf(sqrt(scale / x) * ((x / mean) - 1) / constants::root_two<RealType>(), Policy()) + 1)
201 // + exp(2 * scale / mean) / 2
202 // * (1 - erf(sqrt(scale / x) * (x / mean + 1) / constants::root_two<RealType>(), Policy()));
203 // so use normal distribution version:
204 // Wikipedia CDF equation http://en.wikipedia.org/wiki/Inverse_Gaussian_distribution.
206 normal_distribution<RealType> n01;
208 RealType n0 = sqrt(scale / x);
209 n0 *= ((x / mean) -1);
210 RealType n1 = cdf(n01, n0);
211 RealType expfactor = exp(2 * scale / mean);
212 RealType n3 = - sqrt(scale / x);
213 n3 *= (x / mean) + 1;
214 RealType n4 = cdf(n01, n3);
215 result = n1 + expfactor * n4;
219 template <class RealType, class Policy>
220 struct inverse_gaussian_quantile_functor
223 inverse_gaussian_quantile_functor(const boost::math::inverse_gaussian_distribution<RealType, Policy> dist, RealType const& p)
224 : distribution(dist), prob(p)
227 boost::math::tuple<RealType, RealType> operator()(RealType const& x)
229 RealType c = cdf(distribution, x);
230 RealType fx = c - prob; // Difference cdf - value - to minimize.
231 RealType dx = pdf(distribution, x); // pdf is 1st derivative.
232 // return both function evaluation difference f(x) and 1st derivative f'(x).
233 return boost::math::make_tuple(fx, dx);
236 const boost::math::inverse_gaussian_distribution<RealType, Policy> distribution;
240 template <class RealType, class Policy>
241 struct inverse_gaussian_quantile_complement_functor
243 inverse_gaussian_quantile_complement_functor(const boost::math::inverse_gaussian_distribution<RealType, Policy> dist, RealType const& p)
244 : distribution(dist), prob(p)
247 boost::math::tuple<RealType, RealType> operator()(RealType const& x)
249 RealType c = cdf(complement(distribution, x));
250 RealType fx = c - prob; // Difference cdf - value - to minimize.
251 RealType dx = -pdf(distribution, x); // pdf is 1st derivative.
252 // return both function evaluation difference f(x) and 1st derivative f'(x).
253 //return std::tr1::make_tuple(fx, dx); if available.
254 return boost::math::make_tuple(fx, dx);
257 const boost::math::inverse_gaussian_distribution<RealType, Policy> distribution;
263 template <class RealType>
264 inline RealType guess_ig(RealType p, RealType mu = 1, RealType lambda = 1)
265 { // guess at random variate value x for inverse gaussian quantile.
267 using boost::math::policies::policy;
269 using boost::math::policies::overflow_error;
271 using boost::math::policies::ignore_error;
274 overflow_error<ignore_error> // Ignore overflow (return infinity)
275 > no_overthrow_policy;
277 RealType x; // result is guess at random variate value x.
278 RealType phi = lambda / mu;
280 { // Big phi, so starting to look like normal Gaussian distribution.
281 // x=(qnorm(p,0,1,true,false) - 0.5 * sqrt(mu/lambda)) / sqrt(lambda/mu);
282 // Whitmore, G.A. and Yalovsky, M.
283 // A normalising logarithmic transformation for inverse Gaussian random variables,
284 // Technometrics 20-2, 207-208 (1978), but using expression from
285 // V Seshadri, Inverse Gaussian distribution (1998) ISBN 0387 98618 9, page 6.
287 normal_distribution<RealType, no_overthrow_policy> n01;
288 x = mu * exp(quantile(n01, p) / sqrt(phi) - 1/(2 * phi));
291 { // phi < 2 so much less symmetrical with long tail,
292 // so use gamma distribution as an approximation.
293 using boost::math::gamma_distribution;
295 // Define the distribution, using gamma_nooverflow:
296 typedef gamma_distribution<RealType, no_overthrow_policy> gamma_nooverflow;
298 gamma_nooverflow g(static_cast<RealType>(0.5), static_cast<RealType>(1.));
300 // gamma_nooverflow g(static_cast<RealType>(0.5), static_cast<RealType>(1.));
301 // R qgamma(0.2, 0.5, 1) 0.0320923
302 RealType qg = quantile(complement(g, p));
303 //RealType qg1 = qgamma(1.- p, 0.5, 1.0, true, false);
304 x = lambda / (qg * 2);
306 if (x > mu/2) // x > mu /2?
307 { // x too large for the gamma approximation to work well.
308 //x = qgamma(p, 0.5, 1.0); // qgamma(0.270614, 0.5, 1) = 0.05983807
309 RealType q = quantile(g, p);
310 // x = mu * exp(q * static_cast<RealType>(0.1)); // Said to improve at high p
311 // x = mu * x; // Improves at high p?
312 x = mu * exp(q / sqrt(phi) - 1/(2 * phi));
317 } // namespace detail
319 template <class RealType, class Policy>
320 inline RealType quantile(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& p)
322 BOOST_MATH_STD_USING // for ADL of std functions.
323 // No closed form exists so guess and use Newton Raphson iteration.
325 RealType mean = dist.mean();
326 RealType scale = dist.scale();
327 static const char* function = "boost::math::quantile(const inverse_gaussian_distribution<%1%>&, %1%)";
330 if(false == detail::check_scale(function, scale, &result, Policy()))
332 if(false == detail::check_location(function, mean, &result, Policy()))
334 if (false == detail::check_x_gt0(function, mean, &result, Policy()))
336 if(false == detail::check_probability(function, p, &result, Policy()))
340 return 0; // Convenient, even if not defined mathematically?
344 result = policies::raise_overflow_error<RealType>(function,
345 "probability parameter is 1, but must be < 1!", Policy());
346 return result; // std::numeric_limits<RealType>::infinity();
349 RealType guess = detail::guess_ig(p, dist.mean(), dist.scale());
350 using boost::math::tools::max_value;
352 RealType min = 0.; // Minimum possible value is bottom of range of distribution.
353 RealType max = max_value<RealType>();// Maximum possible value is top of range.
354 // int digits = std::numeric_limits<RealType>::digits; // Maximum possible binary digits accuracy for type T.
355 // digits used to control how accurate to try to make the result.
356 // To allow user to control accuracy versus speed,
357 int get_digits = policies::digits<RealType, Policy>();// get digits from policy,
358 boost::uintmax_t m = policies::get_max_root_iterations<Policy>(); // and max iterations.
359 using boost::math::tools::newton_raphson_iterate;
361 newton_raphson_iterate(inverse_gaussian_quantile_functor<RealType, Policy>(dist, p), guess, min, max, get_digits, m);
365 template <class RealType, class Policy>
366 inline RealType cdf(const complemented2_type<inverse_gaussian_distribution<RealType, Policy>, RealType>& c)
368 BOOST_MATH_STD_USING // for ADL of std functions.
370 RealType scale = c.dist.scale();
371 RealType mean = c.dist.mean();
372 RealType x = c.param;
373 static const char* function = "boost::math::cdf(const complement(inverse_gaussian_distribution<%1%>&), %1%)";
374 // infinite arguments not supported.
375 //if((boost::math::isinf)(x))
377 // if(x < 0) return 1; // cdf complement -infinity is unity.
378 // return 0; // cdf complement +infinity is zero
380 // These produce MSVC 4127 warnings, so the above used instead.
381 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
382 //{ // cdf complement +infinity is zero.
385 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
386 //{ // cdf complement -infinity is unity.
390 if(false == detail::check_scale(function, scale, &result, Policy()))
392 if(false == detail::check_location(function, mean, &result, Policy()))
394 if (false == detail::check_x_gt0(function, mean, &result, Policy()))
396 if(false == detail::check_positive_x(function, x, &result, Policy()))
399 normal_distribution<RealType> n01;
400 RealType n0 = sqrt(scale / x);
401 n0 *= ((x / mean) -1);
402 RealType cdf_1 = cdf(complement(n01, n0));
404 RealType expfactor = exp(2 * scale / mean);
405 RealType n3 = - sqrt(scale / x);
406 n3 *= (x / mean) + 1;
408 //RealType n5 = +sqrt(scale/x) * ((x /mean) + 1); // note now positive sign.
409 RealType n6 = cdf(complement(n01, +sqrt(scale/x) * ((x /mean) + 1)));
410 // RealType n4 = cdf(n01, n3); // =
411 result = cdf_1 - expfactor * n6;
415 template <class RealType, class Policy>
416 inline RealType quantile(const complemented2_type<inverse_gaussian_distribution<RealType, Policy>, RealType>& c)
418 BOOST_MATH_STD_USING // for ADL of std functions
420 RealType scale = c.dist.scale();
421 RealType mean = c.dist.mean();
422 static const char* function = "boost::math::quantile(const complement(inverse_gaussian_distribution<%1%>&), %1%)";
424 if(false == detail::check_scale(function, scale, &result, Policy()))
426 if(false == detail::check_location(function, mean, &result, Policy()))
428 if (false == detail::check_x_gt0(function, mean, &result, Policy()))
430 RealType q = c.param;
431 if(false == detail::check_probability(function, q, &result, Policy()))
434 RealType guess = detail::guess_ig(q, mean, scale);
436 using boost::math::tools::max_value;
438 RealType min = 0.; // Minimum possible value is bottom of range of distribution.
439 RealType max = max_value<RealType>();// Maximum possible value is top of range.
440 // int digits = std::numeric_limits<RealType>::digits; // Maximum possible binary digits accuracy for type T.
441 // digits used to control how accurate to try to make the result.
442 int get_digits = policies::digits<RealType, Policy>();
443 boost::uintmax_t m = policies::get_max_root_iterations<Policy>();
444 using boost::math::tools::newton_raphson_iterate;
446 newton_raphson_iterate(inverse_gaussian_quantile_complement_functor<RealType, Policy>(c.dist, q), guess, min, max, get_digits, m);
450 template <class RealType, class Policy>
451 inline RealType mean(const inverse_gaussian_distribution<RealType, Policy>& dist)
456 template <class RealType, class Policy>
457 inline RealType scale(const inverse_gaussian_distribution<RealType, Policy>& dist)
462 template <class RealType, class Policy>
463 inline RealType shape(const inverse_gaussian_distribution<RealType, Policy>& dist)
468 template <class RealType, class Policy>
469 inline RealType standard_deviation(const inverse_gaussian_distribution<RealType, Policy>& dist)
472 RealType scale = dist.scale();
473 RealType mean = dist.mean();
474 RealType result = sqrt(mean * mean * mean / scale);
478 template <class RealType, class Policy>
479 inline RealType mode(const inverse_gaussian_distribution<RealType, Policy>& dist)
482 RealType scale = dist.scale();
483 RealType mean = dist.mean();
484 RealType result = mean * (sqrt(1 + (9 * mean * mean)/(4 * scale * scale))
485 - 3 * mean / (2 * scale));
489 template <class RealType, class Policy>
490 inline RealType skewness(const inverse_gaussian_distribution<RealType, Policy>& dist)
493 RealType scale = dist.scale();
494 RealType mean = dist.mean();
495 RealType result = 3 * sqrt(mean/scale);
499 template <class RealType, class Policy>
500 inline RealType kurtosis(const inverse_gaussian_distribution<RealType, Policy>& dist)
502 RealType scale = dist.scale();
503 RealType mean = dist.mean();
504 RealType result = 15 * mean / scale -3;
508 template <class RealType, class Policy>
509 inline RealType kurtosis_excess(const inverse_gaussian_distribution<RealType, Policy>& dist)
511 RealType scale = dist.scale();
512 RealType mean = dist.mean();
513 RealType result = 15 * mean / scale;
520 // This include must be at the end, *after* the accessors
521 // for this distribution have been defined, in order to
522 // keep compilers that support two-phase lookup happy.
523 #include <boost/math/distributions/detail/derived_accessors.hpp>
525 #endif // BOOST_STATS_INVERSE_GAUSSIAN_HPP