Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/experimental/credit/randomdefaultmodel.hpp
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
/*! \file randomdefaultmodel.hpp
22
    \brief Random default-time scenarios for a pool of credit names
23
*/
24
25
#ifndef quantlib_random_default_model_hpp
26
#define quantlib_random_default_model_hpp
27
28
#include <ql/math/randomnumbers/rngtraits.hpp>
29
#include <ql/experimental/credit/pool.hpp>
30
#include <ql/experimental/credit/onefactorcopula.hpp>
31
#include <ql/experimental/credit/defaultprobabilitykey.hpp>
32
33
namespace QuantLib {
34
35
    //! Base class for random default models
36
    /*! Provides sequences of random default times for each name in the pool. */
37
    class RandomDefaultModel : public Observer, public Observable {
38
    public:
39
      RandomDefaultModel(const ext::shared_ptr<Pool>& pool,
40
                         const std::vector<DefaultProbKey>& defaultKeys)
41
0
      : pool_(pool), defaultKeys_(defaultKeys) {
42
          // assuming none defaulted this is true.
43
0
          QL_REQUIRE(defaultKeys.size() == pool->size(), "Incompatible pool and keys sizes.");
44
0
        }
45
0
        ~RandomDefaultModel() override = default;
46
0
        void update() override { notifyObservers(); }
47
        /*!
48
          Generate a sequence of random default times, one for each name in the
49
          pool, and store the result in the Pool using method setTime(name).
50
          tmax denotes the maximum relevant time- default times > tmax are not
51
          computed but set to tmax + 1 instead to save coputation time.
52
         */
53
        virtual void nextSequence(Real tmax = QL_MAX_REAL) = 0;
54
        virtual void reset() = 0;
55
    protected:
56
        ext::shared_ptr<Pool> pool_;
57
        std::vector<DefaultProbKey> defaultKeys_;
58
    };
59
60
    /*!
61
      Random default times using a one-factor Gaussian copula.
62
    */
63
    class GaussianRandomDefaultModel : public RandomDefaultModel {
64
    public:
65
      GaussianRandomDefaultModel(const ext::shared_ptr<Pool>& pool,
66
                                 const std::vector<DefaultProbKey>& defaultKeys,
67
                                 const Handle<OneFactorCopula>& copula,
68
                                 Real accuracy,
69
                                 long seed);
70
      void nextSequence(Real tmax = QL_MAX_REAL) override;
71
      void reset() override;
72
73
    private:
74
        Handle<OneFactorCopula> copula_;
75
        Real accuracy_;
76
        long seed_;
77
        PseudoRandom::rsg_type rsg_;
78
    };
79
80
}
81
82
#endif