1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2006, 2007 Ferdinando Ametrano
5  Copyright (C) 2006, 2007 Mark Joshi
6 
7  This file is part of QuantLib, a free-software/open-source library
8  for financial quantitative analysts and developers - http://quantlib.org/
9 
10  QuantLib is free software: you can redistribute it and/or modify it
11  under the terms of the QuantLib license.  You should have received a
12  copy of the license along with this program; if not, please email
13  <quantlib-dev@lists.sf.net>. The license is also available online at
14  <http://quantlib.org/license.shtml>.
15 
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the license for more details.
19 */
20 
21 
22 #ifndef quantlib_curve_state_hpp
23 #define quantlib_curve_state_hpp
24 
25 #include <ql/math/array.hpp>
26 #include <vector>
27 #include <memory>
28 
29 namespace QuantLib {
30 
31     //! %Curve state for market-model simulations
32     /*! This class stores the state of the yield curve associated to the
33         fixed calendar times within the simulation.
34         This is the workhorse discounting object associated to the rate times
35         of the simulation. It's important to pass the rates via an object like
36         this to the product rather than directly to make it easier to switch
37         to other engines such as a coterminal swap rate engine.
38         Many products will not need expired rates and others will only require
39         the first rate.
40     */
41     class CurveState {
42     /* There will n+1 rate times expressing payment and reset times
43         of forward rates.
44 
45                 |-----|-----|-----|-----|-----|      (size = 6)
46                 t0    t1    t2    t3    t4    t5     rateTimes
47                 f0    f1    f2    f3    f4           forwardRates
48                 d0    d1    d2    d3    d4    d5     discountBonds
49                 d0/d0 d1/d0 d2/d0 d3/d0 d4/d0 d5/d0  discountRatios
50                 sr0   sr1   sr2   sr3   sr4          cotSwaps
51     */
52       public:
53         CurveState(const std::vector<Time>& rateTimes);
~CurveState()54         virtual ~CurveState() {}
55 
56         //! \name Inspectors
57         //@{
numberOfRates() const58         Size numberOfRates() const { return numberOfRates_; }
59 
rateTimes() const60         const std::vector<Time>& rateTimes() const { return rateTimes_; }
rateTaus() const61         const std::vector<Time>& rateTaus() const { return rateTaus_; }
62 
63         virtual Real discountRatio(Size i,
64                                    Size j) const = 0;
65         virtual Rate forwardRate(Size i) const = 0;
66         virtual Rate coterminalSwapAnnuity(Size numeraire,
67                                            Size i) const = 0;
68         virtual Rate coterminalSwapRate(Size i) const = 0;
69         virtual Rate cmSwapAnnuity(Size numeraire,
70                                    Size i,
71                                    Size spanningForwards) const = 0;
72         virtual Rate cmSwapRate(Size i,
73                                 Size spanningForwards) const = 0;
74 
75         virtual const std::vector<Rate>& forwardRates() const = 0;
76         virtual const std::vector<Rate>& coterminalSwapRates() const = 0;
77         virtual const std::vector<Rate>& cmSwapRates(Size spanningForwards) const = 0;
78         Rate swapRate(Size begin,
79                       Size end) const;
80 
81         #if defined(QL_USE_STD_UNIQUE_PTR)
82         virtual std::unique_ptr<CurveState> clone() const = 0;
83         #else
84         virtual std::auto_ptr<CurveState> clone() const = 0;
85         #endif
86         //@}
87       protected:
88         Size numberOfRates_;
89         std::vector<Time> rateTimes_, rateTaus_;
90     };
91 
92     void forwardsFromDiscountRatios(Size firstValidIndex,
93                                     const std::vector<DiscountFactor>& ds,
94                                     const std::vector<Time>& taus,
95                                     std::vector<Rate>& fwds);
96 
97     void coterminalFromDiscountRatios(Size firstValidIndex,
98                                       const std::vector<DiscountFactor>& ds,
99                                       const std::vector<Time>& taus,
100                                       std::vector<Rate>& cotSwapRates,
101                                       std::vector<Real>& cotSwapAnnuities);
102 
103     void constantMaturityFromDiscountRatios( // Size i, // to be added later
104         Size spanningForwards,
105         Size firstValidIndex,
106         const std::vector<DiscountFactor>& ds,
107         const std::vector<Time>& taus,
108         std::vector<Rate>& cotSwapRates,
109         std::vector<Real>& cotSwapAnnuities);
110 }
111 
112 #endif
113