Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
adjustmentfactors.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 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
22
23namespace ore {
24namespace data {
25
26bool AdjustmentFactors::hasFactor(const string& name) const { return data_.find(name) != data_.end(); }
27
28Real AdjustmentFactors::getFactor(const string& name, const QuantLib::Date& d) const {
29 Real baseFactor = 1.0;
30 // If no adjustments return a factor of 1
31 if (!hasFactor(name))
32 return baseFactor;
33
34 // for the given name loop through all the adjustments
35 // adjustments are applied backwards to a time series,
36 // if the date is before asof date:
37 // we multiply by the factor from any future adjustments but before the asof date,
38 // this ensures all data is on the same scale at the asof date
39 // if date is after asof date:
40 // we divide by the factor from any historical adjustments between asof and date
41 for (auto f : data_.at(name)) {
42 if (d < f.first && asof_ > f.first) {
43 baseFactor = baseFactor * f.second;
44 }
45 if (asof_ < f.first && f.first <= d) {
46 baseFactor = baseFactor / f.second;
47 }
48 }
49 return baseFactor;
50}
51
52void AdjustmentFactors::addFactor(string name, QuantLib::Date d, Real factor) {
53 data_[name].push_back(std::pair<Date, Real>(d, factor));
54}
55
57 XMLUtils::checkNode(node, "AdditionalData");
58
59 XMLNode* childNode = XMLUtils::locateNode(node, "AdjustmentFactors");
60 for (XMLNode* child = XMLUtils::getChildNode(childNode); child; child = XMLUtils::getNextSibling(child)) {
61
62 Date date = parseDate(XMLUtils::getChildValue(child, "Date", true));
63 std::string quote = XMLUtils::getChildValue(child, "Quote", true);
64 Real factor = XMLUtils::getChildValueAsDouble(child, "Factor", true);
65
66 addFactor(quote, date, factor);
67 }
68}
69
71 XMLNode* node = doc.allocNode("AdjustmentFactors");
72 for (auto d : data_) {
73 for (auto f : d.second) {
74 XMLNode* factorNode = XMLUtils::addChild(doc, node, "AdjustmentFactor");
75 XMLUtils::addChild(doc, factorNode, "Date", ore::data::to_string(f.first));
76 XMLUtils::addChild(doc, factorNode, "Quote", d.first);
77 XMLUtils::addChild(doc, factorNode, "Factor", f.second);
78 }
79 }
80 return node;
81}
82
83std::set<std::string> AdjustmentFactors::names() const {
84 std::set<std::string> result;
85 for (auto const& m : data_)
86 result.insert(m.first);
87 return result;
88}
89
90std::set<QuantLib::Date> AdjustmentFactors::dates(const std::string& name) const {
91 std::set<QuantLib::Date> result;
92 auto d = data_.find(name);
93 if (d != data_.end()) {
94 for (auto const& m : d->second)
95 result.insert(m.first);
96 }
97 return result;
98}
99
100QuantLib::Real AdjustmentFactors::getFactorContribution(const std::string& name, const QuantLib::Date& d) const {
101 auto adj = data_.find(name);
102 if (adj != data_.end()) {
103 auto it = std::find_if(adj->second.begin(), adj->second.end(),
104 [&d](const std::pair<QuantLib::Date, QuantLib::Real>& m) { return m.first == d; });
105 if (it != adj->second.end())
106 return it->second;
107 }
108 return 1.0;
109}
110
111} // namespace data
112} // namespace ore
QuantLib::Real getFactor(const std::string &name, const QuantLib::Date &d) const
Returns the adjustment factor for a name on a given date.
std::map< std::string, std::vector< std::pair< QuantLib::Date, QuantLib::Real > > > data_
Map of names to adjustment factors.
virtual void fromXML(ore::data::XMLNode *node) override
std::set< QuantLib::Date > dates(const std::string &name) const
dates with contributions to an adjustment factor for a name
QuantLib::Date asof_
Asof date - only apply adjustments before this date.
std::set< std::string > names() const
names with adjustment factors
virtual ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
void addFactor(std::string name, QuantLib::Date d, QuantLib::Real factor)
Add an adjustment factor.
QuantLib::Real getFactorContribution(const std::string &name, const QuantLib::Date &d) const
gets the contribution to an adjustment factor for a name on a given date
bool hasFactor(const std::string &name) const
Check if we have any adjustment factors for a name.
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 checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
static XMLNode * locateNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:393
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 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
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
@ data
Definition: log.hpp:77
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string conversion utilities
string name