Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
105 lines (83 loc) · 3.85 KB

File metadata and controls

105 lines (83 loc) · 3.85 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// error_policies_example.cpp
// Copyright Paul A. Bristow 2007, 2010.
// Copyright John Maddock 2007.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/math/distributions/normal.hpp>
using boost::math::normal_distribution;
#include <boost/math/distributions/students_t.hpp>
using boost::math::students_t; // Probability of students_t(df, t).
using boost::math::students_t_distribution;
// using namespace boost::math; causes:
//.\error_policy_normal.cpp(30) : error C2872: 'policy' : ambiguous symbol
// could be '\boost/math/policies/policy.hpp(392) : boost::math::policies::policy'
// or 'boost::math::policies'
// So should not use this 'using namespace boost::math;' command.
// Suppose we want a statistical distribution to return infinities,
// rather than throw exceptions (the default policy), then we can use:
// std
#include <iostream>
using std::cout;
using std::endl;
// using namespace boost::math::policies; or
using boost::math::policies::policy;
// Possible errors
using boost::math::policies::overflow_error;
using boost::math::policies::underflow_error;
using boost::math::policies::domain_error;
using boost::math::policies::pole_error;
using boost::math::policies::denorm_error;
using boost::math::policies::evaluation_error;
using boost::math::policies::ignore_error;
// Define a custom policy to ignore just overflow:
typedef policy<
overflow_error<ignore_error>
> my_policy;
// Define another custom policy (perhaps ill-advised?)
// to ignore all errors: domain, pole, overflow, underflow, denorm & evaluation:
typedef policy<
domain_error<ignore_error>,
pole_error<ignore_error>,
overflow_error<ignore_error>,
underflow_error<ignore_error>,
denorm_error<ignore_error>,
evaluation_error<ignore_error>
> my_ignoreall_policy;
// Define a new distribution with a custom policy to ignore_error
// (& thus perhaps return infinity for some arguments):
typedef boost::math::normal_distribution<double, my_policy> my_normal;
// Note: uses default parameters zero mean and unit standard deviation.
// We could also do the same for another distribution, for example:
using boost::math::students_t_distribution;
typedef students_t_distribution<double, my_ignoreall_policy> my_students_t;
int main()
{
cout << "quantile(my_normal(), 0.05); = " << quantile(my_normal(), 0.05) << endl; // 0.05 is argument within normal range.
cout << "quantile(my_normal(), 0.); = " << quantile(my_normal(), 0.) << endl; // argument zero, so expect infinity.
cout << "quantile(my_normal(), 0.); = " << quantile(my_normal(), 0.F) << endl; // argument zero, so expect infinity.
cout << "quantile(my_students_t(), 0.); = " << quantile(my_students_t(-1), 0.F) << endl; // 'bad' argument negative, so expect NaN.
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
// Construct a (0, 1) normal distribution that ignores all errors,
// returning NaN, infinity, zero, or best guess,
// and NOT setting errno.
normal_distribution<long double, my_ignoreall_policy> my_normal2(0.L, 1.L); // explicit parameters for distribution.
cout << "quantile(my_normal2(), 0.); = " << quantile(my_normal2, 0.01) << endl; // argument 0.01, so result finite.
cout << "quantile(my_normal2(), 0.); = " << quantile(my_normal2, 0.) << endl; // argument zero, so expect infinity.
#endif
return 0;
}
/*
Output:
error_policies_example.cpp
Generating code
Finished generating code
error_policy_normal_example.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\error_policies_example.exe
quantile(my_normal(), 0.05); = -1.64485
quantile(my_normal(), 0.); = -1.#INF
quantile(my_normal(), 0.); = -1.#INF
quantile(my_students_t(), 0.); = 1.#QNAN
quantile(my_normal2(), 0.); = -2.32635
quantile(my_normal2(), 0.); = -1.#INF
*/
Morty Proxy This is a proxified and sanitized view of the page, visit original site.