Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
zerotoparshift.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2024 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
19/*! \file orea/engine/zerotoparshift.cpp
20 \brief applies a zero scenario and return the par instrument shifts
21 \ingroup simulation
22*/
23
29
30namespace ore {
31namespace analytics {
32
34 const QuantLib::Date& asof,
35 const QuantLib::ext::shared_ptr<ore::analytics::ScenarioSimMarketParameters>& simMarketParams,
36 const ore::analytics::SensitivityScenarioData& sensitivityData,
37 const std::set<ore::analytics::RiskFactorKey::KeyType>& typesDisabled,
38 const std::set<ore::analytics::RiskFactorKey::KeyType>& parTypes,
39 const std::set<ore::analytics::RiskFactorKey>& relevantRiskFactors, const bool continueOnError,
40 const std::string& marketConfiguration, const QuantLib::ext::shared_ptr<ScenarioSimMarket>& simMarket)
41 : simMarket_(simMarket) {
42 ParSensitivityInstrumentBuilder().createParInstruments(instruments_, asof, simMarketParams, sensitivityData,
43 typesDisabled, parTypes, relevantRiskFactors,
44 continueOnError, marketConfiguration, simMarket);
45};
46
48 const QuantLib::ext::shared_ptr<ScenarioSimMarket>& simMarket)
49 : instruments_(instruments_), simMarket_(simMarket) {
50 QL_REQUIRE(simMarket_ != nullptr, "ZeroToParShiftConverter: need a simmarket");
51 simMarket_->reset();
52
53 if (ObservationMode::instance().mode() == ObservationMode::Mode::Disable) {
54 for (auto it : instruments_.parHelpers_)
55 it.second->deepUpdate();
56 for (auto it : instruments_.parCaps_)
57 it.second->deepUpdate();
58 for (auto it : instruments_.parYoYCaps_)
59 it.second->deepUpdate();
60 }
62};
63
64class SimMarketReseter {
65public:
66 SimMarketReseter(const ext::shared_ptr<ScenarioSimMarket>& simMarket) : simMarket_(simMarket) {
67 simMarket_->reset();
68 }
69 ~SimMarketReseter() { simMarket_->reset(); }
70 const ext::shared_ptr<ScenarioSimMarket>& market() const { return simMarket_; }
71
72private:
73 ext::shared_ptr<ScenarioSimMarket> simMarket_;
74};
75
76std::unordered_map<RiskFactorKey, double>
77ZeroToParShiftConverter::parShifts(QuantLib::ext::shared_ptr<Scenario> scenario) const {
78 QL_REQUIRE(simMarket_ != nullptr, "ZeroToParShiftConverter: need a simmarket");
79 SimMarketReseter market(simMarket_);
80
81 market.market()->applyScenario(scenario);
82
83 if (ObservationMode::instance().mode() == ObservationMode::Mode::Disable) {
84 for (auto it : instruments_.parHelpers_)
85 it.second->deepUpdate();
86 for (auto it : instruments_.parCaps_)
87 it.second->deepUpdate();
88 for (auto it : instruments_.parYoYCaps_)
89 it.second->deepUpdate();
90 }
91
92 auto scenarioValues = parRates();
93 // Check if both maps have same keys;
94 QL_REQUIRE(baseValues_.size() == scenarioValues.size(),
95 "ZeroToParShiftConverter: internal error, both maps should have the same entries");
96 bool sameKeys = std::equal(baseValues_.begin(), baseValues_.end(), scenarioValues.begin(),
97 [](const auto& a, const auto& b) { return a.first == b.first; });
98 QL_REQUIRE(sameKeys, "ZeroToParShiftConverter: internal error, both maps should have the same entries");
99 std::unordered_map<RiskFactorKey, double> shifts;
100 for (const auto& [key, amount] : baseValues_) {
101 shifts[key] = scenarioValues[key] - amount;
102 }
103 return shifts;
104}
105
106std::unordered_map<RiskFactorKey, double> ZeroToParShiftConverter::parRates() const {
107 std::unordered_map<RiskFactorKey, double> results;
108 for (const auto& [key, parInstrument] : instruments_.parHelpers_) {
109 results[key] = impliedQuote(parInstrument);
110 }
111 for (const auto& [key, cap] : instruments_.parCaps_) {
112 results[key] = impliedVolatility(key, instruments_);
113 }
114 for (const auto& [key, cap] : instruments_.parYoYCaps_) {
115 results[key] = impliedVolatility(key, instruments_);
116 }
117 return results;
118}
119
120} // namespace analytics
121} // namespace ore
void createParInstruments(ParSensitivityInstrumentBuilder::Instruments &instruments, const QuantLib::Date &asof, const QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > &simMarketParams, const ore::analytics::SensitivityScenarioData &sensitivityData, const std::set< ore::analytics::RiskFactorKey::KeyType > &typesDisabled={}, const std::set< ore::analytics::RiskFactorKey::KeyType > &parTypes={}, const std::set< ore::analytics::RiskFactorKey > &relevantRiskFactors={}, const bool continueOnError=false, const std::string &marketConfiguration=ore::data::Market::defaultConfiguration, const QuantLib::ext::shared_ptr< ore::analytics::Market > &simMarket=nullptr) const
Create par QuantLib::Instruments.
Description of sensitivity shift scenarios.
ZeroToParShiftConverter(const QuantLib::Date &asof, const QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > &simMarketParams, const ore::analytics::SensitivityScenarioData &sensitivityData, const std::set< ore::analytics::RiskFactorKey::KeyType > &typesDisabled, const std::set< ore::analytics::RiskFactorKey::KeyType > &parTypes, const std::set< ore::analytics::RiskFactorKey > &relevantRiskFactors, const bool continueOnError, const std::string &marketConfiguration, const QuantLib::ext::shared_ptr< ScenarioSimMarket > &simMarket)
std::unordered_map< RiskFactorKey, double > parRates() const
ext::shared_ptr< ScenarioSimMarket > simMarket_
std::unordered_map< RiskFactorKey, double > parShifts(QuantLib::ext::shared_ptr< Scenario > scenario) const
ParSensitivityInstrumentBuilder::Instruments instruments_
std::unordered_map< RiskFactorKey, double > baseValues_
Volatility impliedVolatility(const QuantLib::CapFloor &cap, Real targetValue, const Handle< YieldTermStructure > &d, Volatility guess, VolatilityType type, Real displacement)
Real impliedQuote(const QuantLib::ext::shared_ptr< Instrument > &i)
Singleton class to hold global Observation Mode.
std::map< ore::analytics::RiskFactorKey, QuantLib::ext::shared_ptr< QuantLib::Instrument > > parHelpers_
par helpers (all except cap/floors)
std::map< ore::analytics::RiskFactorKey, QuantLib::ext::shared_ptr< QuantLib::CapFloor > > parCaps_
par helpers: IR cap / floors
std::map< ore::analytics::RiskFactorKey, QuantLib::ext::shared_ptr< QuantLib::YoYInflationCapFloor > > parYoYCaps_
Date asof(14, Jun, 2018)
applies a zero scenario and return the par instrument shifts