Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Attributes | List of all members
ShiftScenarioGenerator Class Reference

Shift Scenario Generator. More...

#include <orea/scenario/shiftscenariogenerator.hpp>

+ Inheritance diagram for ShiftScenarioGenerator:
+ Collaboration diagram for ShiftScenarioGenerator:

Classes

class  ScenarioDescription
 

Public Member Functions

 ShiftScenarioGenerator (const QuantLib::ext::shared_ptr< Scenario > &baseScenario, const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > &simMarketData, const QuantLib::ext::weak_ptr< ScenarioSimMarket > &simMarket)
 Constructor. More...
 
 ~ShiftScenarioGenerator ()
 Default destructor. More...
 
QuantLib::ext::shared_ptr< Scenarionext (const Date &d) override
 Scenario Generator interface. More...
 
void reset () override
 Reset the generator so calls to next() return the first scenario. More...
 
Size samples ()
 Inspectors. More...
 
const QuantLib::ext::shared_ptr< Scenario > & baseScenario ()
 Return the base scenario, i.e. cached initial values of all relevant market points. More...
 
const std::vector< QuantLib::ext::shared_ptr< Scenario > > & scenarios ()
 Return vector of sensitivity scenarios, scenario 0 is the base scenario. More...
 
std::vector< ScenarioDescriptionscenarioDescriptions ()
 Return vector of scenario descriptions. More...
 
const std::map< RiskFactorKey, std::string > & keyToFactor ()
 
const std::map< std::string, RiskFactorKey > & factorToKey ()
 Return revers map of factors to RiskFactorKeys. More...
 
void applyShift (Size j, Real shiftSize, bool up, ShiftType type, const vector< Time > &shiftTimes, const vector< Real > &values, const vector< Time > &times, vector< Real > &shiftedValues, bool initialise)
 Apply 1d triangular shift to 1d data such as yield curves, public to allow test suite access. More...
 
void applyShift (Size j, Size k, Real shiftSize, bool up, ShiftType type, const vector< Time > &shiftX, const vector< Time > &shiftY, const vector< Time > &dataX, const vector< Time > &dataY, const vector< vector< Real > > &data, vector< vector< Real > > &shiftedData, bool initialise)
 Apply 2d shift to 2d matrix such as swaption volatilities, public to allow test suite access. More...
 
QuantLib::ext::shared_ptr< ScenariobaseScenario () const
 return the base scenario More...
 
- Public Member Functions inherited from ScenarioGenerator
virtual ~ScenarioGenerator ()
 Default destructor. More...
 
virtual QuantLib::ext::shared_ptr< Scenarionext (const Date &d)=0
 Return the next scenario for the given date. More...
 
virtual void reset ()=0
 Reset the generator so calls to next() return the first scenario. More...
 

Protected Attributes

const QuantLib::ext::shared_ptr< ScenariobaseScenario_
 
const QuantLib::ext::shared_ptr< ScenarioSimMarketParameterssimMarketData_
 
const QuantLib::ext::weak_ptr< ScenarioSimMarketsimMarket_
 
std::vector< QuantLib::ext::shared_ptr< Scenario > > scenarios_
 
Size counter_
 
std::vector< ScenarioDescriptionscenarioDescriptions_
 
std::map< RiskFactorKey, std::string > keyToFactor_
 
std::map< std::string, RiskFactorKeyfactorToKey_
 

Detailed Description

Shift Scenario Generator.

Base class for sensitivity and stress scenario generators

Definition at line 43 of file shiftscenariogenerator.hpp.

Constructor & Destructor Documentation

◆ ShiftScenarioGenerator()

ShiftScenarioGenerator ( const QuantLib::ext::shared_ptr< Scenario > &  baseScenario,
const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > &  simMarketData,
const QuantLib::ext::weak_ptr< ScenarioSimMarket > &  simMarket 
)

Constructor.

Definition at line 130 of file shiftscenariogenerator.cpp.

