• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 26158000839

20 May 2026 10:55AM UTC coverage: 74.637% (-0.001%) from 74.638%
26158000839

Pull #2593

github

web-flow
Merge 64ef5a544 into 8d6014303
Pull Request #2593: Fix yield term structure time error message

58969 of 79008 relevant lines covered (74.64%)

8598945.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.75
/ql/termstructures/yieldtermstructure.cpp
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2

3
/*
4
 Copyright (C) 2004, 2009 Ferdinando Ametrano
5
 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6
 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
7

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

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

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

22
#include <ql/termstructures/yieldtermstructure.hpp>
23
#include <ql/utilities/dataformatters.hpp>
24
#include <utility>
25

26
namespace QuantLib {
27

28
    namespace {
29
        // time interval used in finite differences
30
        const Time dt = 0.0001;
31
    }
32

33
    YieldTermStructure::YieldTermStructure(const DayCounter& dc) : TermStructure(dc) {}
127,218✔
34

35
    YieldTermStructure::YieldTermStructure(const Date& referenceDate,
524,323✔
36
                                           const Calendar& cal,
37
                                           const DayCounter& dc,
38
                                           std::vector<Handle<Quote>> jumps,
39
                                           const std::vector<Date>& jumpDates)
×
40
    : TermStructure(referenceDate, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
524,323✔
41
      jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
1,572,969✔
42
        setJumps(YieldTermStructure::referenceDate());
524,323✔
43
        for (Size i = 0; i < nJumps_; ++i)
524,323✔
44
            registerWith(jumps_[i]);
×
45
    }
524,323✔
46

47
    YieldTermStructure::YieldTermStructure(Natural settlementDays,
3,306✔
48
                                           const Calendar& cal,
49
                                           const DayCounter& dc,
50
                                           std::vector<Handle<Quote>> jumps,
51
                                           const std::vector<Date>& jumpDates)
×
52
    : TermStructure(settlementDays, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
3,306✔
53
      jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
9,918✔
54
        setJumps(YieldTermStructure::referenceDate());
3,306✔
55
        for (Size i = 0; i < nJumps_; ++i)
3,306✔
56
            registerWith(jumps_[i]);
×
57
    }
3,306✔
58

59
    void YieldTermStructure::setJumps(const Date& referenceDate) {
599,814✔
60
        if (jumpDates_.empty() && !jumps_.empty()) { // turn of year dates
599,814✔
61
            jumpDates_.resize(nJumps_);
×
62
            jumpTimes_.resize(nJumps_);
×
63
            Year y = referenceDate.year();
×
64
            for (Size i = 0; i < nJumps_; ++i)
×
65
                jumpDates_[i] = Date(31, December, y + i);
×
66
        } else { // fixed dates
67
            QL_REQUIRE(jumpDates_.size() == nJumps_, "mismatch between number of jumps ("
599,814✔
68
                                                         << nJumps_ << ") and jump dates ("
69
                                                         << jumpDates_.size() << ")");
70
        }
71
        for (Size i = 0; i < nJumps_; ++i)
599,814✔
72
            jumpTimes_[i] = timeFromReference(jumpDates_[i]);
×
73
        latestReference_ = referenceDate;
599,814✔
74
    }
599,814✔
75

76
    DiscountFactor YieldTermStructure::discount(Time t, bool extrapolate) const {
1,489,675,941✔
77
        checkRange(t, extrapolate);
1,489,675,941✔
78

79
        if (jumps_.empty())
1,489,675,936✔
80
            return discountImpl(t);
1,489,675,936✔
81

82
        DiscountFactor jumpEffect = 1.0;
83
        for (Size i = 0; i < nJumps_; ++i) {
×
84
            if (jumpTimes_[i] > 0 && jumpTimes_[i] < t) {
×
85
                QL_REQUIRE(jumps_[i]->isValid(), "invalid " << io::ordinal(i + 1) << " jump quote");
×
86
                DiscountFactor thisJump = jumps_[i]->value();
×
87
                QL_REQUIRE(thisJump > 0.0,
×
88
                           "invalid " << io::ordinal(i + 1) << " jump value: " << thisJump);
89
                jumpEffect *= thisJump;
×
90
            }
91
        }
92
        return jumpEffect * discountImpl(t);
×
93
    }
94

95
    InterestRate YieldTermStructure::zeroRate(const Date& d,
409,573✔
96
                                              const DayCounter& dayCounter,
97
                                              Compounding comp,
98
                                              Frequency freq,
99
                                              bool extrapolate) const {
100
        Time t = timeFromReference(d);
409,573✔
101
        if (t == 0) {
409,573✔
102
            Real compound = 1.0 / discount(dt, extrapolate);
1,102✔
103
            // t has been calculated with a possibly different daycounter
104
            // but the difference should not matter for very small times
105
            return InterestRate::impliedRate(compound, dayCounter, comp, freq, dt);
1,102✔
106
        }
107
        Real compound = 1.0 / discount(t, extrapolate);
408,471✔
108
        return InterestRate::impliedRate(compound, dayCounter, comp, freq, referenceDate(), d);
408,471✔
109
    }
110

111
    InterestRate
112
    YieldTermStructure::zeroRate(Time t, Compounding comp, Frequency freq, bool extrapolate) const {
897,572✔
113
        if (t == 0.0)
897,572✔
114
            t = dt;
115
        Real compound = 1.0 / discount(t, extrapolate);
897,572✔
116
        return InterestRate::impliedRate(compound, dayCounter(), comp, freq, t);
1,795,144✔
117
    }
118

119
    InterestRate YieldTermStructure::forwardRate(const Date& d1,
751,526✔
120
                                                 const Date& d2,
121
                                                 const DayCounter& dayCounter,
122
                                                 Compounding comp,
123
                                                 Frequency freq,
124
                                                 bool extrapolate) const {
125
        if (d1 == d2) {
751,526✔
126
            checkRange(d1, extrapolate);
492✔
127
            Time t1 = std::max(timeFromReference(d1) - dt / 2.0, 0.0);
492✔
128
            Time t2 = t1 + dt;
492✔
129
            Real compound = discount(t1, true) / discount(t2, true);
492✔
130
            // times have been calculated with a possibly different daycounter
131
            // but the difference should not matter for very small times
132
            return InterestRate::impliedRate(compound, dayCounter, comp, freq, dt);
492✔
133
        }
134
        QL_REQUIRE(d1 < d2, d1 << " later than " << d2);
751,034✔
135
        Real compound = discount(d1, extrapolate) / discount(d2, extrapolate);
751,034✔
136
        return InterestRate::impliedRate(compound, dayCounter, comp, freq, d1, d2);
751,034✔
137
    }
138

139
    InterestRate YieldTermStructure::forwardRate(
626,609,328✔
140
        Time t1, Time t2, Compounding comp, Frequency freq, bool extrapolate) const {
141
        Real compound;
142
        if (t2 == t1) {
626,609,328✔
143
            checkRange(t1, extrapolate);
140,416,293✔
144
            t1 = std::max(t1 - dt / 2.0, 0.0);
140,416,293✔
145
            t2 = t1 + dt;
140,416,293✔
146
            compound = discount(t1, true) / discount(t2, true);
140,416,293✔
147
        } else {
148
            QL_REQUIRE(t2 > t1, "t1 (" << t1 << ") >= t2 (" << t2 << ")");
486,193,035✔
149
            compound = discount(t1, extrapolate) / discount(t2, extrapolate);
486,193,035✔
150
        }
151
        return InterestRate::impliedRate(compound, dayCounter(), comp, freq, t2 - t1);
1,253,218,656✔
152
    }
153

154
    void YieldTermStructure::update() {
221,597✔
155
        TermStructure::update();
221,597✔
156
        Date newReference = Date();
221,597✔
157
        try {
158
            newReference = referenceDate();
221,597✔
159
            if (newReference != latestReference_)
221,597✔
160
                setJumps(newReference);
72,185✔
161
        } catch (Error&) {
×
162
            if (newReference == Date()) {
×
163
                // the curve couldn't calculate the reference
164
                // date. Most of the times, this is because some
165
                // underlying handle wasn't set, so we can just absorb
166
                // the exception and continue; the jumps will be set
167
                // correctly when a valid underlying is set.
168
                return;
169
            } else {
170
                // something else happened during the call to
171
                // setJumps(), so we let the exception bubble up.
172
                throw;
×
173
            }
174
        }
×
175
    }
176

177
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc