File: termstructure.hpp

package info (click to toggle)
quantlib 0.2.1.cvs20020322-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,716 kB
  • ctags: 4,614
  • sloc: cpp: 19,601; sh: 7,389; makefile: 796; ansic: 22
file content (575 lines) | stat: -rw-r--r-- 19,810 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575


/*
 Copyright (C) 2000, 2001, 2002 RiskMap srl

 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 ferdinando@ametrano.net
 The license is also available online at http://quantlib.org/html/license.html

 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 termstructure.hpp
    \brief Term structure

    \fullpath
    ql/%termstructure.hpp
*/

// $Id: termstructure.hpp,v 1.19 2002/03/05 16:58:02 lballabio Exp $

#ifndef quantlib_term_structure_hpp
#define quantlib_term_structure_hpp

#include <ql/calendar.hpp>
#include <ql/currency.hpp>
#include <ql/daycounter.hpp>
#include <ql/marketelement.hpp>
#include <vector>

/*! \namespace QuantLib::TermStructures
    \brief Concrete implementations of the TermStructure interface

    See sect. \ref termstructures
*/

namespace QuantLib {

    //! Term structure
    /*! This abstract class defines the interface of concrete
        rate structures which will be derived from this one.
    */
    class TermStructure : public Patterns::Observable {
      public:
        virtual ~TermStructure() {}
        //! \name Rates and discount
        //@{
        //! zero yield rate at a given date
        Rate zeroYield(const Date&, bool extrapolate = false) const;
        //! zero yield rate at a given time from settlement
        Rate zeroYield(Time, bool extrapolate = false) const;
        //! discount factor at a given date
        DiscountFactor discount(const Date&, bool extrapolate = false) const;
        //! discount factor at a given time from settlement
        DiscountFactor discount(Time, bool extrapolate = false) const;
        //! instantaneous forward rate at a given date
        Rate forward(const Date&, bool extrapolate = false) const;
        //! instantaneous forward rate at a given time from settlement
        Rate forward(Time, bool extrapolate = false) const;
        //@}

        //! \name Dates
        //@{
        //! returns today's date
        virtual Date todaysDate() const = 0;
        //! returns the number of settlement days
        virtual int settlementDays() const = 0;
        //! returns the calendar for settlement calculation
        virtual Calendar calendar() const = 0;
        //! returns the day counter
        virtual DayCounter dayCounter() const = 0;

        //! returns the settlement date
        virtual Date settlementDate() const = 0;
        //! returns the earliest date for which the curve can return rates
        virtual Date minDate() const = 0;
        //! returns the latest date for which the curve can return rates
        virtual Date maxDate() const = 0;
        //! returns the earliest time for which the curve can return rates
        virtual Time minTime() const = 0;
        //! returns the latest date for which the curve can return rates
        virtual Time maxTime() const = 0;
        //@}

        //! \name Other inspectors
        //@{
        //! returns the currency upon which the term structure is defined
        virtual Currency currency() const = 0;
        //@}
      protected:
        //! implements the actual zero yield calculation in derived classes
        virtual Rate zeroYieldImpl(Time,
            bool extrapolate = false) const = 0;
        //! implements the actual discount calculation in derived classes
        virtual DiscountFactor discountImpl(Time,
            bool extrapolate = false) const = 0;
        //! implements the actual forward rate calculation in derived classes
        virtual Rate forwardImpl(Time,
            bool extrapolate = false) const = 0;
    };

    //! Zero yield term structure
    /*! This abstract class acts as an adapter to TermStructure allowing the
        programmer to implement only the <tt>zeroYield(Time)</tt> method in
        derived classes.
    */
    class ZeroYieldStructure : public TermStructure {
      public:
        virtual ~ZeroYieldStructure() {}
      protected:
        /*! Returns the discount factor for the given date calculating it
            from the zero yield.
        */
        DiscountFactor discountImpl(Time, bool extrapolate = false) const;
        /*! Returns the instantaneous forward rate for the given date
            calculating it from the zero yield.
        */
        Rate forwardImpl(Time, bool extrapolate = false) const;
    };

    //! Discount factor term structure
    /*! This abstract class acts as an adapter to TermStructure allowing the
        programmer to implement only the <tt>discount(const Date&)</tt>
        method in derived classes.
    */
    class DiscountStructure : public TermStructure {
      public:
        virtual ~DiscountStructure() {}
      protected:
        /*! Returns the zero yield rate for the given date calculating it
            from the discount.
        */
        Rate zeroYieldImpl(Time, bool extrapolate = false) const;
        /*! Returns the instantaneous forward rate for the given date
            calculating it from the discount.
        */
        Rate forwardImpl(Time, bool extrapolate = false) const;
    };

