Chris@16
|
1 // Copyright John Maddock 2010.
|
Chris@16
|
2 // Copyright Paul A. Bristow 2010.
|
Chris@16
|
3
|
Chris@16
|
4 // Use, modification and distribution are subject to the
|
Chris@16
|
5 // Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_STATS_INVERSE_GAUSSIAN_HPP
|
Chris@16
|
9 #define BOOST_STATS_INVERSE_GAUSSIAN_HPP
|
Chris@16
|
10
|
Chris@16
|
11 #ifdef _MSC_VER
|
Chris@16
|
12 #pragma warning(disable: 4512) // assignment operator could not be generated
|
Chris@16
|
13 #endif
|
Chris@16
|
14
|
Chris@16
|
15 // http://en.wikipedia.org/wiki/Normal-inverse_Gaussian_distribution
|
Chris@16
|
16 // http://mathworld.wolfram.com/InverseGaussianDistribution.html
|
Chris@16
|
17
|
Chris@16
|
18 // The normal-inverse Gaussian distribution
|
Chris@16
|
19 // also called the Wald distribution (some sources limit this to when mean = 1).
|
Chris@16
|
20
|
Chris@16
|
21 // It is the continuous probability distribution
|
Chris@16
|
22 // that is defined as the normal variance-mean mixture where the mixing density is the
|
Chris@16
|
23 // inverse Gaussian distribution. The tails of the distribution decrease more slowly
|
Chris@16
|
24 // than the normal distribution. It is therefore suitable to model phenomena
|
Chris@16
|
25 // where numerically large values are more probable than is the case for the normal distribution.
|
Chris@16
|
26
|
Chris@16
|
27 // The Inverse Gaussian distribution was first studied in relationship to Brownian motion.
|
Chris@16
|
28 // In 1956 M.C.K. Tweedie used the name 'Inverse Gaussian' because there is an inverse
|
Chris@16
|
29 // relationship between the time to cover a unit distance and distance covered in unit time.
|
Chris@16
|
30
|
Chris@16
|
31 // Examples are returns from financial assets and turbulent wind speeds.
|
Chris@16
|
32 // The normal-inverse Gaussian distributions form
|
Chris@16
|
33 // a subclass of the generalised hyperbolic distributions.
|
Chris@16
|
34
|
Chris@16
|
35 // See also
|
Chris@16
|
36
|
Chris@16
|
37 // http://en.wikipedia.org/wiki/Normal_distribution
|
Chris@16
|
38 // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm
|
Chris@16
|
39 // Also:
|
Chris@16
|
40 // Weisstein, Eric W. "Normal Distribution."
|
Chris@16
|
41 // From MathWorld--A Wolfram Web Resource.
|
Chris@16
|
42 // http://mathworld.wolfram.com/NormalDistribution.html
|
Chris@16
|
43
|
Chris@16
|
44 // http://www.jstatsoft.org/v26/i04/paper General class of inverse Gaussian distributions.
|
Chris@16
|
45 // ig package - withdrawn but at http://cran.r-project.org/src/contrib/Archive/ig/
|
Chris@16
|
46
|
Chris@16
|
47 // http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/SuppDists/html/inverse_gaussian.html
|
Chris@16
|
48 // R package for dinverse_gaussian, ...
|
Chris@16
|
49
|
Chris@16
|
50 // http://www.statsci.org/s/inverse_gaussian.s and http://www.statsci.org/s/inverse_gaussian.html
|
Chris@16
|
51
|
Chris@16
|
52 //#include <boost/math/distributions/fwd.hpp>
|
Chris@16
|
53 #include <boost/math/special_functions/erf.hpp> // for erf/erfc.
|
Chris@16
|
54 #include <boost/math/distributions/complement.hpp>
|
Chris@16
|
55 #include <boost/math/distributions/detail/common_error_handling.hpp>
|
Chris@16
|
56 #include <boost/math/distributions/normal.hpp>
|
Chris@16
|
57 #include <boost/math/distributions/gamma.hpp> // for gamma function
|
Chris@16
|
58 // using boost::math::gamma_p;
|
Chris@16
|
59
|
Chris@16
|
60 #include <boost/math/tools/tuple.hpp>
|
Chris@16
|
61 //using std::tr1::tuple;
|
Chris@16
|
62 //using std::tr1::make_tuple;
|
Chris@16
|
63 #include <boost/math/tools/roots.hpp>
|
Chris@16
|
64 //using boost::math::tools::newton_raphson_iterate;
|
Chris@16
|
65
|
Chris@16
|
66 #include <utility>
|
Chris@16
|
67
|
Chris@16
|
68 namespace boost{ namespace math{
|
Chris@16
|
69
|
Chris@16
|
70 template <class RealType = double, class Policy = policies::policy<> >
|
Chris@16
|
71 class inverse_gaussian_distribution
|
Chris@16
|
72 {
|
Chris@16
|
73 public:
|
Chris@16
|
74 typedef RealType value_type;
|
Chris@16
|
75 typedef Policy policy_type;
|
Chris@16
|
76
|
Chris@16
|
77 inverse_gaussian_distribution(RealType l_mean = 1, RealType l_scale = 1)
|
Chris@16
|
78 : m_mean(l_mean), m_scale(l_scale)
|
Chris@16
|
79 { // Default is a 1,1 inverse_gaussian distribution.
|
Chris@16
|
80 static const char* function = "boost::math::inverse_gaussian_distribution<%1%>::inverse_gaussian_distribution";
|
Chris@16
|
81
|
Chris@16
|
82 RealType result;
|
Chris@16
|
83 detail::check_scale(function, l_scale, &result, Policy());
|
Chris@16
|
84 detail::check_location(function, l_mean, &result, Policy());
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 RealType mean()const
|
Chris@16
|
88 { // alias for location.
|
Chris@16
|
89 return m_mean; // aka mu
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 // Synonyms, provided to allow generic use of find_location and find_scale.
|
Chris@16
|
93 RealType location()const
|
Chris@16
|
94 { // location, aka mu.
|
Chris@16
|
95 return m_mean;
|
Chris@16
|
96 }
|
Chris@16
|
97 RealType scale()const
|
Chris@16
|
98 { // scale, aka lambda.
|
Chris@16
|
99 return m_scale;
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 RealType shape()const
|
Chris@16
|
103 { // shape, aka phi = lambda/mu.
|
Chris@16
|
104 return m_scale / m_mean;
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 private:
|
Chris@16
|
108 //
|
Chris@16
|
109 // Data members:
|
Chris@16
|
110 //
|
Chris@16
|
111 RealType m_mean; // distribution mean or location, aka mu.
|
Chris@16
|
112 RealType m_scale; // distribution standard deviation or scale, aka lambda.
|
Chris@16
|
113 }; // class normal_distribution
|
Chris@16
|
114
|
Chris@16
|
115 typedef inverse_gaussian_distribution<double> inverse_gaussian;
|
Chris@16
|
116
|
Chris@16
|
117 template <class RealType, class Policy>
|
Chris@16
|
118 inline const std::pair<RealType, RealType> range(const inverse_gaussian_distribution<RealType, Policy>& /*dist*/)
|
Chris@16
|
119 { // Range of permissible values for random variable x, zero to max.
|
Chris@16
|
120 using boost::math::tools::max_value;
|
Chris@16
|
121 return std::pair<RealType, RealType>(static_cast<RealType>(0.), max_value<RealType>()); // - to + max value.
|
Chris@16
|
122 }
|
Chris@16
|
123
|
Chris@16
|
124 template <class RealType, class Policy>
|
Chris@16
|
125 inline const std::pair<RealType, RealType> support(const inverse_gaussian_distribution<RealType, Policy>& /*dist*/)
|
Chris@16
|
126 { // Range of supported values for random variable x, zero to max.
|
Chris@16
|
127 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
|
Chris@16
|
128 using boost::math::tools::max_value;
|
Chris@16
|
129 return std::pair<RealType, RealType>(static_cast<RealType>(0.), max_value<RealType>()); // - to + max value.
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 template <class RealType, class Policy>
|
Chris@16
|
133 inline RealType pdf(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& x)
|
Chris@16
|
134 { // Probability Density Function
|
Chris@16
|
135 BOOST_MATH_STD_USING // for ADL of std functions
|
Chris@16
|
136
|
Chris@16
|
137 RealType scale = dist.scale();
|
Chris@16
|
138 RealType mean = dist.mean();
|
Chris@16
|
139 RealType result = 0;
|
Chris@16
|
140 static const char* function = "boost::math::pdf(const inverse_gaussian_distribution<%1%>&, %1%)";
|
Chris@16
|
141 if(false == detail::check_scale(function, scale, &result, Policy()))
|
Chris@16
|
142 {
|
Chris@16
|
143 return result;
|
Chris@16
|
144 }
|
Chris@16
|
145 if(false == detail::check_location(function, mean, &result, Policy()))
|
Chris@16
|
146 {
|
Chris@16
|
147 return result;
|
Chris@16
|
148 }
|
Chris@16
|
149 if(false == detail::check_positive_x(function, x, &result, Policy()))
|
Chris@16
|
150 {
|
Chris@16
|
151 return result;
|
Chris@16
|
152 }
|
Chris@16
|
153
|
Chris@16
|
154 if (x == 0)
|
Chris@16
|
155 {
|
Chris@16
|
156 return 0; // Convenient, even if not defined mathematically.
|
Chris@16
|
157 }
|
Chris@16
|
158
|
Chris@16
|
159 result =
|
Chris@16
|
160 sqrt(scale / (constants::two_pi<RealType>() * x * x * x))
|
Chris@16
|
161 * exp(-scale * (x - mean) * (x - mean) / (2 * x * mean * mean));
|
Chris@16
|
162 return result;
|
Chris@16
|
163 } // pdf
|
Chris@16
|
164
|
Chris@16
|
165 template <class RealType, class Policy>
|
Chris@16
|
166 inline RealType cdf(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& x)
|
Chris@16
|
167 { // Cumulative Density Function.
|
Chris@16
|
168 BOOST_MATH_STD_USING // for ADL of std functions.
|
Chris@16
|
169
|
Chris@16
|
170 RealType scale = dist.scale();
|
Chris@16
|
171 RealType mean = dist.mean();
|
Chris@16
|
172 static const char* function = "boost::math::cdf(const inverse_gaussian_distribution<%1%>&, %1%)";
|
Chris@16
|
173 RealType result = 0;
|
Chris@16
|
174 if(false == detail::check_scale(function, scale, &result, Policy()))
|
Chris@16
|
175 {
|
Chris@16
|
176 return result;
|
Chris@16
|
177 }
|
Chris@16
|
178 if(false == detail::check_location(function, mean, &result, Policy()))
|
Chris@16
|
179 {
|
Chris@16
|
180 return result;
|
Chris@16
|
181 }
|
Chris@16
|
182 if(false == detail::check_positive_x(function, x, &result, Policy()))
|
Chris@16
|
183 {
|
Chris@16
|
184 return result;
|
Chris@16
|
185 }
|
Chris@16
|
186 if (x == 0)
|
Chris@16
|
187 {
|
Chris@16
|
188 return 0; // Convenient, even if not defined mathematically.
|
Chris@16
|
189 }
|
Chris@16
|
190 // Problem with this formula for large scale > 1000 or small x,
|
Chris@16
|
191 //result = 0.5 * (erf(sqrt(scale / x) * ((x / mean) - 1) / constants::root_two<RealType>(), Policy()) + 1)
|
Chris@16
|
192 // + exp(2 * scale / mean) / 2
|
Chris@16
|
193 // * (1 - erf(sqrt(scale / x) * (x / mean + 1) / constants::root_two<RealType>(), Policy()));
|
Chris@16
|
194 // so use normal distribution version:
|
Chris@16
|
195 // Wikipedia CDF equation http://en.wikipedia.org/wiki/Inverse_Gaussian_distribution.
|
Chris@16
|
196
|
Chris@16
|
197 normal_distribution<RealType> n01;
|
Chris@16
|
198
|
Chris@16
|
199 RealType n0 = sqrt(scale / x);
|
Chris@16
|
200 n0 *= ((x / mean) -1);
|
Chris@16
|
201 RealType n1 = cdf(n01, n0);
|
Chris@16
|
202 RealType expfactor = exp(2 * scale / mean);
|
Chris@16
|
203 RealType n3 = - sqrt(scale / x);
|
Chris@16
|
204 n3 *= (x / mean) + 1;
|
Chris@16
|
205 RealType n4 = cdf(n01, n3);
|
Chris@16
|
206 result = n1 + expfactor * n4;
|
Chris@16
|
207 return result;
|
Chris@16
|
208 } // cdf
|
Chris@16
|
209
|
Chris@16
|
210 template <class RealType, class Policy>
|
Chris@16
|
211 struct inverse_gaussian_quantile_functor
|
Chris@16
|
212 {
|
Chris@16
|
213
|
Chris@16
|
214 inverse_gaussian_quantile_functor(const boost::math::inverse_gaussian_distribution<RealType, Policy> dist, RealType const& p)
|
Chris@16
|
215 : distribution(dist), prob(p)
|
Chris@16
|
216 {
|
Chris@16
|
217 }
|
Chris@16
|
218 boost::math::tuple<RealType, RealType> operator()(RealType const& x)
|
Chris@16
|
219 {
|
Chris@16
|
220 RealType c = cdf(distribution, x);
|
Chris@16
|
221 RealType fx = c - prob; // Difference cdf - value - to minimize.
|
Chris@16
|
222 RealType dx = pdf(distribution, x); // pdf is 1st derivative.
|
Chris@16
|
223 // return both function evaluation difference f(x) and 1st derivative f'(x).
|
Chris@16
|
224 return boost::math::make_tuple(fx, dx);
|
Chris@16
|
225 }
|
Chris@16
|
226 private:
|
Chris@16
|
227 const boost::math::inverse_gaussian_distribution<RealType, Policy> distribution;
|
Chris@16
|
228 RealType prob;
|
Chris@16
|
229 };
|
Chris@16
|
230
|
Chris@16
|
231 template <class RealType, class Policy>
|
Chris@16
|
232 struct inverse_gaussian_quantile_complement_functor
|
Chris@16
|
233 {
|
Chris@16
|
234 inverse_gaussian_quantile_complement_functor(const boost::math::inverse_gaussian_distribution<RealType, Policy> dist, RealType const& p)
|
Chris@16
|
235 : distribution(dist), prob(p)
|
Chris@16
|
236 {
|
Chris@16
|
237 }
|
Chris@16
|
238 boost::math::tuple<RealType, RealType> operator()(RealType const& x)
|
Chris@16
|
239 {
|
Chris@16
|
240 RealType c = cdf(complement(distribution, x));
|
Chris@16
|
241 RealType fx = c - prob; // Difference cdf - value - to minimize.
|
Chris@16
|
242 RealType dx = -pdf(distribution, x); // pdf is 1st derivative.
|
Chris@16
|
243 // return both function evaluation difference f(x) and 1st derivative f'(x).
|
Chris@16
|
244 //return std::tr1::make_tuple(fx, dx); if available.
|
Chris@16
|
245 return boost::math::make_tuple(fx, dx);
|
Chris@16
|
246 }
|
Chris@16
|
247 private:
|
Chris@16
|
248 const boost::math::inverse_gaussian_distribution<RealType, Policy> distribution;
|
Chris@16
|
249 RealType prob;
|
Chris@16
|
250 };
|
Chris@16
|
251
|
Chris@16
|
252 namespace detail
|
Chris@16
|
253 {
|
Chris@16
|
254 template <class RealType>
|
Chris@16
|
255 inline RealType guess_ig(RealType p, RealType mu = 1, RealType lambda = 1)
|
Chris@16
|
256 { // guess at random variate value x for inverse gaussian quantile.
|
Chris@16
|
257 BOOST_MATH_STD_USING
|
Chris@16
|
258 using boost::math::policies::policy;
|
Chris@16
|
259 // Error type.
|
Chris@16
|
260 using boost::math::policies::overflow_error;
|
Chris@16
|
261 // Action.
|
Chris@16
|
262 using boost::math::policies::ignore_error;
|
Chris@16
|
263
|
Chris@16
|
264 typedef policy<
|
Chris@16
|
265 overflow_error<ignore_error> // Ignore overflow (return infinity)
|
Chris@16
|
266 > no_overthrow_policy;
|
Chris@16
|
267
|
Chris@16
|
268 RealType x; // result is guess at random variate value x.
|
Chris@16
|
269 RealType phi = lambda / mu;
|
Chris@16
|
270 if (phi > 2.)
|
Chris@16
|
271 { // Big phi, so starting to look like normal Gaussian distribution.
|
Chris@16
|
272 // x=(qnorm(p,0,1,true,false) - 0.5 * sqrt(mu/lambda)) / sqrt(lambda/mu);
|
Chris@16
|
273 // Whitmore, G.A. and Yalovsky, M.
|
Chris@16
|
274 // A normalising logarithmic transformation for inverse Gaussian random variables,
|
Chris@16
|
275 // Technometrics 20-2, 207-208 (1978), but using expression from
|
Chris@16
|
276 // V Seshadri, Inverse Gaussian distribution (1998) ISBN 0387 98618 9, page 6.
|
Chris@16
|
277
|
Chris@16
|
278 normal_distribution<RealType, no_overthrow_policy> n01;
|
Chris@16
|
279 x = mu * exp(quantile(n01, p) / sqrt(phi) - 1/(2 * phi));
|
Chris@16
|
280 }
|
Chris@16
|
281 else
|
Chris@16
|
282 { // phi < 2 so much less symmetrical with long tail,
|
Chris@16
|
283 // so use gamma distribution as an approximation.
|
Chris@16
|
284 using boost::math::gamma_distribution;
|
Chris@16
|
285
|
Chris@16
|
286 // Define the distribution, using gamma_nooverflow:
|
Chris@16
|
287 typedef gamma_distribution<RealType, no_overthrow_policy> gamma_nooverflow;
|
Chris@16
|
288
|
Chris@16
|
289 gamma_nooverflow g(static_cast<RealType>(0.5), static_cast<RealType>(1.));
|
Chris@16
|
290
|
Chris@16
|
291 // gamma_nooverflow g(static_cast<RealType>(0.5), static_cast<RealType>(1.));
|
Chris@16
|
292 // R qgamma(0.2, 0.5, 1) 0.0320923
|
Chris@16
|
293 RealType qg = quantile(complement(g, p));
|
Chris@16
|
294 //RealType qg1 = qgamma(1.- p, 0.5, 1.0, true, false);
|
Chris@16
|
295 x = lambda / (qg * 2);
|
Chris@16
|
296 //
|
Chris@16
|
297 if (x > mu/2) // x > mu /2?
|
Chris@16
|
298 { // x too large for the gamma approximation to work well.
|
Chris@16
|
299 //x = qgamma(p, 0.5, 1.0); // qgamma(0.270614, 0.5, 1) = 0.05983807
|
Chris@16
|
300 RealType q = quantile(g, p);
|
Chris@16
|
301 // x = mu * exp(q * static_cast<RealType>(0.1)); // Said to improve at high p
|
Chris@16
|
302 // x = mu * x; // Improves at high p?
|
Chris@16
|
303 x = mu * exp(q / sqrt(phi) - 1/(2 * phi));
|
Chris@16
|
304 }
|
Chris@16
|
305 }
|
Chris@16
|
306 return x;
|
Chris@16
|
307 } // guess_ig
|
Chris@16
|
308 } // namespace detail
|
Chris@16
|
309
|
Chris@16
|
310 template <class RealType, class Policy>
|
Chris@16
|
311 inline RealType quantile(const inverse_gaussian_distribution<RealType, Policy>& dist, const RealType& p)
|
Chris@16
|
312 {
|
Chris@16
|
313 BOOST_MATH_STD_USING // for ADL of std functions.
|
Chris@16
|
314 // No closed form exists so guess and use Newton Raphson iteration.
|
Chris@16
|
315
|
Chris@16
|
316 RealType mean = dist.mean();
|
Chris@16
|
317 RealType scale = dist.scale();
|
Chris@16
|
318 static const char* function = "boost::math::quantile(const inverse_gaussian_distribution<%1%>&, %1%)";
|
Chris@16
|
319
|
Chris@16
|
320 RealType result = 0;
|
Chris@16
|
321 if(false == detail::check_scale(function, scale, &result, Policy()))
|
Chris@16
|
322 return result;
|
Chris@16
|
323 if(false == detail::check_location(function, mean, &result, Policy()))
|
Chris@16
|
324 return result;
|
Chris@16
|
325 if(false == detail::check_probability(function, p, &result, Policy()))
|
Chris@16
|
326 return result;
|
Chris@16
|
327 if (p == 0)
|
Chris@16
|
328 {
|
Chris@16
|
329 return 0; // Convenient, even if not defined mathematically?
|
Chris@16
|
330 }
|
Chris@16
|
331 if (p == 1)
|
Chris@16
|
332 { // overflow
|
Chris@16
|
333 result = policies::raise_overflow_error<RealType>(function,
|
Chris@16
|
334 "probability parameter is 1, but must be < 1!", Policy());
|
Chris@16
|
335 return result; // std::numeric_limits<RealType>::infinity();
|
Chris@16
|
336 }
|
Chris@16
|
337
|
Chris@16
|
338 RealType guess = detail::guess_ig(p, dist.mean(), dist.scale());
|
Chris@16
|
339 using boost::math::tools::max_value;
|
Chris@16
|
340
|
Chris@16
|
341 RealType min = 0.; // Minimum possible value is bottom of range of distribution.
|
Chris@16
|
342 RealType max = max_value<RealType>();// Maximum possible value is top of range.
|
Chris@16
|
343 // int digits = std::numeric_limits<RealType>::digits; // Maximum possible binary digits accuracy for type T.
|
Chris@16
|
344 // digits used to control how accurate to try to make the result.
|
Chris@16
|
345 // To allow user to control accuracy versus speed,
|
Chris@16
|
346 int get_digits = policies::digits<RealType, Policy>();// get digits from policy,
|
Chris@16
|
347 boost::uintmax_t m = policies::get_max_root_iterations<Policy>(); // and max iterations.
|
Chris@16
|
348 using boost::math::tools::newton_raphson_iterate;
|
Chris@16
|
349 result =
|
Chris@16
|
350 newton_raphson_iterate(inverse_gaussian_quantile_functor<RealType, Policy>(dist, p), guess, min, max, get_digits, m);
|
Chris@16
|
351 return result;
|
Chris@16
|
352 } // quantile
|
Chris@16
|
353
|
Chris@16
|
354 template <class RealType, class Policy>
|
Chris@16
|
355 inline RealType cdf(const complemented2_type<inverse_gaussian_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
356 {
|
Chris@16
|
357 BOOST_MATH_STD_USING // for ADL of std functions.
|
Chris@16
|
358
|
Chris@16
|
359 RealType scale = c.dist.scale();
|
Chris@16
|
360 RealType mean = c.dist.mean();
|
Chris@16
|
361 RealType x = c.param;
|
Chris@16
|
362 static const char* function = "boost::math::cdf(const complement(inverse_gaussian_distribution<%1%>&), %1%)";
|
Chris@16
|
363 // infinite arguments not supported.
|
Chris@16
|
364 //if((boost::math::isinf)(x))
|
Chris@16
|
365 //{
|
Chris@16
|
366 // if(x < 0) return 1; // cdf complement -infinity is unity.
|
Chris@16
|
367 // return 0; // cdf complement +infinity is zero
|
Chris@16
|
368 //}
|
Chris@16
|
369 // These produce MSVC 4127 warnings, so the above used instead.
|
Chris@16
|
370 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
|
Chris@16
|
371 //{ // cdf complement +infinity is zero.
|
Chris@16
|
372 // return 0;
|
Chris@16
|
373 //}
|
Chris@16
|
374 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
|
Chris@16
|
375 //{ // cdf complement -infinity is unity.
|
Chris@16
|
376 // return 1;
|
Chris@16
|
377 //}
|
Chris@16
|
378 RealType result = 0;
|
Chris@16
|
379 if(false == detail::check_scale(function, scale, &result, Policy()))
|
Chris@16
|
380 return result;
|
Chris@16
|
381 if(false == detail::check_location(function, mean, &result, Policy()))
|
Chris@16
|
382 return result;
|
Chris@16
|
383 if(false == detail::check_positive_x(function, x, &result, Policy()))
|
Chris@16
|
384 return result;
|
Chris@16
|
385
|
Chris@16
|
386 normal_distribution<RealType> n01;
|
Chris@16
|
387 RealType n0 = sqrt(scale / x);
|
Chris@16
|
388 n0 *= ((x / mean) -1);
|
Chris@16
|
389 RealType cdf_1 = cdf(complement(n01, n0));
|
Chris@16
|
390
|
Chris@16
|
391 RealType expfactor = exp(2 * scale / mean);
|
Chris@16
|
392 RealType n3 = - sqrt(scale / x);
|
Chris@16
|
393 n3 *= (x / mean) + 1;
|
Chris@16
|
394
|
Chris@16
|
395 //RealType n5 = +sqrt(scale/x) * ((x /mean) + 1); // note now positive sign.
|
Chris@16
|
396 RealType n6 = cdf(complement(n01, +sqrt(scale/x) * ((x /mean) + 1)));
|
Chris@16
|
397 // RealType n4 = cdf(n01, n3); // =
|
Chris@16
|
398 result = cdf_1 - expfactor * n6;
|
Chris@16
|
399 return result;
|
Chris@16
|
400 } // cdf complement
|
Chris@16
|
401
|
Chris@16
|
402 template <class RealType, class Policy>
|
Chris@16
|
403 inline RealType quantile(const complemented2_type<inverse_gaussian_distribution<RealType, Policy>, RealType>& c)
|
Chris@16
|
404 {
|
Chris@16
|
405 BOOST_MATH_STD_USING // for ADL of std functions
|
Chris@16
|
406
|
Chris@16
|
407 RealType scale = c.dist.scale();
|
Chris@16
|
408 RealType mean = c.dist.mean();
|
Chris@16
|
409 static const char* function = "boost::math::quantile(const complement(inverse_gaussian_distribution<%1%>&), %1%)";
|
Chris@16
|
410 RealType result = 0;
|
Chris@16
|
411 if(false == detail::check_scale(function, scale, &result, Policy()))
|
Chris@16
|
412 return result;
|
Chris@16
|
413 if(false == detail::check_location(function, mean, &result, Policy()))
|
Chris@16
|
414 return result;
|
Chris@16
|
415 RealType q = c.param;
|
Chris@16
|
416 if(false == detail::check_probability(function, q, &result, Policy()))
|
Chris@16
|
417 return result;
|
Chris@16
|
418
|
Chris@16
|
419 RealType guess = detail::guess_ig(q, mean, scale);
|
Chris@16
|
420 // Complement.
|
Chris@16
|
421 using boost::math::tools::max_value;
|
Chris@16
|
422
|
Chris@16
|
423 RealType min = 0.; // Minimum possible value is bottom of range of distribution.
|
Chris@16
|
424 RealType max = max_value<RealType>();// Maximum possible value is top of range.
|
Chris@16
|
425 // int digits = std::numeric_limits<RealType>::digits; // Maximum possible binary digits accuracy for type T.
|
Chris@16
|
426 // digits used to control how accurate to try to make the result.
|
Chris@16
|
427 int get_digits = policies::digits<RealType, Policy>();
|
Chris@16
|
428 boost::uintmax_t m = policies::get_max_root_iterations<Policy>();
|
Chris@16
|
429 using boost::math::tools::newton_raphson_iterate;
|
Chris@16
|
430 result =
|
Chris@16
|
431 newton_raphson_iterate(inverse_gaussian_quantile_complement_functor<RealType, Policy>(c.dist, q), guess, min, max, get_digits, m);
|
Chris@16
|
432 return result;
|
Chris@16
|
433 } // quantile
|
Chris@16
|
434
|
Chris@16
|
435 template <class RealType, class Policy>
|
Chris@16
|
436 inline RealType mean(const inverse_gaussian_distribution<RealType, Policy>& dist)
|
Chris@16
|
437 { // aka mu
|
Chris@16
|
438 return dist.mean();
|
Chris@16
|
439 }
|
Chris@16
|
440
|
Chris@16
|
441 template <class RealType, class Policy>
|
Chris@16
|
442 inline RealType scale(const inverse_gaussian_distribution<RealType, Policy>& dist)
|
Chris@16
|
443 { // aka lambda
|
Chris@16
|
444 return dist.scale();
|
Chris@16
|
445 }
|
Chris@16
|
446
|
Chris@16
|
447 template <class RealType, class Policy>
|
Chris@16
|
448 inline RealType shape(const inverse_gaussian_distribution<RealType, Policy>& dist)
|
Chris@16
|
449 { // aka phi
|
Chris@16
|
450 return dist.shape();
|
Chris@16
|
451 }
|
Chris@16
|
452
|
Chris@16
|
453 template <class RealType, class Policy>
|
Chris@16
|
454 inline RealType standard_deviation(const inverse_gaussian_distribution<RealType, Policy>& dist)
|
Chris@16
|
455 {
|
Chris@16
|
456 BOOST_MATH_STD_USING
|
Chris@16
|
457 RealType scale = dist.scale();
|
Chris@16
|
458 RealType mean = dist.mean();
|
Chris@16
|
459 RealType result = sqrt(mean * mean * mean / scale);
|
Chris@16
|
460 return result;
|
Chris@16
|
461 }
|
Chris@16
|
462
|
Chris@16
|
463 template <class RealType, class Policy>
|
Chris@16
|
464 inline RealType mode(const inverse_gaussian_distribution<RealType, Policy>& dist)
|
Chris@16
|
465 {
|
Chris@16
|
466 BOOST_MATH_STD_USING
|
Chris@16
|
467 RealType scale = dist.scale();
|
Chris@16
|
468 RealType mean = dist.mean();
|
Chris@16
|
469 RealType result = mean * (sqrt(1 + (9 * mean * mean)/(4 * scale * scale))
|
Chris@16
|
470 - 3 * mean / (2 * scale));
|
Chris@16
|
471 return result;
|
Chris@16
|
472 }
|
Chris@16
|
473
|
Chris@16
|
474 template <class RealType, class Policy>
|
Chris@16
|
475 inline RealType skewness(const inverse_gaussian_distribution<RealType, Policy>& dist)
|
Chris@16
|
476 {
|
Chris@16
|
477 BOOST_MATH_STD_USING
|
Chris@16
|
478 RealType scale = dist.scale();
|
Chris@16
|
479 RealType mean = dist.mean();
|
Chris@16
|
480 RealType result = 3 * sqrt(mean/scale);
|
Chris@16
|
481 return result;
|
Chris@16
|
482 }
|
Chris@16
|
483
|
Chris@16
|
484 template <class RealType, class Policy>
|
Chris@16
|
485 inline RealType kurtosis(const inverse_gaussian_distribution<RealType, Policy>& dist)
|
Chris@16
|
486 {
|
Chris@16
|
487 RealType scale = dist.scale();
|
Chris@16
|
488 RealType mean = dist.mean();
|
Chris@16
|
489 RealType result = 15 * mean / scale -3;
|
Chris@16
|
490 return result;
|
Chris@16
|
491 }
|
Chris@16
|
492
|
Chris@16
|
493 template <class RealType, class Policy>
|
Chris@16
|
494 inline RealType kurtosis_excess(const inverse_gaussian_distribution<RealType, Policy>& dist)
|
Chris@16
|
495 {
|
Chris@16
|
496 RealType scale = dist.scale();
|
Chris@16
|
497 RealType mean = dist.mean();
|
Chris@16
|
498 RealType result = 15 * mean / scale;
|
Chris@16
|
499 return result;
|
Chris@16
|
500 }
|
Chris@16
|
501
|
Chris@16
|
502 } // namespace math
|
Chris@16
|
503 } // namespace boost
|
Chris@16
|
504
|
Chris@16
|
505 // This include must be at the end, *after* the accessors
|
Chris@16
|
506 // for this distribution have been defined, in order to
|
Chris@16
|
507 // keep compilers that support two-phase lookup happy.
|
Chris@16
|
508 #include <boost/math/distributions/detail/derived_accessors.hpp>
|
Chris@16
|
509
|
Chris@16
|
510 #endif // BOOST_STATS_INVERSE_GAUSSIAN_HPP
|
Chris@16
|
511
|
Chris@16
|
512
|