QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
randomdefaultmodel.cpp
Go to the documentation of this file.
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
24#include <utility>
25
26using namespace std;
27
28namespace QuantLib {
29
30 namespace {
31
32 // Utility for the numerical solver
33 class Root {
34 public:
35 Root(Handle<DefaultProbabilityTermStructure> dts, Real pd)
36 : dts_(std::move(dts)), pd_(pd) {}
37 Real operator()(Real t) const {
38 QL_REQUIRE(t >= 0.0, "GaussianRandomDefaultModel: internal error, t < 0 ("
39 << t << ") during root searching.");
40 return dts_->defaultProbability(t, true) - pd_;
41 }
42 private:
43 const Handle<DefaultProbabilityTermStructure> dts_;
45 };
46
47 }
48
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 : RandomDefaultModel(pool, defaultKeys), copula_(copula), accuracy_(accuracy), seed_(seed),
56 rsg_(PseudoRandom::make_sequence_generator(pool->size() + 1, seed)) {
57 registerWith(copula);
58 }
59
61 Size dim = pool_->size() + 1;
63 }
64
66 const std::vector<Real>& values = rsg_.nextSequence().value;
67 Real a = sqrt(copula_->correlation());
68 for (Size j = 0; j < pool_->size(); j++) {
69 const string name = pool_->names()[j];
71 dts = pool_->get(name).defaultProbability(defaultKeys_[j]);
72
73 Real y = a * values[0] + sqrt(1-a*a) * values[j+1];
75
76 if (dts->defaultProbability(tmax) < p)
77 pool_->setTime(name, tmax + 1);
78 else {
79 // we know there is a zero of f(t) = dts->defaultProbability(t) - p in [0, tmax]
80 try {
81 // try bracketing the root and find it with Brent
82 Brent brent;
83 brent.setLowerBound(0.0);
84 brent.setUpperBound(tmax);
85 pool_->setTime(name, brent.solve(Root(dts, p), accuracy_, tmax / 2.0, 1.0));
86 } catch (...) {
87 // if Brent fails, use Bisection, this is guaranteed to find the root
88 pool_->setTime(
89 name, Bisection().solve(Root(dts, p), accuracy_, tmax / 2.0, 0.0, tmax));
90 }
91 }
92 }
93 }
94
95}
96
bisection 1-D solver
Brent 1-D solver.
Bisection 1-D solver
Definition: bisection.hpp:37
Brent 1-D solver
Definition: brent.hpp:37
Cumulative normal distribution function.
GaussianRandomDefaultModel(const ext::shared_ptr< Pool > &pool, const std::vector< DefaultProbKey > &defaultKeys, const Handle< OneFactorCopula > &copula, Real accuracy, long seed)
void nextSequence(Real tmax=QL_MAX_REAL) override
Shared handle to an observable.
Definition: handle.hpp:41
const sample_type & nextSequence() const
returns next sample from the inverse cumulative distribution
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Base class for random default models.
ext::shared_ptr< Pool > pool_
std::vector< DefaultProbKey > defaultKeys_
void setLowerBound(Real lowerBound)
sets the lower bound for the function domain
Definition: solver1d.hpp:243
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
void setUpperBound(Real upperBound)
sets the upper bound for the function domain
Definition: solver1d.hpp:249
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.
Real pd_
const Handle< DefaultProbabilityTermStructure > dts_
Random default-time scenarios for a pool of credit names.
static rsg_type make_sequence_generator(Size dimension, BigNatural seed)
Definition: rngtraits.hpp:52