Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenarioshiftcalculator.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
20
24#include <ql/time/daycounters/actualactual.hpp>
25#include <ql/time/daycounters/actual365fixed.hpp>
26#include <ql/time/daycounters/actual360.hpp>
27
35using namespace QuantLib;
36using std::log;
37
38namespace ore {
39namespace analytics {
40
43
44ScenarioShiftCalculator::ScenarioShiftCalculator(const QuantLib::ext::shared_ptr<SensitivityScenarioData>& sensitivityConfig,
45 const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters>& simMarketConfig,
46 const QuantLib::ext::shared_ptr<ore::analytics::ScenarioSimMarket>& simMarket)
47 : sensitivityConfig_(sensitivityConfig), simMarketConfig_(simMarketConfig), simMarket_(simMarket) {}
48
49Real ScenarioShiftCalculator::shift(const RiskFactorKey& key, const Scenario& s_1, const Scenario& s_2) const {
50
51 // Get the respective (transformed) scenario values
52 Real v_1 = transform(key, s_1.get(key), s_1.asof());
53 Real v_2 = transform(key, s_2.get(key), s_2.asof());
54
55 // If for any reason v_1 or v_2 are not finite or nan, log an alert and return 0
56 if (!std::isfinite(v_1) || std::isnan(v_1)) {
57 ALOG("The scenario value v_1 for key '" << key << "' is " << v_1 << " and is not usable so we are returning 0");
58 return 0.0;
59 }
60 if (!std::isfinite(v_2) || std::isnan(v_2)) {
61 ALOG("The scenario value v_2 for key '" << key << "' is " << v_2 << " and is not usable so we are returning 0");
62 return 0.0;
63 }
64
65 // Get the shift size and type from the sensitivity configuration
66 const ShiftData& shiftData = sensitivityConfig_->shiftData(key.keytype, key.name);
67 Real shiftSize = shiftData.shiftSize;
68 ShiftType shiftType = shiftData.shiftType;
69
70 // If shiftSize is zero, log an alert and return 0 early
71 if (close(shiftSize, 0.0)) {
72 ALOG("The shift size for key '" << key << "' in sensitivity config is zero");
73 return 0.0;
74 }
75
76 // Get the multiple of the sensitivity shift size in moving from scenario 1 to 2
77 Real result = 0.0;
78 if (shiftType == ShiftType::Absolute) {
79 result = v_2 - v_1;
80 } else {
81 if (close(v_1, 0.0)) {
82 ALOG("The reference scenario value for key '"
83 << key << "' is zero and the shift is relative so must return a shift of zero");
84 } else {
85 result = v_2 / v_1 - 1.0;
86 }
87 }
88 result /= shiftSize;
89
90 return result;
91}
92
93Real ScenarioShiftCalculator::transform(const RiskFactorKey& key, Real value, const Date& asof) const {
94
95 Period p;
96 DayCounter dc = Actual365Fixed();
97
98 switch (key.keytype) {
99 case RFType::DiscountCurve:
100 case RFType::YieldCurve:
101 case RFType::IndexCurve:
102 p = simMarketConfig_->yieldCurveTenors(key.name).at(key.index);
103 if (simMarket_)
104 dc = simMarket_->iborIndex(key.name)->forwardingTermStructure()->dayCounter();
105 break;
106 case RFType::DividendYield:
107 p = simMarketConfig_->equityDividendTenors(key.name).at(key.index);
108 if (simMarket_)
109 dc = simMarket_->equityDividendCurve(key.name)->dayCounter();
110 break;
111 case RFType::SurvivalProbability:
112 p = simMarketConfig_->defaultTenors(key.name).at(key.index);
113 if (simMarket_)
114 dc = simMarket_->defaultCurve(key.name)->curve()->dayCounter();
115 break;
116 default:
117 // If we get to here, return untransformed value
118 return value;
119 break;
120 }
121
122 // If we get to here, calculate transformed value
123 Time t = dc.yearFraction(asof, asof + p);
124
125 // The way that this is used above should be ok i.e. will always be 0 - 0 when t = 0
126 if (close(t, 0.0)) {
127 ALOG("The time needed in the denominator of the transform for key '"
128 << key << "' is zero so we return a transformed value of zero");
129 return 0.0;
130 }
131
132 return -log(value) / t;
133}
134
135} // namespace analytics
136} // namespace ore
Data types stored in the scenario class.
Definition: scenario.hpp:48
KeyType keytype
Key type.
Definition: scenario.hpp:89
std::string name
Key name.
Definition: scenario.hpp:94
KeyType
Risk Factor types.
Definition: scenario.hpp:51
Scenario Base Class.
Definition: scenario.hpp:138
virtual const Date & asof() const =0
Return the scenario asof date.
virtual Real get(const RiskFactorKey &key) const =0
Get an element from the scenario.
QuantLib::Real shift(const ore::analytics::RiskFactorKey &key, const ore::analytics::Scenario &s_1, const ore::analytics::Scenario &s_2) const
ScenarioShiftCalculator(const QuantLib::ext::shared_ptr< ore::analytics::SensitivityScenarioData > &sensitivityConfig, const QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > &simMarketConfig, const QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarket > &simMarket=QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarket >())
QuantLib::Real transform(const ore::analytics::RiskFactorKey &key, QuantLib::Real value, const QuantLib::Date &asof) const
QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > simMarketConfig_
QuantLib::ext::shared_ptr< ore::analytics::SensitivityScenarioData > sensitivityConfig_
QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarket > simMarket_
Description of sensitivity shift scenarios.
SafeStack< ValueType > value
DayCounter parseDayCounter(const string &s)
#define ALOG(text)
RandomVariable log(RandomVariable x)
bool close(const Real &t_1, const Real &t_2)
ShiftType parseShiftType(const std::string &s)
Definition: scenario.cpp:216
Class for calculating the shift multiple between two scenarios for a given key.
Shift scenario generation.
Date asof(14, Jun, 2018)