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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Johannes Göttker-Schnetmann
Copyright (C) 2015 Klaus Spanderen
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
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
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.
*/
#include <ql/math/interpolations/linearinterpolation.hpp>
#include <ql/termstructures/volatility/equityfx/fixedlocalvolsurface.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
#include <ql/time/daycounters/yearfractiontodate.hpp>
#include <utility>
namespace QuantLib {
FixedLocalVolSurface::FixedLocalVolSurface(const Date& referenceDate,
const std::vector<Date>& dates,
const std::vector<Real>& strikes,
ext::shared_ptr<Matrix> localVolMatrix,
const DayCounter& dayCounter,
Extrapolation lowerExtrapolation,
Extrapolation upperExtrapolation)
: LocalVolTermStructure(referenceDate, NullCalendar(), Following, dayCounter),
maxDate_(dates.back()), localVolMatrix_(std::move(localVolMatrix)),
strikes_(dates.size(), ext::make_shared<std::vector<Real> >(strikes)),
localVolInterpol_(dates.size()), lowerExtrapolation_(lowerExtrapolation),
upperExtrapolation_(upperExtrapolation) {
QL_REQUIRE(dates[0]>=referenceDate,
"cannot have dates[0] < referenceDate");
times_ = std::vector<Time>(dates.size());
for (Size j=0; j<times_.size(); j++)
times_[j] = timeFromReference(dates[j]);
checkSurface();
setInterpolation<Linear>();
}
FixedLocalVolSurface::FixedLocalVolSurface(const Date& referenceDate,
const std::vector<Time>& times,
const std::vector<Real>& strikes,
ext::shared_ptr<Matrix> localVolMatrix,
const DayCounter& dayCounter,
Extrapolation lowerExtrapolation,
Extrapolation upperExtrapolation)
: LocalVolTermStructure(referenceDate, NullCalendar(), Following, dayCounter),
maxDate_(yearFractionToDate(dayCounter, referenceDate, times.back())), times_(times),
localVolMatrix_(std::move(localVolMatrix)),
strikes_(times.size(), ext::make_shared<std::vector<Real> >(strikes)),
localVolInterpol_(times.size()), lowerExtrapolation_(lowerExtrapolation),
upperExtrapolation_(upperExtrapolation) {
QL_REQUIRE(times_[0]>=0, "cannot have times[0] < 0");
checkSurface();
setInterpolation<Linear>();
}
FixedLocalVolSurface::FixedLocalVolSurface(
const Date& referenceDate,
const std::vector<Time>& times,
const std::vector<ext::shared_ptr<std::vector<Real> > >& strikes,
ext::shared_ptr<Matrix> localVolMatrix,
const DayCounter& dayCounter,
Extrapolation lowerExtrapolation,
Extrapolation upperExtrapolation)
: LocalVolTermStructure(referenceDate, NullCalendar(), Following, dayCounter),
maxDate_(yearFractionToDate(dayCounter, referenceDate, times.back())), times_(times),
localVolMatrix_(std::move(localVolMatrix)), strikes_(strikes),
localVolInterpol_(times.size()), lowerExtrapolation_(lowerExtrapolation),
upperExtrapolation_(upperExtrapolation) {
QL_REQUIRE(times_[0]>=0, "cannot have times[0] < 0");
QL_REQUIRE(times.size() == strikes.size(),
"need strikes for every time step");
checkSurface();
setInterpolation<Linear>();
}
void FixedLocalVolSurface::checkSurface() {
QL_REQUIRE(times_.size()==localVolMatrix_->columns(),
"mismatch between date vector and vol matrix colums");
for (const auto& strike : strikes_) {
QL_REQUIRE(strike->size() == localVolMatrix_->rows(),
"mismatch between money-strike vector and "
"vol matrix rows");
}
for (Size j=1; j<times_.size(); j++) {
QL_REQUIRE(times_[j]>times_[j-1],
"dates must be sorted unique!");
}
for (const auto& strike : strikes_)
for (Size j = 1; j < strike->size(); j++) {
QL_REQUIRE((*strike)[j] >= (*strike)[j - 1], "strikes must be sorted");
}
}
Date FixedLocalVolSurface::maxDate() const {
return maxDate_;
}
Time FixedLocalVolSurface::maxTime() const {
return times_.back();
}
Real FixedLocalVolSurface::minStrike() const {
return strikes_.back()->front();
}
Real FixedLocalVolSurface::maxStrike() const {
return strikes_.back()->back();
}
Volatility FixedLocalVolSurface::localVolImpl(Time t, Real strike) const {
t = std::min(times_.back(), std::max(t, times_.front()));
const Size idx = std::distance(times_.begin(),
std::lower_bound(times_.begin(), times_.end(), t));
if (close_enough(t, times_[idx])) {
if (strikes_[idx]->front() < strikes_[idx]->back())
return localVolInterpol_[idx](strike, true);
else
return (*localVolMatrix_)[localVolMatrix_->rows()/2][idx];
}
else {
Real earlierStrike = strike, laterStrike = strike;
if (lowerExtrapolation_ == ConstantExtrapolation) {
if (strike < strikes_[idx-1]->front())
earlierStrike = strikes_[idx-1]->front();
if (strike < strikes_[idx]->front())
laterStrike = strikes_[idx]->front();
}
if (upperExtrapolation_ == ConstantExtrapolation) {
if (strike > strikes_[idx-1]->back())
earlierStrike = strikes_[idx-1]->back();
if (strike > strikes_[idx]->back())
laterStrike = strikes_[idx]->back();
}
const Real earlyVol =
(strikes_[idx-1]->front() < strikes_[idx-1]->back())
? localVolInterpol_[idx-1](earlierStrike, true)
: (*localVolMatrix_)[localVolMatrix_->rows()/2][idx-1];
const Real laterVol = localVolInterpol_[idx](laterStrike, true);
return earlyVol
+ (laterVol-earlyVol)/(times_[idx]-times_[idx-1])
*(t-times_[idx-1]);
}
}
}
|