File: BasketLosses.cpp

package info (click to toggle)
quantlib 1.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,480 kB
  • sloc: cpp: 400,885; makefile: 6,547; python: 214; sh: 150; lisp: 86
file content (274 lines) | stat: -rw-r--r-- 11,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2014 Jose Aparicio

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include <ql/qldefines.hpp>
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#  include <ql/auto_link.hpp>
#endif
#include <ql/experimental/credit/gaussianlhplossmodel.hpp>
#include <ql/experimental/credit/constantlosslatentmodel.hpp>
#include <ql/experimental/credit/binomiallossmodel.hpp>
#include <ql/experimental/credit/randomdefaultlatentmodel.hpp>
#include <ql/experimental/credit/randomlosslatentmodel.hpp>
#include <ql/experimental/credit/spotlosslatentmodel.hpp>
#include <ql/experimental/credit/basecorrelationlossmodel.hpp>
#include <ql/termstructures/credit/flathazardrate.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/currencies/europe.hpp>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
using namespace QuantLib;

int main(int, char* []) {

    try {

        std::cout << std::endl;

        Calendar calendar = TARGET();
        Date todaysDate(19, March, 2014);
        // must be a business day
        todaysDate = calendar.adjust(todaysDate);

        Settings::instance().evaluationDate() = todaysDate;


        /* --------------------------------------------------------------
                        SET UP BASKET PORTFOLIO
        -------------------------------------------------------------- */
        // build curves and issuers into a basket of ten names
        std::vector<Real> hazardRates = {
            0.001, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09
        };
        std::vector<std::string> names;
        names.reserve(hazardRates.size());
        for(Size i=0; i<hazardRates.size(); i++)
            names.push_back(std::string("Acme") + std::to_string(i));
        std::vector<Handle<DefaultProbabilityTermStructure>> defTS;
        for (Real& hazardRate : hazardRates) {
            defTS.emplace_back(
                ext::make_shared<FlatHazardRate>(0, TARGET(), hazardRate, Actual365Fixed()));
            defTS.back()->enableExtrapolation();
        }
        std::vector<Issuer> issuers;
        for(Size i=0; i<hazardRates.size(); i++) {
            std::vector<QuantLib::Issuer::key_curve_pair> curves(1, 
                std::make_pair(NorthAmericaCorpDefaultKey(
                    EURCurrency(), QuantLib::SeniorSec,
                    Period(), 1. // amount threshold
                    ), defTS[i]));
            issuers.emplace_back(curves);
        }

        auto thePool = ext::make_shared<Pool>();
        for(Size i=0; i<hazardRates.size(); i++)
            thePool->add(names[i], issuers[i], NorthAmericaCorpDefaultKey(
                    EURCurrency(), QuantLib::SeniorSec, Period(), 1.));

        std::vector<DefaultProbKey> defaultKeys(hazardRates.size(), 
            NorthAmericaCorpDefaultKey(EURCurrency(), SeniorSec, Period(), 1.));
        auto theBskt = ext::make_shared<Basket>(
            todaysDate, 
            names, std::vector<Real>(hazardRates.size(), 100.), thePool,
         //   0.0, 0.78);
            0.03, .06);

        /* --------------------------------------------------------------
                        SET UP DEFAULT LOSS MODELS
        -------------------------------------------------------------- */

        std::vector<Real> recoveries(hazardRates.size(), 0.4);

        Date calcDate(TARGET().advance(Settings::instance().evaluationDate(),
            Period(60, Months)));
        Real factorValue = 0.05;
        std::vector<std::vector<Real>> fctrsWeights(hazardRates.size(),
            std::vector<Real>(1, std::sqrt(factorValue)));

        // --- LHP model --------------------------
        #ifndef QL_PATCH_SOLARIS
        auto lmGLHP = ext::make_shared<GaussianLHPLossModel>(
                fctrsWeights[0][0] * fctrsWeights[0][0], recoveries);
        theBskt->setLossModel(lmGLHP);

        std::cout << "GLHP Expected 10-Yr Losses: "  << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;

        // --- G Binomial model --------------------
        auto ktLossLM = ext::make_shared<GaussianConstantLossLM>(fctrsWeights,
            recoveries, LatentModelIntegrationType::GaussianQuadrature,
            GaussianCopulaPolicy::initTraits());
        auto lmBinomial = ext::make_shared<GaussianBinomialLossModel>(ktLossLM);
        theBskt->setLossModel(lmBinomial);

        std::cout << "Gaussian Binomial Expected 10-Yr Losses: "  << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;

        #endif

        // --- T Binomial model --------------------
        TCopulaPolicy::initTraits initT;
        initT.tOrders = std::vector<Integer>(2, 3);
        auto ktTLossLM = ext::make_shared<TConstantLossLM>(fctrsWeights,
            recoveries, 
            //LatentModelIntegrationType::GaussianQuadrature,
              LatentModelIntegrationType::Trapezoid,
            initT);
        auto lmTBinomial = ext::make_shared<TBinomialLossModel>(ktTLossLM);
        theBskt->setLossModel(lmTBinomial);

        std::cout << "T Binomial Expected 10-Yr Losses: "  << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;

        // --- G Inhomogeneous model ---------------
        Size numSimulations = 100000;
        #ifndef QL_PATCH_SOLARIS
        auto gLM = ext::make_shared<GaussianConstantLossLM>(fctrsWeights,
            recoveries,
            LatentModelIntegrationType::GaussianQuadrature,
            // g++ requires this when using make_shared
            GaussianCopulaPolicy::initTraits());

        Size numBuckets = 100;
        auto inhomogeneousLM = ext::make_shared<IHGaussPoolLossModel>(gLM, numBuckets);
        theBskt->setLossModel(inhomogeneousLM);

        std::cout << "G Inhomogeneous Expected 10-Yr Losses: "  << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;

        // --- G Random model ---------------------
        // Gaussian random joint default model:
        // Size numCoresUsed = 4;
        // Sobol, many cores
        auto rdlmG = ext::make_shared<RandomDefaultLM<GaussianCopulaPolicy,
            RandomSequenceGenerator<
                BoxMullerGaussianRng<MersenneTwisterUniformRng>>>>(gLM,
                    recoveries, numSimulations, 1.e-6, 2863311530UL);
        //auto rdlmG = ext::make_shared<RandomDefaultLM<GaussianCopulaPolicy>>(gLM,
        //        recoveries, numSimulations, 1.e-6, 2863311530);
        theBskt->setLossModel(rdlmG);

        std::cout << "Random G Expected 10-Yr Losses: "  << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
        #endif

        // --- StudentT Random model ---------------------
        // Sobol, many cores
        auto rdlmT = ext::make_shared<RandomDefaultLM<TCopulaPolicy,
            RandomSequenceGenerator<
                PolarStudentTRng<MersenneTwisterUniformRng>>>>(ktTLossLM,
                    recoveries, numSimulations, 1.e-6, 2863311530UL);
        //auto rdlmT = ext::make_shared<RandomDefaultLM<TCopulaPolicy>>(ktTLossLM,
        //        recoveries, numSimulations, 1.e-6, 2863311530);
        theBskt->setLossModel(rdlmT);

        std::cout << "Random T Expected 10-Yr Losses: "  << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;


        // Spot Loss latent model: 
        #ifndef QL_PATCH_SOLARIS
        std::vector<std::vector<Real>> fctrsWeightsRR(2 * hazardRates.size(),
            std::vector<Real>(1, std::sqrt(factorValue)));
        Real modelA = 2.2;
        auto sptLG = ext::make_shared<GaussianSpotLossLM>(
            fctrsWeightsRR, recoveries, modelA,
            LatentModelIntegrationType::GaussianQuadrature,
            GaussianCopulaPolicy::initTraits());
        auto sptLT = ext::make_shared<TSpotLossLM>(fctrsWeightsRR,
            recoveries, modelA,
            LatentModelIntegrationType::GaussianQuadrature, initT);


        // --- G Random Loss model ---------------------
        // Gaussian random joint default model:
        // Sobol, many cores
        auto rdLlmG = ext::make_shared<RandomLossLM<GaussianCopulaPolicy>>(sptLG,
                numSimulations, 1.e-6, 2863311530UL);
        theBskt->setLossModel(rdLlmG);

        std::cout << "Random Loss G Expected 10-Yr Losses: "  << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;

        // --- T Random Loss model ---------------------
        // Gaussian random joint default model:
        // Sobol, many cores
        auto rdLlmT = ext::make_shared<RandomLossLM<TCopulaPolicy>>(sptLT,
                numSimulations, 1.e-6, 2863311530UL);
        theBskt->setLossModel(rdLlmT);

        std::cout << "Random Loss T Expected 10-Yr Losses: "  << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;

        // Base Correlation model set up to test coherence with base LHP model
        std::vector<Period> bcTenors = {{1, Years}, {5, Years}};
        std::vector<Real> bcLossPercentages = {0.03, 0.12};
        std::vector<std::vector<Handle<Quote>>> correls;
        // 
        std::vector<Handle<Quote>> corr1Y;
        // 3%
        corr1Y.emplace_back(
            ext::make_shared<SimpleQuote>(fctrsWeights[0][0] * fctrsWeights[0][0]));
        // 12%
        corr1Y.emplace_back(
            ext::make_shared<SimpleQuote>(fctrsWeights[0][0] * fctrsWeights[0][0]));
        correls.push_back(corr1Y);
        std::vector<Handle<Quote>> corr2Y;
        // 3%
        corr2Y.emplace_back(
            ext::make_shared<SimpleQuote>(fctrsWeights[0][0] * fctrsWeights[0][0]));
        // 12%
        corr2Y.emplace_back(
            ext::make_shared<SimpleQuote>(fctrsWeights[0][0] * fctrsWeights[0][0]));
        correls.push_back(corr2Y);
        auto correlSurface = ext::make_shared<BaseCorrelationTermStructure<BilinearInterpolation>>(
                // first one would do, all should be the same.
                defTS[0]->settlementDays(),
                defTS[0]->calendar(),
                Unadjusted,
                bcTenors,
                bcLossPercentages,
                correls,
                Actual365Fixed());
        Handle<BaseCorrelationTermStructure<BilinearInterpolation>> correlHandle(correlSurface);
        auto bcLMG_LHP_Bilin = ext::make_shared<GaussianLHPFlatBCLM>(correlHandle, recoveries,
                GaussianCopulaPolicy::initTraits());

        theBskt->setLossModel(bcLMG_LHP_Bilin);

        std::cout << "Base Correlation GLHP Expected 10-Yr Losses: "  
            << std::endl;
        std::cout << theBskt->expectedTrancheLoss(calcDate) << std::endl;
        #endif


        return 0;
    } catch (exception& e) {
        cerr << e.what() << endl;
        return 1;
    } catch (...) {
        cerr << "unknown error" << endl;
        return 1;
    }
}