Coverage Report

Created: 2025-08-28 06:30

/src/quantlib/ql/termstructures/defaulttermstructure.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) 2008 Chris Kenyon
6
 Copyright (C) 2008 StatPro Italia srl
7
 Copyright (C) 2009 Ferdinando Ametrano
8
9
 This file is part of QuantLib, a free-software/open-source library
10
 for financial quantitative analysts and developers - http://quantlib.org/
11
12
 QuantLib is free software: you can redistribute it and/or modify it
13
 under the terms of the QuantLib license.  You should have received a
14
 copy of the license along with this program; if not, please email
15
 <quantlib-dev@lists.sf.net>. The license is also available online at
16
 <http://quantlib.org/license.shtml>.
17
18
 This program is distributed in the hope that it will be useful, but WITHOUT
19
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20
 FOR A PARTICULAR PURPOSE.  See the license for more details.
21
*/
22
23
#include <ql/termstructures/defaulttermstructure.hpp>
24
#include <ql/utilities/dataformatters.hpp>
25
#include <utility>
26
27
namespace QuantLib {
28
29
    DefaultProbabilityTermStructure::DefaultProbabilityTermStructure(
30
        const DayCounter& dc, std::vector<Handle<Quote> > jumps, const std::vector<Date>& jumpDates)
31
0
    : TermStructure(dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
32
0
      jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
33
0
        setJumps();
34
0
        for (Size i=0; i<nJumps_; ++i)
35
0
            registerWith(jumps_[i]);
36
0
    }
37
38
    DefaultProbabilityTermStructure::DefaultProbabilityTermStructure(
39
        const Date& referenceDate,
40
        const Calendar& cal,
41
        const DayCounter& dc,
42
        std::vector<Handle<Quote> > jumps,
43
        const std::vector<Date>& jumpDates)
44
0
    : TermStructure(referenceDate, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
45
0
      jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
46
0
        setJumps();
47
0
        for (Size i=0; i<nJumps_; ++i)
48
0
            registerWith(jumps_[i]);
49
0
    }
50
51
    DefaultProbabilityTermStructure::DefaultProbabilityTermStructure(
52
        Natural settlementDays,
53
        const Calendar& cal,
54
        const DayCounter& dc,
55
        std::vector<Handle<Quote> > jumps,
56
        const std::vector<Date>& jumpDates)
57
0
    : TermStructure(settlementDays, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
58
0
      jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
59
0
        setJumps();
60
0
        for (Size i=0; i<nJumps_; ++i)
61
0
            registerWith(jumps_[i]);
62
0
    }
63
64
0
    void DefaultProbabilityTermStructure::setJumps() {
65
0
        if (jumpDates_.empty() && !jumps_.empty()) { // turn of year dates
66
0
            jumpDates_.resize(nJumps_);
67
0
            jumpTimes_.resize(nJumps_);
68
0
            Year y = referenceDate().year();
69
0
            for (Size i=0; i<nJumps_; ++i)
70
0
                jumpDates_[i] = Date(31, December, y+i);
71
0
        } else { // fixed dats
72
0
            QL_REQUIRE(jumpDates_.size()==nJumps_,
73
0
                       "mismatch between number of jumps (" << nJumps_ <<
74
0
                       ") and jump dates (" << jumpDates_.size() << ")");
75
0
        }
76
0
        for (Size i=0; i<nJumps_; ++i)
77
0
            jumpTimes_[i] = timeFromReference(jumpDates_[i]);
78
0
        latestReference_ = referenceDate();
79
0
    }
80
81
    Probability DefaultProbabilityTermStructure::survivalProbability(
82
                                                     Time t,
83
0
                                                     bool extrapolate) const {
84
0
        checkRange(t, extrapolate);
85
86
0
        if (!jumps_.empty()) {
87
0
            Probability jumpEffect = 1.0;
88
0
            for (Size i=0; i<nJumps_ && jumpTimes_[i]<t; ++i) {
89
0
                QL_REQUIRE(jumps_[i]->isValid(),
90
0
                           "invalid " << io::ordinal(i+1) << " jump quote");
91
0
                DiscountFactor thisJump = jumps_[i]->value();
92
0
                QL_REQUIRE(thisJump > 0.0 && thisJump <= 1.0,
93
0
                           "invalid " << io::ordinal(i+1) << " jump value: " <<
94
0
                           thisJump);
95
0
                jumpEffect *= thisJump;
96
0
            }
97
0
            return jumpEffect * survivalProbabilityImpl(t);
98
0
        }
99
100
0
        return survivalProbabilityImpl(t);
101
0
    }
102
103
    Probability DefaultProbabilityTermStructure::defaultProbability(
104
                                                     const Date& d1,
105
                                                     const Date& d2,
106
0
                                                     bool extrapolate) const {
107
0
        QL_REQUIRE(d1 <= d2,
108
0
                   "initial date (" << d1 << ") "
109
0
                   "later than final date (" << d2 << ")");
110
0
        Probability p1 = d1 < referenceDate() ? 0.0 :
111
0
                                           defaultProbability(d1,extrapolate),
112
0
                    p2 = defaultProbability(d2,extrapolate);
113
0
        return p2 - p1;
114
0
    }
115
116
    Probability DefaultProbabilityTermStructure::defaultProbability(
117
                                                     Time t1,
118
                                                     Time t2,
119
0
                                                     bool extrapolate) const {
120
0
        QL_REQUIRE(t1 <= t2,
121
0
                   "initial time (" << t1 << ") "
122
0
                   "later than final time (" << t2 << ")");
123
0
        Probability p1 = t1 < 0.0 ? 0.0 : defaultProbability(t1,extrapolate),
124
0
                    p2 = defaultProbability(t2,extrapolate);
125
0
        return p2 - p1;
126
0
    }
127
128
}