QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
basecorrelationlossmodel.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2014 Jose Aparicio
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#ifndef quantlib_base_correl_lossmodel_hpp
21#define quantlib_base_correl_lossmodel_hpp
22
23
24#include <ql/quote.hpp>
25#include <ql/quotes/simplequote.hpp>
26
27#include <ql/experimental/credit/basket.hpp>
28#include <ql/experimental/credit/defaultlossmodel.hpp>
29#include <ql/experimental/credit/basecorrelationstructure.hpp>
30
31// move these to the CPP (and the template spezs)
32#include <ql/experimental/credit/binomiallossmodel.hpp>
33#include <ql/experimental/credit/gaussianlhplossmodel.hpp>
34#include <ql/experimental/credit/inhomogeneouspooldef.hpp>
35#include <utility>
36
37namespace QuantLib {
38
79 /* Criticism:
80 This model is not as generic as it could be. In principle a default loss
81 model dependent on a single factor correlation parameter is the only
82 restriction on the base loss model(s) type. This class however is tied to a
83 LatentModel single factor. But there is no need for the
84 underlying model to be of a latent type. This link is due to the copula
85 initialization traits which have to be present for non trivial copula
86 policies initialization (e.g. Student-T base correl models)
87
88 Maybe a possibility is to pass copiable instances of the model and relinking
89 to the correlation in two internal copies.
90 */
91 template <class BaseModel_T, class Corr2DInt_T>
93 public virtual Observer {
94 private:
95 typedef typename BaseModel_T::copulaType::initTraits initTraits;
96 public:
98 std::vector<Real> recoveries,
99 const initTraits& traits = initTraits())
100 : localCorrelationAttach_(ext::make_shared<SimpleQuote>(0.)),
101 localCorrelationDetach_(ext::make_shared<SimpleQuote>(0.)),
102 recoveries_(std::move(recoveries)), correlTS_(correlTS), copulaTraits_(traits) {
103 registerWith(correlTS);
104 registerWith(Settings::instance().evaluationDate());
105 }
106
107 private:
108 // react to base correl surface notifications (quotes or reference date)
109 void update() override {
110 setupModels();
111 // tell basket to notify instruments, etc, we are invalid
112 if (!basket_.empty())
113 basket_->notifyObservers();
114 }
115
116 /* Update model caches after basket assignement. */
117 void resetModel() override {
118 remainingNotional_ = basket_->remainingNotional();
119 attachRatio_ = basket_->remainingAttachmentAmount() / remainingNotional_;
120 detachRatio_ = basket_->remainingDetachmentAmount() / remainingNotional_;
121
122 basketAttach_ = ext::make_shared<Basket>(basket_->refDate(), basket_->remainingNames(),
123 basket_->remainingNotionals(), basket_->pool(),
124 0.0, attachRatio_, basket_->claim());
125 basketDetach_ = ext::make_shared<Basket>(basket_->refDate(), basket_->remainingNames(),
126 basket_->remainingNotionals(), basket_->pool(),
127 0.0, detachRatio_, basket_->claim());
128 setupModels();
129 }
130 /* Most of the statistics are not implemented, not impossible but
131 the model is intended for pricing rather than ptfolio risk management.
132 */
133 Real expectedTrancheLoss(const Date& d) const override;
134
135 protected:
139 void setupModels() const;
140 private:
143
145 // trigger calculation.
146 ext::shared_ptr<SimpleQuote> localCorrelationAttach_,
148 mutable ext::shared_ptr<Basket> basketAttach_,
150 // just cached for the update method
151 mutable std::vector<Real> recoveries_;
153 // Initialization parameters for models copula
154 mutable typename BaseModel_T::copulaType::initTraits copulaTraits_;
155 // Models of equity baskets.
156 mutable ext::shared_ptr<BaseModel_T> scalarCorrelModelAttach_;
157 mutable ext::shared_ptr<BaseModel_T> scalarCorrelModelDetach_;
158 };
159
160
161 // Remember ETL returns the EL on the live part of the basket.
162 template<class LM, class I>
164 const Date& d) const
165 {
166 Real correlK1 = correlTS_->correlation(d, attachRatio_);
167 Real correlK2 = correlTS_->correlation(d, detachRatio_);
168
169 /* reset correl and call base models which have the different baskets
170 associated.*/
171 localCorrelationAttach_->setValue(correlK1);
172 Real expLossK1 =
173 basketAttach_->expectedTrancheLoss(d);
174 localCorrelationDetach_->setValue(correlK2);
175 Real expLossK2 =
176 basketDetach_->expectedTrancheLoss(d);
177 return expLossK2 - expLossK1;
178 }
179
180
181 // ----------------------------------------------------------------------
182
183
184 /* Concrete specializations submodels construction. With the dummy template
185 parameter trick partial specializations leaving the interpolation open
186 would be possible.
187 */
188
189 #ifndef QL_PATCH_SOLARIS
190
191 template<>
193 BilinearInterpolation>::setupModels() const
194 {
195 // on this assignment any previous registration with the attach and
196 // detach baskets should be removed
197 scalarCorrelModelAttach_ = ext::make_shared<GaussianLHPLossModel>(
198 Handle<Quote>(localCorrelationAttach_), recoveries_);
199 scalarCorrelModelDetach_ = ext::make_shared<GaussianLHPLossModel>(
200 Handle<Quote>(localCorrelationDetach_), recoveries_);
201
202 basketAttach_->setLossModel(scalarCorrelModelAttach_);
203 basketDetach_->setLossModel(scalarCorrelModelDetach_);
204 }
205
206 template<>
208 BilinearInterpolation>::setupModels() const
209 {
210 ext::shared_ptr<GaussianConstantLossLM> lmA =
211 ext::make_shared<GaussianConstantLossLM>(
212 Handle<Quote>(localCorrelationAttach_), recoveries_,
214 recoveries_.size(), copulaTraits_);
215 ext::shared_ptr<GaussianConstantLossLM> lmD =
216 ext::make_shared<GaussianConstantLossLM>(
217 Handle<Quote>(localCorrelationDetach_), recoveries_,
219 recoveries_.size(), copulaTraits_);
220 scalarCorrelModelAttach_ =
221 ext::make_shared<GaussianBinomialLossModel>(lmA);
222 scalarCorrelModelDetach_ =
223 ext::make_shared<GaussianBinomialLossModel>(lmD);
224
225 basketAttach_->setLossModel(scalarCorrelModelAttach_);
226 basketDetach_->setLossModel(scalarCorrelModelDetach_);
227
228 }
229
230 template<>
232 BilinearInterpolation>::setupModels() const
233 {
234 ext::shared_ptr<TConstantLossLM> lmA =
235 ext::make_shared<TConstantLossLM>(
236 Handle<Quote>(localCorrelationAttach_), recoveries_,
238 recoveries_.size(), copulaTraits_);
239 ext::shared_ptr<TConstantLossLM> lmD =
240 ext::make_shared<TConstantLossLM>(
241 Handle<Quote>(localCorrelationDetach_), recoveries_,
243 recoveries_.size(), copulaTraits_);
244
245 scalarCorrelModelAttach_ =
246 ext::make_shared<TBinomialLossModel>(lmA);
247 scalarCorrelModelDetach_ =
248 ext::make_shared<TBinomialLossModel>(lmD);
249
250 basketAttach_->setLossModel(scalarCorrelModelAttach_);
251 basketDetach_->setLossModel(scalarCorrelModelDetach_);
252 }
253
254 /* \todo Fix this model, is failing for equity tranches at least, the
255 base model works all right, its the link here.
256 */
257 template<>
259 BilinearInterpolation>::setupModels() const
260 {
261 ext::shared_ptr<GaussianConstantLossLM> lmA =
262 ext::make_shared<GaussianConstantLossLM>(
263 Handle<Quote>(localCorrelationAttach_), recoveries_,
265 recoveries_.size(), copulaTraits_);
266 ext::shared_ptr<GaussianConstantLossLM> lmD =
267 ext::make_shared<GaussianConstantLossLM>(
268 Handle<Quote>(localCorrelationDetach_), recoveries_,
270 recoveries_.size(), copulaTraits_);
271
272 // \todo Allow the sending specific model params, as the number of
273 // buckets here.
274 scalarCorrelModelAttach_ =
275 ext::make_shared<IHGaussPoolLossModel>(lmA, 500);
276 scalarCorrelModelDetach_ =
277 ext::make_shared<IHGaussPoolLossModel>(lmD, 500);
278
279 basketAttach_->setLossModel(scalarCorrelModelAttach_);
280 basketDetach_->setLossModel(scalarCorrelModelDetach_);
281 }
282
283 #endif
284
285
286 // Vanilla BC model
287 #ifndef QL_PATCH_SOLARIS
290 #endif
291
292}
293
294#endif
BaseModel_T::copulaType::initTraits initTraits
Handle< BaseCorrelationTermStructure< Corr2DInt_T > > correlTS_
ext::shared_ptr< BaseModel_T > scalarCorrelModelAttach_
BaseCorrelationLossModel(const Handle< BaseCorrelationTermStructure< Corr2DInt_T > > &correlTS, std::vector< Real > recoveries, const initTraits &traits=initTraits())
ext::shared_ptr< SimpleQuote > localCorrelationAttach_
Correlation buffer to pick up values from the surface and.
ext::shared_ptr< SimpleQuote > localCorrelationDetach_
void resetModel() override
Concrete models do now any updates/inits they need on basket reset.
Real expectedTrancheLoss(const Date &d) const override
BaseModel_T::copulaType::initTraits copulaTraits_
ext::shared_ptr< BaseModel_T > scalarCorrelModelDetach_
bilinear interpolation between discrete points
Concrete date class.
Definition: date.hpp:125
RelinkableHandle< Basket > basket_
Shared handle to an observable.
Definition: handle.hpp:41
Object that gets notified when a given observable changes.
Definition: observable.hpp:116
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
market element returning a stored value
Definition: simplequote.hpp:33
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
InhomogeneousPoolLossModel< GaussianCopulaPolicy > IHGaussPoolLossModel
BinomialLossModel< GaussianConstantLossLM > GaussianBinomialLossModel
BinomialLossModel< TConstantLossLM > TBinomialLossModel
BaseCorrelationLossModel< GaussianLHPLossModel, BilinearInterpolation > GaussianLHPFlatBCLM
STL namespace.