Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
lgmdata.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
25
35
36#include <ql/math/optimization/levenbergmarquardt.hpp>
37#include <ql/models/shortrate/calibrationhelpers/swaptionhelper.hpp>
38#include <ql/quotes/simplequote.hpp>
39#include <ql/utilities/dataformatters.hpp>
40
41#include <boost/algorithm/string/case_conv.hpp>
42#include <boost/lexical_cast.hpp>
43
44namespace ore {
45namespace data {
46
47bool LgmData::operator==(const LgmData& rhs) {
48
50 volType_ != rhs.volType_ || calibrateH_ != rhs.calibrateH_ || hType_ != rhs.hType_ || hTimes_ != rhs.hTimes_ ||
51 hValues_ != rhs.hValues_ || calibrateA_ != rhs.calibrateA_ || aType_ != rhs.aType_ || aTimes_ != rhs.aTimes_ ||
52 aValues_ != rhs.aValues_ || shiftHorizon_ != rhs.shiftHorizon_ || scaling_ != rhs.scaling_ ||
55 return false;
56 }
57 return true;
58}
59
60bool LgmData::operator!=(const LgmData& rhs) { return !(*this == rhs); }
61
63 if (boost::algorithm::to_upper_copy(s) == "HULLWHITE")
65 else if (boost::algorithm::to_upper_copy(s) == "HAGAN")
67 else
68 QL_FAIL("Reversion type " << s << " not recognized");
69}
70
71std::ostream& operator<<(std::ostream& oss, const LgmData::ReversionType& type) {
73 oss << "HULLWHITE";
74 else if (type == LgmData::ReversionType::Hagan)
75 oss << "HAGAN";
76 else
77 QL_FAIL("Reversion type not covered");
78 return oss;
79}
80
82 if (boost::algorithm::to_upper_copy(s) == "HULLWHITE")
84 else if (boost::algorithm::to_upper_copy(s) == "HAGAN")
86 else
87 QL_FAIL("Volatility type " << s << " not recognized");
88}
89
90std::ostream& operator<<(std::ostream& oss, const LgmData::VolatilityType& type) {
92 oss << "HULLWHITE";
93 else if (type == LgmData::VolatilityType::Hagan)
94 oss << "HAGAN";
95 else
96 QL_FAIL("Volatility type not covered");
97 return oss;
98}
99
101 if (boost::algorithm::to_upper_copy(s) == "NEXTCOUPON")
103 else if (boost::algorithm::to_upper_copy(s) == "PRORATA")
105 else if (boost::algorithm::to_upper_copy(s) == "SIMPLE")
106 return QuantExt::AnalyticLgmSwaptionEngine::simple;
107 else
108 QL_FAIL("FloatSpreadMapping '" << s << "' not recognized");
109}
110
111std::ostream& operator<<(std::ostream& oss, const QuantExt::AnalyticLgmSwaptionEngine::FloatSpreadMapping& m) {
113 oss << "NEXTCOUPON";
115 oss << "PRORATA";
116 else if (m == QuantExt::AnalyticLgmSwaptionEngine::simple)
117 oss << "SIMPLE";
118 else
119 QL_FAIL("FloatSpreadMapping type not covered");
120 return oss;
121}
122
124 optionExpiries_.clear();
125 optionTerms_.clear();
126 optionStrikes_.clear();
127}
128
133 calibrateH_ = false;
135 hTimes_ = {};
136 hValues_ = {0.03};
137 calibrateA_ = false;
139 aTimes_ = {};
140 aValues_ = {0.01};
141 shiftHorizon_ = 0.0;
142 scaling_ = 1.0;
143}
144
146 XMLNode* volNode = XMLUtils::getChildNode(node, "Volatility");
147
148 calibrateA_ = XMLUtils::getChildValueAsBool(volNode, "Calibrate", true);
149 LOG("LGM Volatility calibrate = " << calibrateA_);
150
151 std::string volTypeString = XMLUtils::getChildValue(volNode, "VolatilityType", true);
152 volType_ = parseVolatilityType(volTypeString);
153 LOG("LGM Volatility type = " << volTypeString);
154
155 std::string alphaTypeString = XMLUtils::getChildValue(volNode, "ParamType", true);
156 aType_ = parseParamType(alphaTypeString);
157 LOG("LGM Volatility param type = " << alphaTypeString);
158
159 aTimes_ = XMLUtils::getChildrenValuesAsDoublesCompact(volNode, "TimeGrid", true);
160 LOG("LGM Volatility time grid size = " << aTimes_.size());
161
162 aValues_ = XMLUtils::getChildrenValuesAsDoublesCompact(volNode, "InitialValue", true);
163 LOG("LGM Volatility initial values size = " << aValues_.size());
164
165 // Reversion config
166
167 XMLNode* revNode = XMLUtils::getChildNode(node, "Reversion");
168
169 calibrateH_ = XMLUtils::getChildValueAsBool(revNode, "Calibrate", true);
170 LOG("LGM Reversion calibrate = " << calibrateH_);
171
172 std::string revTypeString = XMLUtils::getChildValue(revNode, "ReversionType", true);
173 revType_ = parseReversionType(revTypeString);
174 LOG("LGM Reversion type = " << revTypeString);
175
176 std::string hTypeString = XMLUtils::getChildValue(revNode, "ParamType", true);
177 hType_ = parseParamType(hTypeString);
178 LOG("LGM Reversion parameter type = " << hTypeString);
179
180 hTimes_ = XMLUtils::getChildrenValuesAsDoublesCompact(revNode, "TimeGrid", true);
181 LOG("LGM Reversion time grid size = " << hTimes_.size());
182
183 hValues_ = XMLUtils::getChildrenValuesAsDoublesCompact(revNode, "InitialValue", true);
184 LOG("LGM Reversion initial values size = " << hValues_.size());
185
186 // Parameter transformation config
187
188 if (XMLNode* tranformNode = XMLUtils::getChildNode(node, "ParameterTransformation")) {
189 shiftHorizon_ = XMLUtils::getChildValueAsDouble(tranformNode, "ShiftHorizon", true);
190 LOG("LGM shift horizon = " << shiftHorizon_);
191
192 scaling_ = XMLUtils::getChildValueAsDouble(tranformNode, "Scaling", true);
193 LOG("LGM scaling = " << scaling_);
194 } else {
195 shiftHorizon_ = 0.0;
196 scaling_ = 1.0;
197 }
198
200 parseFloatSpreadMapping(XMLUtils::getChildValue(node, "FloatSpreadMapping", false, "proRata"));
201
203
204 LOG("LgmData done");
205}
206
208
209 XMLNode* lgmNode = IrModelData::toXML(doc);
210
211 // volatility
212 XMLNode* volatilityNode = XMLUtils::addChild(doc, lgmNode, "Volatility");
213 XMLUtils::addChild(doc, volatilityNode, "Calibrate", calibrateA_);
214
215 XMLNode* volatilityTypeNode = doc.allocNode("VolatilityType", to_string(volType_));
216 XMLUtils::appendNode(volatilityNode, volatilityTypeNode);
217
218 XMLUtils::addGenericChild(doc, volatilityNode, "ParamType", aType_);
219 XMLUtils::addGenericChildAsList(doc, volatilityNode, "TimeGrid", aTimes_);
220 XMLUtils::addGenericChildAsList(doc, volatilityNode, "InitialValue", aValues_);
221
222 // reversion
223 XMLNode* reversionNode = XMLUtils::addChild(doc, lgmNode, "Reversion");
224 XMLUtils::addChild(doc, reversionNode, "Calibrate", calibrateH_);
225
226 XMLNode* reversionTypeNode = doc.allocNode("ReversionType", to_string(revType_));
227 XMLUtils::appendNode(reversionNode, reversionTypeNode);
228
229 XMLUtils::addGenericChild(doc, reversionNode, "ParamType", hType_);
230 XMLUtils::addGenericChildAsList(doc, reversionNode, "TimeGrid", hTimes_);
231 XMLUtils::addGenericChildAsList(doc, reversionNode, "InitialValue", hValues_);
232
233 // parameter transformation
234 XMLNode* parameterTransformationNode = XMLUtils::addChild(doc, lgmNode, "ParameterTransformation");
235 XMLUtils::addChild(doc, parameterTransformationNode, "ShiftHorizon", shiftHorizon_);
236 XMLUtils::addChild(doc, parameterTransformationNode, "Scaling", scaling_);
237
238 XMLUtils::addChild(doc, lgmNode, "FloatSpreadMapping", ore::data::to_string(floatSpreadMapping_));
239
240 return lgmNode;
241}
242
245}
246
249}
250
252
254 : horizon_(horizon), scaling_(scaling) {}
255
257
259
261 XMLUtils::checkNode(node, "ParameterTransformation");
262 horizon_ = XMLUtils::getChildValueAsDouble(node, "ShiftHorizon", true);
263 scaling_ = XMLUtils::getChildValueAsDouble(node, "Scaling", true);
264}
265
267 XMLNode* node = doc.allocNode("ParameterTransformation");
268 XMLUtils::addChild(doc, node, "ShiftHorizon", horizon_);
269 XMLUtils::addChild(doc, node, "Scaling", scaling_);
270 return node;
271}
272
273} // namespace data
274} // namespace ore
CalibrationType calibrationType_
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
virtual void reset()
Reset member variables to defaults.
Linear Gauss Markov Model Parameters.
Definition: lgmdata.hpp:53
ParamType aType_
Definition: lgmdata.hpp:142
VolatilityParameter volatilityParameter() const
Definition: lgmdata.cpp:247
bool operator!=(const LgmData &rhs)
Definition: lgmdata.cpp:60
std::vector< std::string > optionTerms_
Definition: lgmdata.hpp:147
VolatilityType volType_
Definition: lgmdata.hpp:136
ReversionParameter reversionParameter() const
Definition: lgmdata.cpp:243
QuantExt::AnalyticLgmSwaptionEngine::FloatSpreadMapping floatSpreadMapping_
Definition: lgmdata.hpp:149
std::vector< std::string > optionExpiries_
Definition: lgmdata.hpp:146
std::vector< Time > hTimes_
Definition: lgmdata.hpp:139
std::vector< Time > aTimes_
Definition: lgmdata.hpp:143
virtual void fromXML(XMLNode *node) override
Definition: lgmdata.cpp:145
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: lgmdata.cpp:207
ReversionType
Supported mean reversion types.
Definition: lgmdata.hpp:56
@ Hagan
Parametrize LGM H(t) as H(t) = int_0^t h(s) ds with constant or piecewise h(s)
VolatilityType
Supported volatility types.
Definition: lgmdata.hpp:67
@ HullWhite
Parametrize volatility as HullWhite sigma(t)
@ Hagan
Parametrize volatility as Hagan alpha(t)
std::vector< Real > aValues_
Definition: lgmdata.hpp:144
bool operator==(const LgmData &rhs)
Definition: lgmdata.cpp:47
std::vector< Real > hValues_
Definition: lgmdata.hpp:140
ParamType hType_
Definition: lgmdata.hpp:138
void reset() override
Reset member variables to defaults.
Definition: lgmdata.cpp:129
void clear() override
Clear list of calibration instruments.
Definition: lgmdata.cpp:123
std::vector< std::string > optionStrikes_
Definition: lgmdata.hpp:148
ReversionType revType_
Definition: lgmdata.hpp:135
void fromXML(XMLNode *node) override
Definition: lgmdata.cpp:260
XMLNode * toXML(XMLDocument &doc) const override
Definition: lgmdata.cpp:266
LgmReversionTransformation()
Default constructor setting the horizon to 0.0 and the scaling to 1.0.
Definition: lgmdata.cpp:251
QuantLib::Real scaling() const
Definition: lgmdata.cpp:258
QuantLib::Time horizon() const
Definition: lgmdata.cpp:256
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static void addGenericChildAsList(XMLDocument &doc, XMLNode *n, const string &name, const vector< T > &values, const string &attrName="", const string &attr="")
Definition: xmlutils.hpp:144
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static void addGenericChild(XMLDocument &doc, XMLNode *n, const char *name, const T &value)
Adds <Name>p1,p2,p3</Name>
Definition: xmlutils.hpp:137
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static bool getChildValueAsBool(XMLNode *node, const string &name, bool mandatory=false, bool defaultValue=true)
Definition: xmlutils.cpp:296
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static vector< Real > getChildrenValuesAsDoublesCompact(XMLNode *node, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:327
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
configuration class for building correlation matrices
Linear Gauss Markov model data.
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define LOG(text)
Logging Macro (Level = Notice)
Definition: log.hpp:552
class for holding model parameter data
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
ParamType parseParamType(const string &s)
Convert parameter type string into enumerated class value.
Definition: irmodeldata.cpp:38
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
LgmData::ReversionType parseReversionType(const string &s)
Enum parsers.
Definition: lgmdata.cpp:62
QuantExt::AnalyticLgmSwaptionEngine::FloatSpreadMapping parseFloatSpreadMapping(const string &s)
Definition: lgmdata.cpp:100
LgmData::VolatilityType parseVolatilityType(const string &s)
Definition: lgmdata.cpp:81
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string conversion utilities