| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2022 Skandinaviska Enskilda Banken AB (publ) |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | #include "svivolatility.hpp" |
| 21 | #include "utilities.hpp" |
| 22 | #include <ql/experimental/volatility/svismilesection.hpp> |
| 23 | |
| 24 | using namespace QuantLib; |
| 25 | using namespace boost::unit_test_framework; |
| 26 | |
| 27 | |
| 28 | void SviVolatilityTest::testSviSmileSection() { |
| 29 | |
| 30 | BOOST_TEST_MESSAGE("Testing SviSmileSection construction..." ); |
| 31 | |
| 32 | Date today = Settings::instance().evaluationDate(); |
| 33 | |
| 34 | // Test time based constructor |
| 35 | Time tte = 11.0 / 365; |
| 36 | Real forward = 123.45; |
| 37 | Real a = -0.0666; |
| 38 | Real b = 0.229; |
| 39 | Real sigma = 0.337; |
| 40 | Real rho = 0.439; |
| 41 | Real m = 0.193; |
| 42 | std::vector<Real> sviParameters = {a, b, sigma, rho, m}; |
| 43 | // Compute the strike that yields x (log-moneyness) equal to m, |
| 44 | // this simplifies the variance expression to a+b*sigma so we can test the correctness |
| 45 | // against the input parameters |
| 46 | Real strike = forward * std::exp(x: m); |
| 47 | ext::shared_ptr<SviSmileSection> time_section; |
| 48 | |
| 49 | BOOST_CHECK_NO_THROW(time_section = |
| 50 | ext::make_shared<SviSmileSection>(tte, forward, sviParameters)); |
| 51 | BOOST_CHECK_EQUAL(time_section->atmLevel(), forward); |
| 52 | QL_CHECK_CLOSE(time_section->variance(strike), a + b * sigma, 1E-10); |
| 53 | |
| 54 | // Test date based constructor |
| 55 | Date date = today + Period(11, Days); |
| 56 | ext::shared_ptr<SviSmileSection> date_section; |
| 57 | |
| 58 | BOOST_CHECK_NO_THROW(date_section = |
| 59 | ext::make_shared<SviSmileSection>(date, forward, sviParameters)); |
| 60 | |
| 61 | BOOST_CHECK_EQUAL(date_section->atmLevel(), forward); |
| 62 | QL_CHECK_CLOSE(date_section->variance(strike), a + b * sigma, 1E-10); |
| 63 | } |
| 64 | |
| 65 | test_suite* SviVolatilityTest::experimental() { |
| 66 | auto* suite = BOOST_TEST_SUITE("SVI volatility tests" ); |
| 67 | suite->add(QUANTLIB_TEST_CASE(&SviVolatilityTest::testSviSmileSection)); |
| 68 | return suite; |
| 69 | } |
| 70 | |