Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
crossassetmodeldata.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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 <boost/test/unit_test.hpp>
20#include <oret/datapaths.hpp>
21#include <oret/fileutilities.hpp>
22#include <oret/toplevelfixture.hpp>
23
25
30
31using namespace QuantLib;
32using namespace QuantExt;
33using namespace boost::unit_test_framework;
34using namespace std;
35using namespace ore;
36using namespace ore::data;
37
38using ore::test::TopLevelFixture;
39
40namespace {
41
42QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<IrModelData>>> irConfigsData() {
43
44 // Create three instances
45 QuantLib::ext::shared_ptr<IrLgmData> lgmData1(new data::IrLgmData());
46 QuantLib::ext::shared_ptr<IrLgmData> lgmData2(new data::IrLgmData());
47 QuantLib::ext::shared_ptr<IrLgmData> lgmData3(new data::IrLgmData());
48
49 vector<std::string> expiries = {"1Y", "2Y", "36M"};
50 vector<std::string> terms = {"5Y", "2Y", "6M"};
51 vector<std::string> strikes = {"ATM", "ATM", "ATM"};
52
53 // First instance
54 lgmData1->qualifier() = "EUR";
55
56 lgmData1->calibrationType() = parseCalibrationType("BOOTSTRAP");
57 lgmData1->reversionType() = parseReversionType("HULLWHITE");
58 lgmData1->volatilityType() = parseVolatilityType("HAGAN");
59 std::vector<Time> hTimes = {1.0, 2.0, 3.0, 4.0};
60 std::vector<Real> hValues = {1.0, 2.0, 3.0, 4.0};
61 std::vector<Time> aTimes = {1.0, 2.0, 3.0, 4.0};
62 std::vector<Real> aValues = {1.0, 2.0, 3.0, 4.0};
63
64 lgmData1->calibrateH() = false;
65 lgmData1->hParamType() = parseParamType("PIECEWISE");
66
67 lgmData1->hTimes() = hTimes;
68
69 lgmData1->hValues() = hValues;
70
71 lgmData1->calibrateA() = false;
72 lgmData1->aParamType() = parseParamType("PIECEWISE");
73 lgmData1->aTimes() = aTimes;
74 lgmData1->aValues() = aValues;
75 lgmData1->shiftHorizon() = 1.0;
76
77 lgmData1->optionExpiries() = expiries;
78 lgmData1->optionTerms() = terms;
79 lgmData1->optionStrikes() = strikes;
80
81 lgmData1->scaling() = 1.0;
82
83 // Second instance
84 lgmData2->qualifier() = "USD";
85
86 lgmData2->calibrationType() = parseCalibrationType("BOOTSTRAP");
87 lgmData2->reversionType() = parseReversionType("HULLWHITE");
88 lgmData2->volatilityType() = parseVolatilityType("HAGAN");
89
90 lgmData2->calibrateH() = false;
91 lgmData2->hParamType() = parseParamType("PIECEWISE");
92 lgmData2->hTimes() = hTimes;
93 lgmData2->hValues() = hValues;
94
95 lgmData2->calibrateA() = false;
96 lgmData2->aParamType() = parseParamType("PIECEWISE");
97
98 lgmData2->aTimes() = aTimes;
99 lgmData2->aValues() = aValues;
100 lgmData2->shiftHorizon() = 1.0;
101
102 lgmData2->optionExpiries() = expiries;
103 lgmData2->optionTerms() = terms;
104 lgmData2->optionStrikes() = strikes;
105
106 lgmData2->scaling() = 1.0;
107
108 // Third instance
109 lgmData3->qualifier() = "JPY";
110
111 lgmData3->calibrationType() = parseCalibrationType("BOOTSTRAP");
112 lgmData3->reversionType() = parseReversionType("HULLWHITE");
113 lgmData3->volatilityType() = parseVolatilityType("HAGAN");
114
115 lgmData3->calibrateH() = false;
116 lgmData3->hParamType() = parseParamType("PIECEWISE");
117 lgmData3->hTimes() = hTimes;
118 lgmData3->hValues() = hValues;
119
120 lgmData3->calibrateA() = false;
121 lgmData3->aParamType() = parseParamType("PIECEWISE");
122
123 lgmData3->aTimes() = aTimes;
124 lgmData3->aValues() = aValues;
125 lgmData3->shiftHorizon() = 1.0;
126
127 lgmData3->optionExpiries() = expiries;
128 lgmData3->optionTerms() = terms;
129 lgmData3->optionStrikes() = strikes;
130
131 lgmData3->scaling() = 1.0;
132
133 QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<IrModelData>>> lgmDataVector(new vector<QuantLib::ext::shared_ptr<IrModelData>>);
134 *lgmDataVector = {lgmData1, lgmData2, lgmData3};
135 return lgmDataVector;
136}
137
138vector<QuantLib::ext::shared_ptr<InflationModelData>> infConfigsData() {
139
140 // TODO: Replacing the data that was here for now. It doesn't make sense.
141
142 vector<QuantLib::ext::shared_ptr<CalibrationInstrument>> instruments;
143 vector<Period> expiries = { 1 * Years, 2 * Years, 36 * Months };
144 auto strike = QuantLib::ext::make_shared<AbsoluteStrike>(0.03);
145 for (const Period& expiry : expiries) {
146 instruments.push_back(QuantLib::ext::make_shared<CpiCapFloor>(CapFloor::Floor, expiry, strike));
147 }
148 CalibrationBasket cb(instruments);
149 vector<CalibrationBasket> calibrationBaskets = { cb };
150
151 ReversionParameter reversion(LgmData::ReversionType::HullWhite, false, ParamType::Piecewise,
152 { 1.0, 2.0, 3.0, 4.0 }, { 1.0, 2.0, 3.0, 4.0, 4.0 });
153
154 VolatilityParameter volatility(LgmData::VolatilityType::Hagan, false, ParamType::Piecewise,
155 { 1.0, 2.0, 3.0, 4.0 }, { 1.0, 2.0, 3.0, 4.0, 4.0 });
156
157 LgmReversionTransformation rt(1.0, 1.0);
158
159 QuantLib::ext::shared_ptr<InfDkData> data = QuantLib::ext::make_shared<InfDkData>(CalibrationType::Bootstrap, calibrationBaskets,
160 "EUR", "EUHICPXT", reversion, volatility, rt);
161
162 return {data};
163}
164
165QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<FxBsData>>> fxConfigsData() {
166
167 // Create two instances
168 QuantLib::ext::shared_ptr<FxBsData> fxBsData1(new data::FxBsData());
169 QuantLib::ext::shared_ptr<FxBsData> fxBsData2(new data::FxBsData());
170
171 vector<std::string> expiries = {"1Y", "2Y", "36M"};
172 vector<std::string> strikes = {"ATMF", "ATMF", "ATMF"};
173 std::vector<Time> times = {1.0, 2.0, 3.0, 4.0};
174
175 // First instance
176 fxBsData1->foreignCcy() = "USD";
177 fxBsData1->domesticCcy() = "EUR";
178 fxBsData1->calibrationType() = parseCalibrationType("BOOTSTRAP");
179 fxBsData1->calibrateSigma() = true;
180 fxBsData1->sigmaParamType() = parseParamType("CONSTANT");
181 fxBsData1->sigmaTimes() = times;
182 fxBsData1->optionExpiries() = expiries;
183 fxBsData1->optionStrikes() = strikes;
184
185 // Second instance
186 fxBsData2->foreignCcy() = "JPY";
187 fxBsData2->domesticCcy() = "EUR";
188 fxBsData2->calibrationType() = parseCalibrationType("BOOTSTRAP");
189 fxBsData2->calibrateSigma() = true;
190 fxBsData2->sigmaParamType() = parseParamType("CONSTANT");
191 fxBsData2->sigmaTimes() = times;
192 fxBsData2->optionExpiries() = expiries;
193 fxBsData2->optionStrikes() = strikes;
194
195 QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<FxBsData>>> fxBsDataVector(new vector<QuantLib::ext::shared_ptr<FxBsData>>);
196 *fxBsDataVector = {fxBsData1, fxBsData2};
197 return fxBsDataVector;
198}
199
200QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<EqBsData>>> eqConfigsData() {
201
202 // Create two instances
203 QuantLib::ext::shared_ptr<EqBsData> eqBsData1(new data::EqBsData());
204
205 vector<std::string> expiries = {"1Y", "2Y", "36M"};
206 vector<std::string> strikes = {"ATMF", "ATMF", "ATMF"};
207 std::vector<Time> times = {1.0, 2.0, 3.0, 4.0};
208
209 // First instance
210 eqBsData1->eqName() = "SP5";
211 eqBsData1->currency() = "EUR";
212 eqBsData1->calibrationType() = parseCalibrationType("BOOTSTRAP");
213 eqBsData1->calibrateSigma() = true;
214 eqBsData1->sigmaParamType() = parseParamType("CONSTANT");
215 eqBsData1->sigmaTimes() = times;
216 eqBsData1->optionExpiries() = expiries;
217 eqBsData1->optionStrikes() = strikes;
218
219 QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<EqBsData>>> eqBsDataVector(new vector<QuantLib::ext::shared_ptr<EqBsData>>);
220 *eqBsDataVector = {eqBsData1};
221 return eqBsDataVector;
222}
223
224QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<CrLgmData>>> crLgmConfigsData() {
225
226 // Create three instances
227 QuantLib::ext::shared_ptr<CrLgmData> lgmData(new data::CrLgmData());
228
229 lgmData->name() = "ItraxxEuropeS9V1";
230
231 lgmData->calibrationType() = parseCalibrationType("BOOTSTRAP");
232 lgmData->reversionType() = parseReversionType("HULLWHITE");
233 lgmData->volatilityType() = parseVolatilityType("HAGAN");
234 std::vector<Time> hTimes = {1.0, 2.0, 3.0, 4.0};
235 std::vector<Real> hValues = {1.0, 2.0, 3.0, 4.0};
236 std::vector<Time> aTimes = {1.0, 2.0, 3.0, 4.0};
237 std::vector<Real> aValues = {1.0, 2.0, 3.0, 4.0};
238
239 lgmData->calibrateH() = false;
240 lgmData->hParamType() = parseParamType("CONSTANT");
241
242 lgmData->hTimes() = hTimes;
243
244 lgmData->hValues() = hValues;
245
246 lgmData->calibrateA() = false;
247 lgmData->aParamType() = parseParamType("CONSTANT");
248 lgmData->aTimes() = aTimes;
249 lgmData->aValues() = aValues;
250 lgmData->shiftHorizon() = 1.0;
251
252 lgmData->scaling() = 1.0;
253
254 QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<CrLgmData>>> lgmDataVector(new vector<QuantLib::ext::shared_ptr<CrLgmData>>);
255 *lgmDataVector = {lgmData};
256 return lgmDataVector;
257}
258
259QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<CrCirData>>> crCirConfigsData() {
260
261 // Create three instances
262 QuantLib::ext::shared_ptr<CrCirData> cirData(new data::CrCirData());
263
264 cirData->name() = "CDX.NA.S33v1";
265
266 cirData->currency() = "USD";
267 cirData->calibrationType() = parseCalibrationType("None");
268 cirData->calibrationStrategy() = parseCirCalibrationStrategy("None");
269 cirData->startValue() = 0.1;
270 cirData->reversionValue() = 0.1;
271 cirData->longTermValue() = 0.1;
272 cirData->volatility() = 0.1;
273 cirData->relaxedFeller() = true;
274 cirData->fellerFactor() = 1.1;
275 cirData->tolerance() = 1e-8;
276
277 QuantLib::ext::shared_ptr<vector<QuantLib::ext::shared_ptr<CrCirData>>> cirDataVector(new vector<QuantLib::ext::shared_ptr<CrCirData>>);
278 *cirDataVector = {cirData};
279 return cirDataVector;
280}
281
282QuantLib::ext::shared_ptr<data::CrossAssetModelData> crossAssetData() {
283
284 QuantLib::ext::shared_ptr<data::CrossAssetModelData> crossAssetData(new data::CrossAssetModelData());
285
286 crossAssetData->domesticCurrency() = "EUR";
287 crossAssetData->currencies() = {"EUR", "USD", "JPY"}; // need to check how to set this up
288 crossAssetData->equities() = {"SP5"};
289 crossAssetData->infIndices() = {"EUHICPXT"};
290 crossAssetData->creditNames() = {"ItraxxEuropeS9V1", "CDX.NA.S33v1"};
291 crossAssetData->irConfigs() = *irConfigsData();
292 crossAssetData->fxConfigs() = *fxConfigsData();
293 crossAssetData->eqConfigs() = *eqConfigsData();
294 crossAssetData->infConfigs() = infConfigsData();
295 crossAssetData->crLgmConfigs() = *crLgmConfigsData();
296 crossAssetData->crCirConfigs() = *crCirConfigsData();
297
299 cmb.addCorrelation("IR:EUR", "IR:USD", 1.0);
300 cmb.addCorrelation("IR:EUR", "IR:JPY", 1.0);
301 cmb.addCorrelation("IR:USD", "IR:JPY", 1.0);
302 cmb.addCorrelation("INF:EUHICPXT", "IR:EUR", 1.0);
303 cmb.addCorrelation("IR:EUR", "CR:ItraxxEuropeS9V1", 1.0);
304 cmb.addCorrelation("IR:USD", "CR:CDX.NA.S33v1", 1.0);
305 cmb.addCorrelation("CR:ItraxxEuropeS9V1", "CR:CDX.NA.S33v1", 1.0);
306
307 crossAssetData->setCorrelations(cmb.correlations());
308
309 crossAssetData->bootstrapTolerance() = 0.001;
310
311 return crossAssetData;
312}
313
314// Fixture to remove output files
315class F : public TopLevelFixture {
316public:
317 F() {}
318 ~F() { clearOutput(TEST_OUTPUT_PATH); }
319};
320} // namespace
321
322BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, TopLevelFixture)
323
324BOOST_FIXTURE_TEST_SUITE(CrossAssetModelDataTests, F)
325
326BOOST_AUTO_TEST_CASE(testToXMLFromXML) {
327
328 BOOST_TEST_MESSAGE("Testing toXML/fromXML...");
329
330 data::CrossAssetModelData data = *crossAssetData();
331 XMLDocument OutDoc;
332
333 XMLNode* simulationNode = OutDoc.allocNode("Simulation");
334 OutDoc.appendNode(simulationNode);
335
336 XMLNode* crossAssetModelNode = data.toXML(OutDoc);
337 XMLUtils::appendNode(simulationNode, crossAssetModelNode);
338
339 std::string filename = TEST_OUTPUT_FILE("simulationtest.xml");
340 OutDoc.toFile(filename);
341
343 newData.fromFile(filename);
344
345 BOOST_CHECK(data == newData);
346
347 newData.irConfigs() = {};
348 BOOST_CHECK(data != newData);
349}
350
351BOOST_AUTO_TEST_SUITE_END()
352
353BOOST_AUTO_TEST_SUITE_END()
void addCorrelation(const std::string &factor1, const std::string &factor2, QuantLib::Real correlation)
const std::map< CorrelationKey, QuantLib::Handle< QuantLib::Quote > > & correlations()
Get the raw correlation data.
CR LGM Model Parameters.
Definition: crlgmdata.hpp:48
Cross Asset Model Parameters.
const vector< QuantLib::ext::shared_ptr< IrModelData > > & irConfigs() const
EQ Model Parameters.
Definition: eqbsdata.hpp:50
FX Model Parameters.
Definition: fxbsdata.hpp:50
INF Model Parameters.
Definition: irlgmdata.hpp:51
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
void appendNode(XMLNode *)
Definition: xmlutils.cpp:118
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
void toFile(const string &filename) const
save the XML Document to the given file.
Definition: xmlutils.cpp:120
void fromFile(const std::string &filename)
Definition: xmlutils.cpp:150
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
configuration class for building correlation matrices
Cross asset model data.
Dodgson Kainth inflation model component data for the cross asset model.
IR component data for the cross asset model.
@ data
Definition: log.hpp:77
class for holding details of a zero coupon CPI cap floor calibration instrument.
CalibrationType parseCalibrationType(const string &s)
Convert calibration type string into enumerated class value.
Definition: irmodeldata.cpp:47
ParamType parseParamType(const string &s)
Convert parameter type string into enumerated class value.
Definition: irmodeldata.cpp:38
CrCirData::CalibrationStrategy parseCirCalibrationStrategy(const string &s)
Definition: crcirdata.cpp:42
LgmData::ReversionType parseReversionType(const string &s)
Enum parsers.
Definition: lgmdata.cpp:62
LgmData::VolatilityType parseVolatilityType(const string &s)
Definition: lgmdata.cpp:81
Serializable Credit Default Swap.
Definition: namespaces.docs:23
vector< Real > strikes
BOOST_AUTO_TEST_CASE(testToXMLFromXML)