Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
randomvariable.cpp File Reference
#include "toplevelfixture.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <qle/math/randomvariable.hpp>
#include <ql/time/date.hpp>
#include <ql/pricingengines/blackformula.hpp>
#include <boost/math/distributions/normal.hpp>
#include <iostream>
#include <iomanip>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testFilter)
 
 BOOST_AUTO_TEST_CASE (testRandomVariable)
 
 BOOST_AUTO_TEST_CASE (testFunctions)
 
 BOOST_AUTO_TEST_CASE (testBlack)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/4]

BOOST_AUTO_TEST_CASE ( testFilter  )

Definition at line 43 of file randomvariable.cpp.

43 {
44 BOOST_TEST_MESSAGE("Testing filter...");
45
46 Filter f(100, false);
47 BOOST_CHECK(f.deterministic());
48
49 f.set(5, true);
50 BOOST_CHECK(!f.deterministic());
51 BOOST_CHECK_EQUAL(f[0], false);
52 BOOST_CHECK_EQUAL(f[5], true);
53 BOOST_CHECK_EQUAL(f[10], false);
54
55 Filter g(100, true);
56 Filter r = f && g;
57 ;
58 BOOST_CHECK_EQUAL(r[0], false);
59 BOOST_CHECK_EQUAL(r[5], true);
60
61 Filter h(100, false);
62 r = f && h;
63 BOOST_CHECK_EQUAL(r[0], false);
64 BOOST_CHECK_EQUAL(r[5], false);
65 BOOST_CHECK(r.deterministic());
66
67 g.set(0, false);
68 r = f && g;
69 BOOST_CHECK_EQUAL(r[0], false);
70 BOOST_CHECK_EQUAL(r[5], true);
71
72 g.set(5, false);
73 r = f && g;
74 BOOST_CHECK_EQUAL(r[0], false);
75 BOOST_CHECK_EQUAL(r[5], false);
76
77 g.set(10, true);
78 r = f || g;
79 BOOST_CHECK_EQUAL(r[0], false);
80 BOOST_CHECK_EQUAL(r[5], true);
81 BOOST_CHECK_EQUAL(r[10], true);
82
83 r = !r;
84 BOOST_CHECK_EQUAL(r[0], true);
85 BOOST_CHECK_EQUAL(r[5], false);
86 BOOST_CHECK_EQUAL(r[10], false);
87
88 Filter x(100, false), y(100, true);
89 BOOST_CHECK((x && y).deterministic());
90 BOOST_CHECK((x || y).deterministic());
91 BOOST_CHECK((!x).deterministic());
92
93 Filter z(200, false);
94 BOOST_CHECK_THROW(x && z, QuantLib::Error);
95 BOOST_CHECK_THROW(x || z, QuantLib::Error);
96
97 BOOST_CHECK_THROW(r.at(100), QuantLib::Error);
98}
bool deterministic() const
bool at(const Size i) const
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/4]

BOOST_AUTO_TEST_CASE ( testRandomVariable  )

Definition at line 100 of file randomvariable.cpp.

100 {
101 BOOST_TEST_MESSAGE("Testing random variable...");
102
103 double tol = 1E-10;
104
105 RandomVariable r(100, 1.0);
106 BOOST_CHECK(r.deterministic());
107
108 r.set(5, 2.0);
109 BOOST_CHECK(!r.deterministic());
110 BOOST_CHECK_CLOSE(r[0], 1.0, tol);
111 BOOST_CHECK_CLOSE(r[5], 2.0, tol);
112 BOOST_CHECK_CLOSE(r[10], 1.0, tol);
113
114 RandomVariable s = r + r;
115 BOOST_CHECK_CLOSE(s[0], 2.0, tol);
116 BOOST_CHECK_CLOSE(s[5], 4.0, tol);
117 BOOST_CHECK_CLOSE(s[10], 2.0, tol);
118
119 RandomVariable x(100), y(100);
120 BOOST_CHECK((x + y).deterministic());
121 BOOST_CHECK((x - y).deterministic());
122 BOOST_CHECK((x * y).deterministic());
123 BOOST_CHECK((x / y).deterministic());
124
125 x.set(5, 2.0);
126 y.set(5, 2.0);
127 Filter c = close_enough(x, y);
128 for (Size i = 0; i < c.size(); ++i)
129 BOOST_CHECK_EQUAL(c[i], true);
130 BOOST_CHECK(close_enough_all(x, y));
131
132 y.set(5, 3.0);
133 c = close_enough(x, y);
134 BOOST_CHECK(!c.deterministic());
135 BOOST_CHECK_EQUAL(c[0], true);
136 BOOST_CHECK_EQUAL(c[5], false);
137
138 BOOST_CHECK_THROW(r.at(100), QuantLib::Error);
139}
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
bool close_enough_all(const RandomVariable &x, const RandomVariable &y)
Size size() const
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/4]

