Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
blackvariancesurfacesparse.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21using namespace std;
22using namespace QuantLib;
23
24namespace QuantExt {
25
26BlackVarianceSurfaceSparse::BlackVarianceSurfaceSparse(const Date& referenceDate, const Calendar& cal,
27 const vector<Date>& dates, const vector<Real>& strikes,
28 const vector<Volatility>& volatilities,
29 const DayCounter& dayCounter, bool lowerStrikeConstExtrap,
30 bool upperStrikeConstExtrap, bool timeFlatExtrapolation)
31 : BlackVarianceTermStructure(referenceDate, cal), OptionInterpolator2d<Linear, Linear>(referenceDate, dayCounter,
32 lowerStrikeConstExtrap,
33 upperStrikeConstExtrap),
34 timeFlatExtrapolation_(timeFlatExtrapolation) {
35
36 QL_REQUIRE((strikes.size() == dates.size()) && (dates.size() == volatilities.size()),
37 "dates, strikes and volatilities vectors not of equal size.");
38
39 // convert volatilities to variance
40 vector<Real> variances(volatilities.size());
41 for (Size i = 0; i < volatilities.size(); i++) {
42 Time t = dayCounter.yearFraction(referenceDate, dates[i]);
43 variances[i] = volatilities[i] * volatilities[i] * t;
44 }
45
46 // variance must be 0 at time 0, add a variance of zero, time 0, for 2 strikes to ensure interpolation
47 vector<Date> modDates(dates.begin(), dates.end());
48 vector<Real> modStrikes(strikes.begin(), strikes.end());
49 vector<Real> tempStrikes;
50 tempStrikes.push_back(1.0);
51 tempStrikes.push_back(100.0);
52 for (Size i = 0; i < tempStrikes.size(); i++) {
53 modDates.push_back(referenceDate);
54 modStrikes.push_back(tempStrikes[i]);
55 variances.push_back(0.0);
56 }
57
58 initialise(modDates, modStrikes, variances);
59}
60
61QuantLib::Real BlackVarianceSurfaceSparse::blackVarianceImpl(QuantLib::Time t, QuantLib::Real strike) const {
62
63 QuantLib::Time tb = times().back();
64 if (timeFlatExtrapolation_ && t > tb) {
65 return getValue(tb, strike) * t / tb;
66 } else {
67 return getValue(t, strike);
68 }
69};
70} // namespace QuantExt
Black volatility surface modeled as variance surface.
virtual QuantLib::Real blackVarianceImpl(QuantLib::Time t, QuantLib::Real strike) const override
QuantLib::DayCounter dayCounter() const override
BlackVarianceSurfaceSparse(const QuantLib::Date &referenceDate, const QuantLib::Calendar &cal, const std::vector< QuantLib::Date > &dates, const std::vector< QuantLib::Real > &strikes, const std::vector< QuantLib::Volatility > &volatilities, const QuantLib::DayCounter &dayCounter, bool lowerStrikeConstExtrap=true, bool upperStrikeConstExtrap=true, bool timeFlatExtrapolation=false)
const QuantLib::Date & referenceDate() const override
std::vector< std::vector< QuantLib::Real > > strikes() const
void initialise(const std::vector< QuantLib::Date > &dates, const std::vector< QuantLib::Real > &strikes, const std::vector< QuantLib::Real > &values)
Initialise.
QuantLib::Real getValue(QuantLib::Time t, QuantLib::Real strike) const override
vector< Real > strikes