File: compoundforward.hpp

package info (click to toggle)
quantlib 0.9.0.20071224-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 22,216 kB
  • ctags: 34,951
  • sloc: cpp: 167,744; ansic: 21,483; sh: 8,947; makefile: 3,327; lisp: 86
file content (130 lines) | stat: -rw-r--r-- 4,984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2002, 2003 Decillion Pty(Ltd)

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

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

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

/*! \file compoundforward.hpp
    \brief compounded forward term structure
*/

#ifndef quantlib_compoundforward_curve_hpp
#define quantlib_compoundforward_curve_hpp

#include <ql/termstructures/yield/forwardstructure.hpp>
#include <ql/legacy/termstructures/extendeddiscountcurve.hpp>

namespace QuantLib {

    //! compound-forward structure
    /*! \test
        - the correctness of the curve is tested by reproducing the
          input data.
        - the correctness of the curve is tested by checking the
          consistency between returned rates and swaps priced on the
          curve.

        \bug swap rates are not reproduced exactly when using indexed
             coupons. Apparently, some assumption about the swap
             fixings is hard-coded into the bootstrapping algorithm.
    */
    class CompoundForward : public ForwardRateStructure {
      public:
        // constructor
        CompoundForward(const Date& referenceDate,
                        const std::vector<Date>& dates,
                        const std::vector<Rate>& forwards,
                        const Calendar& calendar,
                        const BusinessDayConvention conv,
                        const Integer compounding,
                        const DayCounter& dayCounter);
        BusinessDayConvention businessDayConvention() const { return conv_; }
        Integer compounding() const { return compounding_; }
        Date maxDate() const;
        const std::vector<Time>& times() const;
        const std::vector<Date>& dates() const;
        const std::vector<Rate>& forwards() const;
        boost::shared_ptr<ExtendedDiscountCurve> discountCurve() const;
        Rate compoundForward(const Date& d1,
                             Integer f,
                             bool extrapolate = false) const;
        Rate compoundForward(Time t1,
                             Integer f,
                             bool extrapolate = false) const;
      protected:
        // methods
        void calibrateNodes() const;
        boost::shared_ptr<YieldTermStructure> bootstrap() const;
        Rate zeroYieldImpl(Time) const;
        DiscountFactor discountImpl(Time) const;
        Size referenceNode(Time) const;
        Rate forwardImpl(Time) const;
        Rate compoundForwardImpl(Time, Integer) const;
      private:
        // data members
        BusinessDayConvention conv_;
        Integer compounding_;
        mutable bool needsBootstrap_;
        mutable std::vector<Date> dates_;
        mutable std::vector<Rate> forwards_;
        mutable std::vector<Time> times_;
        mutable Interpolation fwdinterp_;
        mutable boost::shared_ptr<ExtendedDiscountCurve> discountCurve_;
    };

    // inline definitions

    inline Date CompoundForward::maxDate() const {
        return dates_.back();
    }

    inline const std::vector<Time>& CompoundForward::times() const {
        return times_;
    }

    inline const std::vector<Date>& CompoundForward::dates() const {
        return dates_;
    }

    inline const std::vector<Rate>& CompoundForward::forwards() const {
        return forwards_;
    }

    inline Rate CompoundForward::compoundForward(const Date& d,
                                                 Integer f,
                                                 bool extrapolate) const {
        Time t = timeFromReference(d);
        QL_REQUIRE(t >= 0.0,
                   "negative time (" << t << ") given");
        QL_REQUIRE(extrapolate || allowsExtrapolation() || t <= maxTime(),
                   "time (" << t << ") is past max curve time ("
                   << maxTime() << ")");
        return compoundForwardImpl(timeFromReference(d),f);
    }

    inline Rate CompoundForward::compoundForward(Time t, Integer f,
                                                 bool extrapolate) const {
        QL_REQUIRE(t >= 0.0,
                   "negative time (" << t << ") given");
        QL_REQUIRE(extrapolate || allowsExtrapolation() || t <= maxTime(),
                   "time (" << t << ") is past max curve time ("
                   << maxTime() << ")");
        return compoundForwardImpl(t,f);
    }

}

#endif