QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fixedlocalvolsurface.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2015 Johannes Göttker-Schnetmann
5 Copyright (C) 2015 Klaus Spanderen
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/math/interpolations/linearinterpolation.hpp>
22#include <ql/termstructures/volatility/equityfx/fixedlocalvolsurface.hpp>
23#include <ql/time/calendars/nullcalendar.hpp>
24#include <ql/time/daycounters/yearfractiontodate.hpp>
25#include <utility>
26
27
28namespace QuantLib {
29
31 const std::vector<Date>& dates,
32 const std::vector<Real>& strikes,
33 ext::shared_ptr<Matrix> localVolMatrix,
34 const DayCounter& dayCounter,
35 Extrapolation lowerExtrapolation,
36 Extrapolation upperExtrapolation)
37 : LocalVolTermStructure(referenceDate, NullCalendar(), Following, dayCounter),
38 maxDate_(dates.back()), localVolMatrix_(std::move(localVolMatrix)),
39 strikes_(dates.size(), ext::make_shared<std::vector<Real> >(strikes)),
40 localVolInterpol_(dates.size()), lowerExtrapolation_(lowerExtrapolation),
41 upperExtrapolation_(upperExtrapolation) {
42
43 QL_REQUIRE(dates[0]>=referenceDate,
44 "cannot have dates[0] < referenceDate");
45
46 times_ = std::vector<Time>(dates.size());
47 for (Size j=0; j<times_.size(); j++)
48 times_[j] = timeFromReference(dates[j]);
49
51 setInterpolation<Linear>();
52 }
53
55 const std::vector<Time>& times,
56 const std::vector<Real>& strikes,
57 ext::shared_ptr<Matrix> localVolMatrix,
58 const DayCounter& dayCounter,
59 Extrapolation lowerExtrapolation,
60 Extrapolation upperExtrapolation)
61 : LocalVolTermStructure(referenceDate, NullCalendar(), Following, dayCounter),
62 maxDate_(yearFractionToDate(dayCounter, referenceDate, times.back())), times_(times),
63 localVolMatrix_(std::move(localVolMatrix)),
64 strikes_(times.size(), ext::make_shared<std::vector<Real> >(strikes)),
65 localVolInterpol_(times.size()), lowerExtrapolation_(lowerExtrapolation),
66 upperExtrapolation_(upperExtrapolation) {
67
68 QL_REQUIRE(times_[0]>=0, "cannot have times[0] < 0");
69
71 setInterpolation<Linear>();
72 }
73
75 const Date& referenceDate,
76 const std::vector<Time>& times,
77 const std::vector<ext::shared_ptr<std::vector<Real> > >& strikes,
78 ext::shared_ptr<Matrix> localVolMatrix,
79 const DayCounter& dayCounter,
80 Extrapolation lowerExtrapolation,
81 Extrapolation upperExtrapolation)
82 : LocalVolTermStructure(referenceDate, NullCalendar(), Following, dayCounter),
83 maxDate_(yearFractionToDate(dayCounter, referenceDate, times.back())), times_(times),
84 localVolMatrix_(std::move(localVolMatrix)), strikes_(strikes),
85 localVolInterpol_(times.size()), lowerExtrapolation_(lowerExtrapolation),
86 upperExtrapolation_(upperExtrapolation) {
87
88 QL_REQUIRE(times_[0]>=0, "cannot have times[0] < 0");
89 QL_REQUIRE(times.size() == strikes.size(),
90 "need strikes for every time step");
92 setInterpolation<Linear>();
93 }
94
95
97 QL_REQUIRE(times_.size()==localVolMatrix_->columns(),
98 "mismatch between date vector and vol matrix colums");
99 for (const auto& strike : strikes_) {
100 QL_REQUIRE(strike->size() == localVolMatrix_->rows(),
101 "mismatch between money-strike vector and "
102 "vol matrix rows");
103 }
104
105 for (Size j=1; j<times_.size(); j++) {
106 QL_REQUIRE(times_[j]>times_[j-1],
107 "dates must be sorted unique!");
108 }
109
110 for (const auto& strike : strikes_)
111 for (Size j = 1; j < strike->size(); j++) {
112 QL_REQUIRE((*strike)[j] >= (*strike)[j - 1], "strikes must be sorted");
113 }
114 }
115
117 return maxDate_;
118 }
120 return times_.back();
121 }
123 return strikes_.back()->front();
124 }
126 return strikes_.back()->back();
127 }
128
130 t = std::min(times_.back(), std::max(t, times_.front()));
131
132 const Size idx = std::distance(times_.begin(),
133 std::lower_bound(times_.begin(), times_.end(), t));
134
135 if (close_enough(t, times_[idx])) {
136 if (strikes_[idx]->front() < strikes_[idx]->back())
137 return localVolInterpol_[idx](strike, true);
138 else
139 return (*localVolMatrix_)[localVolMatrix_->rows()/2][idx];
140 }
141 else {
142 Real earlierStrike = strike, laterStrike = strike;
144 if (strike < strikes_[idx-1]->front())
145 earlierStrike = strikes_[idx-1]->front();
146 if (strike < strikes_[idx]->front())
147 laterStrike = strikes_[idx]->front();
148 }
149
151 if (strike > strikes_[idx-1]->back())
152 earlierStrike = strikes_[idx-1]->back();
153 if (strike > strikes_[idx]->back())
154 laterStrike = strikes_[idx]->back();
155 }
156
157 const Real earlyVol =
158 (strikes_[idx-1]->front() < strikes_[idx-1]->back())
159 ? localVolInterpol_[idx-1](earlierStrike, true)
160 : (*localVolMatrix_)[localVolMatrix_->rows()/2][idx-1];
161 const Real laterVol = localVolInterpol_[idx](laterStrike, true);
162
163 return earlyVol
164 + (laterVol-earlyVol)/(times_[idx]-times_[idx-1])
165 *(t-times_[idx-1]);
166 }
167 }
168
169}
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
std::vector< Interpolation > localVolInterpol_
Real minStrike() const override
the minimum strike for which the term structure can return vols
Volatility localVolImpl(Time t, Real strike) const override
local vol calculation
const std::vector< ext::shared_ptr< std::vector< Real > > > strikes_
const ext::shared_ptr< Matrix > localVolMatrix_
Date maxDate() const override
the latest date for which the curve can return values
Real maxStrike() const override
the maximum strike for which the term structure can return vols
Time maxTime() const override
the latest time for which the curve can return values
FixedLocalVolSurface(const Date &referenceDate, const std::vector< Date > &dates, const std::vector< Real > &strikes, ext::shared_ptr< Matrix > localVolMatrix, const DayCounter &dayCounter, Extrapolation lowerExtrapolation=ConstantExtrapolation, Extrapolation upperExtrapolation=ConstantExtrapolation)
Calendar for reproducing theoretical calculations.
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
Time timeFromReference(const Date &date) const
date/time conversion
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Date yearFractionToDate(const DayCounter &dayCounter, const Date &referenceDate, Time t)
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
STL namespace.