Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
creditmigrationcalculator.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 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
21
22namespace ore {
23namespace analytics {
24
26 const QuantLib::ext::shared_ptr<Portfolio>& portfolio,
27 const QuantLib::ext::shared_ptr<CreditSimulationParameters>& creditSimulationParameters,
28 const QuantLib::ext::shared_ptr<NPVCube>& cube, const QuantLib::ext::shared_ptr<CubeInterpretation> cubeInterpretation,
29 const QuantLib::ext::shared_ptr<NPVCube>& nettedCube,
30 const QuantLib::ext::shared_ptr<AggregationScenarioData>& aggregationScenarioData,
31 const std::vector<Real>& creditMigrationDistributionGrid, const std::vector<Size>& creditMigrationTimeSteps,
32 const Matrix& creditStateCorrelationMatrix, const std::string baseCurrency)
33 : portfolio_(portfolio), creditSimulationParameters_(creditSimulationParameters), cube_(cube),
34 cubeInterpretation_(cubeInterpretation), nettedCube_(nettedCube),
35 aggregationScenarioData_(aggregationScenarioData),
36 creditMigrationDistributionGrid_(creditMigrationDistributionGrid),
37 creditMigrationTimeSteps_(creditMigrationTimeSteps), creditStateCorrelationMatrix_(creditStateCorrelationMatrix),
38 baseCurrency_(baseCurrency) {}
39
41
42 LOG("Credit migration computation started.");
43
44 // checks
45
46 QL_REQUIRE(portfolio_ != nullptr, "CreditMigrationCalculator::build(): portfolio is null");
47 QL_REQUIRE(creditSimulationParameters_ != nullptr,
48 "CreditMigrationCalculator::build(): creditSimulationParameters is null");
49 QL_REQUIRE(cube_ != nullptr, "CreditMigrationCalculator::build(): cube is null");
50 QL_REQUIRE(cubeInterpretation_ != nullptr, "CreditMigrationCalculator::build(): cube interpretation is null");
51 QL_REQUIRE(nettedCube_ != nullptr, "CreditMigrationCalculator::build(): netted cube is null");
52 QL_REQUIRE(aggregationScenarioData_ != nullptr,
53 "CreditMigrationCalculator::build(): aggregation scenario data is null");
54 QL_REQUIRE(!baseCurrency_.empty(), "CreditMigrationCalculator::build(): base currency is empty");
56 "CreditMigrationCalculator::build(): credit state correlation matrix is not square ("
57 << creditStateCorrelationMatrix_.rows() << " x " << creditStateCorrelationMatrix_.columns() << ")");
58 QL_REQUIRE(
60 "CreditMigrationCalculator::build(): credit migration distribution grid spec must consist of 3 numbers (got "
61 << creditMigrationDistributionGrid_.size() << ")");
62
63 // create helper
64
66 cubeInterpretation_->mporFlowsIndex(), cubeInterpretation_->creditStateNPVsIndex(),
70
71 hlp.build(portfolio_->trades());
72
73 // compute output
74
76 if (!upperBucketBounds_.empty())
77 upperBucketBounds_.pop_back();
78 cdf_.clear();
79 pdf_.clear();
80
81 for (Size i = 0; i < creditMigrationTimeSteps_.size(); ++i) {
82 DLOG("Generating pnl distribution for timestep " << creditMigrationTimeSteps_[i]);
83 cdf_.push_back({});
84 pdf_.push_back({});
85 Array dist = hlp.pnlDistribution(creditMigrationTimeSteps_[i]);
86 Real mean = 0.0, stdev = 0.0;
87 Real sum = 0.0;
88 for (Size j = 1; j < hlp.upperBucketBound().size() - 1; ++j) {
89 Real pnl = 0.5 * (hlp.upperBucketBound()[j] + hlp.upperBucketBound()[j - 1]);
90 mean += pnl * dist[j];
91 stdev += pnl * pnl * dist[j];
92 sum += dist[j];
93 }
94 stdev = sqrt(stdev - mean * mean);
95 TLOG("Total PnL at time step " << creditMigrationTimeSteps_[i] << ": Mean " << std::fixed << mean << " StdDev "
96 << stdev << " Prob " << std::scientific << sum << " Left " << dist[0]
97 << " Right " << dist[hlp.upperBucketBound().size() - 1]);
98 Real p = 0.0;
99 for (Size j = 0; j < hlp.upperBucketBound().size() - 1; ++j) {
100 p += dist[j];
101 cdf_.back().push_back(p);
102 pdf_.back().push_back(dist[j]);
103 }
104 }
105
106 LOG("Credit migration computation finished.");
107}
108
109} // namespace analytics
110} // namespace ore
CreditMigrationCalculator(const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const QuantLib::ext::shared_ptr< CreditSimulationParameters > &creditSimulationParameters, const QuantLib::ext::shared_ptr< NPVCube > &cube, const QuantLib::ext::shared_ptr< CubeInterpretation > cubeInterpretation, const QuantLib::ext::shared_ptr< NPVCube > &nettedcube, const QuantLib::ext::shared_ptr< AggregationScenarioData > &aggregationScenarioData, const std::vector< Real > &creditMigrationDistributionGrid, const std::vector< Size > &creditMigrationTimeSteps, const Matrix &creditStateCorrelationMatrix, const std::string baseCurrency)
QuantLib::ext::shared_ptr< NPVCube > nettedCube_
QuantLib::ext::shared_ptr< AggregationScenarioData > aggregationScenarioData_
QuantLib::ext::shared_ptr< Portfolio > portfolio_
QuantLib::ext::shared_ptr< CubeInterpretation > cubeInterpretation_
QuantLib::ext::shared_ptr< CreditSimulationParameters > creditSimulationParameters_
QuantLib::ext::shared_ptr< NPVCube > cube_
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
const std::vector< Real > & upperBucketBound() const
Exposure calculator.
Credit migration helper class.
#define LOG(text)
#define DLOG(text)
#define TLOG(text)
RandomVariable sqrt(RandomVariable x)
Real sum(const Cash &c, const Cash &d)
Size size(const ValueType &v)