133 : baseScenario_(baseScenario), simMarketData_(simMarketData), simMarket_(simMarket), counter_(0) {
134 QL_REQUIRE(baseScenario_ != NULL, "ShiftScenarioGenerator: baseScenario is null");
135 QL_REQUIRE(simMarketData_ != NULL, "ShiftScenarioGenerator: simMarketData is null");
136 scenarios_.push_back(baseScenario_);
137 scenarioDescriptions_.push_back(ScenarioDescription(ScenarioDescription::Type::Base));
138}
std::vector< ScenarioDescription > scenarioDescriptions_
const QuantLib::ext::shared_ptr< Scenario > & baseScenario()
Return the base scenario, i.e. cached initial values of all relevant market points.
std::vector< QuantLib::ext::shared_ptr< Scenario > > scenarios_
const QuantLib::ext::shared_ptr< Scenario > baseScenario_
const QuantLib::ext::weak_ptr< ScenarioSimMarket > simMarket_
const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > simMarketData_

◆ ~ShiftScenarioGenerator()

Default destructor.

Definition at line 93 of file shiftscenariogenerator.hpp.

93{};

Member Function Documentation

◆ next()

QuantLib::ext::shared_ptr< Scenario > next ( const Date &  d)
overridevirtual

Scenario Generator interface.

Implements ScenarioGenerator.

Definition at line 140 of file shiftscenariogenerator.cpp.

140 {
141 QL_REQUIRE(counter_ < scenarios_.size(), "scenario vector size " << scenarios_.size() << " exceeded");
142 return scenarios_[counter_++];
143}

◆ reset()

void reset ( )
overridevirtual

Reset the generator so calls to next() return the first scenario.

This allows re-generation of scenarios if required.

Implements ScenarioGenerator.

Definition at line 98 of file shiftscenariogenerator.hpp.

98{ counter_ = 0; }

◆ samples()

Size samples ( )

Inspectors.

Number of shift scenarios

Definition at line 104 of file shiftscenariogenerator.hpp.

104{ return scenarios_.size(); }

◆ baseScenario() [1/2]

const QuantLib::ext::shared_ptr< Scenario > & baseScenario ( )

Return the base scenario, i.e. cached initial values of all relevant market points.

Definition at line 106 of file shiftscenariogenerator.hpp.

106{ return scenarios_.front(); }

◆ scenarios()

const std::vector< QuantLib::ext::shared_ptr< Scenario > > & scenarios ( )

Return vector of sensitivity scenarios, scenario 0 is the base scenario.

Definition at line 108 of file shiftscenariogenerator.hpp.

108{ return scenarios_; }

◆ scenarioDescriptions()

std::vector< ScenarioDescription > scenarioDescriptions ( )

Return vector of scenario descriptions.

Definition at line 110 of file shiftscenariogenerator.hpp.

110{ return scenarioDescriptions_; }

◆ keyToFactor()

const std::map< RiskFactorKey, std::string > & keyToFactor ( )

Definition at line 112 of file shiftscenariogenerator.hpp.

112{ return keyToFactor_; }
std::map< RiskFactorKey, std::string > keyToFactor_

◆ factorToKey()

const std::map< std::string, RiskFactorKey > & factorToKey ( )

Return revers map of factors to RiskFactorKeys.

Definition at line 114 of file shiftscenariogenerator.hpp.

114{ return factorToKey_; }
std::map< std::string, RiskFactorKey > factorToKey_

◆ applyShift() [1/2]

void applyShift ( Size  j,
Real  shiftSize,
bool  up,
ShiftType  type,
const vector< Time > &  shiftTimes,
const vector< Real > &  values,
const vector< Time > &  times,
vector< Real > &  shiftedValues,
bool  initialise 
)

Apply 1d triangular shift to 1d data such as yield curves, public to allow test suite access.

Apply triangular shaped shifts to the underlying curve where the triangle reaches from the previous to the next shift tenor point with peak at the current shift tenor point. At the initial and final shift tenor the shape is replaced such that the full shift is applied to all curve grid points to the left of the first shift point and to the right of the last shift point, respectively. The procedure guarantees that no sensitivity to original curve points is "missed" when the shift curve is less granular, e.g. original curve |...|...|...|...|...|...|...|...|...| shift curve ......|...........|...........|......

