41 {
42
43 BOOST_TEST_MESSAGE("Testing QuantExt::BlackVolSurfaceProxy...");
44
45
46
47
48 Date today = Date(1, Jan, 2020);
49 DayCounter dc = ActualActual(ActualActual::ISDA);
50
51 Settings::instance().evaluationDate() = today;
52
53 vector<Date> dates;
54 dates.push_back(Date(3, Feb, 2020));
55 dates.push_back(Date(2, Mar, 2020));
56 dates.push_back(Date(1, Apr, 2020));
57 dates.push_back(Date(4, Jan, 2021));
58
63
64 Matrix vols = Matrix(3, 4);
65 vols[0][0] = 0.12;
66 vols[1][0] = 0.10;
67 vols[2][0] = 0.13;
68 vols[0][1] = 0.22;
69 vols[1][1] = 0.20;
70 vols[2][1] = 0.23;
71 vols[0][2] = 0.32;
72 vols[1][2] = 0.30;
73 vols[2][2] = 0.33;
74 vols[0][3] = 0.42;
75 vols[1][3] = 0.40;
76 vols[2][3] = 0.43;
77
78
79 Handle<Quote> indexSpot = Handle<Quote>(QuantLib::ext::shared_ptr<Quote>(new SimpleQuote(1000)));
80 Handle<Quote> underlyingSpot = Handle<Quote>(QuantLib::ext::shared_ptr<Quote>(new SimpleQuote(150)));
81
82
83 Handle<YieldTermStructure> indexForecast = Handle<YieldTermStructure>(
84 QuantLib::ext::make_shared<FlatForward>(today, Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.03)), dc));
85 Handle<YieldTermStructure> indexDividend = Handle<YieldTermStructure>(
86 QuantLib::ext::make_shared<FlatForward>(today, Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.02)), dc));
87
88
89 Handle<YieldTermStructure> underlyingForecast = Handle<YieldTermStructure>(
90 QuantLib::ext::make_shared<FlatForward>(today, Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.02)), dc));
91 Handle<YieldTermStructure> underlyingDividend = Handle<YieldTermStructure>(
92 QuantLib::ext::make_shared<FlatForward>(today, Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.01)), dc));
93
94
95 QuantLib::ext::shared_ptr<EquityIndex2> index = QuantLib::ext::make_shared<EquityIndex2>("Index", UnitedStates(UnitedStates::Settlement), USDCurrency(),
96 indexSpot, indexForecast, indexDividend);
97 QuantLib::ext::shared_ptr<EquityIndex2> underlying = QuantLib::ext::make_shared<EquityIndex2>(
98 "Underlying", UnitedStates(UnitedStates::Settlement), USDCurrency(), underlyingSpot, underlyingForecast, underlyingDividend);
99
100
101 QuantLib::ext::shared_ptr<BlackVolTermStructure> indexVolSurface =
102 QuantLib::ext::make_shared<BlackVarianceSurface>(today, UnitedStates(UnitedStates::Settlement), dates,
strikes, vols, dc);
103
104
105 QuantLib::ext::shared_ptr<BlackVolatilitySurfaceProxy> underlyingVolSurface =
106 QuantLib::ext::make_shared<BlackVolatilitySurfaceProxy>(indexVolSurface, underlying, index);
107
108
109 for (auto d : dates) {
110
111 Real underlyingF = underlying->fixing(d);
112
113 Real underlyingVol = underlyingVolSurface->blackVol(d, underlyingF);
114
115
116 Real indexF = index->fixing(d);
117
118 Real indexVol = indexVolSurface->blackVol(d, indexF);
119
120 BOOST_CHECK_CLOSE(underlyingVol, indexVol, 0.001);
121 }
122}