/src/quantlib/ql/experimental/credit/randomdefaultmodel.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2008 Roland Lichters |
5 | | Copyright (C) 2009 Jose Aparicio |
6 | | |
7 | | This file is part of QuantLib, a free-software/open-source library |
8 | | for financial quantitative analysts and developers - http://quantlib.org/ |
9 | | |
10 | | QuantLib is free software: you can redistribute it and/or modify it |
11 | | under the terms of the QuantLib license. You should have received a |
12 | | copy of the license along with this program; if not, please email |
13 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
14 | | <http://quantlib.org/license.shtml>. |
15 | | |
16 | | This program is distributed in the hope that it will be useful, but WITHOUT |
17 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
18 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
19 | | */ |
20 | | |
21 | | #include <ql/experimental/credit/randomdefaultmodel.hpp> |
22 | | #include <ql/math/solvers1d/bisection.hpp> |
23 | | #include <ql/math/solvers1d/brent.hpp> |
24 | | #include <utility> |
25 | | |
26 | | using namespace std; |
27 | | |
28 | | namespace QuantLib { |
29 | | |
30 | | namespace { |
31 | | |
32 | | // Utility for the numerical solver |
33 | | class Root { |
34 | | public: |
35 | | Root(Handle<DefaultProbabilityTermStructure> dts, Real pd) |
36 | 0 | : dts_(std::move(dts)), pd_(pd) {} |
37 | 0 | Real operator()(Real t) const { |
38 | 0 | QL_REQUIRE(t >= 0.0, "GaussianRandomDefaultModel: internal error, t < 0 (" |
39 | 0 | << t << ") during root searching."); |
40 | 0 | return dts_->defaultProbability(t, true) - pd_; |
41 | 0 | } |
42 | | private: |
43 | | const Handle<DefaultProbabilityTermStructure> dts_; |
44 | | Real pd_; |
45 | | }; |
46 | | |
47 | | } |
48 | | |
49 | | GaussianRandomDefaultModel::GaussianRandomDefaultModel( |
50 | | const ext::shared_ptr<Pool>& pool, |
51 | | const std::vector<DefaultProbKey>& defaultKeys, |
52 | | const Handle<OneFactorCopula>& copula, |
53 | | Real accuracy, |
54 | | long seed) |
55 | 0 | : RandomDefaultModel(pool, defaultKeys), copula_(copula), accuracy_(accuracy), seed_(seed), |
56 | 0 | rsg_(PseudoRandom::make_sequence_generator(pool->size() + 1, seed)) { |
57 | 0 | registerWith(copula); |
58 | 0 | } |
59 | | |
60 | 0 | void GaussianRandomDefaultModel::reset() { |
61 | 0 | Size dim = pool_->size() + 1; |
62 | 0 | rsg_ = PseudoRandom::make_sequence_generator(dim, seed_); |
63 | 0 | } |
64 | | |
65 | 0 | void GaussianRandomDefaultModel::nextSequence(Real tmax) { |
66 | 0 | const std::vector<Real>& values = rsg_.nextSequence().value; |
67 | 0 | Real a = sqrt(copula_->correlation()); |
68 | 0 | for (Size j = 0; j < pool_->size(); j++) { |
69 | 0 | const string name = pool_->names()[j]; |
70 | 0 | const Handle<DefaultProbabilityTermStructure>& |
71 | 0 | dts = pool_->get(name).defaultProbability(defaultKeys_[j]); |
72 | |
|
73 | 0 | Real y = a * values[0] + sqrt(1-a*a) * values[j+1]; |
74 | 0 | Real p = CumulativeNormalDistribution()(y); |
75 | |
|
76 | 0 | if (dts->defaultProbability(tmax) < p) |
77 | 0 | pool_->setTime(name, tmax + 1); |
78 | 0 | else { |
79 | | // we know there is a zero of f(t) = dts->defaultProbability(t) - p in [0, tmax] |
80 | 0 | try { |
81 | | // try bracketing the root and find it with Brent |
82 | 0 | Brent brent; |
83 | 0 | brent.setLowerBound(0.0); |
84 | 0 | brent.setUpperBound(tmax); |
85 | 0 | pool_->setTime(name, brent.solve(Root(dts, p), accuracy_, tmax / 2.0, 1.0)); |
86 | 0 | } catch (...) { |
87 | | // if Brent fails, use Bisection, this is guaranteed to find the root |
88 | 0 | pool_->setTime( |
89 | 0 | name, Bisection().solve(Root(dts, p), accuracy_, tmax / 2.0, 0.0, tmax)); |
90 | 0 | } |
91 | 0 | } |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | } |
96 | | |