Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
calibrationconfiguration.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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
21
22using QuantLib::BoundaryConstraint;
23using QuantLib::Constraint;
24using QuantLib::NoConstraint;
25using QuantLib::Null;
26using QuantLib::Real;
27using QuantLib::Size;
28using std::string;
29using std::make_pair;
30using std::map;
31using std::pair;
32
33namespace ore {
34namespace data {
35
36CalibrationConfiguration::CalibrationConfiguration(Real rmseTolerance, Size maxIterations)
37 : rmseTolerance_(rmseTolerance), maxIterations_(maxIterations) {}
38
40 return rmseTolerance_;
41}
42
44 return maxIterations_;
45}
46
47QuantLib::ext::shared_ptr<Constraint> CalibrationConfiguration::constraint(const string& name) const {
48 auto it = constraints_.find(name);
49 if (it != constraints_.end()) {
50 return QuantLib::ext::make_shared<BoundaryConstraint>(it->second.first, it->second.second);
51 } else {
52 return QuantLib::ext::make_shared<NoConstraint>();
53 }
54}
55
56pair<Real, Real> CalibrationConfiguration::boundaries(const string& name) const {
57 auto it = constraints_.find(name);
58 if (it != constraints_.end()) {
59 return it->second;
60 } else {
61 return make_pair(Null<Real>(), Null<Real>());
62 }
63}
64
65void CalibrationConfiguration::add(const string& name, QuantLib::Real lowerBound, QuantLib::Real upperBound) {
66 QL_REQUIRE(lowerBound < upperBound, "CalibrationConfiguration: Lower bound (" << lowerBound <<
67 ") must be less than upper bound (" << upperBound << ").");
68 constraints_[name] = make_pair(lowerBound, upperBound);
69 DLOG("Boundary constraint [" << lowerBound << "," << upperBound << "] added for parameter " << name << ".");
70}
71
73
74 XMLUtils::checkNode(node, "CalibrationConfiguration");
75
76 rmseTolerance_ = 0.0001;
77 if (XMLNode* n = XMLUtils::getChildNode(node, "RmseTolerance")) {
79 }
80
81 maxIterations_ = 50;
82 if (XMLNode* n = XMLUtils::getChildNode(node, "MaxIterations")) {
84 }
85
86 XMLNode* constraintsNode = XMLUtils::getChildNode(node, "Constraints");
87 for (XMLNode* cn = XMLUtils::getChildNode(constraintsNode); cn; cn = XMLUtils::getNextSibling(cn)) {
88
89 // Only support boundary constraints for the moment.
90 string constraintName = XMLUtils::getNodeName(cn);
91 if (constraintName != "BoundaryConstraint") {
92 DLOG("CalibrationConfiguration skipping constraint with name " << constraintName << ". Only " <<
93 "BoundaryConstraint is currently supported.");
94 continue;
95 }
96
97 auto name = XMLUtils::getAttribute(cn, "parameter");
98 auto lowerBound = parseReal(XMLUtils::getChildValue(cn, "LowerBound", true));
99 auto upperBound = parseReal(XMLUtils::getChildValue(cn, "UpperBound", true));
100 add(name, lowerBound, upperBound);
101 }
102
103}
104
106
107 XMLNode* node = doc.allocNode("CalibrationConfiguration");
108
109 XMLUtils::addChild(doc, node, "RmseTolerance", rmseTolerance_);
110 XMLUtils::addChild(doc, node, "MaxIterations", static_cast<int>(maxIterations_));
111
112 XMLNode* constraintsNode = doc.allocNode("Constraints");
113 for (const auto& kv : constraints_) {
114 XMLNode* n = doc.allocNode("BoundaryConstraint");
115 XMLUtils::addChild(doc, n, "LowerBound", kv.second.first);
116 XMLUtils::addChild(doc, n, "UpperBound", kv.second.second);
117 XMLUtils::addAttribute(doc, n, "parameter", kv.first);
118 XMLUtils::appendNode(constraintsNode, n);
119 }
120 XMLUtils::appendNode(node, constraintsNode);
121
122 return node;
123}
124
125}
126}
class for holding calibration configuration details
QuantLib::Real rmseTolerance() const
A final tolerance on the RMSE of the calibration that may be used by various builders.
QuantLib::ext::shared_ptr< QuantLib::Constraint > constraint(const std::string &name) const
std::map< std::string, std::pair< QuantLib::Real, QuantLib::Real > > constraints_
std::pair< QuantLib::Real, QuantLib::Real > boundaries(const std::string &name) const
XMLNode * toXML(XMLDocument &doc) const override
void add(const std::string &name, QuantLib::Real lowerBound, QuantLib::Real upperBound)
CalibrationConfiguration(QuantLib::Real rmseTolerance=0.0001, QuantLib::Size maxIterations=50)
Constructor.
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 addAttribute(XMLDocument &doc, XMLNode *node, const string &attrName, const string &attrValue)
Definition: xmlutils.cpp:412
static string getAttribute(XMLNode *node, const string &attrName)
Definition: xmlutils.cpp:419
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static string getNodeName(XMLNode *n)
Get and set a node's name.
Definition: xmlutils.cpp:473
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static string getNodeValue(XMLNode *node)
Get a node's value.
Definition: xmlutils.cpp:489
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
Get a node's next sibling node.
Definition: xmlutils.cpp:484
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
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string name