]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/math/example/laplace_example.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / math / example / laplace_example.cpp
CommitLineData
7c673cae
FG
1// laplace_example.cpp
2
3// Copyright Paul A. Bristow 2008, 2010.
4
5// Use, modification and distribution are subject to the
6// Boost Software License, Version 1.0.
7// (See accompanying file LICENSE_1_0.txt
8// or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10// Example of using laplace (& comparing with normal) distribution.
11
12// Note that this file contains Quickbook mark-up as well as code
13// and comments, don't change any of the special comment mark-ups!
14
15//[laplace_example1
16/*`
17First we need some includes to access the laplace & normal distributions
18(and some std output of course).
19*/
20
21#include <boost/math/distributions/laplace.hpp> // for laplace_distribution
22 using boost::math::laplace; // typedef provides default type is double.
23#include <boost/math/distributions/normal.hpp> // for normal_distribution
24 using boost::math::normal; // typedef provides default type is double.
25
26#include <iostream>
27 using std::cout; using std::endl; using std::left; using std::showpoint; using std::noshowpoint;
28#include <iomanip>
29 using std::setw; using std::setprecision;
30#include <limits>
31 using std::numeric_limits;
32
33int main()
34{
35 cout << "Example: Laplace distribution." << endl;
36
37 try
38 {
39 { // Traditional tables and values.
40/*`Let's start by printing some traditional tables.
41*/
42 double step = 1.; // in z
43 double range = 4; // min and max z = -range to +range.
44 //int precision = 17; // traditional tables are only computed to much lower precision.
45 int precision = 4; // traditional table at much lower precision.
46 int width = 10; // for use with setw.
47
48 // Construct standard laplace & normal distributions l & s
49 normal s; // (default location or mean = zero, and scale or standard deviation = unity)
50 cout << "Standard normal distribution, mean or location = "<< s.location()
51 << ", standard deviation or scale = " << s.scale() << endl;
52 laplace l; // (default mean = zero, and standard deviation = unity)
53 cout << "Laplace normal distribution, location = "<< l.location()
54 << ", scale = " << l.scale() << endl;
55
56/*` First the probability distribution function (pdf).
57*/
58 cout << "Probability distribution function values" << endl;
59 cout << " z PDF normal laplace (difference)" << endl;
60 cout.precision(5);
61 for (double z = -range; z < range + step; z += step)
62 {
63 cout << left << setprecision(3) << setw(6) << z << " "
64 << setprecision(precision) << setw(width) << pdf(s, z) << " "
65 << setprecision(precision) << setw(width) << pdf(l, z)<< " ("
66 << setprecision(precision) << setw(width) << pdf(l, z) - pdf(s, z) // difference.
67 << ")" << endl;
68 }
69 cout.precision(6); // default
70/*`Notice how the laplace is less at z = 1 , but has 'fatter' tails at 2 and 3.
71
72 And the area under the normal curve from -[infin] up to z,
73 the cumulative distribution function (cdf).
74*/
75 // For a standard distribution
76 cout << "Standard location = "<< s.location()
77 << ", scale = " << s.scale() << endl;
78 cout << "Integral (area under the curve) from - infinity up to z " << endl;
79 cout << " z CDF normal laplace (difference)" << endl;
80 for (double z = -range; z < range + step; z += step)
81 {
82 cout << left << setprecision(3) << setw(6) << z << " "
83 << setprecision(precision) << setw(width) << cdf(s, z) << " "
84 << setprecision(precision) << setw(width) << cdf(l, z) << " ("
85 << setprecision(precision) << setw(width) << cdf(l, z) - cdf(s, z) // difference.
86 << ")" << endl;
87 }
88 cout.precision(6); // default
89
90/*`
91Pretty-printing a traditional 2-dimensional table is left as an exercise for the student,
92but why bother now that the Boost Math Toolkit lets you write
93*/
94 double z = 2.;
95 cout << "Area for gaussian z = " << z << " is " << cdf(s, z) << endl; // to get the area for z.
96 cout << "Area for laplace z = " << z << " is " << cdf(l, z) << endl; //
97/*`
98Correspondingly, we can obtain the traditional 'critical' values for significance levels.
99For the 95% confidence level, the significance level usually called alpha,
100is 0.05 = 1 - 0.95 (for a one-sided test), so we can write
101*/
102 cout << "95% of gaussian area has a z below " << quantile(s, 0.95) << endl;
103 cout << "95% of laplace area has a z below " << quantile(l, 0.95) << endl;
104 // 95% of area has a z below 1.64485
105 // 95% of laplace area has a z below 2.30259
106/*`and a two-sided test (a comparison between two levels, rather than a one-sided test)
107
108*/
109 cout << "95% of gaussian area has a z between " << quantile(s, 0.975)
110 << " and " << -quantile(s, 0.975) << endl;
111 cout << "95% of laplace area has a z between " << quantile(l, 0.975)
112 << " and " << -quantile(l, 0.975) << endl;
113 // 95% of area has a z between 1.95996 and -1.95996
114 // 95% of laplace area has a z between 2.99573 and -2.99573
115/*`Notice how much wider z has to be to enclose 95% of the area.
116*/
117 }
118//] [/[laplace_example1]
119 }
120 catch(const std::exception& e)
121 { // Always useful to include try & catch blocks because default policies
122 // are to throw exceptions on arguments that cause errors like underflow, overflow.
123 // Lacking try & catch blocks, the program will abort without a message below,
124 // which may give some helpful clues as to the cause of the exception.
125 std::cout <<
126 "\n""Message from thrown exception was:\n " << e.what() << std::endl;
127 }
128 return 0;
129} // int main()
130
131/*
132
133Output is:
134
135Example: Laplace distribution.
136Standard normal distribution, mean or location = 0, standard deviation or scale = 1
137Laplace normal distribution, location = 0, scale = 1
138Probability distribution function values
139 z PDF normal laplace (difference)
140-4 0.0001338 0.009158 (0.009024 )
141-3 0.004432 0.02489 (0.02046 )
142-2 0.05399 0.06767 (0.01368 )
143-1 0.242 0.1839 (-0.05803 )
1440 0.3989 0.5 (0.1011 )
1451 0.242 0.1839 (-0.05803 )
1462 0.05399 0.06767 (0.01368 )
1473 0.004432 0.02489 (0.02046 )
1484 0.0001338 0.009158 (0.009024 )
149Standard location = 0, scale = 1
150Integral (area under the curve) from - infinity up to z
151 z CDF normal laplace (difference)
152-4 3.167e-005 0.009158 (0.009126 )
153-3 0.00135 0.02489 (0.02354 )
154-2 0.02275 0.06767 (0.04492 )
155-1 0.1587 0.1839 (0.02528 )
1560 0.5 0.5 (0 )
1571 0.8413 0.8161 (-0.02528 )
1582 0.9772 0.9323 (-0.04492 )
1593 0.9987 0.9751 (-0.02354 )
1604 1 0.9908 (-0.009126 )
161Area for gaussian z = 2 is 0.97725
162Area for laplace z = 2 is 0.932332
16395% of gaussian area has a z below 1.64485
16495% of laplace area has a z below 2.30259
16595% of gaussian area has a z between 1.95996 and -1.95996
16695% of laplace area has a z between 2.99573 and -2.99573
167
168*/
169