Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/termstructures/yield/zeroyieldstructure.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) 2000, 2001, 2002, 2003 RiskMap srl
5
 Copyright (C) 2003, 2004 StatPro Italia srl
6
 Copyright (C) 2009 Ferdinando Ametrano
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
/*! \file zeroyieldstructure.hpp
23
    \brief Zero-yield based term structure
24
*/
25
26
#ifndef quantlib_zero_yield_structure_hpp
27
#define quantlib_zero_yield_structure_hpp
28
29
#include <ql/termstructures/yieldtermstructure.hpp>
30
31
namespace QuantLib {
32
33
    //! Zero-yield term structure
34
    /*! This abstract class acts as an adapter to YieldTermStructure
35
        allowing the programmer to implement only the
36
        <tt>zeroYieldImpl(Time)</tt> method in derived classes.
37
38
        Discount and forward are calculated from zero yields.
39
40
        Zero rates are assumed to be annual continuous compounding.
41
42
        \ingroup yieldtermstructures
43
    */
44
    class ZeroYieldStructure : public YieldTermStructure {
45
      public:
46
        /*! \name Constructors
47
            See the TermStructure documentation for issues regarding
48
            constructors.
49
        */
50
        //@{
51
        explicit ZeroYieldStructure(
52
            const DayCounter& dc = DayCounter());
53
        explicit ZeroYieldStructure(
54
            const Date& referenceDate,
55
            const Calendar& calendar = Calendar(),
56
            const DayCounter& dc = DayCounter(),
57
            const std::vector<Handle<Quote> >& jumps = {},
58
            const std::vector<Date>& jumpDates = {});
59
        ZeroYieldStructure(
60
            Natural settlementDays,
61
            const Calendar& calendar,
62
            const DayCounter& dc = DayCounter(),
63
            const std::vector<Handle<Quote> >& jumps = {},
64
            const std::vector<Date>& jumpDates = {});
65
        //@}
66
      protected:
67
        /*! \name Calculations
68
69
            This method must be implemented in derived classes to
70
            perform the actual calculations. When it is called,
71
            range check has already been performed; therefore, it
72
            must assume that extrapolation is required.
73
        */
74
        //@{
75
        //! zero-yield calculation
76
        virtual Rate zeroYieldImpl(Time) const = 0;
77
        //@}
78
79
        //! \name YieldTermStructure implementation
80
        //@{
81
        /*! Returns the discount factor for the given date calculating it
82
            from the zero yield.
83
        */
84
        DiscountFactor discountImpl(Time) const override;
85
        //@}
86
    };
87
88
    // inline definitions
89
90
0
    inline DiscountFactor ZeroYieldStructure::discountImpl(Time t) const {
91
0
        if (t == 0.0)     // this acts as a safe guard in cases where
92
0
            return 1.0;   // zeroYieldImpl(0.0) would throw.
93
94
0
        Rate r = zeroYieldImpl(t);
95
0
        return DiscountFactor(std::exp(-r*t));
96
0
    }
97
98
}
99
100
#endif