    //! Forward rate term structure
    /*! This abstract class acts as an adapter to TermStructure allowing the
        programmer to implement only the <tt>forward(const Date&)</tt> method
        in derived classes.
    */
    class ForwardRateStructure : public TermStructure {
      public:
        virtual ~ForwardRateStructure() {}
      protected:
        /*! Returns the zero yield rate for the given date calculating it
            from the instantaneous forward rate.
            \warning This is just a default, highly inefficient
                implementation. Derived classes should implement their own
                zeroYield method.
        */
        Rate zeroYieldImpl(Time, bool extrapolate = false) const;
        /*! Returns the discount factor for the given date calculating it
            from the instantaneous forward rate.
        */
        DiscountFactor discountImpl(Time, bool extrapolate = false) const;
    };

    //! Implied term structure at a given date in the future
    /*! The given date will be the implied today's date.
        \note This term structure will remain linked to the original
            structure, i.e., any changes in the latter will be reflected in
            this structure as well.
    */
    class ImpliedTermStructure : public DiscountStructure,
                                 public Patterns::Observer {
      public:
        ImpliedTermStructure(const RelinkableHandle<TermStructure>&,
            const Date& todaysDate);
        //! \name TermStructure interface
        //@{
        Currency currency() const;
        Date todaysDate() const;
        int settlementDays() const;
        Calendar calendar() const;
        DayCounter dayCounter() const;
        Date settlementDate() const;
        Date maxDate() const;
        Date minDate() const;
        Time maxTime() const;
        Time minTime() const;
        //@}
        //! \name Observer interface
        //@{
        void update();
        //@}
      protected:
        //! returns the discount factor as seen from the evaluation date
        DiscountFactor discountImpl(Time, bool extrapolate = false) const;
      private:
        RelinkableHandle<TermStructure> originalCurve_;
        Date todaysDate_;
    };

    //! Term structure with an added spread on the zero yield rate
    /*! \note This term structure will remain linked to the original
            structure, i.e., any changes in the latter will be reflected in
            this structure as well.
    */
    class ZeroSpreadedTermStructure : public ZeroYieldStructure,
                                      public Patterns::Observer {
      public:
        ZeroSpreadedTermStructure(const RelinkableHandle<TermStructure>&,
            const RelinkableHandle<MarketElement>& spread);
        //! \name TermStructure interface
        //@{
        Currency currency() const;
        Date todaysDate() const;
        int settlementDays() const;
        Calendar calendar() const;
        DayCounter dayCounter() const;
        Date settlementDate() const;
        Date maxDate() const;
        Date minDate() const;
        Time maxTime() const;
        Time minTime() const;
        //@}
        //! \name Observer interface
        //@{
        void update();
        //@}
      protected:
        //! returns the spreaded zero yield rate
        Rate zeroYieldImpl(Time, bool extrapolate = false) const;
        //! returns the spreaded forward rate
        /*! \warning This method must disappear should the spread become a curve */
        Rate forwardImpl(Time, bool extrapolate = false) const;
      private:
        RelinkableHandle<TermStructure> originalCurve_;
        RelinkableHandle<MarketElement> spread_;
    };



    //! Term structure with an added spread on the instantaneous forward rate
    /*! \note This term structure will remain linked to the original
            structure, i.e., any changes in the latter will be reflected in
            this structure as well.
    */
    class ForwardSpreadedTermStructure : public ForwardRateStructure,
                                         public Patterns::Observer {
      public:
        ForwardSpreadedTermStructure(const RelinkableHandle<TermStructure>&,
            const RelinkableHandle<MarketElement>& spread);
        //! \name TermStructure interface
        //@{
        Currency currency() const;
        Date todaysDate() const;
        int settlementDays() const;
        Calendar calendar() const;
        DayCounter dayCounter() const;
        Date settlementDate() const;
        Date maxDate() const;
        Date minDate() const;
        Time maxTime() const;
        Time minTime() const;
        //@}
        //! \name Observer interface
        //@{
        void update();
        //@}
      protected:
        //! returns the spreaded forward rate
        Rate forwardImpl(Time, bool extrapolate = false) const;
        //! returns the spreaded zero yield rate
        /*! \warning This method must disappear should the spread become a curve */
        Rate zeroYieldImpl(Time, bool extrapolate = false) const;
      private:
        RelinkableHandle<TermStructure> originalCurve_;
        RelinkableHandle<MarketElement> spread_;
    };




