19#include <boost/make_shared.hpp>
38using ShiftData = SensitivityScenarioData::ShiftData;
44 vector<string> shiftTypeKeys;
45 vector<string> shiftSizeKeys;
46 vector<string> shiftSchemeKeys;
50 auto shiftSizes = XMLUtils::getChildrenValuesWithAttributes<Real>(child, std::string(),
"ShiftSize",
"key",
57 QL_REQUIRE(shiftTypeKeys.size() == std::set<string>(shiftTypeKeys.begin(), shiftTypeKeys.end()).size(),
58 "SensitivityScenarioData::shiftDataFromXML(): non-unique attributes for ShiftType in node '"
60 QL_REQUIRE(shiftSizeKeys.size() == std::set<string>(shiftSizeKeys.begin(), shiftSizeKeys.end()).size(),
61 "SensitivityScenarioData::shiftDataFromXML(): non-unique attributes for ShiftSize in node '"
63 QL_REQUIRE(shiftSchemeKeys.size() == std::set<string>(shiftSchemeKeys.begin(), shiftSchemeKeys.end()).size(),
64 "SensitivityScenarioData::shiftDataFromXML(): non-unique attributes for ShiftScheme in node '"
69 auto shiftTypeEmptyKey = std::find(shiftTypeKeys.begin(), shiftTypeKeys.end(), std::string());
70 auto shiftSizeEmptyKey = std::find(shiftSizeKeys.begin(), shiftSizeKeys.end(), std::string());
71 auto shiftSchemeEmptyKey = std::find(shiftSchemeKeys.begin(), shiftSchemeKeys.end(), std::string());
73 QL_REQUIRE(shiftTypeEmptyKey != shiftTypeKeys.end(),
74 "SensitivityScenarioData::shiftDataFromXML(): no ShiftType without attribute defined in node '"
76 QL_REQUIRE(shiftSizeEmptyKey != shiftSizeKeys.end(),
77 "SensitivityScenarioData::shiftDataFromXML(): no ShiftSize without attribute defined in node '"
80 data.shiftType =
parseShiftType(shiftTypes.at(std::distance(shiftTypeKeys.begin(), shiftTypeEmptyKey)));
81 data.shiftSize = shiftSizes.at(std::distance(shiftSizeKeys.begin(), shiftSizeEmptyKey));
83 if (shiftSchemeEmptyKey == shiftSchemeKeys.end())
87 parseShiftScheme(shiftSchemes.at(std::distance(shiftSchemeKeys.begin(), shiftSchemeEmptyKey)));
91 for (Size i = 0; i < shiftTypeKeys.size(); ++i) {
92 if (!shiftTypeKeys[i].empty()) {
96 for (Size i = 0; i < shiftSizeKeys.size(); ++i) {
97 if (!shiftSizeKeys[i].empty()) {
98 data.keyedShiftSize[shiftSizeKeys[i]] = shiftSizes[i];
101 for (Size i = 0; i < shiftSchemeKeys.size(); ++i) {
102 if (!shiftSchemeKeys[i].empty()) {
117 if (
data.shiftStrikes.size() == 0)
118 data.shiftStrikes = {0.0};
128 for (
auto const& [k, v] :
data.keyedShiftType)
131 for (
auto const& [k, v] :
data.keyedShiftSize)
134 for (
auto const& [k, v] :
data.keyedShiftScheme)
152 case RFType::DiscountCurve:
154 case RFType::IndexCurve:
156 case RFType::YieldCurve:
160 case RFType::SwaptionVolatility:
162 case RFType::YieldVolatility:
164 case RFType::OptionletVolatility:
166 case RFType::FXVolatility:
168 case RFType::CDSVolatility:
170 case RFType::BaseCorrelation:
172 case RFType::ZeroInflationCurve:
174 case RFType::SurvivalProbability:
176 case RFType::YoYInflationCurve:
178 case RFType::YoYInflationCapFloorVolatility:
180 case RFType::ZeroInflationCapFloorVolatility:
182 case RFType::EquitySpot:
184 case RFType::EquityVolatility:
186 case RFType::DividendYield:
188 case RFType::CommodityCurve:
190 case RFType::CommodityVolatility:
192 case RFType::SecuritySpread:
194 case RFType::Correlation:
197 QL_FAIL(
"Cannot return shift data for key type: " << keyType);
205 DLOG(
"Get discount curve sensitivity parameters");
207 if (discountCurves) {
211 DLOG(
"Discount curve for ccy " << ccy);
218 DLOG(
"Get index curve sensitivity parameters");
230 DLOG(
"Get yield curve sensitivity parameters");
244 DLOG(
"Get dividend yield curve sensitivity parameters");
246 if (dividendYieldCurves) {
250 DLOG(
"Add dividend yield curve data for equity " << curveName);
257 DLOG(
"Get FX spot sensitivity parameters");
269 DLOG(
"Get swaption vol sensitivity parameters");
277 if(!ccyAttr.empty()) {
279 WLOG(
"SensitivityData: attribute 'ccy' for SwaptionVolatilities is deprecated, use 'key' instead.");
285 if (
data.shiftStrikes.size() == 0)
286 data.shiftStrikes = {0.0};
291 DLOG(
"Get yield vol sensitivity parameters");
300 QL_REQUIRE(
data.shiftStrikes.size() == 0 ||
302 "no shift strikes (or exactly {0.0}) should be given for yield volatilities");
303 data.shiftStrikes = {0.0};
308 DLOG(
"Get cap/floor vol sensitivity parameters");
316 if(!ccyAttr.empty()) {
318 WLOG(
"SensitivityData: attribute 'ccy' for CapFloorVolatilities is deprecated, use 'key' instead.");
321 auto data = QuantLib::ext::make_shared<CapFloorVolShiftData>();
328 DLOG(
"Get fx vol sensitivity parameters");
340 DLOG(
"Get credit curve sensitivity parameters");
354 DLOG(
"Get cds vol sensitivity parameters");
367 DLOG(
"Get Base Correlation sensitivity parameters");
381 DLOG(
"Get Equity spot sensitivity parameters");
393 DLOG(
"Get Equity vol sensitivity parameters");
405 DLOG(
"Get Zero Inflation sensitivity parameters");
417 DLOG(
"Get Yoy Inflation sensitivity parameters");
429 DLOG(
"Get yoy inflation cap/floor vol sensitivity parameters");
435 auto data = QuantLib::ext::make_shared<CapFloorVolShiftData>();
441 DLOG(
"Get zero inflation cap/floor vol sensitivity parameters");
447 auto data = QuantLib::ext::make_shared<CapFloorVolShiftData>();
453 DLOG(
"Get commodity curve sensitivity parameters");
466 DLOG(
"Get commodity volatility sensitivity parameters");
479 data.shiftStrikes[0] = 1.0;
485 DLOG(
"Get security spread sensitivity parameters");
487 if (securitySpreads) {
497 DLOG(
"Get correlation sensitivity parameters");
504 string label = index1 +
":" + index2;
513 DLOG(
"Get cross gamma parameters");
515 for (Size i = 0; i <
filter.size(); ++i) {
516 vector<string> tokens;
517 boost::split(tokens,
filter[i], boost::is_any_of(
","));
518 QL_REQUIRE(tokens.size() == 2,
"expected 2 tokens, found " << tokens.size() <<
" in " <<
filter[i]);
523 DLOG(
"Get compute gamma flag");
526 DLOG(
"Get useSpreadedTermStructures flag");
538 DLOG(
"Get discount curve parSensitivity parameters");
540 if (discountCurves) {
550 DLOG(
"Get index curve parSensitivity parameters");
562 DLOG(
"Get yield curve parSensitivity parameters");
575 DLOG(
"Get cap/floor vol par sensitivity parameters");
583 if(!ccyAttr.empty()) {
585 WLOG(
"SensitivityData: attribute 'ccy' for CapFloorVolatilities is deprecated, use 'key' instead.");
597 DLOG(
"Get credit curve parSensitivity parameters");
609 DLOG(
"Get Zero Inflation parSensitivity parameters");
621 DLOG(
"Get Yoy Inflation parSensitivity parameters");
633 DLOG(
"Get Yoy cap/floor vol par sensitivity parameters");
647 data.parInstrumentConventions =
660 DLOG(
"toXML for DiscountCurves");
670 DLOG(
"toXML for IndexCurves");
681 DLOG(
"toXML for YieldCurves");
690 DLOG(
"toXML for DividendYieldCurves");
700 DLOG(
"toXML for FxSpots");
710 DLOG(
"toXML for SwaptionVolatilities");
721 DLOG(
"toXML for YieldVolatilities");
732 DLOG(
"toXML for CapFloorVolatilities");
744 DLOG(
"toXML for FxVolatilities");
754 DLOG(
"toXML for CreditCurves");
765 DLOG(
"toXML for CDSVolatilities");
776 DLOG(
"toXML for BaseCorrelations");
788 DLOG(
"toXML for EquitySpots");
798 DLOG(
"toXML for EquityVolatilities");
808 DLOG(
"toXML for ZeroInflationIndexCurves");
818 DLOG(
"toXML for YYInflationIndexCurves");
828 DLOG(
"toXML for YYInflationCapFloorVolatilities");
838 DLOG(
"toXML for CPIInflationCapFloorVolatilities");
848 DLOG(
"toXML for CommodityCurves");
859 DLOG(
"toXML for CommodityVolatilities");
869 DLOG(
"toXML for SecuritySpreads");
879 DLOG(
"toXML for Correlations");
883 string label = kv.first;
892 DLOG(
"toXML for CrossGammaFilter");
908 if (discountCurves) {
909 DLOG(
"toXML for DiscountCurves ParConversion node");
920 DLOG(
"toXML for IndexCurves ParConversion node");
931 DLOG(
"toXML for YieldCurves ParConversion node");
946 DLOG(
"toXML for CreditCurves ParConversion node");
957 DLOG(
"toXML for ZeroInflationIndexCurves ParConversion node");
968 DLOG(
"toXML for YYInflationIndexCurves ParConversion node");
979 DLOG(
"toXML for CapFloorVolatilities ParConversion node");
986 if (!
data->discountCurve.empty())
995 DLOG(
"toXML for YYCapFloorVolatilities ParConversion node");
1004 if (!
data->discountCurve.empty())
1008 for (
const auto& kv :
data->parInstrumentConventions) {
1021 vector<string> tokens;
1022 boost::split(tokens, indexName, boost::is_any_of(
"-"));
1023 QL_REQUIRE(tokens.size() > 1,
"expected 2 or 3 tokens, found " << tokens.size() <<
" in " << indexName);
1041 const QuantLib::ext::shared_ptr<CurveShiftData>& csd)
const {
1044 auto data = QuantLib::ext::dynamic_pointer_cast<CurveShiftParData>(csd);
1047 QL_REQUIRE(
data,
"The sensitivity configuration should have par conversion data");
1052 if (!
data->discountCurve.empty())
1054 if (!
data->otherCurrency.empty())
1057 for (
const auto& kv :
data->parInstrumentConventions) {
1067void extractKeysFromShiftData(
const ShiftData& d, std::set<string>& pids) {
1078 std::set<std::string> pids;
1080 extractKeysFromShiftData(*v, pids);
1082 extractKeysFromShiftData(*v, pids);
1084 extractKeysFromShiftData(v, pids);
1086 extractKeysFromShiftData(*v, pids);
1088 extractKeysFromShiftData(v, pids);
1090 extractKeysFromShiftData(v, pids);
1092 extractKeysFromShiftData(v, pids);
1094 extractKeysFromShiftData(v, pids);
1096 extractKeysFromShiftData(v, pids);
1098 extractKeysFromShiftData(*v, pids);
1100 extractKeysFromShiftData(*v, pids);
1102 extractKeysFromShiftData(*v, pids);
1104 extractKeysFromShiftData(*v, pids);
1106 extractKeysFromShiftData(*v, pids);
1108 extractKeysFromShiftData(v, pids);
1110 extractKeysFromShiftData(v, pids);
1112 extractKeysFromShiftData(*v, pids);
1114 extractKeysFromShiftData(*v, pids);
1116 extractKeysFromShiftData(v, pids);
1118 extractKeysFromShiftData(v, pids);
1120 extractKeysFromShiftData(v, pids);
Data types stored in the scenario class.
KeyType
Risk Factor types.
Description of sensitivity shift scenarios.
void shiftDataFromXML(XMLNode *child, ShiftData &data)
map< string, GenericYieldVolShiftData > swaptionVolShiftData_
const map< string, QuantLib::ext::shared_ptr< CapFloorVolShiftData > > & yoyInflationCapFloorVolShiftData() const
const map< string, QuantLib::ext::shared_ptr< CurveShiftData > > & indexCurveShiftData() const
map< string, QuantLib::ext::shared_ptr< CapFloorVolShiftData > > capFloorVolShiftData_
map< string, QuantLib::ext::shared_ptr< CurveShiftData > > commodityCurveShiftData_
const map< string, QuantLib::ext::shared_ptr< CapFloorVolShiftData > > & capFloorVolShiftData() const
const map< string, SpotShiftData > & securityShiftData() const
const ShiftData & shiftData(const ore::analytics::RiskFactorKey::KeyType &keyType, const std::string &name) const
Give back the shift data for the given risk factor type, keyType, with the given name.
map< string, QuantLib::ext::shared_ptr< CapFloorVolShiftData > > yoyInflationCapFloorVolShiftData_
map< string, string > creditCcys_
const map< string, VolShiftData > & equityVolShiftData() const
map< string, VolShiftData > correlationShiftData_
string getIndexCurrency(string indexName)
Utilities.
map< string, QuantLib::ext::shared_ptr< CurveShiftData > > creditCurveShiftData_
map< string, SpotShiftData > equityShiftData_
const map< string, QuantLib::ext::shared_ptr< CurveShiftData > > & discountCurveShiftData() const
const map< string, QuantLib::ext::shared_ptr< CurveShiftData > > & dividendYieldShiftData() const
vector< pair< string, string > > crossGammaFilter_
map< string, QuantLib::ext::shared_ptr< CurveShiftData > > zeroInflationCurveShiftData_
bool useSpreadedTermStructures_
const map< string, VolShiftData > & fxVolShiftData() const
const map< string, GenericYieldVolShiftData > & yieldVolShiftData() const
map< string, QuantLib::ext::shared_ptr< CurveShiftData > > yoyInflationCurveShiftData_
const map< string, VolShiftData > & correlationShiftData() const
const map< string, QuantLib::ext::shared_ptr< CurveShiftData > > & zeroInflationCurveShiftData() const
void parDataFromXML(XMLNode *child, CurveShiftParData &data)
map< string, GenericYieldVolShiftData > yieldVolShiftData_
const map< string, QuantLib::ext::shared_ptr< CurveShiftData > > & creditCurveShiftData() const
map< string, CdsVolShiftData > cdsVolShiftData_
XMLNode * parDataToXML(ore::data::XMLDocument &doc, const QuantLib::ext::shared_ptr< CurveShiftData > &csd) const
toXML helper method
virtual void fromXML(XMLNode *node) override
map< string, QuantLib::ext::shared_ptr< CurveShiftData > > yieldCurveShiftData_
void curveShiftDataFromXML(XMLNode *child, CurveShiftData &data)
const map< string, SpotShiftData > & fxShiftData() const
const map< string, GenericYieldVolShiftData > & swaptionVolShiftData() const
const map< string, QuantLib::ext::shared_ptr< CurveShiftData > > & yoyInflationCurveShiftData() const
void volShiftDataToXML(ore::data::XMLDocument &doc, XMLNode *node, const VolShiftData &data) const
map< string, std::string > commodityCurrencies_
const map< string, QuantLib::ext::shared_ptr< CapFloorVolShiftData > > & zeroInflationCapFloorVolShiftData() const
virtual XMLNode * toXML(ore::data::XMLDocument &doc) const override
void volShiftDataFromXML(XMLNode *child, VolShiftData &data, const bool requireShiftStrikes=true)
map< string, VolShiftData > commodityVolShiftData_
const map< string, BaseCorrelationShiftData > & baseCorrelationShiftData() const
map< string, BaseCorrelationShiftData > baseCorrelationShiftData_
const map< string, QuantLib::ext::shared_ptr< CurveShiftData > > & yieldCurveShiftData() const
map< string, VolShiftData > equityVolShiftData_
const map< string, QuantLib::ext::shared_ptr< CurveShiftData > > & commodityCurveShiftData() const
map< string, QuantLib::ext::shared_ptr< CurveShiftData > > dividendYieldShiftData_
map< string, SpotShiftData > fxShiftData_
map< string, SpotShiftData > securityShiftData_
map< string, VolShiftData > fxVolShiftData_
map< string, QuantLib::ext::shared_ptr< CurveShiftData > > discountCurveShiftData_
void curveShiftDataToXML(ore::data::XMLDocument &doc, XMLNode *node, const CurveShiftData &data) const
map< string, QuantLib::ext::shared_ptr< CurveShiftData > > indexCurveShiftData_
map< string, QuantLib::ext::shared_ptr< CapFloorVolShiftData > > zeroInflationCapFloorVolShiftData_
const map< string, VolShiftData > & commodityVolShiftData() const
void shiftDataToXML(ore::data::XMLDocument &doc, XMLNode *node, const ShiftData &data) const
toXML helper methods
const map< string, CdsVolShiftData > & cdsVolShiftData() const
const map< string, SpotShiftData > & equityShiftData() const
XMLNode * allocNode(const string &nodeName)
static void addAttribute(XMLDocument &doc, XMLNode *node, const string &attrName, const string &attrValue)
static string getAttribute(XMLNode *node, const string &attrName)
static void addGenericChildAsList(XMLDocument &doc, XMLNode *n, const string &name, const vector< T > &values, const string &attrName="", const string &attr="")
static void checkNode(XMLNode *n, const string &expectedName)
static XMLNode * locateNode(XMLNode *n, const string &name="")
static string getNodeName(XMLNode *n)
static map< string, string > getChildrenAttributesAndValues(XMLNode *parent, const string &names, const string &attributeName, bool mandatory=false)
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
static bool getChildValueAsBool(XMLNode *node, const string &name, bool mandatory=false, bool defaultValue=true)
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static string getNodeValue(XMLNode *node)
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
static vector< string > getChildrenValuesAsStrings(XMLNode *node, const string &name, bool mandatory=false)
static vector< string > getChildrenValuesWithAttributes(XMLNode *node, const string &names, const string &name, const string &attrName, vector< string > &attrs, bool mandatory=false)
static vector< Real > getChildrenValuesAsDoublesCompact(XMLNode *node, const string &name, bool mandatory=false)
static string convertToString(const Real value)
static vector< string > getChildrenValues(XMLNode *node, const string &names, const string &name, bool mandatory=false)
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
static void appendNode(XMLNode *parent, XMLNode *child)
static vector< Period > getChildrenValuesAsPeriods(XMLNode *node, const string &name, bool mandatory=false)
SafeStack< Filter > filter
bool parseBool(const string &s)
Real parseReal(const string &s)
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
SensitivityScenarioData::ShiftData ShiftData
ShiftScheme parseShiftScheme(const std::string &s)
std::set< std::string > getShiftSpecKeys(const SensitivityScenarioData &d)
ShiftType parseShiftType(const std::string &s)
std::vector< std::string > getCorrelationTokens(const std::string &name)
std::string to_string(const LocationInfo &l)
A class to hold the parametrisation for building sensitivity scenarios.
map< string, ShiftScheme > keyedShiftScheme
map< string, ShiftType > keyedShiftType
map< string, Real > keyedShiftSize