Parameters
jNumber of the shift curve tenor point to be shifted here
shiftSizeShift size interpreted as either absolute or relative shift
upUpwards shift if true, otherwise downwards
typeAbsolute: newValue = oldValue + shiftSize. Relative: newValue = oldValue * (1 + shiftSize)
shiftTimesShift tenors expressed as times
valuesInput curve values such as zero rates
timesTenor points of the input curve, expressed as times
shiftedValuesResulting shifted curve with same tenor structure as the input curve
initialiseInitialise shiftedValues vector before applying this shift j (yes for sensitivity, no for stress)
+ Here is the caller graph for this function:

◆ applyShift() [2/2]

void applyShift ( Size  j,
Size  k,
Real  shiftSize,
bool  up,
ShiftType  type,
const vector< Time > &  shiftX,
const vector< Time > &  shiftY,
const vector< Time > &  dataX,
const vector< Time > &  dataY,
const vector< vector< Real > > &  data,
vector< vector< Real > > &  shiftedData,
bool  initialise 
)

Apply 2d shift to 2d matrix such as swaption volatilities, public to allow test suite access.

This is the 2d generalisation of the 1d version of applyShift()

Parameters
jIndex of the shift tenor in "expiry" direction
kIndex of the shift tenor in "term" (Swaptions) or "strike" (Caps) direction
shiftSizeShift size interpreted as either absolute or relative shift
upUpwards shift if true, otherwise downwards
typeAbsolute: newValue = oldValue + shiftSize. Relative: newValue = oldValue * (1 + shiftSize)
shiftXCoordinate time in "expiry" direction of the shift curve
shiftYCoordinate time in "term" or "strike" direction of the shift curve
dataXCoordinate time in "expiry" direction of the underlying data
dataYCoordinate time in "term" or "strike" direction of the underlying data
dataMatrix of input data
shiftedDataMatrix of shifted result data
initialiseInitialise shiftedData vector before applying this shift j/k (yes for sensitivity, no for stress)

Definition at line 277 of file shiftscenariogenerator.cpp.

