QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
liborforwardmodel.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005, 2006 Klaus Spanderen
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/legacy/libormarketmodels/liborforwardmodel.hpp>
21#include <ql/pricingengines/blackformula.hpp>
22#include <ql/time/calendars/nullcalendar.hpp>
23#include <limits>
24
25namespace QuantLib {
26
28 const ext::shared_ptr<LiborForwardModelProcess> & process,
29 const ext::shared_ptr<LmVolatilityModel> & volaModel,
30 const ext::shared_ptr<LmCorrelationModel> & corrModel)
31 : CalibratedModel(volaModel->params().size()+corrModel->params().size()),
32 f_(process->size()),
33 accrualPeriod_(process->size()),
34 covarProxy_(new LfmCovarianceProxy(volaModel, corrModel)),
35 process_(process)
36 {
37
38 const Size k=volaModel->params().size();
39 std::copy(volaModel->params().begin(), volaModel->params().end(),
40 arguments_.begin());
41 std::copy(corrModel->params().begin(), corrModel->params().end(),
42 arguments_.begin()+k);
43
44 for (Size i=0; i < process->size(); ++i) {
45 accrualPeriod_[i] = process->accrualEndTimes()[i]
46 - process->accrualStartTimes()[i];
47 f_[i] = 1.0/(1.0+accrualPeriod_[i]*process_->initialValues()[i]);
48 }
49 }
50
51 void LiborForwardModel::setParams(const Array& params) {
53
54 const Size k=covarProxy_->volatilityModel()->params().size();
55
56 covarProxy_->volatilityModel()->setParams(
57 std::vector<Parameter>(arguments_.begin(), arguments_.begin()+k));
58 covarProxy_->correlationModel()->setParams(
59 std::vector<Parameter>(arguments_.begin()+k, arguments_.end()));
60
61 swaptionVola = ext::shared_ptr<SwaptionVolatilityMatrix>();
62 }
63
65 Real strike, Time maturity,
66 Time bondMaturity) const {
67
68 const std::vector<Time> & accrualStartTimes
69 = process_->accrualStartTimes();
70 const std::vector<Time> & accrualEndTimes
71 = process_->accrualEndTimes();
72
73 QL_REQUIRE( accrualStartTimes.front()<= maturity
74 && accrualStartTimes.back() >= maturity,
75 "capet maturity does not fit to the process");
76
77 const Size i = std::lower_bound(accrualStartTimes.begin(),
78 accrualStartTimes.end(),
79 maturity) - accrualStartTimes.begin();
80
81 QL_REQUIRE( i<process_->size()
82 && std::fabs(maturity - accrualStartTimes[i])
83 < 100*std::numeric_limits<Real>::epsilon()
84 && std::fabs(bondMaturity - accrualEndTimes[i])
85 < 100*std::numeric_limits<Real>::epsilon(),
86 "irregular fixings are not (yet) supported");
87
88 const Real tenor = accrualEndTimes[i] - accrualStartTimes[i];
89 const Real forward = process_->initialValues()[i];
90 const Real capRate = (1.0/strike - 1.0)/tenor;
91 const Volatility var = covarProxy_
92 ->integratedCovariance(i, i, process_->fixingTimes()[i]);
93 const DiscountFactor dis =
94 process_->index()->forwardingTermStructure()->discount(bondMaturity);
95
96 const Real black = blackFormula(
98 capRate, forward, std::sqrt(var));
99
100 const Real npv = dis * tenor * black;
101
102 return npv / (1.0 + capRate*tenor);
103 }
104
106 Array omega(beta+1,0.0);
107 QL_REQUIRE(alpha<beta, "alpha needs to be smaller than beta");
108
109 Real s=0.0;
110 for (Size k=alpha+1; k<=beta; ++k) {
111 Real b=accrualPeriod_[k];
112 for (Size j=alpha+1; j<=k; ++j) {
113 b*=f_[j];
114 }
115 s+=b;
116 }
117
118 for (Size i=alpha+1; i<=beta; ++i) {
119 Real a=accrualPeriod_[i];
120 for (Size j=alpha+1; j<=i; ++j) {
121 a*=f_[j];
122 }
123
124 omega[i] = a/s;
125 }
126
127 return omega;
128 }
129
131 const Array w = w_0(alpha, beta);
132 const Array f = process_->initialValues();
133
134 Rate fwdRate=0.0;
135 for (Size i=alpha+1; i <=beta; ++i) {
136 fwdRate+=w[i]*f[i];
137 }
138
139 return fwdRate;
140 }
141
142 // calculating swaption volatility matrix using
143 // Rebonatos approx. formula. Be aware that this
144 // matrix is valid only for regular fixings and
145 // assumes that the fix and floating leg have the
146 // same frequency
147 ext::shared_ptr<SwaptionVolatilityMatrix>
149 if (swaptionVola != nullptr) {
150 return swaptionVola;
151 }
152
153 const ext::shared_ptr<IborIndex> index = process_->index();
154 const Date today = process_->fixingDates()[0];
155
156 const Size size=process_->size()/2;
157 Matrix volatilities(size, size);
158
159 std::vector<Date> exercises(process_->fixingDates().begin() + 1,
160 process_->fixingDates().begin() + size+1);
161
162 std::vector<Period> lengths(size);
163 for (Size i=0; i < size; ++i) {
164 lengths[i] = (i+1)*index->tenor();
165 }
166
167 const Array f = process_->initialValues();
168 for (Size k=0; k < size; ++k) {
169 const Size alpha =k;
170 const Time t_alpha=process_->fixingTimes()[alpha+1];
171
172 Matrix var(size, size);
173 for (Size i=alpha+1; i <= k+size; ++i) {
174 for (Size j=i; j <= k+size; ++j) {
175 var[i-alpha-1][j-alpha-1] = var[j-alpha-1][i-alpha-1] =
176 covarProxy_->integratedCovariance(i, j, t_alpha);
177 }
178 }
179
180 for (Size l=1; l <= size; ++l) {
181 const Size beta =l + k;
182 const Array w = w_0(alpha, beta);
183
184 Real sum=0.0;
185 for (Size i=alpha+1; i <= beta; ++i) {
186 for (Size j=alpha+1; j <= beta; ++j) {
187 sum+=w[i]*w[j]*f[i]*f[j]*var[i-alpha-1][j-alpha-1];
188 }
189 }
190 volatilities[k][l-1] =
191 std::sqrt(sum/t_alpha)/S_0(alpha, beta);
192 }
193 }
194
195 return swaptionVola = ext::make_shared<SwaptionVolatilityMatrix>(
196 today, NullCalendar(), Following,
197 exercises, lengths, volatilities,
198 index->dayCounter());
199 }
200
201 // the next two methods are meaningless within this context
202 // we might remove them from the AffineModel interface
204 return process_->index()->forwardingTermStructure()->discount(t);
205 }
206
208 return discount(maturity);
209 }
210
211}
1-D array used in linear algebra.
Definition: array.hpp:52
Calibrated model class.
Definition: model.hpp:86
Array params() const
Returns array of arguments on which calibration is done.
Definition: model.cpp:126
std::vector< Parameter > arguments_
Definition: model.hpp:126
virtual void setParams(const Array &params)
Definition: model.cpp:138
Concrete date class.
Definition: date.hpp:125
proxy for a libor forward model covariance parameterization
Real discountBond(Time now, Time maturity, Array factors) const override
const ext::shared_ptr< LfmCovarianceProxy > covarProxy_
void setParams(const Array &params) override
Rate S_0(Size alpha, Size beta) const
LiborForwardModel(const ext::shared_ptr< LiborForwardModelProcess > &process, const ext::shared_ptr< LmVolatilityModel > &volaModel, const ext::shared_ptr< LmCorrelationModel > &corrModel)
Array w_0(Size alpha, Size beta) const
std::vector< Time > accrualPeriod_
Real discountBondOption(Option::Type type, Real strike, Time maturity, Time bondMaturity) const override
DiscountFactor discount(Time t) const override
Implied discount curve.
virtual ext::shared_ptr< SwaptionVolatilityMatrix > getSwaptionVolatilityMatrix() const
const ext::shared_ptr< LiborForwardModelProcess > process_
ext::shared_ptr< SwaptionVolatilityMatrix > swaptionVola
Matrix used in linear algebra.
Definition: matrix.hpp:41
Calendar for reproducing theoretical calculations.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)