Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
value.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// clang-format off
20#include <boost/test/unit_test.hpp>
21#include <boost/test/data/test_case.hpp>
22// clang-format on
23
25
26#include <oret/toplevelfixture.hpp>
27
28#include <ql/time/date.hpp>
29#include <ql/tuple.hpp>
30
31#include <iostream>
32#include <iomanip>
33
34using namespace ore::data;
35using namespace QuantLib;
36
37BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
38
39BOOST_AUTO_TEST_SUITE(ValueTypeTest)
40
41BOOST_AUTO_TEST_CASE(testValueRandomVariableBinaryOps) {
42 BOOST_TEST_MESSAGE("Testing Value RandomVariable binary ops...");
43 constexpr Size n = 1;
44 RandomVariable a(n, 23), b(n, -10);
45 ValueType x(a), y(b);
46 BOOST_REQUIRE_EQUAL(x.which(), 0);
47 BOOST_REQUIRE_EQUAL(y.which(), 0);
48 ValueType z = x + y;
49 BOOST_REQUIRE_EQUAL(z.which(), 0);
50 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), a + b);
51 z = x - y;
52 BOOST_REQUIRE_EQUAL(z.which(), 0);
53 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), a - b);
54 z = x * y;
55 BOOST_REQUIRE_EQUAL(z.which(), 0);
56 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), a * b);
57 z = x / y;
58 BOOST_REQUIRE_EQUAL(z.which(), 0);
59 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), a / b);
60 z = min(x, y);
61 BOOST_REQUIRE_EQUAL(z.which(), 0);
62 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), min(a, b));
63 z = max(x, y);
64 BOOST_REQUIRE_EQUAL(z.which(), 0);
65 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), max(a, b));
66 z = pow(x, y);
67 BOOST_REQUIRE_EQUAL(z.which(), 0);
68 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), pow(a, b));
69}
70
71BOOST_AUTO_TEST_CASE(testValueRandomVariableUnaryOps) {
72 BOOST_TEST_MESSAGE("Testing Value RandomVariable unary ops...");
73 constexpr Size n = 1;
74 RandomVariable a(n, 3.0);
75 ValueType x(a);
76 BOOST_REQUIRE_EQUAL(x.which(), 0);
77 ValueType z = -x;
78 BOOST_REQUIRE_EQUAL(z.which(), 0);
79 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), -a);
80 z = abs(x);
81 BOOST_REQUIRE_EQUAL(z.which(), 0);
82 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), abs(a));
83 z = exp(x);
84 BOOST_REQUIRE_EQUAL(z.which(), 0);
85 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), exp(a));
86 z = log(x);
87 BOOST_REQUIRE_EQUAL(z.which(), 0);
88 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), log(a));
89 z = sqrt(x);
90 BOOST_REQUIRE_EQUAL(z.which(), 0);
91 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), sqrt(a));
92 z = normalCdf(x);
93 BOOST_REQUIRE_EQUAL(z.which(), 0);
94 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), normalCdf(a));
95 z = normalPdf(x);
96 BOOST_REQUIRE_EQUAL(z.which(), 0);
97 BOOST_CHECK_EQUAL(QuantLib::ext::get<RandomVariable>(z), normalPdf(a));
98}
99
100BOOST_AUTO_TEST_CASE(testValueIllegalOps) {
101 BOOST_TEST_MESSAGE("Testing Value illegal ops...");
102 constexpr Size n = 1;
103 ValueType a = RandomVariable(n, 23.0);
104 ValueType c = EventVec{n, Date()};
105 ValueType d = CurrencyVec{n, ""};
106 ValueType e = IndexVec{n, ""};
107 BOOST_CHECK_THROW(a + c, QuantLib::Error);
108 BOOST_CHECK_THROW(a + d, QuantLib::Error);
109 BOOST_CHECK_THROW(a + e, QuantLib::Error);
110 BOOST_CHECK_THROW(a - c, QuantLib::Error);
111 BOOST_CHECK_THROW(a - d, QuantLib::Error);
112 BOOST_CHECK_THROW(a - e, QuantLib::Error);
113 BOOST_CHECK_THROW(a * c, QuantLib::Error);
114 BOOST_CHECK_THROW(a * d, QuantLib::Error);
115 BOOST_CHECK_THROW(a * e, QuantLib::Error);
116 BOOST_CHECK_THROW(a / c, QuantLib::Error);
117 BOOST_CHECK_THROW(a / d, QuantLib::Error);
118 BOOST_CHECK_THROW(a / e, QuantLib::Error);
119 BOOST_CHECK_THROW(min(a, c), QuantLib::Error);
120 BOOST_CHECK_THROW(min(a, d), QuantLib::Error);
121 BOOST_CHECK_THROW(min(a, e), QuantLib::Error);
122 BOOST_CHECK_THROW(max(a, c), QuantLib::Error);
123 BOOST_CHECK_THROW(max(a, d), QuantLib::Error);
124 BOOST_CHECK_THROW(max(a, e), QuantLib::Error);
125 BOOST_CHECK_THROW(pow(a, c), QuantLib::Error);
126 BOOST_CHECK_THROW(pow(a, d), QuantLib::Error);
127 BOOST_CHECK_THROW(pow(a, e), QuantLib::Error);
128 BOOST_CHECK_THROW(-c, QuantLib::Error);
129 BOOST_CHECK_THROW(-d, QuantLib::Error);
130 BOOST_CHECK_THROW(-e, QuantLib::Error);
131 BOOST_CHECK_THROW(abs(c), QuantLib::Error);
132 BOOST_CHECK_THROW(abs(d), QuantLib::Error);
133 BOOST_CHECK_THROW(abs(e), QuantLib::Error);
134 BOOST_CHECK_THROW(exp(c), QuantLib::Error);
135 BOOST_CHECK_THROW(exp(d), QuantLib::Error);
136 BOOST_CHECK_THROW(exp(e), QuantLib::Error);
137 BOOST_CHECK_THROW(log(c), QuantLib::Error);
138 BOOST_CHECK_THROW(log(d), QuantLib::Error);
139 BOOST_CHECK_THROW(log(e), QuantLib::Error);
140}
141
142BOOST_AUTO_TEST_CASE(testAssignments) {
143 BOOST_TEST_MESSAGE("Testing Value assignments...");
144 constexpr Size n = 1;
145 RandomVariable a(n, 10);
146 EventVec c{n, Date(2, Jan, 2017)};
147 CurrencyVec d{n, "EUR"};
148 IndexVec e{n, "EUR-EURIBOR-6M"};
149 ValueType a2(RandomVariable(n, 0.0));
150 ValueType c2(EventVec{n, Date()});
151 ValueType d2(CurrencyVec{n, ""});
152 ValueType e2(IndexVec{n, ""});
153 // type = type
154 BOOST_REQUIRE_NO_THROW(typeSafeAssign(a2, a););
155 BOOST_REQUIRE_EQUAL(a2, ValueType(a));
156 try {
157 typeSafeAssign(c2, c);
158 } catch (const std::exception& e) {
159 BOOST_FAIL(e.what());
160 }
161 BOOST_REQUIRE_NO_THROW(typeSafeAssign(c2, c););
162 BOOST_REQUIRE_EQUAL(c2, ValueType(c));
163 BOOST_REQUIRE_NO_THROW(typeSafeAssign(d2, d););
164 BOOST_REQUIRE_EQUAL(d2, ValueType(d));
165 BOOST_REQUIRE_NO_THROW(typeSafeAssign(e2, e););
166 BOOST_REQUIRE_EQUAL(e2, ValueType(e));
167}
168
169BOOST_AUTO_TEST_CASE(testIllegalAssignments) {
170 BOOST_TEST_MESSAGE("Testing Value illegal assignments...");
171 constexpr Size n = 1;
172 RandomVariable a(n, 10);
173 EventVec c{n, Date(2, Jan, 2017)};
174 CurrencyVec d{n, "EUR"};
175 IndexVec e{n, "EUR-EURIBOR-6M"};
176 ValueType a2(RandomVariable(n, 0.0));
177 ValueType c2(EventVec{n, Date()});
178 ValueType d2(CurrencyVec{n, ""});
179 ValueType e2(IndexVec{n, ""});
180 // Illegal assignments
181 BOOST_CHECK_THROW(typeSafeAssign(a2, c);, QuantLib::Error);
182 BOOST_CHECK_THROW(typeSafeAssign(a2, d);, QuantLib::Error);
183 BOOST_CHECK_THROW(typeSafeAssign(a2, e);, QuantLib::Error);
184 BOOST_CHECK_THROW(typeSafeAssign(c2, a);, QuantLib::Error);
185 BOOST_CHECK_THROW(typeSafeAssign(c2, d);, QuantLib::Error);
186 BOOST_CHECK_THROW(typeSafeAssign(c2, e);, QuantLib::Error);
187 BOOST_CHECK_THROW(typeSafeAssign(d2, a);, QuantLib::Error);
188 BOOST_CHECK_THROW(typeSafeAssign(d2, c);, QuantLib::Error);
189 BOOST_CHECK_THROW(typeSafeAssign(d2, e);, QuantLib::Error);
190 BOOST_CHECK_THROW(typeSafeAssign(e2, a);, QuantLib::Error);
191 BOOST_CHECK_THROW(typeSafeAssign(e2, c);, QuantLib::Error);
192 BOOST_CHECK_THROW(typeSafeAssign(e2, d);, QuantLib::Error);
193}
194
195BOOST_AUTO_TEST_CASE(testComparisons) {
196 BOOST_TEST_MESSAGE("Testing Value comparisons...");
197 constexpr Size n = 1;
198 ValueType a = RandomVariable(n, 10.0), a2 = RandomVariable(n, 11.0);
199 ValueType c = EventVec{n, Date(2, Jan, 2017)}, c2 = EventVec{n, Date(3, Jan, 2017)};
200 ValueType d = CurrencyVec{n, "EUR"}, d2 = CurrencyVec{n, "USD"};
201 ValueType e = IndexVec{n, "EUR-EURIBOR-6M"}, e2 = IndexVec{n, "USD-LIBOR-3M"};
202 // RandomVariable
203 BOOST_CHECK(equal(a, a).at(0));
204 BOOST_CHECK(notequal(a, a2).at(0));
205 BOOST_CHECK(lt(a, a2).at(0));
206 BOOST_CHECK(leq(a, a).at(0));
207 BOOST_CHECK(gt(a2, a).at(0));
208 BOOST_CHECK(geq(a, a).at(0));
209 // EventVec
210 BOOST_CHECK(equal(c, c).at(0));
211 BOOST_CHECK(notequal(c, c2).at(0));
212 BOOST_CHECK(lt(c, c2).at(0));
213 BOOST_CHECK(leq(c, c).at(0));
214 BOOST_CHECK(gt(c2, c).at(0));
215 BOOST_CHECK(geq(c, c).at(0));
216 // CurrencyVec
217 BOOST_CHECK(equal(d, d).at(0));
218 BOOST_CHECK(notequal(d, d2).at(0));
219 BOOST_CHECK_THROW(lt(d, d2), QuantLib::Error);
220 BOOST_CHECK_THROW(leq(d, d2), QuantLib::Error);
221 BOOST_CHECK_THROW(gt(d, d2), QuantLib::Error);
222 BOOST_CHECK_THROW(geq(d, d2), QuantLib::Error);
223 // IndexVec
224 BOOST_CHECK(equal(e, e).at(0));
225 BOOST_CHECK(notequal(e, e2).at(0));
226 BOOST_CHECK_THROW(lt(e, e2), QuantLib::Error);
227 BOOST_CHECK_THROW(leq(e, e2), QuantLib::Error);
228 BOOST_CHECK_THROW(gt(e, e2), QuantLib::Error);
229 BOOST_CHECK_THROW(geq(e, e2), QuantLib::Error);
230}
231
232BOOST_AUTO_TEST_CASE(testIllegalComparisons) {
233 BOOST_TEST_MESSAGE("Testing Value illegal comparisons...");
234 constexpr Size n = 1;
235 ValueType a = RandomVariable(n, 23.0);
236 ValueType c = EventVec{n, Date()};
237 ValueType d = CurrencyVec{n, ""};
238 ValueType e = IndexVec{n, ""};
239 BOOST_CHECK_THROW(equal(a, c), QuantLib::Error);
240 BOOST_CHECK_THROW(equal(a, d), QuantLib::Error);
241 BOOST_CHECK_THROW(equal(a, e), QuantLib::Error);
242 BOOST_CHECK_THROW(equal(c, d), QuantLib::Error);
243 BOOST_CHECK_THROW(equal(c, e), QuantLib::Error);
244 BOOST_CHECK_THROW(equal(d, e), QuantLib::Error);
245 BOOST_CHECK_THROW(notequal(a, c), QuantLib::Error);
246 BOOST_CHECK_THROW(notequal(a, d), QuantLib::Error);
247 BOOST_CHECK_THROW(notequal(a, e), QuantLib::Error);
248 BOOST_CHECK_THROW(notequal(c, d), QuantLib::Error);
249 BOOST_CHECK_THROW(notequal(c, e), QuantLib::Error);
250 BOOST_CHECK_THROW(notequal(d, e), QuantLib::Error);
251 BOOST_CHECK_THROW(lt(a, c), QuantLib::Error);
252 BOOST_CHECK_THROW(lt(a, d), QuantLib::Error);
253 BOOST_CHECK_THROW(lt(a, e), QuantLib::Error);
254 BOOST_CHECK_THROW(lt(c, d), QuantLib::Error);
255 BOOST_CHECK_THROW(lt(c, e), QuantLib::Error);
256 BOOST_CHECK_THROW(lt(d, e), QuantLib::Error);
257 BOOST_CHECK_THROW(leq(a, c), QuantLib::Error);
258 BOOST_CHECK_THROW(leq(a, d), QuantLib::Error);
259 BOOST_CHECK_THROW(leq(a, e), QuantLib::Error);
260 BOOST_CHECK_THROW(leq(c, d), QuantLib::Error);
261 BOOST_CHECK_THROW(leq(c, e), QuantLib::Error);
262 BOOST_CHECK_THROW(leq(d, e), QuantLib::Error);
263 BOOST_CHECK_THROW(gt(a, c), QuantLib::Error);
264 BOOST_CHECK_THROW(gt(a, d), QuantLib::Error);
265 BOOST_CHECK_THROW(gt(a, e), QuantLib::Error);
266 BOOST_CHECK_THROW(gt(c, d), QuantLib::Error);
267 BOOST_CHECK_THROW(gt(c, e), QuantLib::Error);
268 BOOST_CHECK_THROW(gt(d, e), QuantLib::Error);
269 BOOST_CHECK_THROW(geq(a, c), QuantLib::Error);
270 BOOST_CHECK_THROW(geq(a, d), QuantLib::Error);
271 BOOST_CHECK_THROW(geq(a, e), QuantLib::Error);
272 BOOST_CHECK_THROW(geq(c, d), QuantLib::Error);
273 BOOST_CHECK_THROW(geq(c, e), QuantLib::Error);
274 BOOST_CHECK_THROW(geq(d, e), QuantLib::Error);
275}
276
277BOOST_AUTO_TEST_SUITE_END()
278
279BOOST_AUTO_TEST_SUITE_END()
RandomVariable max(RandomVariable x, const RandomVariable &y)
RandomVariable exp(RandomVariable x)
RandomVariable sqrt(RandomVariable x)
RandomVariable log(RandomVariable x)
RandomVariable pow(RandomVariable x, const RandomVariable &y)
RandomVariable normalCdf(RandomVariable x)
RandomVariable abs(RandomVariable x)
RandomVariable normalPdf(RandomVariable x)
Filter equal(Filter x, const Filter &y)
RandomVariable min(RandomVariable x, const RandomVariable &y)
Filter lt(const ValueType &x, const ValueType &y)
Definition: value.cpp:263
Filter notequal(const ValueType &x, const ValueType &y)
Definition: value.cpp:239
Filter gt(const ValueType &x, const ValueType &y)
Definition: value.cpp:282
ValueType typeSafeAssign(ValueType &x, const ValueType &y)
Definition: value.cpp:213
Filter leq(const ValueType &x, const ValueType &y)
Definition: value.cpp:301
Filter geq(const ValueType &x, const ValueType &y)
Definition: value.cpp:321
boost::variant< RandomVariable, EventVec, CurrencyVec, IndexVec, DaycounterVec, Filter > ValueType
Definition: value.hpp:60
BOOST_AUTO_TEST_CASE(testValueRandomVariableBinaryOps)
Definition: value.cpp:41
value type and operations