Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
randomvariable.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include "toplevelfixture.hpp"
20
21// clang-format off
22#include <boost/test/unit_test.hpp>
23#include <boost/test/data/test_case.hpp>
24// clang-format on
25
27
28#include <ql/time/date.hpp>
29#include <ql/pricingengines/blackformula.hpp>
30
31#include <boost/math/distributions/normal.hpp>
32
33#include <iostream>
34#include <iomanip>
35
36using namespace QuantExt;
37using namespace QuantLib;
38
39BOOST_AUTO_TEST_SUITE(QuantExtTestSuite)
40
41BOOST_AUTO_TEST_SUITE(RandomVariableTest)
42
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}
99
100BOOST_AUTO_TEST_CASE(testRandomVariable) {
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}
140
141BOOST_AUTO_TEST_CASE(testFunctions) {
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}
163
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}
194
195BOOST_AUTO_TEST_SUITE_END()
196
197BOOST_AUTO_TEST_SUITE_END()
RandomVariable sqrt(RandomVariable x)
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
CompiledFormula exp(CompiledFormula x)
bool close_enough_all(const RandomVariable &x, const RandomVariable &y)
RandomVariable black(const RandomVariable &omega, const RandomVariable &t, const RandomVariable &strike, const RandomVariable &forward, const RandomVariable &impliedVol)
RandomVariable normalCdf(RandomVariable x)
CompiledFormula abs(CompiledFormula x)
RandomVariable normalPdf(RandomVariable x)
CompiledFormula log(CompiledFormula x)
void set(const Size i, const bool v)
bool deterministic() const
Size size() const
bool at(const Size i) const
Real at(const Size i) const
void set(const Size i, const Real v)
void setAll(const Real v)
BOOST_AUTO_TEST_CASE(testFilter)
Fixture that can be used at top level.