Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
blackvariancecurve.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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
19#include "toplevelfixture.hpp"
20#include <boost/make_shared.hpp>
21#include <boost/test/unit_test.hpp>
22#include <ql/quotes/simplequote.hpp>
23#include <ql/termstructures/volatility/equityfx/blackvariancecurve.hpp>
24#include <ql/time/calendars/target.hpp>
25#include <ql/time/daycounters/actualactual.hpp>
27
28using namespace boost::unit_test_framework;
29using namespace QuantLib;
30using namespace QuantExt;
31using namespace std;
32
33BOOST_FIXTURE_TEST_SUITE(QuantExtTestSuite, qle::test::TopLevelFixture)
34
35BOOST_AUTO_TEST_SUITE(BlackVarianceCurveTest)
36
37BOOST_AUTO_TEST_CASE(testBlackVarianceCurve) {
38
39 BOOST_TEST_MESSAGE("Testing QuantExt::BlackVarianceCurve3...");
40
41 SavedSettings backup;
42 Settings::instance().evaluationDate() = Date(1, Dec, 2015);
43 Date today = Settings::instance().evaluationDate();
44
45 Natural settlementDays = 0;
46 Calendar cal = TARGET();
47 BusinessDayConvention bdc = Following;
48 DayCounter dc = ActualActual(ActualActual::ISDA);
49
50 vector<Time> times;
51 vector<Date> dates;
52 vector<Volatility> vols;
53 vector<QuantLib::ext::shared_ptr<SimpleQuote> > simpleQuotes;
54 vector<Handle<Quote> > quotes;
55
56 Size numYears = 10;
57 for (Size i = 1; i < numYears; i++) {
58
59 Volatility vol = 0.1 + (0.01 * i); // 11% at 1Y, 12% at 2Y
60 vols.push_back(vol);
61
62 simpleQuotes.push_back(QuantLib::ext::make_shared<SimpleQuote>(vol));
63 quotes.push_back(Handle<Quote>(simpleQuotes.back()));
64
65 dates.push_back(Date(1, Dec, today.year() + i));
66 times.push_back(dc.yearFraction(today, dates.back()));
67 }
68
69 // Build a QuantLib::BlackVarianceCurve
70 BlackVarianceCurve bvcBase(today, dates, vols, dc);
71 bvcBase.enableExtrapolation();
72
73 // Build a QuantExt::BlackVarianceCurve3
74 BlackVarianceCurve3 bvcTest(settlementDays, cal, bdc, dc, times, quotes);
75 bvcTest.enableExtrapolation();
76
77 Real strike = 1.0; // this is all ATM so we don't care
78
79 // Check that bvcTest returns the expected values
80 for (Size i = 0; i < times.size(); ++i) {
81 BOOST_CHECK_CLOSE(bvcTest.blackVol(times[i], strike), vols[i], 1e-12);
82 BOOST_CHECK_CLOSE(bvcTest.blackVol(dates[i], strike), vols[i], 1e-12);
83 }
84
85 // Now check that they give the same vols (including extrapolation)
86 for (Time t = 0.1; t < numYears + 10.0; t += 0.1) {
87 BOOST_CHECK_CLOSE(bvcBase.blackVol(t, strike), bvcTest.blackVol(t, strike), 1e-12);
88 }
89
90 // Now double the quotes
91 for (Size i = 0; i < simpleQuotes.size(); ++i) {
92 simpleQuotes[i]->setValue(simpleQuotes[i]->value() * 2.0);
93 }
94 // and check again
95 for (Time t = 0.1; t < numYears + 10.0; t += 0.1) {
96 BOOST_CHECK_CLOSE(bvcBase.blackVol(t, strike), 0.5 * bvcTest.blackVol(t, strike), 1e-12);
97 }
98}
99
100BOOST_AUTO_TEST_SUITE_END()
101
102BOOST_AUTO_TEST_SUITE_END()
Black volatility curve modeled as variance curve.
BOOST_AUTO_TEST_CASE(testBlackVarianceCurve)
Black volatility curve modeled as variance curve.
Fixture that can be used at top level.