281 {
282 QL_REQUIRE(shiftX.size() >= 1 && shiftY.size() >= 1, "shift vector size >= 1 required");
283 QL_REQUIRE(i < shiftX.size(), "index i out of range");
284 QL_REQUIRE(j < shiftY.size(), "index j out of range");
285
286 // initialise the shifted data
287 if (initialise) {
288 for (Size k = 0; k < dataX.size(); ++k) {
289 for (Size l = 0; l < dataY.size(); ++l)
290 shiftedData[k][l] = data[k][l];
291 }
292 }
293
294 // single shift point means parallel shift
295 if (shiftX.size() == 1 && shiftY.size() == 1) {
296 Real w = up ? 1.0 : -1.0;
297 for (Size k = 0; k < dataX.size(); ++k) {
298 for (Size l = 0; l < dataY.size(); ++l) {
299 if (shiftType == ShiftType::Absolute)
300 shiftedData[k][l] += w * shiftSize;
301 else
302 shiftedData[k][l] *= (1.0 + w * shiftSize);
303 }
304 }
305 return;
306 }
307
308 Size iMax = shiftX.size() - 1;
309 Size jMax = shiftY.size() - 1;
310 Real tx = shiftX[i];
311 Real ty = shiftY[j];
312 Real tx1 = i > 0 ? shiftX[i - 1] : QL_MAX_REAL;
313 Real ty1 = j > 0 ? shiftY[j - 1] : QL_MAX_REAL;
314 Real tx2 = i < iMax ? shiftX[i + 1] : -QL_MAX_REAL;
315 Real ty2 = j < jMax ? shiftY[j + 1] : -QL_MAX_REAL;
316
317 for (Size ix = 0; ix < dataX.size(); ++ix) {
318 Real x = dataX[ix];
319 for (Size iy = 0; iy < dataY.size(); ++iy) {
320 Real y = dataY[iy];
321 Real wx = 0.0;
322 Real wy = 0.0;
323 if (x >= tx && x <= tx2 && y >= ty && y <= ty2) {
324 wx = (tx2 - x) / (tx2 - tx);
325 wy = (ty2 - y) / (ty2 - ty);
326 } else if (x >= tx && x <= tx2 && y >= ty1 && y <= ty) {
327 wx = (tx2 - x) / (tx2 - tx);
328 wy = (y - ty1) / (ty - ty1);
329 } else if (x >= tx1 && x <= tx && y >= ty1 && y <= ty) {
330 wx = (x - tx1) / (tx - tx1);
331 wy = (y - ty1) / (ty - ty1);
332 } else if (x >= tx1 && x <= tx && y >= ty && y <= ty2) {
333 wx = (x - tx1) / (tx - tx1);
334 wy = (ty2 - y) / (ty2 - ty);
335 } else if ((x <= tx && i == 0 && y < ty && j == 0) || (x <= tx && i == 0 && y >= ty && j == jMax) ||
336 (x >= tx && i == iMax && y >= ty && j == jMax) || (x >= tx && i == iMax && y < ty && j == 0)) {
337 wx = 1.0;
338 wy = 1.0;
339 } else if (((x <= tx && i == 0) || (x >= tx && i == iMax)) && y >= ty1 && y <= ty) {
340 wx = 1.0;
341 wy = (y - ty1) / (ty - ty1);
342 } else if (((x <= tx && i == 0) || (x >= tx && i == iMax)) && y >= ty && y <= ty2) {
343 wx = 1.0;
344 wy = (ty2 - y) / (ty2 - ty);
345 } else if (x >= tx1 && x <= tx && ((y < ty && j == 0) || (y >= ty && j == jMax))) {
346 wx = (x - tx1) / (tx - tx1);
347 wy = 1.0;
348 } else if (x >= tx && x <= tx2 && ((y < ty && j == 0) || (y >= ty && j == jMax))) {
349 wx = (tx2 - x) / (tx2 - tx);
350 wy = 1.0;
351 }
352 QL_REQUIRE(wx >= 0.0 && wx <= 1.0, "wx out of range");
353 QL_REQUIRE(wy >= 0.0 && wy <= 1.0, "wy out of range");
354
355 Real w = up ? 1.0 : -1.0;
356 if (shiftType == ShiftType::Absolute)
357 shiftedData[ix][iy] += w * wx * wy * shiftSize;
358 else
359 shiftedData[ix][iy] *= (1.0 + w * wx * wy * shiftSize);
360 }
361 }
362}

◆ baseScenario() [2/2]

QuantLib::ext::shared_ptr< Scenario > baseScenario ( ) const

return the base scenario

Definition at line 180 of file shiftscenariogenerator.hpp.

180{ return scenarios_.front(); }

Member Data Documentation

◆ baseScenario_

const QuantLib::ext::shared_ptr<Scenario> baseScenario_
protected

Definition at line 183 of file shiftscenariogenerator.hpp.

◆ simMarketData_

const QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> simMarketData_
protected

Definition at line 184 of file shiftscenariogenerator.hpp.

◆ simMarket_

const QuantLib::ext::weak_ptr<ScenarioSimMarket> simMarket_
protected

Definition at line 185 of file shiftscenariogenerator.hpp.

◆ scenarios_

std::vector<QuantLib::ext::shared_ptr<Scenario> > scenarios_
protected

Definition at line 186 of file shiftscenariogenerator.hpp.

◆ counter_

Size counter_
protected

Definition at line 187 of file shiftscenariogenerator.hpp.

◆ scenarioDescriptions_

std::vector<ScenarioDescription> scenarioDescriptions_
protected

Definition at line 188 of file shiftscenariogenerator.hpp.

◆ keyToFactor_

std::map<RiskFactorKey, std::string> keyToFactor_
protected

Definition at line 190 of file shiftscenariogenerator.hpp.

◆ factorToKey_

std::map<std::string, RiskFactorKey> factorToKey_
protected

Definition at line 192 of file shiftscenariogenerator.hpp.