Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cdo.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
23#include <ql/math/interpolations/backwardflatinterpolation.hpp>
24#include <ql/math/interpolations/loginterpolation.hpp>
25#include <ql/termstructures/credit/flathazardrate.hpp>
31
32namespace ore {
33namespace data {
34using namespace std;
35using namespace QuantLib;
36using namespace QuantExt;
37
38//! Disable sensitivites for the all but the first default probability curve
39//! Shift all other curves parallel with the first curve, reduce the numbers of calculations
40std::vector<Handle<DefaultProbabilityTermStructure>> buildPerformanceOptimizedDefaultCurves(const std::vector<Handle<DefaultProbabilityTermStructure>>& curves) {
41 std::vector<Handle<DefaultProbabilityTermStructure>> dpts;
42 Handle<DefaultProbabilityTermStructure> clientCurve;
43 Handle<DefaultProbabilityTermStructure> baseCurve;
44 vector<Time> baseCurveTimes;
45
46 for (const auto& targetCurve : curves) {
47 //For all but the first curve use a spreaded curve, with the first curve as reference and
48 //the inital spread between the curve and the first curve, keep the spread constant
49 //in case of a spreaded curve we need to use the underlying reference curve to retrieve the
50 //correct pillar times, otherwise the interpolation grid is to coarse and we wouldnt match
51 //today's market prices
52 vector<Time> targetCurveTimes = ore::data::SyntheticCDO::extractTimeGridDefaultCurve(targetCurve);
53 // Sort times we need to interpolate an all pillars of the base and target curve
54 std::sort(targetCurveTimes.begin(), targetCurveTimes.end());
55 // Use the first curve as basis curve and update times
56 if (targetCurve == curves.front()) {
57 baseCurve = targetCurve;
58 clientCurve = baseCurve;
59 baseCurveTimes = targetCurveTimes;
60 } else {
61 std::vector<Time> times;
62 std::set_union(baseCurveTimes.begin(), baseCurveTimes.end(), targetCurveTimes.begin(),
63 targetCurveTimes.end(), std::back_inserter(times));
64 vector<Handle<Quote>> spreads(times.size());
65 std::transform(times.begin(), times.end(), spreads.begin(), [&baseCurve, &targetCurve](Time t) {
66 Probability spread =
67 targetCurve->survivalProbability(t, true) / baseCurve->survivalProbability(t, true);
68 if (close_enough(spread, 0.0)) {
69 spread = 1e-18;
70 }
71 return Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(spread));
72 });
73 clientCurve = Handle<DefaultProbabilityTermStructure>(
74 QuantLib::ext::make_shared<SpreadedSurvivalProbabilityTermStructure>(baseCurve, times, spreads));
75 if (baseCurve->allowsExtrapolation())
76 clientCurve->enableExtrapolation();
77 }
78 dpts.push_back(clientCurve);
79 }
80 return dpts;
81}
82
83QuantLib::ext::shared_ptr<PricingEngine> GaussCopulaBucketingCdoEngineBuilder::engineImpl(
84 const Currency& ccy, bool isIndexCDS, const vector<string>& creditCurves,
85 const QuantLib::ext::shared_ptr<SimpleQuote>& calibrationFactor,
86 const QuantLib::Real fixedRecovery) {
87 if (isIndexCDS) {
88 Handle<YieldTermStructure> yts = market_->discountCurve(ccy.code(), configuration(MarketContext::pricing));
89 std::vector<Handle<DefaultProbabilityTermStructure>> dpts;
90 std::vector<Real> recovery;
91 for (auto& c : creditCurves) {
92 Real recoveryRate = market_->recoveryRate(c, configuration(MarketContext::pricing))->value();
93 Handle<DefaultProbabilityTermStructure> targetCurve;
94
95 auto it = globalParameters_.find("RunType");
96 if (calibrateConstituentCurve() && it != globalParameters_.end() && it->second != "PortfolioAnalyser") {
97 auto orgCurve = market_->defaultCurve(c, configuration(MarketContext::pricing))->curve();
98 targetCurve =
99 SyntheticCDO::buildCalibratedConstiuentCurve(orgCurve, calibrationFactor);
100 } else {
101 targetCurve = market_->defaultCurve(c, configuration(MarketContext::pricing))->curve();
102 }
103 recovery.push_back(fixedRecovery != Null<Real>() ? fixedRecovery : recoveryRate);
104 dpts.push_back(targetCurve);
105 }
108 }
109 return QuantLib::ext::make_shared<QuantExt::MidPointIndexCdsEngine>(dpts, recovery, yts);
110 } else {
111 Handle<YieldTermStructure> yts = market_->discountCurve(ccy.code(), configuration(MarketContext::pricing));
112 return QuantLib::ext::make_shared<QuantExt::IndexCdsTrancheEngine>(yts);
113 }
114}
115} // namespace data
116} // namespace ore
Mid point CDO engines cached by currency.
bool optimizedSensitivityCalculation() const
Definition: cdo.hpp:85
bool calibrateConstituentCurve() const
Definition: cdo.hpp:76
QuantLib::ext::shared_ptr< Market > market_
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
std::map< std::string, std::string > globalParameters_
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const Currency &ccy, bool isIndexCDS, const vector< string > &creditCurves, const QuantLib::ext::shared_ptr< QuantLib::SimpleQuote > &calibrationFactor, const QuantLib::Real fixedRecovery=QuantLib::Null< QuantLib::Real >()) override
Definition: cdo.cpp:83
static vector< Time > extractTimeGridDefaultCurve(const QuantLib::Handle< QuantLib::DefaultProbabilityTermStructure > &dpts)
Definition: cdo.cpp:739
static QuantLib::Handle< QuantLib::DefaultProbabilityTermStructure > buildCalibratedConstiuentCurve(const QuantLib::Handle< QuantLib::DefaultProbabilityTermStructure > &curve, const QuantLib::ext::shared_ptr< SimpleQuote > &calibrationFactor)
Definition: cdo.cpp:755
@ data
Definition: log.hpp:77
std::vector< Handle< DefaultProbabilityTermStructure > > buildPerformanceOptimizedDefaultCurves(const std::vector< Handle< DefaultProbabilityTermStructure > > &curves)
Definition: cdo.cpp:40
Serializable Credit Default Swap.
Definition: namespaces.docs:23