Imported Upstream version 1.49.0
[platform/upstream/boost.git] / libs / math / test / test_inverse_chi_squared_distribution.cpp
1 // test_inverse_chi_squared.cpp
2
3 // Copyright Paul A. Bristow 2010.
4 // Copyright John Maddock 2010.
5
6 // Use, modification and distribution are subject to the
7 // Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt
9 // or copy at http://www.boost.org/LICENSE_1_0.txt)
10
11 #ifdef _MSC_VER
12 #  pragma warning (disable : 4310) // cast truncates constant value.
13 #endif
14
15 // http://www.wolframalpha.com/input/?i=inverse+chisquare+distribution
16
17 #include <boost/math/concepts/real_concept.hpp> // for real_concept
18 using ::boost::math::concepts::real_concept;
19
20 //#include <boost/math/tools/test.hpp>
21 #include <boost/test/test_exec_monitor.hpp> // for test_main
22 #include <boost/test/floating_point_comparison.hpp> // for BOOST_CHECK_CLOSE_FRACTION
23
24 #include <boost/math/distributions/inverse_chi_squared.hpp> // for inverse_chisquared_distribution
25 using boost::math::inverse_chi_squared_distribution;
26 using boost::math::cdf;
27 using boost::math::pdf;
28
29 // Use Inverse Gamma distribution to check their relationship:
30 // inverse_chi_squared<>(v) == inverse_gamma<>(v / 2., 0.5)
31 #include <boost/math/distributions/inverse_gamma.hpp> // for inverse_gamma_distribution
32 using boost::math::inverse_gamma_distribution;
33 using boost::math::inverse_gamma;
34 //  using  ::boost::math::cdf;
35 //  using  ::boost::math::pdf;
36
37 #include <boost/math/special_functions/gamma.hpp> 
38 using boost::math::tgamma; // for naive pdf.
39
40 #include <iostream>
41 using std::cout;
42 using std::endl;
43 #include <limits>
44 using std::numeric_limits; // for epsilon.
45
46 template <class RealType>
47 RealType naive_pdf(RealType df, RealType scale, RealType x)
48 { // Formula from Wikipedia
49    using namespace std; // For ADL of std functions.
50    using boost::math::tgamma;
51    RealType result = pow(scale * df/2, df/2) * exp(-df * scale/(2 * x));
52    result /= tgamma(df/2) * pow(x, 1 + df/2);
53    return result;
54 }
55
56 // Test using a spot value from some other reference source,
57 // in this case test values from output from R provided by Thomas Mang,
58 // and Wolfram Mathematica by Mark Coleman.
59
60 template <class RealType>
61 void test_spot(
62      RealType degrees_of_freedom, // degrees_of_freedom,
63      RealType scale, // scale,
64      RealType x, // random variate x,
65      RealType pd, // expected pdf,
66      RealType P, // expected CDF,
67      RealType Q, // expected complement of CDF,
68      RealType tol) // test tolerance.
69 {
70    boost::math::inverse_chi_squared_distribution<RealType> dist(degrees_of_freedom, scale);
71
72    BOOST_CHECK_CLOSE_FRACTION
73       ( // Compare to expected PDF.
74       pdf(dist, x), // calculated.
75       pd, // expected
76       tol);
77
78    BOOST_CHECK_CLOSE_FRACTION( // Compare to naive pdf formula (probably less accurate).
79       pdf(dist, x), naive_pdf(dist.degrees_of_freedom(), dist.scale(), x), tol);
80
81    BOOST_CHECK_CLOSE_FRACTION( // Compare to expected CDF.
82       cdf(dist, x), P, tol);
83
84    if((P < 0.999) && (Q < 0.999))
85    {  // We can only check this if P is not too close to 1,
86       // so that we can guarantee Q is accurate:
87       BOOST_CHECK_CLOSE_FRACTION(
88         cdf(complement(dist, x)), Q, tol); // 1 - cdf
89       BOOST_CHECK_CLOSE_FRACTION(
90         quantile(dist, P), x, tol); // quantile(cdf) = x
91       BOOST_CHECK_CLOSE_FRACTION(
92         quantile(complement(dist, Q)), x, tol); // quantile(complement(1 - cdf)) = x
93    }
94 } // test_spot
95
96 template <class RealType> // Any floating-point type RealType.
97 void test_spots(RealType)
98 {
99   // Basic sanity checks, some test data is to six decimal places only,
100   // so set tolerance to 0.000001 (expressed as a percentage = 0.0001%).
101
102   RealType tolerance = 0.000001f;
103   cout << "Tolerance = " << tolerance * 100 << "%." << endl;
104
105 // This test values from output from geoR (17 decimal digits) guided by Thomas Mang.
106   test_spot(static_cast<RealType>(2), static_cast<RealType>(1./2.),
107     // degrees_of_freedom, default scale = 1/df.
108   static_cast<RealType>(1.L), // x.
109   static_cast<RealType>(0.30326532985631671L), // pdf.
110   static_cast<RealType>(0.60653065971263365L), // cdf.
111   static_cast<RealType>(1 - 0.606530659712633657L), // cdf complement.
112   tolerance  // tol
113   );
114
115 // Tests from Mark Coleman & Georgi Boshnakov using Wolfram Mathematica.
116   test_spot(static_cast<RealType>(10), static_cast<RealType>(0.1L), // degrees_of_freedom, scale
117   static_cast<RealType>(0.2), // x
118   static_cast<RealType>(1.6700235722635659824529759616528281217001163943570L), // pdf
119   static_cast<RealType>(0.89117801891415124234834646836872197623907651175353L), // cdf
120   static_cast<RealType>(1 - 0.89117801891415127L), // cdf complement
121   tolerance  // tol
122   );
123
124   test_spot(static_cast<RealType>(10), static_cast<RealType>(0.1L), // degrees_of_freedom, scale
125   static_cast<RealType>(0.5), // x
126   static_cast<RealType>(0.03065662009762021L), // pdf
127   static_cast<RealType>(0.99634015317265628765454354418728984933240514654437L), // cdf
128   static_cast<RealType>(1 - 0.99634015317265628765454354418728984933240514654437L), // cdf complement
129   tolerance  // tol
130   );
131
132
133   test_spot(static_cast<RealType>(10), static_cast<RealType>(2), // degrees_of_freedom, scale
134   static_cast<RealType>(0.5), // x
135   static_cast<RealType>(0.00054964096598361569L), // pdf
136   static_cast<RealType>(0.000016944743930067383903707995865261004246785511612700L), // cdf
137   static_cast<RealType>(1 - 0.000016944743930067383903707995865261004246785511612700L), // cdf complement
138   tolerance  // tol
139   );
140   
141   // Check some bad parameters to the distribution cause expected exception to be thrown.
142   BOOST_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> ichsqbad1(-1), std::domain_error); // negative degrees_of_freedom.
143   BOOST_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> ichsqbad2(1, -1), std::domain_error); // negative scale.
144   BOOST_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> ichsqbad3(-1, -1), std::domain_error); // negative scale and degrees_of_freedom.
145
146   inverse_chi_squared_distribution<RealType> ichsq;
147
148   if(std::numeric_limits<RealType>::has_infinity)
149   {
150     BOOST_CHECK_THROW(pdf(ichsq, +std::numeric_limits<RealType>::infinity()), std::domain_error); // x = + infinity, pdf = 0
151     BOOST_CHECK_THROW(pdf(ichsq, -std::numeric_limits<RealType>::infinity()),  std::domain_error); // x = - infinity, pdf = 0
152     BOOST_CHECK_THROW(cdf(ichsq, +std::numeric_limits<RealType>::infinity()),std::domain_error ); // x = + infinity, cdf = 1
153     BOOST_CHECK_THROW(cdf(ichsq, -std::numeric_limits<RealType>::infinity()), std::domain_error); // x = - infinity, cdf = 0
154     BOOST_CHECK_THROW(cdf(complement(ichsq, +std::numeric_limits<RealType>::infinity())), std::domain_error); // x = + infinity, c cdf = 0
155     BOOST_CHECK_THROW(cdf(complement(ichsq, -std::numeric_limits<RealType>::infinity())), std::domain_error); // x = - infinity, c cdf = 1
156     BOOST_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> nbad1(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean
157     BOOST_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> nbad1(-std::numeric_limits<RealType>::infinity(),  static_cast<RealType>(1)), std::domain_error); // -infinite mean
158     BOOST_CHECK_THROW(boost::math::inverse_chi_squared_distribution<RealType> nbad1(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd
159   }
160
161   if (std::numeric_limits<RealType>::has_quiet_NaN)
162   { // If no longer allow x or p to be NaN, then these tests should throw.
163     BOOST_CHECK_THROW(pdf(ichsq, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
164     BOOST_CHECK_THROW(cdf(ichsq, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN
165     BOOST_CHECK_THROW(cdf(complement(ichsq, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // x = + infinity
166     BOOST_CHECK_THROW(quantile(ichsq, std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // p = + quiet_NaN
167     BOOST_CHECK_THROW(quantile(complement(ichsq, std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // p = + quiet_NaN
168   }
169     // Spot check for pdf using 'naive pdf' function
170   for(RealType x = 0.5; x < 5; x += 0.5)
171   {
172     BOOST_CHECK_CLOSE_FRACTION(
173       pdf(inverse_chi_squared_distribution<RealType>(5, 6), x),
174       naive_pdf(RealType(5), RealType(6), x),
175       tolerance);
176   }   // Spot checks for parameters:
177
178   RealType tol_2eps = boost::math::tools::epsilon<RealType>() * 2; // 2 eps as a fraction.
179   inverse_chi_squared_distribution<RealType> dist51(5, 1);
180   inverse_chi_squared_distribution<RealType> dist52(5, 2);
181   inverse_chi_squared_distribution<RealType> dist31(3, 1);
182   inverse_chi_squared_distribution<RealType> dist111(11, 1);
183   // 11 mean 0.10000000000000001, variance  0.0011111111111111111, sd 0.033333333333333333
184
185   using namespace std; // ADL of std names.
186   using namespace boost::math;
187   
188   inverse_chi_squared_distribution<RealType> dist10(10);
189   //  mean, variance etc
190   BOOST_CHECK_CLOSE_FRACTION(mean(dist10), static_cast<RealType>(0.125), tol_2eps);
191   BOOST_CHECK_CLOSE_FRACTION(variance(dist10), static_cast<RealType>(0.0052083333333333333333333333333333333333333333333333L), tol_2eps);
192   BOOST_CHECK_CLOSE_FRACTION(mode(dist10), static_cast<RealType>(0.08333333333333333333333333333333333333333333333L), tol_2eps);
193   BOOST_CHECK_CLOSE_FRACTION(median(dist10), static_cast<RealType>(0.10704554778227709530244586234274024205738435512468L), tol_2eps);
194   BOOST_CHECK_CLOSE_FRACTION(cdf(dist10, median(dist10)), static_cast<RealType>(0.5L), tol_2eps);
195   BOOST_CHECK_CLOSE_FRACTION(skewness(dist10), static_cast<RealType>(3.4641016151377545870548926830117447338856105076208L), tol_2eps);
196   BOOST_CHECK_CLOSE_FRACTION(kurtosis(dist10), static_cast<RealType>(45), tol_2eps);
197   BOOST_CHECK_CLOSE_FRACTION(kurtosis_excess(dist10), static_cast<RealType>(45-3), tol_2eps);
198
199   tol_2eps = boost::math::tools::epsilon<RealType>() * 2; // 2 eps as a percentage.
200
201   // Special and limit cases:
202
203   RealType mx = (std::numeric_limits<RealType>::max)();
204   RealType mi = (std::numeric_limits<RealType>::min)();
205
206   BOOST_CHECK_EQUAL(
207   pdf(inverse_chi_squared_distribution<RealType>(1),
208     static_cast<RealType>(mx)), // max()
209     static_cast<RealType>(0)
210     );
211
212   BOOST_CHECK_EQUAL(
213   pdf(inverse_chi_squared_distribution<RealType>(1),
214     static_cast<RealType>(mi)), // min()
215     static_cast<RealType>(0)
216     );
217
218   BOOST_CHECK_EQUAL(
219     pdf(inverse_chi_squared_distribution<RealType>(1), static_cast<RealType>(0)), static_cast<RealType>(0));
220   BOOST_CHECK_EQUAL(
221     pdf(inverse_chi_squared_distribution<RealType>(3), static_cast<RealType>(0))
222     , static_cast<RealType>(0.0f));
223   BOOST_CHECK_EQUAL(
224     cdf(inverse_chi_squared_distribution<RealType>(1), static_cast<RealType>(0))
225     , static_cast<RealType>(0.0f));
226   BOOST_CHECK_EQUAL(
227     cdf(inverse_chi_squared_distribution<RealType>(2), static_cast<RealType>(0))
228     , static_cast<RealType>(0.0f));
229   BOOST_CHECK_EQUAL(
230     cdf(inverse_chi_squared_distribution<RealType>(3L), static_cast<RealType>(0L))
231     , static_cast<RealType>(0));
232   BOOST_CHECK_EQUAL(
233     cdf(complement(inverse_chi_squared_distribution<RealType>(1), static_cast<RealType>(0)))
234     , static_cast<RealType>(1));
235   BOOST_CHECK_EQUAL(
236     cdf(complement(inverse_chi_squared_distribution<RealType>(2), static_cast<RealType>(0)))
237     , static_cast<RealType>(1));
238   BOOST_CHECK_EQUAL(
239     cdf(complement(inverse_chi_squared_distribution<RealType>(3), static_cast<RealType>(0)))
240     , static_cast<RealType>(1));
241
242   BOOST_CHECK_THROW(
243     pdf(
244     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)), // degrees_of_freedom negative.
245     static_cast<RealType>(1)), std::domain_error
246     );
247   BOOST_CHECK_THROW(
248     pdf(
249     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
250     static_cast<RealType>(-1)), std::domain_error
251     );
252   BOOST_CHECK_THROW(
253     cdf(
254     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)),
255     static_cast<RealType>(1)), std::domain_error
256     );
257   BOOST_CHECK_THROW(
258     cdf(
259     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
260     static_cast<RealType>(-1)), std::domain_error
261     );
262   BOOST_CHECK_THROW(
263     cdf(complement(
264     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)),
265     static_cast<RealType>(1))), std::domain_error
266     );
267   BOOST_CHECK_THROW(
268     cdf(complement(
269     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
270     static_cast<RealType>(-1))), std::domain_error
271     );
272   BOOST_CHECK_THROW(
273     quantile(
274     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)),
275     static_cast<RealType>(0.5)), std::domain_error
276     );
277   BOOST_CHECK_THROW(
278     quantile(
279     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
280     static_cast<RealType>(-1)), std::domain_error
281     );
282   BOOST_CHECK_THROW(
283     quantile(
284     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
285     static_cast<RealType>(1.1)), std::domain_error
286     );
287   BOOST_CHECK_THROW(
288     quantile(complement(
289     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(-1)),
290     static_cast<RealType>(0.5))), std::domain_error
291     );
292   BOOST_CHECK_THROW(
293     quantile(complement(
294     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
295     static_cast<RealType>(-1))), std::domain_error
296     );
297   BOOST_CHECK_THROW(
298     quantile(complement(
299     inverse_chi_squared_distribution<RealType>(static_cast<RealType>(8)),
300     static_cast<RealType>(1.1))), std::domain_error
301     );
302 } // template <class RealType>void test_spots(RealType)
303
304
305 int test_main(int, char* [])
306 {
307   BOOST_MATH_CONTROL_FP;
308
309   double tol_few_eps = numeric_limits<double>::epsilon() * 4;
310   
311   // Check that can generate inverse_chi_squared distribution using the two convenience methods:
312   // inverse_chi_squared_distribution; // with default parameters, degrees_of_freedom = 1, scale - 1
313   using boost::math::inverse_chi_squared;
314    
315   // Some constructor tests using default double.
316   double tol4eps = boost::math::tools::epsilon<double>() * 4; // 4 eps as a fraction.
317
318   inverse_chi_squared ichsqdef; // Using typedef and both default parameters.
319
320   BOOST_CHECK_EQUAL(ichsqdef.degrees_of_freedom(), 1.); // df == 1
321   BOOST_CHECK_EQUAL(ichsqdef.scale(), 1); // scale == 1./df
322   BOOST_CHECK_CLOSE_FRACTION(pdf(ichsqdef, 1), 0.24197072451914330, tol4eps);
323   BOOST_CHECK_CLOSE_FRACTION(pdf(ichsqdef, 9), 0.013977156581221969, tol4eps);
324   
325   inverse_chi_squared_distribution<double> ichisq102(10., 2); // Both parameters specified.
326   BOOST_CHECK_EQUAL(ichisq102.degrees_of_freedom(), 10.); // Check both parameters stored OK.
327   BOOST_CHECK_EQUAL(ichisq102.scale(), 2.); // Check both parameters stored OK.
328
329   inverse_chi_squared_distribution<double> ichisq10(10.); // Only df parameter specified (unscaled).
330   BOOST_CHECK_EQUAL(ichisq10.degrees_of_freedom(), 10.); // Check  parameter stored.
331   BOOST_CHECK_EQUAL(ichisq10.scale(), 0.1); // Check default scale = 1/df = 1/10 = 0.1
332   BOOST_CHECK_CLOSE_FRACTION(pdf(ichisq10, 1),  0.00078975346316749169, tol4eps);
333   BOOST_CHECK_CLOSE_FRACTION(pdf(ichisq10, 10), 0.0000000012385799798186384, tol4eps);
334
335   BOOST_CHECK_CLOSE_FRACTION(mode(ichisq10), 0.0833333333333333333333333333333333333333, tol4eps);
336   // nu * xi / nu + 2 = 10 * 0.1 / (10 + 2) = 1/12 =  0.0833333...
337   // mode is not defined in Mathematica.
338   // See Discussion section http://en.wikipedia.org/wiki/Talk:Scaled-inverse-chi-square_distribution
339   // for origin of this formula.
340
341   inverse_chi_squared_distribution<double> ichisq5(5.); // // Only df parameter specified.
342   BOOST_CHECK_EQUAL(ichisq5.degrees_of_freedom(), 5.); // check  parameter stored.
343   BOOST_CHECK_EQUAL(ichisq5.scale(), 1./5.); // check default is 1/df
344   BOOST_CHECK_CLOSE_FRACTION(pdf(ichisq5, 0.2), 3.0510380337346841, tol4eps);
345   BOOST_CHECK_CLOSE_FRACTION(cdf(ichisq5, 0.5), 0.84914503608460956, tol4eps);
346   BOOST_CHECK_CLOSE_FRACTION(cdf(complement(ichisq5, 0.5)), 1 - 0.84914503608460956, tol4eps);
347
348   BOOST_CHECK_CLOSE_FRACTION(quantile(ichisq5, 0.84914503608460956), 0.5, tol4eps*100);
349   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(ichisq5, 1. - 0.84914503608460956)), 0.5, tol4eps*100);
350
351   // Check mean, etc spot values.
352   inverse_chi_squared_distribution<double> ichisq81(8., 1.); // degrees_of_freedom = 5, scale = 1
353   BOOST_CHECK_CLOSE_FRACTION(mean(ichisq81),1.33333333333333333333333333333333333333333, tol4eps);
354   BOOST_CHECK_CLOSE_FRACTION(variance(ichisq81), 0.888888888888888888888888888888888888888888888, tol4eps);
355   BOOST_CHECK_CLOSE_FRACTION(skewness(ichisq81), 2 * std::sqrt(8.), tol4eps);
356   inverse_chi_squared_distribution<double> ichisq21(2., 1.);
357   BOOST_CHECK_CLOSE_FRACTION(mode(ichisq21), 0.5, tol4eps);
358   BOOST_CHECK_CLOSE_FRACTION(median(ichisq21), 1.4426950408889634, tol4eps);
359
360   inverse_chi_squared ichsq4(4.); // Using typedef and degrees_of_freedom parameter (and default scale = 1/df).
361   BOOST_CHECK_EQUAL(ichsq4.degrees_of_freedom(), 4.); // df == 4.
362   BOOST_CHECK_EQUAL(ichsq4.scale(), 0.25); // scale  == 1 /df == 1/4.
363
364   inverse_chi_squared ichsq32(3, 2);
365   BOOST_CHECK_EQUAL(ichsq32.degrees_of_freedom(), 3.); // df == 3.
366   BOOST_CHECK_EQUAL(ichsq32.scale(), 2); // scale  == 2
367   
368   inverse_chi_squared ichsq11(1, 1); // Using explicit degrees_of_freedom parameter, and default scale = 1).
369   BOOST_CHECK_CLOSE_FRACTION(mode(ichsq11), 0.3333333333333333333333333333333333333333, tol4eps);
370   // (1 * 1)/ (1 + 2) = 1/3 using Wikipedia nu * xi /(nu + 2)
371   BOOST_CHECK_EQUAL(ichsq11.degrees_of_freedom(), 1.); // df == 1 (default).
372   BOOST_CHECK_EQUAL(ichsq11.scale(), 1.); // scale == 1.
373   /*
374   // Used to find some 'exact' values for testing mean, variance ...
375   // First with scale fixed at unity (Wikipedia definition 1)
376   cout << "df      scale            mean            variance              sd              median" << endl;
377   for (int degrees_of_freedom = 8; degrees_of_freedom < 30; degrees_of_freedom++)
378   {
379     inverse_chi_squared ichisq(degrees_of_freedom, 1);
380     cout.precision(17);
381     cout << degrees_of_freedom << "    "  << 1 << "  " << mean(ichisq) << ' ' 
382       << variance(ichisq) << ' ' << standard_deviation(ichisq)
383       << ' ' << median(ichisq) << endl;
384   }
385
386   // Default scale = 1 / df
387   cout << "|\n" << "df           scale          mean            variance              sd              median" << endl;
388   for (int degrees_of_freedom = 8; degrees_of_freedom < 30; degrees_of_freedom++)
389   {
390     inverse_chi_squared ichisq(degrees_of_freedom);
391     cout.precision(17);
392     cout << degrees_of_freedom << "    "  << 1./degrees_of_freedom << "  " << mean(ichisq) << ' ' 
393       << variance(ichisq) << ' ' << standard_deviation(ichisq)
394       << ' ' << median(ichisq) << endl;
395   }
396   */
397   inverse_chi_squared_distribution<> ichisq14(14, 1); // Using default RealType double.
398   BOOST_CHECK_CLOSE_FRACTION(mean(ichisq14), 1.166666666666666666666666666666666666666666666, tol4eps);
399   BOOST_CHECK_CLOSE_FRACTION(variance(ichisq14), 0.272222222222222222222222222222222222222222222, tol4eps);
400
401   inverse_chi_squared_distribution<> ichisq121(12); // Using default RealType double.
402   BOOST_CHECK_CLOSE_FRACTION(mean(ichisq121),  0.1, tol4eps);
403   BOOST_CHECK_CLOSE_FRACTION(variance(ichisq121), 0.0025, tol4eps);
404   BOOST_CHECK_CLOSE_FRACTION(standard_deviation(ichisq121), 0.05, tol4eps);
405
406   // and "using boost::math::inverse_chi_squared_distribution;".
407   inverse_chi_squared_distribution<> ichsq23(2., 3.); // Using default RealType double.
408   BOOST_CHECK_EQUAL(ichsq23.degrees_of_freedom(), 2.); //
409   BOOST_CHECK_EQUAL(ichsq23.scale(), 3.); //
410   BOOST_CHECK_THROW(mean(ichsq23), std::domain_error); // Degrees of freedom (nu) must be > 2
411   BOOST_CHECK_THROW(variance(ichsq23), std::domain_error); // Degrees of freedom (nu) must be > 4
412   BOOST_CHECK_THROW(skewness(ichsq23), std::domain_error); // Degrees of freedom (nu) must be > 6
413   BOOST_CHECK_THROW(kurtosis_excess(ichsq23), std::domain_error); // Degrees of freedom (nu) must be > 8
414
415   { // Check relationship between inverse gamma and inverse chi_squared distributions.
416   using boost::math::inverse_gamma_distribution;
417
418   double df = 2.;
419   double scale = 1.;
420   double alpha = df/2; // aka inv_gamma shape
421   double beta = scale /2; // inv_gamma scale.
422  
423   inverse_gamma_distribution<> ig(alpha, beta); 
424
425   inverse_chi_squared_distribution<> ichsq(df, 1./df); // == default scale.
426   BOOST_CHECK_EQUAL(pdf(ichsq, 0), 0); // Special case of zero x.
427
428   double x = 0.5;
429   BOOST_CHECK_EQUAL(pdf(ig, x), pdf(ichsq, x)); // inv_gamma compared to inv_chisq
430   BOOST_CHECK_EQUAL(cdf(ichsq, 0), 0); // Special case of zero.
431   BOOST_CHECK_EQUAL(cdf(ig, x), cdf(ichsq, x)); // invgamma == invchisq
432
433   // Test pdf by comparing using naive_pdf with relation to inverse gamma distribution
434   // wikipedia http://en.wikipedia.org/wiki/Scaled-inverse-chi-square_distribution related distributions.
435   // So if naive_pdf is correct, inverse_chi_squared_distribution should agree.
436   df = 1.; scale = 1.;
437   BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(ichsq11, x), tol_few_eps);
438
439   //inverse_gamma_distribution<> igd(df/2, (df * scale)/2); 
440   inverse_gamma_distribution<> igd11(df/2, df * scale/2);
441   BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(igd11, x), tol_few_eps);
442   BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(ichsq11, x), tol_few_eps);
443
444   df = 2; scale = 1;
445   inverse_gamma_distribution<> igd21(df/2, df * scale/2);
446   inverse_chi_squared_distribution<> ichsq21(df, scale);
447   BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(igd21, x), tol_few_eps); // 0.54134113294645081 OK 
448   BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(ichsq21, x), tol_few_eps); 
449
450   df = 2; scale = 2;
451   inverse_gamma_distribution<> igd22(df/2, df * scale/2);
452   inverse_chi_squared_distribution<> ichsq22(df, scale);
453   BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(igd22, x), tol_few_eps);
454   BOOST_CHECK_CLOSE_FRACTION(naive_pdf(df, scale, x), pdf(ichsq22, x), tol_few_eps);
455   }
456
457   // Check using float.
458   inverse_chi_squared_distribution<float> igf23(1.f, 2.f); // Using explicit RealType float.
459   BOOST_CHECK_EQUAL(igf23.degrees_of_freedom(), 1.f); //
460   BOOST_CHECK_EQUAL(igf23.scale(), 2.f); //
461   
462   // Check throws from bad parameters.
463   inverse_chi_squared ig051(0.5, 1.); // degrees_of_freedom < 1, so wrong for mean.
464   BOOST_CHECK_THROW(mean(ig051), std::domain_error);
465   inverse_chi_squared ig191(1.9999, 1.); // degrees_of_freedom < 2, so wrong for variance.
466   BOOST_CHECK_THROW(variance(ig191), std::domain_error);
467   inverse_chi_squared ig291(2.9999, 1.); // degrees_of_freedom < 3, so wrong for skewness.
468   BOOST_CHECK_THROW(skewness(ig291), std::domain_error);
469   inverse_chi_squared ig391(3.9999, 1.); // degrees_of_freedom < 1, so wrong for kurtosis and kurtosis_excess.
470   BOOST_CHECK_THROW(kurtosis(ig391), std::domain_error);
471   BOOST_CHECK_THROW(kurtosis_excess(ig391), std::domain_error);
472   
473   inverse_chi_squared ig102(10, 2); // Wolfram.com/ page 2, quantile = 2.96859.
474   //http://reference.wolfram.com/mathematica/ref/InverseChiSquareDistribution.html
475   BOOST_CHECK_CLOSE_FRACTION(quantile(ig102, 0.75), 2.96859, 0.000001); 
476   BOOST_CHECK_CLOSE_FRACTION(cdf(ig102, 2.96859), 0.75 , 0.000001); 
477   BOOST_CHECK_CLOSE_FRACTION(cdf(complement(ig102, 2.96859)), 1 - 0.75 , 0.00001); 
478   BOOST_CHECK_CLOSE_FRACTION(quantile(complement(ig102, 1 - 0.75)), 2.96859, 0.000001); 
479  
480   // Basic sanity-check spot values.
481   // (Parameter value, arbitrarily zero, only communicates the floating point type).
482   test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
483   test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 %
484 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
485   test_spots(0.0L); // Test long double.
486 #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x0582))
487   test_spots(boost::math::concepts::real_concept(0.)); // Test real concept.
488 #endif
489 #else
490   std::cout << "<note>The long double tests have been disabled on this platform "
491     "either because the long double overloads of the usual math functions are "
492     "not available at all, or because they are too inaccurate for these tests "
493     "to pass.</note>" << std::cout;
494 #endif
495
496  /*    */
497   return 0;
498 } // int test_main(int, char* [])
499
500 /*
501
502 Output:
503
504
505
506
507 */
508
509
510