QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
capfloortermvolsurface.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Ferdinando Ametrano
5 Copyright (C) 2007 Katiuscia Manzoni
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#include <ql/termstructures/volatility/capfloor/capfloortermvolsurface.hpp>
22#include <ql/math/interpolations/bicubicsplineinterpolation.hpp>
23#include <ql/utilities/dataformatters.hpp>
24#include <ql/quotes/simplequote.hpp>
25
26namespace QuantLib {
27
28 // floating reference date, floating market data
30 Natural settlementDays,
31 const Calendar& calendar,
33 const std::vector<Period>& optionTenors,
34 const std::vector<Rate>& strikes,
35 const std::vector<std::vector<Handle<Quote> > >& vols,
36 const DayCounter& dc)
37 : CapFloorTermVolatilityStructure(settlementDays, calendar, bdc, dc),
38 nOptionTenors_(optionTenors.size()),
39 optionTenors_(optionTenors),
40 optionDates_(nOptionTenors_),
41 optionTimes_(nOptionTenors_),
42 nStrikes_(strikes.size()),
43 strikes_(strikes),
44 volHandles_(vols),
45 vols_(vols.size(), vols[0].size())
46 {
49 for (Size i=0; i<nOptionTenors_; ++i)
50 QL_REQUIRE(volHandles_[i].size()==nStrikes_,
51 io::ordinal(i+1) << " row of vol handles has size " <<
52 volHandles_[i].size() << " instead of " << nStrikes_);
54 for (Size i=0; i<vols_.rows(); ++i)
55 for (Size j=0; j<vols_.columns(); ++j)
56 vols_[i][j] = volHandles_[i][j]->value();
58 }
59
60 // fixed reference date, floating market data
62 const Date& settlementDate,
63 const Calendar& calendar,
65 const std::vector<Period>& optionTenors,
66 const std::vector<Rate>& strikes,
67 const std::vector<std::vector<Handle<Quote> > >& vols,
68 const DayCounter& dc)
69 : CapFloorTermVolatilityStructure(settlementDate, calendar, bdc, dc),
70 nOptionTenors_(optionTenors.size()),
71 optionTenors_(optionTenors),
72 optionDates_(nOptionTenors_),
73 optionTimes_(nOptionTenors_),
74 nStrikes_(strikes.size()),
75 strikes_(strikes),
76 volHandles_(vols),
77 vols_(vols.size(), vols[0].size())
78 {
81 for (Size i=0; i<nOptionTenors_; ++i)
82 QL_REQUIRE(volHandles_[i].size()==nStrikes_,
83 io::ordinal(i+1) << " row of vol handles has size " <<
84 volHandles_[i].size() << " instead of " << nStrikes_);
86 for (Size i=0; i<vols_.rows(); ++i)
87 for (Size j=0; j<vols_.columns(); ++j)
88 vols_[i][j] = volHandles_[i][j]->value();
90 }
91
92 // fixed reference date, fixed market data
94 const Date& settlementDate,
95 const Calendar& calendar,
97 const std::vector<Period>& optionTenors,
98 const std::vector<Rate>& strikes,
99 const Matrix& vols,
100 const DayCounter& dc)
101 : CapFloorTermVolatilityStructure(settlementDate, calendar, bdc, dc),
102 nOptionTenors_(optionTenors.size()),
103 optionTenors_(optionTenors),
104 optionDates_(nOptionTenors_),
105 optionTimes_(nOptionTenors_),
106 nStrikes_(strikes.size()),
107 strikes_(strikes),
108 volHandles_(vols.rows()),
109 vols_(vols)
110 {
111 checkInputs();
113 // fill dummy handles to allow generic handle-based computations later
114 for (Size i=0; i<nOptionTenors_; ++i) {
115 volHandles_[i].resize(nStrikes_);
116 for (Size j=0; j<nStrikes_; ++j)
117 volHandles_[i][j] = Handle<Quote>(ext::shared_ptr<Quote>(new
118 SimpleQuote(vols_[i][j])));
119 }
120 interpolate();
121 }
122
123 // floating reference date, fixed market data
125 Natural settlementDays,
126 const Calendar& calendar,
128 const std::vector<Period>& optionTenors,
129 const std::vector<Rate>& strikes,
130 const Matrix& vols,
131 const DayCounter& dc)
132 : CapFloorTermVolatilityStructure(settlementDays, calendar, bdc, dc),
133 nOptionTenors_(optionTenors.size()),
134 optionTenors_(optionTenors),
135 optionDates_(nOptionTenors_),
136 optionTimes_(nOptionTenors_),
137 nStrikes_(strikes.size()),
138 strikes_(strikes),
139 volHandles_(vols.rows()),
140 vols_(vols)
141 {
142 checkInputs();
144 // fill dummy handles to allow generic handle-based computations later
145 for (Size i=0; i<nOptionTenors_; ++i) {
146 volHandles_[i].resize(nStrikes_);
147 for (Size j=0; j<nStrikes_; ++j)
148 volHandles_[i][j] = Handle<Quote>(ext::shared_ptr<Quote>(new
149 SimpleQuote(vols_[i][j])));
150 }
151 interpolate();
152 }
153
155
156 QL_REQUIRE(!optionTenors_.empty(), "empty option tenor vector");
157 QL_REQUIRE(nOptionTenors_==vols_.rows(),
158 "mismatch between number of option tenors (" <<
159 nOptionTenors_ << ") and number of volatility rows (" <<
160 vols_.rows() << ")");
161 QL_REQUIRE(optionTenors_[0]>0*Days,
162 "negative first option tenor: " << optionTenors_[0]);
163 for (Size i=1; i<nOptionTenors_; ++i)
164 QL_REQUIRE(optionTenors_[i]>optionTenors_[i-1],
165 "non increasing option tenor: " << io::ordinal(i) <<
166 " is " << optionTenors_[i-1] << ", " <<
167 io::ordinal(i+1) << " is " << optionTenors_[i]);
168
169 QL_REQUIRE(nStrikes_==vols_.columns(),
170 "mismatch between strikes(" << strikes_.size() <<
171 ") and vol columns (" << vols_.columns() << ")");
172 for (Size j=1; j<nStrikes_; ++j)
173 QL_REQUIRE(strikes_[j-1]<strikes_[j],
174 "non increasing strikes: " << io::ordinal(j) <<
175 " is " << io::rate(strikes_[j-1]) << ", " <<
176 io::ordinal(j+1) << " is " << io::rate(strikes_[j]));
177 }
178
180 {
181 for (Size i=0; i<nOptionTenors_; ++i)
182 for (Size j=0; j<nStrikes_; ++j)
184 }
185
187 {
189 strikes_.end(),
190 optionTimes_.begin(),
191 optionTimes_.end(),
192 vols_);
193 }
194
196 {
197 // recalculate dates if necessary...
198 if (moving_) {
200 if (evaluationDate_ != d) {
201 evaluationDate_ = d;
203 }
204 }
207 }
208
210 {
211 for (Size i=0; i<nOptionTenors_; ++i) {
214 }
215 }
216
218 {
219 // check if date recalculation must be called here
220
221 for (Size i=0; i<nOptionTenors_; ++i)
222 for (Size j=0; j<nStrikes_; ++j)
223 vols_[i][j] = volHandles_[i][j]->value();
224
226 }
227
228}
bicubic-spline interpolation between discrete points
calendar class
Definition: calendar.hpp:61
std::vector< std::vector< Handle< Quote > > > volHandles_
CapFloorTermVolSurface(Natural settlementDays, const Calendar &calendar, BusinessDayConvention bdc, const std::vector< Period > &optionTenors, const std::vector< Rate > &strikes, const std::vector< std::vector< Handle< Quote > > > &, const DayCounter &dc=Actual365Fixed())
floating reference date, floating market data
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
void update() override
Definition: lazyobject.hpp:188
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
virtual void update()=0
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
market element returning a stored value
Definition: simplequote.hpp:33
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Time timeFromReference(const Date &date) const
date/time conversion
Date optionDateFromTenor(const Period &) const
period/date conversion
BusinessDayConvention
Business Day conventions.
detail::percent_holder rate(Rate)
output rates and spreads as percentages
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35