Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / example / normal_tables.cpp
1
2 // normal_misc_examples.cpp
3
4 // Copyright Paul A. Bristow 2007, 2010, 2014, 2016.
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 of using normal distribution.
12
13 // Note that this file contains Quickbook mark-up as well as code
14 // and comments, don't change any of the special comment mark-ups!
15
16 /*`
17 First we need some includes to access the normal distribution
18 (and some std output of course).
19 */
20
21 #include <boost/cstdfloat.hpp> // MUST be first include!!!
22 // See Implementation of Float128 type, Overloading template functions with float128_t.
23
24 #include <boost/math/distributions/normal.hpp> // for normal_distribution.
25   using boost::math::normal; // typedef provides default type of double.
26
27 #include <iostream>
28   //using std::cout; using std::endl;
29   //using std::left; using std::showpoint; using std::noshowpoint;
30 #include <iomanip>
31   //using std::setw; using std::setprecision;
32 #include <limits>
33   //using std::numeric_limits;
34
35   /*!
36 Function max_digits10
37 Returns maximum number of possibly significant decimal digits for a floating-point type FPT,
38 even for older compilers/standard libraries that
39 lack support for std::std::numeric_limits<FPT>::max_digits10,
40 when the Kahan formula 2 + binary_digits * 0.3010 is used instead.
41 Also provides the correct result for Visual Studio 2010 where the max_digits10 provided for float is wrong.
42 */
43 namespace boost
44 {
45 namespace math
46 {
47 template <typename FPT>
48 int max_digits10()
49 {
50 // Since max_digits10 is not defined (or wrong) on older systems, define a local max_digits10.
51
52   // Usage:   int m = max_digits10<boost::float64_t>();
53   const int m =
54 #if (defined BOOST_NO_CXX11_NUMERIC_LIMITS) || (_MSC_VER == 1600) // is wrongly 8 not 9 for VS2010.
55   2 + std::numeric_limits<FPT>::digits * 3010/10000;
56 #else
57   std::numeric_limits<FPT>::max_digits10;
58 #endif
59   return m;
60 }
61 } // namespace math
62 } // namespace boost
63
64 template <typename FPT>
65 void normal_table()
66 {
67   using namespace boost::math;
68
69   FPT step = static_cast<FPT>(1.); // step in z.
70   FPT range = static_cast<FPT>(10.); // min and max z = -range to +range.
71
72   // Traditional tables are only computed to much lower precision.
73   // but @c std::std::numeric_limits<double>::max_digits10;
74   // on new Standard Libraries gives 17,
75   // the maximum number of digits from 64-bit double that can possibly be significant.
76   // @c std::std::numeric_limits<double>::digits10; == 15
77   // is number of @b guaranteed digits, the other two digits being 'noisy'.
78   // Here we use a custom version of max_digits10 which deals with those platforms
79   // where @c std::numeric_limits is not specialized,
80   // or @c std::numeric_limits<>::max_digits10 not implemented, or wrong.
81   int precision = boost::math::max_digits10<FPT>();
82
83 // std::cout << typeid(FPT).name() << std::endl;
84 // demo_normal.cpp:85: undefined reference to `typeinfo for __float128'
85 // [@http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43622   GCC 43622]
86 //  typeinfo for __float128 was missing GCC 4.9 Mar 2014, but OK for GCC 6.1.1.
87
88    // Construct a standard normal distribution s, with
89    // (default mean = zero, and standard deviation = unity)
90    normal s;
91    std::cout << "\nStandard normal distribution, mean = "<< s.mean()
92       << ", standard deviation = " << s.standard_deviation() << std::endl;
93
94   std::cout << "maxdigits_10 is " << precision
95     << ", digits10 is " << std::numeric_limits<FPT>::digits10 << std::endl;
96
97   std::cout << "Probability distribution function values" << std::endl;
98
99   std::cout << "  z " "   PDF " << std::endl;
100   for (FPT z = -range; z < range + step; z += step)
101   {
102     std::cout << std::left << std::setprecision(3) << std::setw(6) << z << " "
103       << std::setprecision(precision) << std::setw(12) << pdf(s, z) << std::endl;
104   }
105   std::cout.precision(6); // Restore to default precision.
106
107 /*`And the area under the normal curve from -[infin] up to z,
108   the cumulative distribution function (CDF).
109 */
110   // For a standard normal distribution:
111   std::cout << "Standard normal mean = "<< s.mean()
112     << ", standard deviation = " << s.standard_deviation() << std::endl;
113   std::cout << "Integral (area under the curve) from - infinity up to z." << std::endl;
114   std::cout << "  z " "   CDF " << std::endl;
115   for (FPT z = -range; z < range + step; z += step)
116   {
117     std::cout << std::left << std::setprecision(3) << std::setw(6) << z << " "
118       << std::setprecision(precision) << std::setw(12) << cdf(s, z) << std::endl;
119   }
120   std::cout.precision(6); // Reset to default precision.
121 } // template <typename FPT> void normal_table()
122
123 int main()
124 {
125   std::cout << "\nExample: Normal distribution tables." << std::endl;
126
127   using namespace boost::math;
128
129   try
130   {// Tip - always use try'n'catch blocks to ensure that messages from thrown exceptions are shown.
131
132 //[normal_table_1
133 #ifdef BOOST_FLOAT32_C
134     normal_table<boost::float32_t>(); // Usually type float
135 #endif
136     normal_table<boost::float64_t>(); // Usually type double. Assume that float64_t is always available.
137 #ifdef BOOST_FLOAT80_C
138     normal_table<boost::float80_t>(); // Type long double on some X86 platforms.
139 #endif
140 #ifdef BOOST_FLOAT128_C
141     normal_table<boost::float128_t>(); // Type _Quad on some Intel and __float128 on some GCC platforms.
142 #endif
143     normal_table<boost::floatmax_t>();
144 //] [/normal_table_1 ]
145   }
146   catch(std::exception ex)
147   {
148     std::cout << "exception thrown " << ex.what() << std::endl;
149   }
150
151   return 0;
152 }  // int main()
153
154
155 /*
156
157 GCC 4.8.1 with quadmath
158
159 Example: Normal distribution tables.
160
161 Standard normal distribution, mean = 0, standard deviation = 1
162 maxdigits_10 is 9, digits10 is 6
163 Probability distribution function values
164   z    PDF
165 -10    7.69459863e-023
166 -9     1.02797736e-018
167 -8     5.05227108e-015
168 -7     9.13472041e-012
169 -6     6.07588285e-009
170 -5     1.48671951e-006
171 -4     0.000133830226
172 -3     0.00443184841
173 -2     0.0539909665
174 -1     0.241970725
175 0      0.39894228
176 1      0.241970725
177 2      0.0539909665
178 3      0.00443184841
179 4      0.000133830226
180 5      1.48671951e-006
181 6      6.07588285e-009
182 7      9.13472041e-012
183 8      5.05227108e-015
184 9      1.02797736e-018
185 10     7.69459863e-023
186 Standard normal mean = 0, standard deviation = 1
187 Integral (area under the curve) from - infinity up to z.
188   z    CDF
189 -10    7.61985302e-024
190 -9     1.12858841e-019
191 -8     6.22096057e-016
192 -7     1.27981254e-012
193 -6     9.86587645e-010
194 -5     2.86651572e-007
195 -4     3.16712418e-005
196 -3     0.00134989803
197 -2     0.0227501319
198 -1     0.158655254
199 0      0.5
200 1      0.841344746
201 2      0.977249868
202 3      0.998650102
203 4      0.999968329
204 5      0.999999713
205 6      0.999999999
206 7      1
207 8      1
208 9      1
209 10     1
210
211 Standard normal distribution, mean = 0, standard deviation = 1
212 maxdigits_10 is 17, digits10 is 15
213 Probability distribution function values
214   z    PDF
215 -10    7.6945986267064199e-023
216 -9     1.0279773571668917e-018
217 -8     5.0522710835368927e-015
218 -7     9.1347204083645953e-012
219 -6     6.0758828498232861e-009
220 -5     1.4867195147342979e-006
221 -4     0.00013383022576488537
222 -3     0.0044318484119380075
223 -2     0.053990966513188063
224 -1     0.24197072451914337
225 0      0.3989422804014327
226 1      0.24197072451914337
227 2      0.053990966513188063
228 3      0.0044318484119380075
229 4      0.00013383022576488537
230 5      1.4867195147342979e-006
231 6      6.0758828498232861e-009
232 7      9.1347204083645953e-012
233 8      5.0522710835368927e-015
234 9      1.0279773571668917e-018
235 10     7.6945986267064199e-023
236 Standard normal mean = 0, standard deviation = 1
237 Integral (area under the curve) from - infinity up to z.
238   z    CDF
239 -10    7.6198530241605945e-024
240 -9     1.1285884059538422e-019
241 -8     6.2209605742718204e-016
242 -7     1.279812543885835e-012
243 -6     9.865876450377014e-010
244 -5     2.8665157187919455e-007
245 -4     3.1671241833119972e-005
246 -3     0.0013498980316300957
247 -2     0.022750131948179216
248 -1     0.15865525393145705
249 0      0.5
250 1      0.84134474606854293
251 2      0.97724986805182079
252 3      0.9986501019683699
253 4      0.99996832875816688
254 5      0.99999971334842808
255 6      0.9999999990134123
256 7      0.99999999999872013
257 8      0.99999999999999933
258 9      1
259 10     1
260
261 Standard normal distribution, mean = 0, standard deviation = 1
262 maxdigits_10 is 21, digits10 is 18
263 Probability distribution function values
264   z    PDF
265 -10    7.69459862670641993759e-023
266 -9     1.0279773571668916523e-018
267 -8     5.05227108353689273243e-015
268 -7     9.13472040836459525705e-012
269 -6     6.07588284982328608733e-009
270 -5     1.48671951473429788965e-006
271 -4     0.00013383022576488536764
272 -3     0.00443184841193800752729
273 -2     0.0539909665131880628364
274 -1     0.241970724519143365328
275 0      0.398942280401432702863
276 1      0.241970724519143365328
277 2      0.0539909665131880628364
278 3      0.00443184841193800752729
279 4      0.00013383022576488536764
280 5      1.48671951473429788965e-006
281 6      6.07588284982328608733e-009
282 7      9.13472040836459525705e-012
283 8      5.05227108353689273243e-015
284 9      1.0279773571668916523e-018
285 10     7.69459862670641993759e-023
286 Standard normal mean = 0, standard deviation = 1
287 Integral (area under the curve) from - infinity up to z.
288   z    CDF
289 -10    7.61985302416059451083e-024
290 -9     1.12858840595384222719e-019
291 -8     6.22096057427182035917e-016
292 -7     1.279812543885834962e-012
293 -6     9.86587645037701399241e-010
294 -5     2.86651571879194547129e-007
295 -4     3.16712418331199717608e-005
296 -3     0.00134989803163009566139
297 -2     0.0227501319481792155242
298 -1     0.158655253931457046468
299 0      0.5
300 1      0.841344746068542925777
301 2      0.977249868051820791415
302 3      0.998650101968369896532
303 4      0.999968328758166880021
304 5      0.999999713348428076465
305 6      0.999999999013412299576
306 7      0.999999999998720134897
307 8      0.999999999999999333866
308 9      1
309 10     1
310
311 Standard normal distribution, mean = 0, standard deviation = 1
312 maxdigits_10 is 36, digits10 is 34
313 Probability distribution function values
314   z    PDF
315 -10    7.69459862670641993759264402330435296e-023
316 -9     1.02797735716689165230378750485667109e-018
317 -8     5.0522710835368927324337437844893081e-015
318 -7     9.13472040836459525705208369548147081e-012
319 -6     6.07588284982328608733411870229841611e-009
320 -5     1.48671951473429788965346931561839483e-006
321 -4     0.00013383022576488536764006964663309418
322 -3     0.00443184841193800752728870762098267733
323 -2     0.0539909665131880628363703067407186609
324 -1     0.241970724519143365327522587904240936
325 0      0.398942280401432702863218082711682655
326 1      0.241970724519143365327522587904240936
327 2      0.0539909665131880628363703067407186609
328 3      0.00443184841193800752728870762098267733
329 4      0.00013383022576488536764006964663309418
330 5      1.48671951473429788965346931561839483e-006
331 6      6.07588284982328608733411870229841611e-009
332 7      9.13472040836459525705208369548147081e-012
333 8      5.0522710835368927324337437844893081e-015
334 9      1.02797735716689165230378750485667109e-018
335 10     7.69459862670641993759264402330435296e-023
336 Standard normal mean = 0, standard deviation = 1
337 Integral (area under the curve) from - infinity up to z.
338   z    CDF
339 -10    7.61985302416059451083278826816793623e-024
340 -9     1.1285884059538422271881384555435713e-019
341 -8     6.22096057427182035917417257601387863e-016
342 -7     1.27981254388583496200054074948511201e-012
343 -6     9.86587645037701399241244820583623953e-010
344 -5     2.86651571879194547128505464808623238e-007
345 -4     3.16712418331199717608064048146587766e-005
346 -3     0.001349898031630095661392854111682027
347 -2     0.0227501319481792155241528519127314212
348 -1     0.158655253931457046467912164189328905
349 0      0.5
350 1      0.841344746068542925776512220181757584
351 2      0.977249868051820791414741051994496956
352 3      0.998650101968369896532351503992686048
353 4      0.999968328758166880021462930017150939
354 5      0.999999713348428076464813329948810861
355 6      0.999999999013412299575520592043176293
356 7      0.999999999998720134897212119540199637
357 8      0.999999999999999333866185224906075746
358 9      1
359 10     1
360
361 Standard normal distribution, mean = 0, standard deviation = 1
362 maxdigits_10 is 36, digits10 is 34
363 Probability distribution function values
364   z    PDF
365 -10    7.69459862670641993759264402330435296e-023
366 -9     1.02797735716689165230378750485667109e-018
367 -8     5.0522710835368927324337437844893081e-015
368 -7     9.13472040836459525705208369548147081e-012
369 -6     6.07588284982328608733411870229841611e-009
370 -5     1.48671951473429788965346931561839483e-006
371 -4     0.00013383022576488536764006964663309418
372 -3     0.00443184841193800752728870762098267733
373 -2     0.0539909665131880628363703067407186609
374 -1     0.241970724519143365327522587904240936
375 0      0.398942280401432702863218082711682655
376 1      0.241970724519143365327522587904240936
377 2      0.0539909665131880628363703067407186609
378 3      0.00443184841193800752728870762098267733
379 4      0.00013383022576488536764006964663309418
380 5      1.48671951473429788965346931561839483e-006
381 6      6.07588284982328608733411870229841611e-009
382 7      9.13472040836459525705208369548147081e-012
383 8      5.0522710835368927324337437844893081e-015
384 9      1.02797735716689165230378750485667109e-018
385 10     7.69459862670641993759264402330435296e-023
386 Standard normal mean = 0, standard deviation = 1
387 Integral (area under the curve) from - infinity up to z.
388   z    CDF
389 -10    7.61985302416059451083278826816793623e-024
390 -9     1.1285884059538422271881384555435713e-019
391 -8     6.22096057427182035917417257601387863e-016
392 -7     1.27981254388583496200054074948511201e-012
393 -6     9.86587645037701399241244820583623953e-010
394 -5     2.86651571879194547128505464808623238e-007
395 -4     3.16712418331199717608064048146587766e-005
396 -3     0.001349898031630095661392854111682027
397 -2     0.0227501319481792155241528519127314212
398 -1     0.158655253931457046467912164189328905
399 0      0.5
400 1      0.841344746068542925776512220181757584
401 2      0.977249868051820791414741051994496956
402 3      0.998650101968369896532351503992686048
403 4      0.999968328758166880021462930017150939
404 5      0.999999713348428076464813329948810861
405 6      0.999999999013412299575520592043176293
406 7      0.999999999998720134897212119540199637
407 8      0.999999999999999333866185224906075746
408 9      1
409 10     1
410
411 MSVC 2013 64-bit
412 1>
413 1>  Example: Normal distribution tables.
414 1>
415 1>  Standard normal distribution, mean = 0, standard deviation = 1
416 1>  maxdigits_10 is 9, digits10 is 6
417 1>  Probability distribution function values
418 1>    z    PDF
419 1>  -10    7.69459863e-023
420 1>  -9     1.02797736e-018
421 1>  -8     5.05227108e-015
422 1>  -7     9.13472041e-012
423 1>  -6     6.07588285e-009
424 1>  -5     1.48671951e-006
425 1>  -4     0.000133830226
426 1>  -3     0.00443184841
427 1>  -2     0.0539909665
428 1>  -1     0.241970725
429 1>  0      0.39894228
430 1>  1      0.241970725
431 1>  2      0.0539909665
432 1>  3      0.00443184841
433 1>  4      0.000133830226
434 1>  5      1.48671951e-006
435 1>  6      6.07588285e-009
436 1>  7      9.13472041e-012
437 1>  8      5.05227108e-015
438 1>  9      1.02797736e-018
439 1>  10     7.69459863e-023
440 1>  Standard normal mean = 0, standard deviation = 1
441 1>  Integral (area under the curve) from - infinity up to z.
442 1>    z    CDF
443 1>  -10    7.61985302e-024
444 1>  -9     1.12858841e-019
445 1>  -8     6.22096057e-016
446 1>  -7     1.27981254e-012
447 1>  -6     9.86587645e-010
448 1>  -5     2.86651572e-007
449 1>  -4     3.16712418e-005
450 1>  -3     0.00134989803
451 1>  -2     0.0227501319
452 1>  -1     0.158655254
453 1>  0      0.5
454 1>  1      0.841344746
455 1>  2      0.977249868
456 1>  3      0.998650102
457 1>  4      0.999968329
458 1>  5      0.999999713
459 1>  6      0.999999999
460 1>  7      1
461 1>  8      1
462 1>  9      1
463 1>  10     1
464 1>
465 1>  Standard normal distribution, mean = 0, standard deviation = 1
466 1>  maxdigits_10 is 17, digits10 is 15
467 1>  Probability distribution function values
468 1>    z    PDF
469 1>  -10    7.6945986267064199e-023
470 1>  -9     1.0279773571668917e-018
471 1>  -8     5.0522710835368927e-015
472 1>  -7     9.1347204083645953e-012
473 1>  -6     6.0758828498232861e-009
474 1>  -5     1.4867195147342979e-006
475 1>  -4     0.00013383022576488537
476 1>  -3     0.0044318484119380075
477 1>  -2     0.053990966513188063
478 1>  -1     0.24197072451914337
479 1>  0      0.3989422804014327
480 1>  1      0.24197072451914337
481 1>  2      0.053990966513188063
482 1>  3      0.0044318484119380075
483 1>  4      0.00013383022576488537
484 1>  5      1.4867195147342979e-006
485 1>  6      6.0758828498232861e-009
486 1>  7      9.1347204083645953e-012
487 1>  8      5.0522710835368927e-015
488 1>  9      1.0279773571668917e-018
489 1>  10     7.6945986267064199e-023
490 1>  Standard normal mean = 0, standard deviation = 1
491 1>  Integral (area under the curve) from - infinity up to z.
492 1>    z    CDF
493 1>  -10    7.6198530241605813e-024
494 1>  -9     1.1285884059538408e-019
495 1>  -8     6.2209605742718292e-016
496 1>  -7     1.2798125438858352e-012
497 1>  -6     9.8658764503770161e-010
498 1>  -5     2.8665157187919439e-007
499 1>  -4     3.1671241833119979e-005
500 1>  -3     0.0013498980316300957
501 1>  -2     0.022750131948179219
502 1>  -1     0.15865525393145707
503 1>  0      0.5
504 1>  1      0.84134474606854293
505 1>  2      0.97724986805182079
506 1>  3      0.9986501019683699
507 1>  4      0.99996832875816688
508 1>  5      0.99999971334842808
509 1>  6      0.9999999990134123
510 1>  7      0.99999999999872013
511 1>  8      0.99999999999999933
512 1>  9      1
513 1>  10     1
514 1>
515 1>  Standard normal distribution, mean = 0, standard deviation = 1
516 1>  maxdigits_10 is 17, digits10 is 15
517 1>  Probability distribution function values
518 1>    z    PDF
519 1>  -10    7.6945986267064199e-023
520 1>  -9     1.0279773571668917e-018
521 1>  -8     5.0522710835368927e-015
522 1>  -7     9.1347204083645953e-012
523 1>  -6     6.0758828498232861e-009
524 1>  -5     1.4867195147342979e-006
525 1>  -4     0.00013383022576488537
526 1>  -3     0.0044318484119380075
527 1>  -2     0.053990966513188063
528 1>  -1     0.24197072451914337
529 1>  0      0.3989422804014327
530 1>  1      0.24197072451914337
531 1>  2      0.053990966513188063
532 1>  3      0.0044318484119380075
533 1>  4      0.00013383022576488537
534 1>  5      1.4867195147342979e-006
535 1>  6      6.0758828498232861e-009
536 1>  7      9.1347204083645953e-012
537 1>  8      5.0522710835368927e-015
538 1>  9      1.0279773571668917e-018
539 1>  10     7.6945986267064199e-023
540 1>  Standard normal mean = 0, standard deviation = 1
541 1>  Integral (area under the curve) from - infinity up to z.
542 1>    z    CDF
543 1>  -10    7.6198530241605813e-024
544 1>  -9     1.1285884059538408e-019
545 1>  -8     6.2209605742718292e-016
546 1>  -7     1.2798125438858352e-012
547 1>  -6     9.8658764503770161e-010
548 1>  -5     2.8665157187919439e-007
549 1>  -4     3.1671241833119979e-005
550 1>  -3     0.0013498980316300957
551 1>  -2     0.022750131948179219
552 1>  -1     0.15865525393145707
553 1>  0      0.5
554 1>  1      0.84134474606854293
555 1>  2      0.97724986805182079
556 1>  3      0.9986501019683699
557 1>  4      0.99996832875816688
558 1>  5      0.99999971334842808
559 1>  6      0.9999999990134123
560 1>  7      0.99999999999872013
561 1>  8      0.99999999999999933
562 1>  9      1
563 1>  10     1
564
565
566 */