1 // inverse_gamma_distribution_example.cpp
2 
3 // Copyright Paul A. Bristow 2010.
4 // Copyright Thomas Mang 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 // Example 1 of using inverse gamma
12 #include <boost/math/distributions/inverse_gamma.hpp>
13 using boost::math::inverse_gamma_distribution;  //  inverse_gamma_distribution.
14 using boost::math::inverse_gamma;
15 
16 #include <boost/math/special_functions/gamma.hpp>
17 using boost::math::tgamma; // Used for naive pdf as a comparison.
18 
19 #include <boost/math/distributions/gamma.hpp>
20 using boost::math::inverse_gamma_distribution;
21 
22 #include <iostream>
23 using std::cout;    using std::endl;
24 #include <iomanip>
25 using std::setprecision;
26 #include <cmath>
27 using std::sqrt;
28 
main()29 int main()
30 {
31 
32   cout << "Example using Inverse Gamma distribution. " << endl;
33   // TODO - awaiting a real example using Bayesian statistics.
34 
35 #ifdef BOOST_NO_CXX11_NUMERIC_LIMITS
36   int max_digits10 = 2 + (boost::math::policies::digits<double, boost::math::policies::policy<> >() * 30103UL) / 100000UL;
37   cout << "BOOST_NO_CXX11_NUMERIC_LIMITS is defined" << endl;
38 #else
39   int max_digits10 = std::numeric_limits<double>::max_digits10;
40 #endif
41   cout << "Show all potentially significant decimal digits std::numeric_limits<double>::max_digits10 = "
42     << max_digits10 << endl;
43   cout.precision(max_digits10); //
44 
45   double shape = 1.;
46   double scale = 1.;
47   double x = 0.5;
48   // Construction using default RealType double, and default shape and scale..
49   inverse_gamma_distribution<> my_inverse_gamma(shape, scale); // (alpha, beta)
50 
51   cout << "my_inverse_gamma.shape() = " << my_inverse_gamma.shape()
52     << ", scale = "<< my_inverse_gamma.scale() << endl;
53   cout << "x = " << x << ", pdf = " << pdf(my_inverse_gamma, x)
54     << ", cdf = " << cdf(my_inverse_gamma, x) << endl;
55 
56   // Construct using  typedef and default shape and scale parameters.
57   inverse_gamma my_ig;
58 
59   inverse_gamma my_ig23(2, 3);
60   cout << "my_inverse_gamma.shape() = " << my_ig23.shape()
61     << ", scale = "<< my_ig23.scale() << endl;
62   cout << "x = " << x << ", pdf = " << pdf(my_ig23, x)
63     << ", cdf = " << cdf(my_ig23, x) << endl;
64 
65   // Example of providing an 'out of domain' or 'bad' parameter,
66   // here a shape < 1, for which mean is not defined.
67   // Try block is essential to catch the exception message.
68   // (Uses the default policy which is to throw on all errors).
69   try
70   {
71     inverse_gamma if051(0.5, 1);
72     //inverse_gamma if051(0.5, 1);
73     cout << "mean(if051) = " << mean(if051) << endl;
74   }
75   catch(const std::exception& e)
76   { // Always useful to include try & catch blocks because default policies
77     // are to throw exceptions on arguments that cause errors like underflow, overflow.
78     // Lacking try & catch blocks, the program will abort without a message below,
79     // which may give some helpful clues as to the cause of the exception.
80     std::cout <<
81       "\n""Message from thrown exception was:\n   " << e.what() << std::endl;
82   }
83 
84   return 0;
85 }  // int main()
86 
87 /*
88 
89 Output is:
90   Example using Inverse Gamma distribution.
91   std::numeric_limits<double>::max_digits10 = 17
92   my_inverse_gamma.shape() = 1, scale = 1
93   x = 0.5, pdf = 0.54134113294645081, cdf = 0.1353352832366127
94   my_inverse_gamma.shape() = 2, scale = 3
95   x = 0.5, pdf = 0.17847015671997774, cdf = 0.017351265236664509
96 
97   Message from thrown exception was:
98      Error in function boost::math::mean(const inverse_gamma_distribution<double>&): Shape parameter is 0.5, but for a defined mean it must be > 1
99 
100 
101 */
102 
103 
104