BOOST_AUTO_TEST_CASE ( testFunctions  )

Definition at line 141 of file randomvariable.cpp.

141 {
142 BOOST_TEST_MESSAGE("Testing functions...");
143
144 double tol = 1E-12;
145 double x = 2.0, y = -2.0;
146 RandomVariable X(1, x), Y(1, y);
147 boost::math::normal_distribution<double> n;
148
149 BOOST_CHECK_CLOSE((X + Y).at(0), x + y, tol);
150 BOOST_CHECK_CLOSE((X - Y).at(0), x - y, tol);
151 BOOST_CHECK_CLOSE((X * Y).at(0), x * y, tol);
152 BOOST_CHECK_CLOSE((X / Y).at(0), x / y, tol);
153
154 BOOST_CHECK_CLOSE((-X).at(0), -x, tol);
155 BOOST_CHECK_CLOSE((QuantExt::abs(Y)).at(0), std::abs(y), tol);
156 BOOST_CHECK_CLOSE((QuantExt::exp(X)).at(0), std::exp(x), tol);
157 BOOST_CHECK_CLOSE((QuantExt::log(X)).at(0), std::log(x), tol);
158 BOOST_CHECK_CLOSE((QuantExt::sqrt(X)).at(0), std::sqrt(x), tol);
159
160 BOOST_CHECK_CLOSE((normalCdf(X)).at(0), boost::math::cdf(n, x), tol);
161 BOOST_CHECK_CLOSE((normalPdf(X)).at(0), boost::math::pdf(n, x), tol);
162}
RandomVariable sqrt(RandomVariable x)
CompiledFormula exp(CompiledFormula x)
RandomVariable normalCdf(RandomVariable x)
CompiledFormula abs(CompiledFormula x)
RandomVariable normalPdf(RandomVariable x)
CompiledFormula log(CompiledFormula x)
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [4/4]

BOOST_AUTO_TEST_CASE ( testBlack  )

Definition at line 164 of file randomvariable.cpp.

164 {
165 BOOST_TEST_MESSAGE("Testing black formula...");
166
167 Option::Type type[4] = {Option::Call, Option::Call, Option::Put, Option::Put};
168
169 RandomVariable omega(4), t(4), strike(4), forward(4), impliedVol(4);
170
171 omega.set(0, 1.0);
172 omega.set(1, 1.0);
173 omega.set(2, -1.0);
174 omega.set(3, -1.0);
175
176 t.setAll(10.0);
177
178 strike.set(0, 98.0);
179 strike.set(1, 0.0);
180 strike.set(2, 98.0);
181 strike.set(3, 0.0);
182
183 forward.setAll(100.0);
184 impliedVol.setAll(0.2);
185
186 RandomVariable res = black(omega, t, strike, forward, impliedVol);
187
188 for (Size i = 0; i < 4; ++i) {
189 BOOST_CHECK_CLOSE(res.at(i),
190 blackFormula(type[i], strike.at(i), forward.at(i), impliedVol.at(i) * std::sqrt(t.at(i))),
191 1E-12);
192 }
193}
RandomVariable black(const RandomVariable &omega, const RandomVariable &t, const RandomVariable &strike, const RandomVariable &forward, const RandomVariable &impliedVol)
Real at(const Size i) const
+ Here is the call graph for this function: