Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
creditmigrationhelper.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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 aggregation/creditmigrationhelper.hpp
20 \brief Credit migration helper class
21 \ingroup engine
22*/
23
24#pragma once
25
27#include <orea/cube/npvcube.hpp>
29
33
35
36#include <ql/math/matrix.hpp>
37#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
38
39namespace ore {
40namespace analytics {
41
42/*! Helper for credit migration risk calculation
43 Dynamics of entity i's state X_i:
44 \f$ dX_i = dY_i + dZ_i \f$
45 with
46 - systemic part \f$ dY_i = \sum_{j=1}^n \beta_{ij} dG_j \f$
47 - n correlated global factors \f$ G_j \f$
48 - entity specific factor loadings \f$ \beta_{ij} \f$
49 - idiosyncratic part \f$ dZ_i = \sigma_i dW_i \f$
50 - independent Wiener processes W, i.e. \f$ dW_k dW_l = 0 \f$ and \f$ dW_k dG_j = 0 \f$ */
52public:
53 enum class CreditMode { Migration, Default };
56
57 CreditMigrationHelper(const QuantLib::ext::shared_ptr<CreditSimulationParameters> parameters,
58 const QuantLib::ext::shared_ptr<NPVCube> cube, const QuantLib::ext::shared_ptr<NPVCube> nettedCube,
59 const QuantLib::ext::shared_ptr<AggregationScenarioData> aggData, const Size cubeIndexCashflows,
60 const Size cubeIndexStateNpvs, const Real distributionLowerBound,
61 const Real distributionUpperBound, const Size buckets, const Matrix& globalFactorCorrelation,
62 const std::string& baseCurrency);
63
64 //! builds the helper for a specific subset of trades stored in the cube
65 void build(const std::map<std::string, QuantLib::ext::shared_ptr<Trade>>& trades);
66
67 const std::vector<Real>& upperBucketBound() const { return bucketing_.upperBucketBound(); }
68 //
69 Array pnlDistribution(const Size date);
70
71private:
72 /*! Get the transition matrix from today to date by entity,
73 sanitise the annual transition matrix input,
74 rescale to the desired horizon/date using the generator,
75 cache the result so that we do the sanitising/rescaling only once */
76 std::map<string, Matrix> rescaledTransitionMatrices(const Size date);
77
78 /*! Initialise
79 - the variance of the global part Y_i of entity state X_i, for all entities
80 - the global part Y_i of entity i's state X_i by date index, entity index and sample number
81 using the simulated global state paths stored in the aggregation scenario data object */
82 void init();
83
84 //! Allocate 3d storage for the simulated idiosyncratic factors by entity, date and sample
86
87 /*! Initialise the entity state simulationn for a given date for
88 Evaluation = TerminalSimulation:
89 Return transition matrix for each entity for the given date,
90 conditional on the global terminal state on the given path */
91 std::vector<Matrix> initEntityStateSimulation(const Size date, const Size path);
92
93 /*! Generate one entity state sample path for all entities given the global state path
94 and given the conditional transition matrices for all entities at the terminal date. */
95 void simulateEntityStates(const std::vector<Matrix>& cond, const Size path, const MersenneTwisterUniformRng& mt);
96
97 //! Look up the simulated entity credit state for the given entity, date and path
98 Size simulatedEntityState(const Size i, const Size path) const;
99
100 /*! Return a single PnL impact due to credit migration or default of Bond/CDS issuers and default of
101 netting set counterparties on the given global path */
102 Real generateMigrationPnl(const Size date, const Size path, const Size n) const;
103
104 /*! Return a vector of PnL impacts and associated conditional probabilities for the specified global path,
105 due to credit migration or default of Bond/CDS issuers and default of netting set counterparties */
106 void generateConditionalMigrationPnl(const Size date, const Size path, const std::map<string, Matrix>& transMat,
107 std::vector<Array>& condProbs, std::vector<Array>& pnl) const;
108
109 QuantLib::ext::shared_ptr<CreditSimulationParameters> parameters_;
110 QuantLib::ext::shared_ptr<NPVCube> cube_, nettedCube_;
111 QuantLib::ext::shared_ptr<AggregationScenarioData> aggData_;
114 std::string baseCurrency_;
115
119 std::vector<Real> cubeTimes_;
120
122
123 std::vector<std::set<std::string>> issuerTradeIds_;
124 std::vector<std::set<std::string>> cptyNettingSetIds_;
125
126 std::map<std::string, std::string> tradeCreditCurves_;
127 std::map<std::string, Real> tradeNotionals_;
128 std::map<std::string, std::string> tradeCurrencies_;
129 std::map<std::string, Size> tradeCdsCptyIdx_;
130
131 // Transition matrix rows
132 Size n_;
133 std::vector<std::map<string, Matrix>> rescaledTransitionMatrices_;
134 // Variance of the systemic part (Y_i) of entity state X_i
135 std::vector<Real> globalVar_;
136 // Storage for the simulated idiosyncratic factors Z by entity, sample number
137 std::vector<std::vector<Size>> simulatedEntityState_;
138
139 std::vector<std::vector<Matrix>> entityStateSimulationMatrices_;
140 // Systemic part (Y_i) of entity state X_i by date index, entity index, sample number
141 std::vector<std::vector<std::vector<Real>>> globalStates_;
142};
143
147
148} // namespace analytics
149} // namespace ore
this class holds data associated to scenarios
const std::vector< Real > & upperBucketBound() const
std::vector< std::set< std::string > > issuerTradeIds_
std::map< string, Matrix > rescaledTransitionMatrices(const Size date)
std::vector< std::vector< Matrix > > entityStateSimulationMatrices_
void initEntityStateSimulation()
Allocate 3d storage for the simulated idiosyncratic factors by entity, date and sample.
QuantLib::ext::shared_ptr< NPVCube > nettedCube_
QuantLib::ext::shared_ptr< CreditSimulationParameters > parameters_
void build(const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > &trades)
builds the helper for a specific subset of trades stored in the cube
Size simulatedEntityState(const Size i, const Size path) const
Look up the simulated entity credit state for the given entity, date and path.
std::vector< std::vector< std::vector< Real > > > globalStates_
std::vector< std::set< std::string > > cptyNettingSetIds_
void generateConditionalMigrationPnl(const Size date, const Size path, const std::map< string, Matrix > &transMat, std::vector< Array > &condProbs, std::vector< Array > &pnl) const
std::map< std::string, std::string > tradeCreditCurves_
QuantLib::ext::shared_ptr< AggregationScenarioData > aggData_
std::map< std::string, Size > tradeCdsCptyIdx_
std::map< std::string, Real > tradeNotionals_
std::map< std::string, std::string > tradeCurrencies_
std::vector< std::map< string, Matrix > > rescaledTransitionMatrices_
std::vector< std::vector< Size > > simulatedEntityState_
Real generateMigrationPnl(const Size date, const Size path, const Size n) const
QuantLib::ext::shared_ptr< NPVCube > cube_
void simulateEntityStates(const std::vector< Matrix > &cond, const Size path, const MersenneTwisterUniformRng &mt)
const std::vector< Real > & upperBucketBound() const
Credit simulation parameter class.
CreditMigrationHelper::LoanExposureMode parseLoanExposureMode(const std::string &s)
CreditMigrationHelper::Evaluation parseEvaluation(const std::string &s)
CreditMigrationHelper::CreditMode parseCreditMode(const std::string &s)
The base NPV cube class.