    // inline definitions

    inline Rate TermStructure::zeroYield(const Date& d,
        bool extrapolate) const {
            Time t = dayCounter().yearFraction(settlementDate(),d);
            return zeroYieldImpl(t,extrapolate);
    }

    inline Rate TermStructure::zeroYield(Time t, bool extrapolate) const {
        return zeroYieldImpl(t,extrapolate);
    }

    inline DiscountFactor TermStructure::discount(const Date& d,
        bool extrapolate) const {
            Time t = dayCounter().yearFraction(settlementDate(),d);
            return discountImpl(t,extrapolate);
    }

    inline DiscountFactor TermStructure::discount(Time t,
        bool extrapolate) const {
            return discountImpl(t,extrapolate);
    }

    inline Rate TermStructure::forward(const Date& d,
        bool extrapolate) const {
            Time t = dayCounter().yearFraction(settlementDate(),d);
            return forwardImpl(t,extrapolate);
    }

    inline Rate TermStructure::forward(Time t, bool extrapolate) const {
        return forwardImpl(t,extrapolate);
    }


    // curve deriving discount and forward from zero yield

    inline DiscountFactor ZeroYieldStructure::discountImpl(Time t,
        bool extrapolate) const {
            Rate r = zeroYieldImpl(t, extrapolate);
            return DiscountFactor(QL_EXP(-r*t));
    }

    inline Rate ZeroYieldStructure::forwardImpl(Time t,
        bool extrapolate) const {
            Time dt = 0.001;
            Rate r1 = zeroYieldImpl(t, extrapolate),
                 r2 = zeroYieldImpl(t+dt, true);
            return r1+t*(r2-r1)/dt;
    }


    // curve deriving zero yield and forward from discount

    inline Rate DiscountStructure::zeroYieldImpl(Time t,
        bool extrapolate) const {
            DiscountFactor f = discountImpl(t, extrapolate);
            return Rate(-QL_LOG(f)/t);
    }

    inline Rate DiscountStructure::forwardImpl(Time t,
        bool extrapolate) const {
            Time dt = 0.001;
            DiscountFactor f1 = discountImpl(t, extrapolate),
                           f2 = discountImpl(t+dt, true);
            return Rate(QL_LOG(f1/f2)/dt);
    }


    // curve deriving zero yield and discount from forward

    inline Rate ForwardRateStructure::zeroYieldImpl(Time t,
        bool extrapolate) const {
            if (t == 0.0)
                return forwardImpl(0.0);
            double sum = 0.5*forwardImpl(0.0);
            Size N = 1000;
            double dt = t/N;
            for (Time i=dt; i<t; i+=dt)
                sum += forwardImpl(i, extrapolate);
            sum += 0.5*forwardImpl(t, extrapolate);
            return Rate(sum*dt/t);
    }

    inline DiscountFactor ForwardRateStructure::discountImpl(Time t,
        bool extrapolate) const {
            Rate r = zeroYieldImpl(t, extrapolate);
            return DiscountFactor(QL_EXP(-r*t));
    }



    // time-shifted curve

    inline ImpliedTermStructure::ImpliedTermStructure(
        const RelinkableHandle<TermStructure>& h, const Date& todaysDate)
    : originalCurve_(h), todaysDate_(todaysDate) {
        registerWith(originalCurve_);
    }

    inline Currency ImpliedTermStructure::currency() const {
        return originalCurve_->currency();
    }

    inline Date ImpliedTermStructure::todaysDate() const {
        return todaysDate_;
    }

    inline int ImpliedTermStructure::settlementDays() const {
        return originalCurve_->settlementDays();
    }

    inline Calendar ImpliedTermStructure::calendar() const {
        return originalCurve_->calendar();
    }

    inline DayCounter ImpliedTermStructure::dayCounter() const {
        return originalCurve_->dayCounter();
    }

    inline Date ImpliedTermStructure::settlementDate() const {
        return calendar().advance(
            todaysDate_,settlementDays(),Days);
    }

    inline Date ImpliedTermStructure::maxDate() const {
        return originalCurve_->maxDate();
    }

    inline Date ImpliedTermStructure::minDate() const {
        return settlementDate();
    }

    inline Time ImpliedTermStructure::maxTime() const {
        return dayCounter().yearFraction(
            settlementDate(),originalCurve_->maxDate());
    }

    inline Time ImpliedTermStructure::minTime() const {
        return 0.0;
    }

    inline void ImpliedTermStructure::update() {
        notifyObservers();
    }

