Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
calibrationbasket.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 std::string;
23using std::vector;
24
25namespace ore {
26namespace data {
27
29 : instrumentType_(instrumentType) {}
30
32 return instrumentType_;
33}
34
36
37CalibrationBasket::CalibrationBasket(const vector<QuantLib::ext::shared_ptr<CalibrationInstrument>>& instruments)
38 : instruments_(instruments) {
39
40 // Check that all instruments in the basket, if any, are the same type.
41 for (const auto& instrument : instruments_) {
42 string instType = instrument->instrumentType();
43 if (instrumentType_.empty()) {
44 instrumentType_ = instType;
45 } else {
46 QL_REQUIRE(instrumentType_ == instType, "All instruments in CalibrationBasket should have the same " <<
47 "instrument type. Have " << instrumentType_ << " but current instrument is " << instType << ".");
48 }
49 }
50
51}
52
53const string& CalibrationBasket::instrumentType() const {
54 return instrumentType_;
55}
56
57const vector<QuantLib::ext::shared_ptr<CalibrationInstrument>>& CalibrationBasket::instruments() const {
58 return instruments_;
59}
60
61const string& CalibrationBasket::parameter() const {
62 return parameter_;
63}
64
66
67 QL_REQUIRE(empty(), "The calibration basket should be empty before calling fromXML.");
68 XMLUtils::checkNode(node, "CalibrationBasket");
69
70 for (XMLNode* cn = XMLUtils::getChildNode(node); cn; cn = XMLUtils::getNextSibling(cn)) {
71
72 // Take the instrument type from the first node name.
73 // All subsequent nodes should have the same instrument type.
74 string name = XMLUtils::getNodeName(cn);
75 if (instrumentType_.empty()) {
77 } else {
78 QL_REQUIRE(instrumentType_ == name, "All instruments in CalibrationBasket should have " <<
79 "the same instrument type. Have " << instrumentType_ << " but current node is " << name << ".");
80 }
81
82 // Create an instance of the calibration instrument and read it from XML.
83 auto instrument = CalibrationInstrumentFactory::instance().build(instrumentType_);
84 QL_REQUIRE(instrument, "Calibration instrument type " << instrumentType_ <<
85 " has not been registered with the calibration instrument factory.");
86 instrument->fromXML(cn);
87
88 // Add the instrument to the basket.
89 instruments_.push_back(instrument);
90 }
91
92 QL_REQUIRE(!empty(), "The calibration basket should have at least one calibration instrument.");
93
94 parameter_ = XMLUtils::getAttribute(node, "parameter");
95}
96
98
99 XMLNode* node = doc.allocNode("CalibrationBasket");
100
101 if (!parameter_.empty())
102 XMLUtils::addAttribute(doc, node, "parameter", parameter_);
103
104 for (const auto& instrument : instruments_) {
105 XMLUtils::appendNode(node, instrument->toXML(doc));
106 }
107
108 return node;
109}
110
112 return instruments_.empty();
113}
114
115}
116}
class for holding details of the calibration instruments for a model
factory for making calibration instruments.
CalibrationBasket()
Default constructor, empty calibration basket.
std::string parameter_
The parameter tag may be given so that builders know how to use the calibration basket.
std::vector< QuantLib::ext::shared_ptr< CalibrationInstrument > > instruments_
const std::string & parameter() const
bool empty() const
Returns true if the calibration basket is empty.
void fromXML(XMLNode *node) override
XMLNode * toXML(XMLDocument &doc) const override
const std::string & instrumentType() const
const std::vector< QuantLib::ext::shared_ptr< CalibrationInstrument > > & instruments() const
CalibrationInstrument(const std::string &instrumentType)
Constructor.
const std::string & instrumentType() const
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 XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
Get a node's next sibling node.
Definition: xmlutils.cpp:484
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string name