    inline DiscountFactor ImpliedTermStructure::discountImpl(Time t,
        bool extrapolate) const {
            /* t is relative to the current settlement date
               and needs to be converted to the time relative
               to the settlement date of the original curve */
            Time originalTime = t + dayCounter().yearFraction(
                originalCurve_->settlementDate(),settlementDate());
            // evaluationDate cannot be an extrapolation
            /* discount at evaluation date cannot be cached
               since the original curve could change between
               invocations of this method */
            return originalCurve_->discount(originalTime, extrapolate) /
                   originalCurve_->discount(settlementDate(),false);
    }


    // zero-yield spreaded curves
    inline ZeroSpreadedTermStructure::ZeroSpreadedTermStructure(
        const RelinkableHandle<TermStructure>& h, 
        const RelinkableHandle<MarketElement>& spread)
    : originalCurve_(h), spread_(spread) {
        registerWith(originalCurve_);
        registerWith(spread_);
    }

    inline Currency ZeroSpreadedTermStructure::currency() const {
        return originalCurve_->currency();
    }

    inline Date ZeroSpreadedTermStructure::todaysDate() const {
        return originalCurve_->todaysDate();
    }

    inline int ZeroSpreadedTermStructure::settlementDays() const {
        return originalCurve_->settlementDays();
    }

    inline Calendar ZeroSpreadedTermStructure::calendar() const {
        return originalCurve_->calendar();
    }

    inline DayCounter ZeroSpreadedTermStructure::dayCounter() const {
        return originalCurve_->dayCounter();
    }

    inline Date ZeroSpreadedTermStructure::settlementDate() const {
        return originalCurve_->settlementDate();
    }

    inline Date ZeroSpreadedTermStructure::maxDate() const {
        return originalCurve_->maxDate();
    }

    inline Date ZeroSpreadedTermStructure::minDate() const {
        return originalCurve_->minDate();
    }

    inline Time ZeroSpreadedTermStructure::maxTime() const {
        return originalCurve_->maxTime();
    }

    inline Time ZeroSpreadedTermStructure::minTime() const {
        return originalCurve_->minTime();
    }

    inline void ZeroSpreadedTermStructure::update() {
        notifyObservers();
    }

    inline Rate ZeroSpreadedTermStructure::zeroYieldImpl(Time t,
        bool extrapolate) const {
            return originalCurve_->zeroYield(t, extrapolate) + spread_->value();
    }

    inline Rate ZeroSpreadedTermStructure::forwardImpl(Time t,
        bool extrapolate) const {
            return originalCurve_->forward(t, extrapolate) + spread_->value();
    }


    // forward spreaded curves
    inline ForwardSpreadedTermStructure::ForwardSpreadedTermStructure(
        const RelinkableHandle<TermStructure>& h, 
        const RelinkableHandle<MarketElement>& spread)
    : originalCurve_(h), spread_(spread) {
        registerWith(originalCurve_);
        registerWith(spread_);
    }

    inline Currency ForwardSpreadedTermStructure::currency() const {
        return originalCurve_->currency();
    }

    inline Date ForwardSpreadedTermStructure::todaysDate() const {
        return originalCurve_->todaysDate();
    }

    inline int ForwardSpreadedTermStructure::settlementDays() const {
        return originalCurve_->settlementDays();
    }

    inline Calendar ForwardSpreadedTermStructure::calendar() const {
        return originalCurve_->calendar();
    }

    inline DayCounter ForwardSpreadedTermStructure::dayCounter() const {
        return originalCurve_->dayCounter();
    }

    inline Date ForwardSpreadedTermStructure::settlementDate() const {
        return originalCurve_->settlementDate();
    }

    inline Date ForwardSpreadedTermStructure::maxDate() const {
        return originalCurve_->maxDate();
    }

    inline Date ForwardSpreadedTermStructure::minDate() const {
        return originalCurve_->minDate();
    }

    inline Time ForwardSpreadedTermStructure::maxTime() const {
        return originalCurve_->maxTime();
    }

    inline Time ForwardSpreadedTermStructure::minTime() const {
        return originalCurve_->minTime();
    }

    inline void ForwardSpreadedTermStructure::update() {
        notifyObservers();
    }

    inline Rate ForwardSpreadedTermStructure::forwardImpl(Time t,
        bool extrapolate) const {
            return originalCurve_->forward(t, extrapolate) + spread_->value();
    }

    inline Rate ForwardSpreadedTermStructure::zeroYieldImpl(Time t,
        bool extrapolate) const {
            return originalCurve_->zeroYield(t, extrapolate) + spread_->value();
    }

}


#endif