From 21b75a911b94144c8d040c916e351a87432eb95b Mon Sep 17 00:00:00 2001 From: siddharthmehrotra Date: Fri, 3 Nov 2023 15:07:36 +0800 Subject: [PATCH 01/37] Migrated fdcev.cpp --- test-suite/CMakeLists.txt | 1 - test-suite/Makefile.am | 1 - test-suite/fdcev.cpp | 21 +++++++--------- test-suite/fdcev.hpp | 37 ---------------------------- test-suite/quantlibtestsuite.cpp | 2 -- test-suite/testsuite.vcxproj | 1 - test-suite/testsuite.vcxproj.filters | 3 --- 7 files changed, 9 insertions(+), 57 deletions(-) delete mode 100644 test-suite/fdcev.hpp diff --git a/test-suite/CMakeLists.txt b/test-suite/CMakeLists.txt index 52864780f96..c7c20c7b63a 100644 --- a/test-suite/CMakeLists.txt +++ b/test-suite/CMakeLists.txt @@ -174,7 +174,6 @@ set(QL_TEST_SOURCES ) set(QL_TEST_HEADERS - fdcev.hpp fdcir.hpp fdheston.hpp fdmlinearop.hpp diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index 1d986e248b5..aa4168f1034 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -177,7 +177,6 @@ QL_TEST_HDRS = \ fdheston.hpp \ fdcir.hpp \ fdmlinearop.hpp \ - fdcev.hpp \ fdsabr.hpp \ fittedbonddiscountcurve.hpp \ forwardoption.hpp \ diff --git a/test-suite/fdcev.cpp b/test-suite/fdcev.cpp index fe748b2cef9..0030693c269 100644 --- a/test-suite/fdcev.cpp +++ b/test-suite/fdcev.cpp @@ -17,7 +17,7 @@ FOR A PARTICULAR PURPOSE. See the license for more details. */ -#include "fdcev.hpp" +#include "toplevelfixture.hpp" #include "utilities.hpp" #include #include @@ -45,7 +45,11 @@ namespace { }; } -void FdCevTest::testLocalMartingale() { +BOOST_FIXTURE_TEST_SUITE(QuantLibTest, TopLevelFixture) + +BOOST_AUTO_TEST_SUITE(FdCevTest) + +BOOST_AUTO_TEST_CASE(testLocalMartingale) { BOOST_TEST_MESSAGE( "Testing local martingale property of CEV process with PDF..."); @@ -123,7 +127,7 @@ void FdCevTest::testLocalMartingale() { } } -void FdCevTest::testFdmCevOp() { +BOOST_AUTO_TEST_CASE(testFdmCevOp) { BOOST_TEST_MESSAGE( "Testing FDM constant elasticity of variance (CEV) operator..."); @@ -201,13 +205,6 @@ void FdCevTest::testFdmCevOp() { } } +BOOST_AUTO_TEST_SUITE_END() -test_suite* FdCevTest::suite(SpeedLevel speed) { - auto* suite = BOOST_TEST_SUITE("Finite Difference CEV tests"); - - - suite->add(QUANTLIB_TEST_CASE(&FdCevTest::testLocalMartingale)); - suite->add(QUANTLIB_TEST_CASE(&FdCevTest::testFdmCevOp)); - - return suite; -} +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test-suite/fdcev.hpp b/test-suite/fdcev.hpp deleted file mode 100644 index f45aeb918bb..00000000000 --- a/test-suite/fdcev.hpp +++ /dev/null @@ -1,37 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ - -/* - Copyright (C) 2018 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 - . The license is also available online at - . - - 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. -*/ - -#ifndef quantlib_test_fd_vec_hpp -#define quantlib_test_fd_vec_hpp - -#include -#include "speedlevel.hpp" - -/* remember to document new and/or updated tests in the Doxygen - comment block of the corresponding class */ - -class FdCevTest { - public: - static void testLocalMartingale(); - static void testFdmCevOp(); - - static boost::unit_test_framework::test_suite* suite(SpeedLevel speed); -}; - -#endif diff --git a/test-suite/quantlibtestsuite.cpp b/test-suite/quantlibtestsuite.cpp index 5d4918bb399..b8c59e43137 100644 --- a/test-suite/quantlibtestsuite.cpp +++ b/test-suite/quantlibtestsuite.cpp @@ -36,7 +36,6 @@ # include #endif -#include "fdcev.hpp" #include "fdcir.hpp" #include "fdheston.hpp" #include "fdmlinearop.hpp" @@ -182,7 +181,6 @@ test_suite* init_unit_test_suite(int, char* []) { test->add(CPISwapTest::suite()); test->add(FdHestonTest::suite(speed)); test->add(FdmLinearOpTest::suite(speed)); - test->add(FdCevTest::suite(speed)); test->add(FdCIRTest::suite(speed)); test->add(FdSabrTest::suite(speed)); test->add(FittedBondDiscountCurveTest::suite()); diff --git a/test-suite/testsuite.vcxproj b/test-suite/testsuite.vcxproj index f67c8bd2e13..3a698334530 100644 --- a/test-suite/testsuite.vcxproj +++ b/test-suite/testsuite.vcxproj @@ -810,7 +810,6 @@ - diff --git a/test-suite/testsuite.vcxproj.filters b/test-suite/testsuite.vcxproj.filters index 75e25f7ea9b..4b3f40c6a4b 100644 --- a/test-suite/testsuite.vcxproj.filters +++ b/test-suite/testsuite.vcxproj.filters @@ -842,9 +842,6 @@ Header Files - - Header Files - Header Files From 43511ea9556076b2791f2c4ba32dda760b171310 Mon Sep 17 00:00:00 2001 From: siddharthmehrotra Date: Fri, 3 Nov 2023 15:10:35 +0800 Subject: [PATCH 02/37] Migrated fdcir.cpp --- test-suite/CMakeLists.txt | 1 - test-suite/Makefile.am | 1 - test-suite/fdcir.cpp | 16 ++++++------- test-suite/fdcir.hpp | 36 ---------------------------- test-suite/quantlibtestsuite.cpp | 2 -- test-suite/testsuite.vcxproj | 1 - test-suite/testsuite.vcxproj.filters | 3 --- 7 files changed, 8 insertions(+), 52 deletions(-) delete mode 100644 test-suite/fdcir.hpp diff --git a/test-suite/CMakeLists.txt b/test-suite/CMakeLists.txt index c7c20c7b63a..fa588b6077f 100644 --- a/test-suite/CMakeLists.txt +++ b/test-suite/CMakeLists.txt @@ -174,7 +174,6 @@ set(QL_TEST_SOURCES ) set(QL_TEST_HEADERS - fdcir.hpp fdheston.hpp fdmlinearop.hpp fdsabr.hpp diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index aa4168f1034..8e6531d6550 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -175,7 +175,6 @@ QL_TEST_SRCS = \ QL_TEST_HDRS = \ fdheston.hpp \ - fdcir.hpp \ fdmlinearop.hpp \ fdsabr.hpp \ fittedbonddiscountcurve.hpp \ diff --git a/test-suite/fdcir.cpp b/test-suite/fdcir.cpp index ca2aff9f695..55a196ccb4f 100644 --- a/test-suite/fdcir.cpp +++ b/test-suite/fdcir.cpp @@ -17,7 +17,7 @@ FOR A PARTICULAR PURPOSE. See the license for more details. */ -#include "fdcir.hpp" +#include "toplevelfixture.hpp" #include "fdheston.hpp" #include "utilities.hpp" #include @@ -35,7 +35,11 @@ using namespace QuantLib; using boost::unit_test_framework::test_suite; -void FdCIRTest::testFdmCIRConvergence() { +BOOST_FIXTURE_TEST_SUITE(QuantLibTest, TopLevelFixture) + +BOOST_AUTO_TEST_SUITE(FdCIRTest) + +BOOST_AUTO_TEST_CASE(testFdmCIRConvergence) { BOOST_TEST_MESSAGE("Testing FDM CIR convergence..."); FdmSchemeDesc schemes[] = { @@ -108,11 +112,7 @@ void FdCIRTest::testFdmCIRConvergence() { } } -test_suite* FdCIRTest::suite(SpeedLevel speed) { - auto* suite = BOOST_TEST_SUITE("Finite Difference CIR tests"); +BOOST_AUTO_TEST_SUITE_END() - suite->add(QUANTLIB_TEST_CASE(&FdCIRTest::testFdmCIRConvergence)); - - return suite; -} +BOOST_AUTO_TEST_SUITE_END() diff --git a/test-suite/fdcir.hpp b/test-suite/fdcir.hpp deleted file mode 100644 index 12e454e983a..00000000000 --- a/test-suite/fdcir.hpp +++ /dev/null @@ -1,36 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ - -/* - Copyright (C) 2020 Lew Wei Hao - - 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 - . The license is also available online at - . - - 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. -*/ - -#ifndef quantlib_test_fd_cir_hpp -#define quantlib_test_fd_cir_hpp - -#include -#include "speedlevel.hpp" - -/* remember to document new and/or updated tests in the Doxygen - comment block of the corresponding class */ - -class FdCIRTest { -public: - static void testFdmCIRConvergence(); - - static boost::unit_test_framework::test_suite* suite(SpeedLevel); -}; - -#endif diff --git a/test-suite/quantlibtestsuite.cpp b/test-suite/quantlibtestsuite.cpp index b8c59e43137..bcd0a23a04e 100644 --- a/test-suite/quantlibtestsuite.cpp +++ b/test-suite/quantlibtestsuite.cpp @@ -36,7 +36,6 @@ # include #endif -#include "fdcir.hpp" #include "fdheston.hpp" #include "fdmlinearop.hpp" #include "fdsabr.hpp" @@ -181,7 +180,6 @@ test_suite* init_unit_test_suite(int, char* []) { test->add(CPISwapTest::suite()); test->add(FdHestonTest::suite(speed)); test->add(FdmLinearOpTest::suite(speed)); - test->add(FdCIRTest::suite(speed)); test->add(FdSabrTest::suite(speed)); test->add(FittedBondDiscountCurveTest::suite()); test->add(ForwardOptionTest::suite(speed)); diff --git a/test-suite/testsuite.vcxproj b/test-suite/testsuite.vcxproj index 3a698334530..63762feef4f 100644 --- a/test-suite/testsuite.vcxproj +++ b/test-suite/testsuite.vcxproj @@ -814,7 +814,6 @@ - diff --git a/test-suite/testsuite.vcxproj.filters b/test-suite/testsuite.vcxproj.filters index 4b3f40c6a4b..69a96b037a2 100644 --- a/test-suite/testsuite.vcxproj.filters +++ b/test-suite/testsuite.vcxproj.filters @@ -533,9 +533,6 @@ Header Files - - Header Files - Header Files From 3e2498c32b00275894b779d24c5ffd46a151b040 Mon Sep 17 00:00:00 2001 From: siddharthmehrotra Date: Fri, 3 Nov 2023 15:21:51 +0800 Subject: [PATCH 03/37] Migrated fdheston.cpp --- test-suite/CMakeLists.txt | 5 +- test-suite/Makefile.am | 2 - test-suite/fdcir.cpp | 1 - test-suite/fdheston.cpp | 90 +++++++++++----------------- test-suite/fdheston.hpp | 48 --------------- test-suite/quantlibbenchmark.cpp | 8 ++- test-suite/quantlibtestsuite.cpp | 2 - test-suite/testsuite.vcxproj | 1 - test-suite/testsuite.vcxproj.filters | 3 - 9 files changed, 42 insertions(+), 118 deletions(-) delete mode 100644 test-suite/fdheston.hpp diff --git a/test-suite/CMakeLists.txt b/test-suite/CMakeLists.txt index fa588b6077f..e2bc59e4010 100644 --- a/test-suite/CMakeLists.txt +++ b/test-suite/CMakeLists.txt @@ -174,7 +174,6 @@ set(QL_TEST_SOURCES ) set(QL_TEST_HEADERS - fdheston.hpp fdmlinearop.hpp fdsabr.hpp fittedbonddiscountcurve.hpp @@ -298,8 +297,8 @@ set(QL_BENCHMARK_SOURCES digitaloption.cpp dividendoption.cpp europeanoption.cpp - fdheston.cpp fdheston.hpp - hestonmodel.cpp hestonmodel.hpp + fdheston.cpp + hestonmodel.cpp hestonmodel.hpp interpolations.cpp interpolations.hpp jumpdiffusion.cpp jumpdiffusion.hpp lowdiscrepancysequences.cpp lowdiscrepancysequences.hpp diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index 8e6531d6550..cc4f62b7fce 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -174,7 +174,6 @@ QL_TEST_SRCS = \ zerocouponswap.cpp QL_TEST_HDRS = \ - fdheston.hpp \ fdmlinearop.hpp \ fdsabr.hpp \ fittedbonddiscountcurve.hpp \ @@ -313,7 +312,6 @@ QL_BENCHMARK_SRCS = \ utilities.cpp QL_BENCHMARK_HDRS = \ - fdheston.hpp \ hestonmodel.hpp \ interpolations.hpp \ jumpdiffusion.hpp \ diff --git a/test-suite/fdcir.cpp b/test-suite/fdcir.cpp index 55a196ccb4f..189a0f095d5 100644 --- a/test-suite/fdcir.cpp +++ b/test-suite/fdcir.cpp @@ -18,7 +18,6 @@ */ #include "toplevelfixture.hpp" -#include "fdheston.hpp" #include "utilities.hpp" #include #include diff --git a/test-suite/fdheston.cpp b/test-suite/fdheston.cpp index 8dc40325f0a..0db15c41067 100644 --- a/test-suite/fdheston.cpp +++ b/test-suite/fdheston.cpp @@ -18,9 +18,9 @@ FOR A PARTICULAR PURPOSE. See the license for more details. */ -#include "fdheston.hpp" +#include "speedlevel.hpp" +#include "toplevelfixture.hpp" #include "utilities.hpp" - #include #include #include @@ -44,7 +44,7 @@ #include using namespace QuantLib; -using boost::unit_test_framework::test_suite; +using namespace boost::unit_test_framework; namespace fd_heston_test { @@ -89,7 +89,23 @@ namespace fd_heston_test { }; } -void FdHestonTest::testFdmHestonVarianceMesher() { +namespace { + struct HestonTestData { + Real kappa; + Real theta; + Real sigma; + Real rho; + Real r; + Real q; + Real T; + Real K; + }; +} +BOOST_FIXTURE_TEST_SUITE(QuantLibTest, TopLevelFixture) + +BOOST_AUTO_TEST_SUITE(FdHestonTest) + +BOOST_AUTO_TEST_CASE(testFdmHestonVarianceMesher) { BOOST_TEST_MESSAGE("Testing FDM Heston variance mesher..."); using namespace fd_heston_test; @@ -187,7 +203,7 @@ void FdHestonTest::testFdmHestonVarianceMesher() { } } -void FdHestonTest::testFdmHestonBarrierVsBlackScholes() { +BOOST_AUTO_TEST_CASE(testFdmHestonBarrierVsBlackScholes, *precondition(if_speed(Fast))) { BOOST_TEST_MESSAGE("Testing FDM with barrier option in Heston model..."); @@ -336,7 +352,7 @@ void FdHestonTest::testFdmHestonBarrierVsBlackScholes() { } } -void FdHestonTest::testFdmHestonBarrier() { +BOOST_AUTO_TEST_CASE(testFdmHestonBarrier) { BOOST_TEST_MESSAGE("Testing FDM with barrier option for Heston model vs " "Black-Scholes model..."); @@ -388,7 +404,7 @@ void FdHestonTest::testFdmHestonBarrier() { } } -void FdHestonTest::testFdmHestonAmerican() { +BOOST_AUTO_TEST_CASE(testFdmHestonAmerican) { BOOST_TEST_MESSAGE("Testing FDM with American option in Heston model..."); @@ -439,8 +455,7 @@ void FdHestonTest::testFdmHestonAmerican() { } } - -void FdHestonTest::testFdmHestonIkonenToivanen() { +BOOST_AUTO_TEST_CASE(testFdmHestonIkonenToivanen) { BOOST_TEST_MESSAGE("Testing FDM Heston for Ikonen and Toivanen tests..."); @@ -487,7 +502,7 @@ void FdHestonTest::testFdmHestonIkonenToivanen() { } } -void FdHestonTest::testFdmHestonBlackScholes() { +BOOST_AUTO_TEST_CASE(testFdmHestonBlackScholes) { BOOST_TEST_MESSAGE("Testing FDM Heston with Black Scholes model..."); @@ -552,9 +567,7 @@ void FdHestonTest::testFdmHestonBlackScholes() { } } - - -void FdHestonTest::testFdmHestonEuropeanWithDividends() { +BOOST_AUTO_TEST_CASE(testFdmHestonEuropeanWithDividends) { BOOST_TEST_MESSAGE("Testing FDM with European option with dividends in Heston model..."); @@ -635,20 +648,7 @@ void FdHestonTest::testFdmHestonEuropeanWithDividends() { } } -namespace { - struct HestonTestData { - Real kappa; - Real theta; - Real sigma; - Real rho; - Real r; - Real q; - Real T; - Real K; - }; -} - -void FdHestonTest::testFdmHestonConvergence() { +BOOST_AUTO_TEST_CASE(testFdmHestonConvergence, *precondition(if_speed(Fast))) { /* convergence tests based on ADI finite difference schemes for option pricing in the @@ -726,8 +726,8 @@ void FdHestonTest::testFdmHestonConvergence() { } } -void FdHestonTest::testFdmHestonIntradayPricing() { #ifdef QL_HIGH_RESOLUTION_DATE +BOOST_AUTO_TEST_CASE(testFdmHestonIntradayPricing) { BOOST_TEST_MESSAGE("Testing FDM Heston intraday pricing..."); @@ -785,10 +785,10 @@ void FdHestonTest::testFdmHestonIntradayPricing() { << "\n calculated: "<< gammaCalculated); } } -#endif } +#endif -void FdHestonTest::testMethodOfLinesAndCN() { +BOOST_AUTO_TEST_CASE(testMethodOfLinesAndCN) { BOOST_TEST_MESSAGE("Testing method of lines to solve Heston PDEs..."); const DayCounter dc = Actual365Fixed(); @@ -904,7 +904,7 @@ void FdHestonTest::testMethodOfLinesAndCN() { } } -void FdHestonTest::testSpuriousOscillations() { +BOOST_AUTO_TEST_CASE(testSpuriousOscillations) { BOOST_TEST_MESSAGE("Testing for spurious oscillations when " "solving the Heston PDEs..."); @@ -981,8 +981,7 @@ void FdHestonTest::testSpuriousOscillations() { } } - -void FdHestonTest::testAmericanCallPutParity() { +BOOST_AUTO_TEST_CASE(testAmericanCallPutParity) { BOOST_TEST_MESSAGE("Testing call/put parity for American options " "under the Heston model..."); @@ -1090,27 +1089,6 @@ void FdHestonTest::testAmericanCallPutParity() { } } -test_suite* FdHestonTest::suite(SpeedLevel speed) { - auto* suite = BOOST_TEST_SUITE("Finite Difference Heston tests"); - - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonVarianceMesher)); - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonBarrier)); - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonAmerican)); - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonIkonenToivanen)); - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonEuropeanWithDividends)); - #ifdef QL_HIGH_RESOLUTION_DATE - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonIntradayPricing)); - #endif - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testMethodOfLinesAndCN)); - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testSpuriousOscillations)); - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testAmericanCallPutParity)); - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonBlackScholes)); - - if (speed <= Fast) { - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonConvergence)); - suite->add(QUANTLIB_TEST_CASE(&FdHestonTest::testFdmHestonBarrierVsBlackScholes)); - } - - return suite; -} +BOOST_AUTO_TEST_SUITE_END() +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test-suite/fdheston.hpp b/test-suite/fdheston.hpp deleted file mode 100644 index e0bdcccee3c..00000000000 --- a/test-suite/fdheston.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ - -/* - Copyright (C) 2008 Klaus Spanderen - Copyright (C) 2014 Johannes Göttker-Schnetmann - - 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 - . The license is also available online at - . - - 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. -*/ - -#ifndef quantlib_test_fd_heston_hpp -#define quantlib_test_fd_heston_hpp - -#include -#include "speedlevel.hpp" - -/* remember to document new and/or updated tests in the Doxygen - comment block of the corresponding class */ - -class FdHestonTest { -public: - static void testFdmHestonVarianceMesher(); - static void testFdmHestonBarrier(); - static void testFdmHestonBarrierVsBlackScholes(); - static void testFdmHestonAmerican(); - static void testFdmHestonIkonenToivanen(); - static void testFdmHestonEuropeanWithDividends(); - static void testFdmHestonConvergence(); - static void testFdmHestonBlackScholes(); - static void testFdmHestonIntradayPricing(); - static void testMethodOfLinesAndCN(); - static void testSpuriousOscillations(); - static void testAmericanCallPutParity(); - - static boost::unit_test_framework::test_suite* suite(SpeedLevel); -}; - -#endif diff --git a/test-suite/quantlibbenchmark.cpp b/test-suite/quantlibbenchmark.cpp index cdd5bc2fe19..6013c4d0b6f 100644 --- a/test-suite/quantlibbenchmark.cpp +++ b/test-suite/quantlibbenchmark.cpp @@ -137,7 +137,6 @@ #endif #include "utilities.hpp" -#include "fdheston.hpp" #include "hestonmodel.hpp" #include "interpolations.hpp" #include "jumpdiffusion.hpp" @@ -208,6 +207,11 @@ namespace QuantLibTest { struct testFdEngines: public BOOST_AUTO_TEST_CASE_FIXTURE { void test_method(); }; } + + namespace FdHestonTest { + struct testFdmHestonAmerican: + public BOOST_AUTO_TEST_CASE_FIXTURE { void test_method(); }; + } } namespace { @@ -253,7 +257,7 @@ namespace { Benchmark("EuropeanOption::FdMcEngines", std::bind(&QuantLibTest::EuropeanOptionTest::testMcEngines::test_method, QuantLibTest::EuropeanOptionTest::testMcEngines()), 1988.63), Benchmark("EuropeanOption::ImpliedVol", std::bind(&QuantLibTest::EuropeanOptionTest::testImpliedVol::test_method, QuantLibTest::EuropeanOptionTest::testImpliedVol()), 131.51), Benchmark("EuropeanOption::FdEngines", std::bind(&QuantLibTest::EuropeanOptionTest::testFdEngines::test_method, QuantLibTest::EuropeanOptionTest::testFdEngines()), 148.43), - Benchmark("FdHestonTest::testFdmHestonAmerican", &FdHestonTest::testFdmHestonAmerican, 234.21), + Benchmark("FdHestonTest::testFdmHestonAmerican", std::bind(&QuantLibTest::FdHestonTest::testFdmHestonAmerican::test_method, QuantLibTest::FdHestonTest::testFdmHestonAmerican()), 234.21), Benchmark("HestonModel::DAXCalibration", &HestonModelTest::testDAXCalibration, 555.19), Benchmark("InterpolationTest::testSabrInterpolation", &InterpolationTest::testSabrInterpolation, 2266.06), Benchmark("JumpDiffusion::Greeks", &JumpDiffusionTest::testGreeks, 433.77), diff --git a/test-suite/quantlibtestsuite.cpp b/test-suite/quantlibtestsuite.cpp index bcd0a23a04e..72e199c6bd2 100644 --- a/test-suite/quantlibtestsuite.cpp +++ b/test-suite/quantlibtestsuite.cpp @@ -36,7 +36,6 @@ # include #endif -#include "fdheston.hpp" #include "fdmlinearop.hpp" #include "fdsabr.hpp" #include "fittedbonddiscountcurve.hpp" @@ -178,7 +177,6 @@ test_suite* init_unit_test_suite(int, char* []) { auto* test = BOOST_TEST_SUITE("QuantLib test suite"); test->add(CPISwapTest::suite()); - test->add(FdHestonTest::suite(speed)); test->add(FdmLinearOpTest::suite(speed)); test->add(FdSabrTest::suite(speed)); test->add(FittedBondDiscountCurveTest::suite()); diff --git a/test-suite/testsuite.vcxproj b/test-suite/testsuite.vcxproj index 63762feef4f..5b955013580 100644 --- a/test-suite/testsuite.vcxproj +++ b/test-suite/testsuite.vcxproj @@ -813,7 +813,6 @@ - diff --git a/test-suite/testsuite.vcxproj.filters b/test-suite/testsuite.vcxproj.filters index 69a96b037a2..e1b6bb47ecb 100644 --- a/test-suite/testsuite.vcxproj.filters +++ b/test-suite/testsuite.vcxproj.filters @@ -530,9 +530,6 @@ Header Files - - Header Files - Header Files From 3a38262e18fb4e556ea97fa45c341adb30bc1825 Mon Sep 17 00:00:00 2001 From: siddharthmehrotra Date: Fri, 3 Nov 2023 15:32:45 +0800 Subject: [PATCH 04/37] Migrated fdmlinearop.cpp --- test-suite/CMakeLists.txt | 1 - test-suite/Makefile.am | 1 - test-suite/fdmlinearop.cpp | 360 ++++++++++++--------------- test-suite/fdmlinearop.hpp | 56 ----- test-suite/quantlibtestsuite.cpp | 2 - test-suite/testsuite.vcxproj | 1 - test-suite/testsuite.vcxproj.filters | 3 - 7 files changed, 162 insertions(+), 262 deletions(-) delete mode 100644 test-suite/fdmlinearop.hpp diff --git a/test-suite/CMakeLists.txt b/test-suite/CMakeLists.txt index e2bc59e4010..72031ce1eef 100644 --- a/test-suite/CMakeLists.txt +++ b/test-suite/CMakeLists.txt @@ -174,7 +174,6 @@ set(QL_TEST_SOURCES ) set(QL_TEST_HEADERS - fdmlinearop.hpp fdsabr.hpp fittedbonddiscountcurve.hpp forwardoption.hpp diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index cc4f62b7fce..896e0f74231 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -174,7 +174,6 @@ QL_TEST_SRCS = \ zerocouponswap.cpp QL_TEST_HDRS = \ - fdmlinearop.hpp \ fdsabr.hpp \ fittedbonddiscountcurve.hpp \ forwardoption.hpp \ diff --git a/test-suite/fdmlinearop.cpp b/test-suite/fdmlinearop.cpp index a8ef6f9a9f3..997de4d8cb7 100644 --- a/test-suite/fdmlinearop.cpp +++ b/test-suite/fdmlinearop.cpp @@ -19,9 +19,9 @@ FOR A PARTICULAR PURPOSE. See the license for more details. */ -#include "fdmlinearop.hpp" +#include "speedlevel.hpp" +#include "toplevelfixture.hpp" #include "utilities.hpp" - #include #include #include @@ -133,7 +133,145 @@ namespace { } -void FdmLinearOpTest::testFdmLinearOpLayout() { +namespace { + + ext::shared_ptr createHestonHullWhite( + Time maturity) { + + DayCounter dc = Actual365Fixed(); + const Date today = Settings::instance().evaluationDate(); + Handle s0(ext::shared_ptr(new SimpleQuote(100.0))); + + std::vector dates; + std::vector rates, divRates; + + for (Size i=0; i <= 25; ++i) { + dates.push_back(today+Period(i, Years)); + rates.push_back(0.05); + divRates.push_back(0.02); + } + + const Handle rTS( + ext::shared_ptr(new ZeroCurve(dates, rates, dc))); + const Handle qTS( + ext::shared_ptr( + new ZeroCurve(dates, divRates, dc))); + + const Real v0 = 0.04; + ext::shared_ptr hestonProcess( + new HestonProcess(rTS, qTS, s0, v0, 1.0, v0*0.75, 0.4, -0.7)); + + ext::shared_ptr hwFwdProcess( + new HullWhiteForwardProcess(rTS, 0.00883, 0.01)); + hwFwdProcess->setForwardMeasureTime(maturity); + + const Real equityShortRateCorr = -0.7; + + return ext::make_shared( + hestonProcess, hwFwdProcess, + equityShortRateCorr); + } + + FdmSolverDesc createSolverDesc( + const std::vector& dim, + const ext::shared_ptr& process) { + + const Time maturity + = process->hullWhiteProcess()->getForwardMeasureTime(); + + ext::shared_ptr layout(new FdmLinearOpLayout(dim)); + + std::vector > mesher1d = { + ext::shared_ptr( + new Uniform1dMesher(std::log(22.0), std::log(440.0), dim[0])), + ext::shared_ptr( + new FdmHestonVarianceMesher(dim[1], process->hestonProcess(), + maturity)), + ext::shared_ptr( + new Uniform1dMesher(-0.15, 0.15, dim[2])) + }; + + const ext::shared_ptr mesher( + new FdmMesherComposite(mesher1d)); + + const FdmBoundaryConditionSet boundaries; + + std::list > stoppingTimes; + std::list > > stepConditions; + + ext::shared_ptr conditions( + new FdmStepConditionComposite( + std::list >(), + FdmStepConditionComposite::Conditions())); + + ext::shared_ptr payoff( + new PlainVanillaPayoff(Option::Call, 160.0)); + + ext::shared_ptr calculator( + new FdmLogInnerValue(payoff, mesher, 0)); + + const Size tGrid = 100; + const Size dampingSteps = 0; + + FdmSolverDesc desc = { mesher, boundaries, + conditions, calculator, + maturity, tGrid, dampingSteps }; + + return desc; + } + + Array axpy(const boost::numeric::ublas::compressed_matrix& A, + const Array& x) { + + boost::numeric::ublas::vector tmpX(x.size()), y(x.size()); + std::copy(x.begin(), x.end(), tmpX.begin()); + boost::numeric::ublas::axpy_prod(A, tmpX, y); + + return Array(y.begin(), y.end()); + } + + boost::numeric::ublas::compressed_matrix createTestMatrix( + Size n, Size m, Real theta) { + + boost::numeric::ublas::compressed_matrix a(n*m, n*m); + + for (Size i=0; i < n; ++i) { + for (Size j=0; j < m; ++j) { + const Size k = i*m+j; + a(k,k)=1.0; + + if (i > 0 && j > 0 && i mesherX( @@ -537,7 +671,7 @@ void FdmLinearOpTest::testDerivativeWeightsOnNonUniformGrids() { } } -void FdmLinearOpTest::testSecondOrderMixedDerivativesMapApply() { +BOOST_AUTO_TEST_CASE(testSecondOrderMixedDerivativesMapApply) { BOOST_TEST_MESSAGE( "Testing application of second-order mixed-derivatives map..."); @@ -628,11 +762,9 @@ void FdmLinearOpTest::testSecondOrderMixedDerivativesMapApply() { << "\n value " << std::fabs(t[i]-u[i])); } } - - } -void FdmLinearOpTest::testTripleBandMapSolve() { +BOOST_AUTO_TEST_CASE(testTripleBandMapSolve) { BOOST_TEST_MESSAGE("Testing triple-band map solution..."); @@ -711,8 +843,7 @@ void FdmLinearOpTest::testTripleBandMapSolve() { } } - -void FdmLinearOpTest::testFdmHestonBarrier() { +BOOST_AUTO_TEST_CASE(testFdmHestonBarrier) { BOOST_TEST_MESSAGE("Testing FDM with barrier option in Heston model..."); @@ -798,7 +929,7 @@ void FdmLinearOpTest::testFdmHestonBarrier() { } } -void FdmLinearOpTest::testFdmHestonAmerican() { +BOOST_AUTO_TEST_CASE(testFdmHestonAmerican) { BOOST_TEST_MESSAGE("Testing FDM with American option in Heston model..."); @@ -869,7 +1000,7 @@ void FdmLinearOpTest::testFdmHestonAmerican() { } } -void FdmLinearOpTest::testFdmHestonExpress() { +BOOST_AUTO_TEST_CASE(testFdmHestonExpress) { BOOST_TEST_MESSAGE("Testing FDM with express certificate in Heston model..."); @@ -959,96 +1090,7 @@ void FdmLinearOpTest::testFdmHestonExpress() { } } - -namespace { - - ext::shared_ptr createHestonHullWhite( - Time maturity) { - - DayCounter dc = Actual365Fixed(); - const Date today = Settings::instance().evaluationDate(); - Handle s0(ext::shared_ptr(new SimpleQuote(100.0))); - - std::vector dates; - std::vector rates, divRates; - - for (Size i=0; i <= 25; ++i) { - dates.push_back(today+Period(i, Years)); - rates.push_back(0.05); - divRates.push_back(0.02); - } - - const Handle rTS( - ext::shared_ptr(new ZeroCurve(dates, rates, dc))); - const Handle qTS( - ext::shared_ptr( - new ZeroCurve(dates, divRates, dc))); - - const Real v0 = 0.04; - ext::shared_ptr hestonProcess( - new HestonProcess(rTS, qTS, s0, v0, 1.0, v0*0.75, 0.4, -0.7)); - - ext::shared_ptr hwFwdProcess( - new HullWhiteForwardProcess(rTS, 0.00883, 0.01)); - hwFwdProcess->setForwardMeasureTime(maturity); - - const Real equityShortRateCorr = -0.7; - - return ext::make_shared( - hestonProcess, hwFwdProcess, - equityShortRateCorr); - } - - FdmSolverDesc createSolverDesc( - const std::vector& dim, - const ext::shared_ptr& process) { - - const Time maturity - = process->hullWhiteProcess()->getForwardMeasureTime(); - - ext::shared_ptr layout(new FdmLinearOpLayout(dim)); - - std::vector > mesher1d = { - ext::shared_ptr( - new Uniform1dMesher(std::log(22.0), std::log(440.0), dim[0])), - ext::shared_ptr( - new FdmHestonVarianceMesher(dim[1], process->hestonProcess(), - maturity)), - ext::shared_ptr( - new Uniform1dMesher(-0.15, 0.15, dim[2])) - }; - - const ext::shared_ptr mesher( - new FdmMesherComposite(mesher1d)); - - const FdmBoundaryConditionSet boundaries; - - std::list > stoppingTimes; - std::list > > stepConditions; - - ext::shared_ptr conditions( - new FdmStepConditionComposite( - std::list >(), - FdmStepConditionComposite::Conditions())); - - ext::shared_ptr payoff( - new PlainVanillaPayoff(Option::Call, 160.0)); - - ext::shared_ptr calculator( - new FdmLogInnerValue(payoff, mesher, 0)); - - const Size tGrid = 100; - const Size dampingSteps = 0; - - FdmSolverDesc desc = { mesher, boundaries, - conditions, calculator, - maturity, tGrid, dampingSteps }; - - return desc; - } -} - -void FdmLinearOpTest::testFdmHestonHullWhiteOp() { +BOOST_AUTO_TEST_CASE(testFdmHestonHullWhiteOp, *precondition(if_speed(Fast))) { BOOST_TEST_MESSAGE("Testing FDM with Heston Hull-White model..."); const Date today = Date(28, March, 2004); @@ -1164,47 +1206,7 @@ void FdmLinearOpTest::testFdmHestonHullWhiteOp() { } } -namespace { - Array axpy(const boost::numeric::ublas::compressed_matrix& A, - const Array& x) { - - boost::numeric::ublas::vector tmpX(x.size()), y(x.size()); - std::copy(x.begin(), x.end(), tmpX.begin()); - boost::numeric::ublas::axpy_prod(A, tmpX, y); - - return Array(y.begin(), y.end()); - } - - boost::numeric::ublas::compressed_matrix createTestMatrix( - Size n, Size m, Real theta) { - - boost::numeric::ublas::compressed_matrix a(n*m, n*m); - - for (Size i=0; i < n; ++i) { - for (Size j=0; j < m; ++j) { - const Size k = i*m+j; - a(k,k)=1.0; - - if (i > 0 && j > 0 && i mesher( @@ -1514,7 +1503,7 @@ void FdmLinearOpTest::testFdmMesherIntegral() { } } -void FdmLinearOpTest::testHighInterestRateBlackScholesMesher() { +BOOST_AUTO_TEST_CASE(testHighInterestRateBlackScholesMesher) { BOOST_TEST_MESSAGE("Testing Black-Scholes mesher in a " "high interest rate scenario..."); @@ -1577,7 +1566,7 @@ void FdmLinearOpTest::testHighInterestRateBlackScholesMesher() { } } -void FdmLinearOpTest::testLowVolatilityHighDiscreteDividendBlackScholesMesher() { +BOOST_AUTO_TEST_CASE(testLowVolatilityHighDiscreteDividendBlackScholesMesher) { BOOST_TEST_MESSAGE("Testing Black-Scholes mesher in a low volatility and " "high discrete dividend scenario..."); @@ -1651,31 +1640,6 @@ void FdmLinearOpTest::testLowVolatilityHighDiscreteDividendBlackScholesMesher() } } -test_suite* FdmLinearOpTest::suite(SpeedLevel speed) { - auto* suite = BOOST_TEST_SUITE("linear operator tests"); - - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testFdmLinearOpLayout)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testUniformGridMesher)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testFirstDerivativesMapApply)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testSecondDerivativesMapApply)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testDerivativeWeightsOnNonUniformGrids)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testSecondOrderMixedDerivativesMapApply)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testTripleBandMapSolve)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testFdmHestonBarrier)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testFdmHestonAmerican)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testFdmHestonExpress)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testBiCGstab)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testGMRES)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testCrankNicolsonWithDamping)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testSpareMatrixReference)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testSparseMatrixZeroAssignment)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testFdmMesherIntegral)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testHighInterestRateBlackScholesMesher)); - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testLowVolatilityHighDiscreteDividendBlackScholesMesher)); - - if (speed <= Fast) { - suite->add(QUANTLIB_TEST_CASE(&FdmLinearOpTest::testFdmHestonHullWhiteOp)); - } - - return suite; -} +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test-suite/fdmlinearop.hpp b/test-suite/fdmlinearop.hpp deleted file mode 100644 index 2292ea842fa..00000000000 --- a/test-suite/fdmlinearop.hpp +++ /dev/null @@ -1,56 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ - -/* - Copyright (C) 2008 Andreas Gaida - Copyright (C) 2008 Ralph Schreyer - Copyright (C) 2008, 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 - . The license is also available online at - . - - 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. -*/ - -#ifndef quantlib_test_fdm_linear_op_hpp -#define quantlib_test_fdm_linear_op_hpp - -#include -#include "speedlevel.hpp" - -/* remember to document new and/or updated tests in the Doxygen - comment block of the corresponding class */ - -class FdmLinearOpTest { -public: - static void testFdmLinearOpLayout(); - static void testUniformGridMesher(); - static void testFirstDerivativesMapApply(); - static void testSecondDerivativesMapApply(); - static void testDerivativeWeightsOnNonUniformGrids(); - static void testSecondOrderMixedDerivativesMapApply(); - static void testTripleBandMapSolve(); - static void testFdmHestonBarrier(); - static void testFdmHestonAmerican(); - static void testFdmHestonExpress(); - static void testFdmHestonHullWhiteOp(); - static void testBiCGstab(); - static void testGMRES(); - static void testCrankNicolsonWithDamping(); - static void testSpareMatrixReference(); - static void testSparseMatrixZeroAssignment(); - static void testFdmMesherIntegral(); - static void testHighInterestRateBlackScholesMesher(); - static void testLowVolatilityHighDiscreteDividendBlackScholesMesher(); - - static boost::unit_test_framework::test_suite* suite(SpeedLevel); -}; - -#endif diff --git a/test-suite/quantlibtestsuite.cpp b/test-suite/quantlibtestsuite.cpp index 72e199c6bd2..365b14535e8 100644 --- a/test-suite/quantlibtestsuite.cpp +++ b/test-suite/quantlibtestsuite.cpp @@ -36,7 +36,6 @@ # include #endif -#include "fdmlinearop.hpp" #include "fdsabr.hpp" #include "fittedbonddiscountcurve.hpp" #include "forwardoption.hpp" @@ -177,7 +176,6 @@ test_suite* init_unit_test_suite(int, char* []) { auto* test = BOOST_TEST_SUITE("QuantLib test suite"); test->add(CPISwapTest::suite()); - test->add(FdmLinearOpTest::suite(speed)); test->add(FdSabrTest::suite(speed)); test->add(FittedBondDiscountCurveTest::suite()); test->add(ForwardOptionTest::suite(speed)); diff --git a/test-suite/testsuite.vcxproj b/test-suite/testsuite.vcxproj index 5b955013580..058914181b0 100644 --- a/test-suite/testsuite.vcxproj +++ b/test-suite/testsuite.vcxproj @@ -813,7 +813,6 @@ - diff --git a/test-suite/testsuite.vcxproj.filters b/test-suite/testsuite.vcxproj.filters index e1b6bb47ecb..cb3773e53dd 100644 --- a/test-suite/testsuite.vcxproj.filters +++ b/test-suite/testsuite.vcxproj.filters @@ -530,9 +530,6 @@ Header Files - - Header Files - Header Files From 68e0e4e904bd4bc6dcfcc3c56946eea59b7ba027 Mon Sep 17 00:00:00 2001 From: siddharthmehrotra Date: Fri, 3 Nov 2023 15:37:36 +0800 Subject: [PATCH 05/37] Migrated fdsabr.cpp --- test-suite/CMakeLists.txt | 1 - test-suite/Makefile.am | 1 - test-suite/fdsabr.cpp | 103 ++++++++++++--------------- test-suite/fdsabr.hpp | 40 ----------- test-suite/quantlibtestsuite.cpp | 2 - test-suite/testsuite.vcxproj | 1 - test-suite/testsuite.vcxproj.filters | 3 - 7 files changed, 46 insertions(+), 105 deletions(-) delete mode 100644 test-suite/fdsabr.hpp diff --git a/test-suite/CMakeLists.txt b/test-suite/CMakeLists.txt index 72031ce1eef..adf883816c3 100644 --- a/test-suite/CMakeLists.txt +++ b/test-suite/CMakeLists.txt @@ -174,7 +174,6 @@ set(QL_TEST_SOURCES ) set(QL_TEST_HEADERS - fdsabr.hpp fittedbonddiscountcurve.hpp forwardoption.hpp forwardrateagreement.hpp diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index 896e0f74231..db7c4d5338a 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -174,7 +174,6 @@ QL_TEST_SRCS = \ zerocouponswap.cpp QL_TEST_HDRS = \ - fdsabr.hpp \ fittedbonddiscountcurve.hpp \ forwardoption.hpp \ forwardrateagreement.hpp \ diff --git a/test-suite/fdsabr.cpp b/test-suite/fdsabr.cpp index 8060f7f82fd..50b3ed7c119 100644 --- a/test-suite/fdsabr.cpp +++ b/test-suite/fdsabr.cpp @@ -17,7 +17,8 @@ FOR A PARTICULAR PURPOSE. See the license for more details. */ -#include "fdsabr.hpp" +#include "speedlevel.hpp" +#include "toplevelfixture.hpp" #include "utilities.hpp" #include #include @@ -36,7 +37,7 @@ #include using namespace QuantLib; -using boost::unit_test_framework::test_suite; +using namespace boost::unit_test_framework; namespace { class SabrMonteCarloPricer { @@ -101,10 +102,46 @@ namespace { const Real alpha_, beta_, nu_, rho_; }; + /* + * Example and reference values are taken from + * B. Chen, C.W. Oosterlee, H. Weide, + * Efficient unbiased simulation scheme for the SABR stochastic volatility model. + * https://http://ta.twi.tudelft.nl/mf/users/oosterle/oosterlee/SABRMC.pdf + */ + + class OsterleeReferenceResults { + public: + explicit OsterleeReferenceResults(Size i) : i_(i) { } + + Real operator()(Real t) const { + Size i; + if (close_enough(t, 1/16.)) + i = 0; + else if (close_enough(t, 1/32.)) + i = 1; + else + QL_FAIL("unmatched reference result lookup"); + + return data_[i_][i]; + } + + private: + const Size i_; + static Real data_[9][3]; + }; + + Real OsterleeReferenceResults::data_[9][3] = { + { 0.0610, 0.0604 }, { 0.0468, 0.0463 }, { 0.0347, 0.0343 }, + { 0.0632, 0.0625 }, { 0.0512, 0.0506 }, { 0.0406, 0.0400 }, + { 0.0635, 0.0630 }, { 0.0523, 0.0520 }, { 0.0422, 0.0421 } + }; } +BOOST_FIXTURE_TEST_SUITE(QuantLibTest, TopLevelFixture) + +BOOST_AUTO_TEST_SUITE(FdSabrTest) -void FdSabrTest::testFdmSabrOp() { +BOOST_AUTO_TEST_CASE(testFdmSabrOp, *precondition(if_speed(Fast))) { BOOST_TEST_MESSAGE("Testing FDM SABR operator..."); const Date today = Date(22, February, 2018); @@ -198,7 +235,7 @@ void FdSabrTest::testFdmSabrOp() { } } -void FdSabrTest::testFdmSabrCevPricing() { +BOOST_AUTO_TEST_CASE(testFdmSabrCevPricing) { BOOST_TEST_MESSAGE("Testing FDM CEV pricing with trivial SABR model..."); const Date today = Date(3, January, 2019); @@ -261,7 +298,7 @@ void FdSabrTest::testFdmSabrCevPricing() { } } -void FdSabrTest::testFdmSabrVsVolApproximation() { +BOOST_AUTO_TEST_CASE(testFdmSabrVsVolApproximation) { BOOST_TEST_MESSAGE("Testing FDM SABR vs approximations..."); const Date today = Date(8, January, 2019); @@ -321,44 +358,7 @@ void FdSabrTest::testFdmSabrVsVolApproximation() { } } - -namespace { - /* - * Example and reference values are taken from - * B. Chen, C.W. Oosterlee, H. Weide, - * Efficient unbiased simulation scheme for the SABR stochastic volatility model. - * https://http://ta.twi.tudelft.nl/mf/users/oosterle/oosterlee/SABRMC.pdf - */ - - class OsterleeReferenceResults { - public: - explicit OsterleeReferenceResults(Size i) : i_(i) { } - - Real operator()(Real t) const { - Size i; - if (close_enough(t, 1/16.)) - i = 0; - else if (close_enough(t, 1/32.)) - i = 1; - else - QL_FAIL("unmatched reference result lookup"); - - return data_[i_][i]; - } - - private: - const Size i_; - static Real data_[9][3]; - }; - - Real OsterleeReferenceResults::data_[9][3] = { - { 0.0610, 0.0604 }, { 0.0468, 0.0463 }, { 0.0347, 0.0343 }, - { 0.0632, 0.0625 }, { 0.0512, 0.0506 }, { 0.0406, 0.0400 }, - { 0.0635, 0.0630 }, { 0.0523, 0.0520 }, { 0.0422, 0.0421 } - }; -} - -void FdSabrTest::testOosterleeTestCaseIV() { +BOOST_AUTO_TEST_CASE(testOosterleeTestCaseIV) { BOOST_TEST_MESSAGE("Testing Chen, Oosterlee and Weide test case IV..."); const Date today = Date(8, January, 2019); @@ -423,7 +423,7 @@ void FdSabrTest::testOosterleeTestCaseIV() { } } -void FdSabrTest::testBenchOpSabrCase() { +BOOST_AUTO_TEST_CASE(testBenchOpSabrCase) { BOOST_TEST_MESSAGE("Testing SABR BenchOp problem..."); /* @@ -508,17 +508,6 @@ void FdSabrTest::testBenchOpSabrCase() { } } -test_suite* FdSabrTest::suite(SpeedLevel speed) { - auto* suite = BOOST_TEST_SUITE("Finite Difference SABR tests"); +BOOST_AUTO_TEST_SUITE_END() - suite->add(QUANTLIB_TEST_CASE(&FdSabrTest::testFdmSabrCevPricing)); - suite->add(QUANTLIB_TEST_CASE(&FdSabrTest::testFdmSabrVsVolApproximation)); - suite->add(QUANTLIB_TEST_CASE(&FdSabrTest::testOosterleeTestCaseIV)); - suite->add(QUANTLIB_TEST_CASE(&FdSabrTest::testBenchOpSabrCase)); - - if (speed <= Fast) { - suite->add(QUANTLIB_TEST_CASE(&FdSabrTest::testFdmSabrOp)); - } - - return suite; -} +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test-suite/fdsabr.hpp b/test-suite/fdsabr.hpp deleted file mode 100644 index d2da8c7fa03..00000000000 --- a/test-suite/fdsabr.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ - -/* - Copyright (C) 2019 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 - . The license is also available online at - . - - 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. -*/ - -#ifndef quantlib_test_fd_sabr_hpp -#define quantlib_test_fd_sabr_hpp - -#include -#include "speedlevel.hpp" - -/* remember to document new and/or updated tests in the Doxygen - comment block of the corresponding class */ - -class FdSabrTest { - public: - static void testFdmSabrOp(); - static void testFdmSabrCevPricing(); - static void testFdmSabrVsVolApproximation(); - static void testOosterleeTestCaseIV(); - static void testBenchOpSabrCase(); - - static boost::unit_test_framework::test_suite* suite(SpeedLevel speed); -}; - -#endif diff --git a/test-suite/quantlibtestsuite.cpp b/test-suite/quantlibtestsuite.cpp index 365b14535e8..3ca7230bc81 100644 --- a/test-suite/quantlibtestsuite.cpp +++ b/test-suite/quantlibtestsuite.cpp @@ -36,7 +36,6 @@ # include #endif -#include "fdsabr.hpp" #include "fittedbonddiscountcurve.hpp" #include "forwardoption.hpp" #include "forwardrateagreement.hpp" @@ -176,7 +175,6 @@ test_suite* init_unit_test_suite(int, char* []) { auto* test = BOOST_TEST_SUITE("QuantLib test suite"); test->add(CPISwapTest::suite()); - test->add(FdSabrTest::suite(speed)); test->add(FittedBondDiscountCurveTest::suite()); test->add(ForwardOptionTest::suite(speed)); test->add(ForwardRateAgreementTest::suite()); diff --git a/test-suite/testsuite.vcxproj b/test-suite/testsuite.vcxproj index 058914181b0..22558d2849a 100644 --- a/test-suite/testsuite.vcxproj +++ b/test-suite/testsuite.vcxproj @@ -810,7 +810,6 @@ - diff --git a/test-suite/testsuite.vcxproj.filters b/test-suite/testsuite.vcxproj.filters index cb3773e53dd..dcba7c41be1 100644 --- a/test-suite/testsuite.vcxproj.filters +++ b/test-suite/testsuite.vcxproj.filters @@ -833,9 +833,6 @@ Header Files - - Header Files - Header Files From 5fc4c7fa7dc9d69d18a127532e9d54c7c3e3933b Mon Sep 17 00:00:00 2001 From: siddharthmehrotra Date: Fri, 3 Nov 2023 16:37:32 +0800 Subject: [PATCH 06/37] Migrated fittedbonddiscountcurve.cpp --- test-suite/CMakeLists.txt | 1 - test-suite/Makefile.am | 1 - test-suite/fittedbonddiscountcurve.cpp | 18 ++++++------- test-suite/fittedbonddiscountcurve.hpp | 36 -------------------------- test-suite/quantlibtestsuite.cpp | 2 -- test-suite/testsuite.vcxproj | 1 - test-suite/testsuite.vcxproj.filters | 3 --- 7 files changed, 9 insertions(+), 53 deletions(-) delete mode 100644 test-suite/fittedbonddiscountcurve.hpp diff --git a/test-suite/CMakeLists.txt b/test-suite/CMakeLists.txt index adf883816c3..8fc4535bcae 100644 --- a/test-suite/CMakeLists.txt +++ b/test-suite/CMakeLists.txt @@ -174,7 +174,6 @@ set(QL_TEST_SOURCES ) set(QL_TEST_HEADERS - fittedbonddiscountcurve.hpp forwardoption.hpp forwardrateagreement.hpp functions.hpp diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index db7c4d5338a..b47cfefa6ca 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -174,7 +174,6 @@ QL_TEST_SRCS = \ zerocouponswap.cpp QL_TEST_HDRS = \ - fittedbonddiscountcurve.hpp \ forwardoption.hpp \ forwardrateagreement.hpp \ functions.hpp \ diff --git a/test-suite/fittedbonddiscountcurve.cpp b/test-suite/fittedbonddiscountcurve.cpp index 4c81cd8e65f..ccbe22d6b6d 100644 --- a/test-suite/fittedbonddiscountcurve.cpp +++ b/test-suite/fittedbonddiscountcurve.cpp @@ -17,7 +17,7 @@ FOR A PARTICULAR PURPOSE. See the license for more details. */ -#include "fittedbonddiscountcurve.hpp" +#include "toplevelfixture.hpp" #include "utilities.hpp" #include #include @@ -33,7 +33,11 @@ using namespace QuantLib; using namespace boost::unit_test_framework; -void FittedBondDiscountCurveTest::testEvaluation() { +BOOST_FIXTURE_TEST_SUITE(QuantLibTest, TopLevelFixture) + +BOOST_AUTO_TEST_SUITE(FittedBondDiscountCurveTest) + +BOOST_AUTO_TEST_CASE(testEvaluation) { BOOST_TEST_MESSAGE("Testing that fitted bond curves work as evaluators..."); @@ -65,7 +69,7 @@ void FittedBondDiscountCurveTest::testEvaluation() { BOOST_CHECK_NO_THROW(curve.discount(3.0)); } -void FittedBondDiscountCurveTest::testFlatExtrapolation() { +BOOST_AUTO_TEST_CASE(testFlatExtrapolation) { BOOST_TEST_MESSAGE("Testing fitted bond curve with flat extrapolation..."); @@ -193,10 +197,6 @@ void FittedBondDiscountCurveTest::testFlatExtrapolation() { } +BOOST_AUTO_TEST_SUITE_END() -test_suite* FittedBondDiscountCurveTest::suite() { - auto* suite = BOOST_TEST_SUITE("Fitted bond discount curve tests"); - suite->add(QUANTLIB_TEST_CASE(&FittedBondDiscountCurveTest::testEvaluation)); - suite->add(QUANTLIB_TEST_CASE(&FittedBondDiscountCurveTest::testFlatExtrapolation)); - return suite; -} +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/test-suite/fittedbonddiscountcurve.hpp b/test-suite/fittedbonddiscountcurve.hpp deleted file mode 100644 index 62f12748ca5..00000000000 --- a/test-suite/fittedbonddiscountcurve.hpp +++ /dev/null @@ -1,36 +0,0 @@ -/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ - -/* - Copyright (C) 2019 StatPro Italia srl - - 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 - . The license is also available online at - . - - 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. -*/ - -#ifndef quantlib_test_fitted_bond_discount_curve_hpp -#define quantlib_test_fitted_bond_discount_curve_hpp - -#include - -/* remember to document new and/or updated tests in the Doxygen - comment block of the corresponding class */ - -class FittedBondDiscountCurveTest { - public: - static void testEvaluation(); - static void testFlatExtrapolation(); - static boost::unit_test_framework::test_suite* suite(); -}; - - -#endif diff --git a/test-suite/quantlibtestsuite.cpp b/test-suite/quantlibtestsuite.cpp index 3ca7230bc81..2da079314dd 100644 --- a/test-suite/quantlibtestsuite.cpp +++ b/test-suite/quantlibtestsuite.cpp @@ -36,7 +36,6 @@ # include #endif -#include "fittedbonddiscountcurve.hpp" #include "forwardoption.hpp" #include "forwardrateagreement.hpp" #include "functions.hpp" @@ -175,7 +174,6 @@ test_suite* init_unit_test_suite(int, char* []) { auto* test = BOOST_TEST_SUITE("QuantLib test suite"); test->add(CPISwapTest::suite()); - test->add(FittedBondDiscountCurveTest::suite()); test->add(ForwardOptionTest::suite(speed)); test->add(ForwardRateAgreementTest::suite()); test->add(FunctionsTest::suite()); diff --git a/test-suite/testsuite.vcxproj b/test-suite/testsuite.vcxproj index 22558d2849a..0c430bead1b 100644 --- a/test-suite/testsuite.vcxproj +++ b/test-suite/testsuite.vcxproj @@ -810,7 +810,6 @@ - diff --git a/test-suite/testsuite.vcxproj.filters b/test-suite/testsuite.vcxproj.filters index dcba7c41be1..682623cd2a5 100644 --- a/test-suite/testsuite.vcxproj.filters +++ b/test-suite/testsuite.vcxproj.filters @@ -833,9 +833,6 @@ Header Files - - Header Files - Header Files From 1d2da6b5d365cbe6cf4ef6fb3c53a023080c48b4 Mon Sep 17 00:00:00 2001 From: siddharthmehrotra Date: Fri, 3 Nov 2023 16:42:37 +0800 Subject: [PATCH 07/37] Migrated forwardoption.cpp --- test-suite/CMakeLists.txt | 1 - test-suite/Makefile.am | 1 - test-suite/forwardoption.cpp | 293 ++++++++++++--------------- test-suite/forwardoption.hpp | 43 ---- test-suite/quantlibtestsuite.cpp | 2 - test-suite/testsuite.vcxproj | 1 - test-suite/testsuite.vcxproj.filters | 3 - 7 files changed, 134 insertions(+), 210 deletions(-) delete mode 100644 test-suite/forwardoption.hpp diff --git a/test-suite/CMakeLists.txt b/test-suite/CMakeLists.txt index 8fc4535bcae..8beeb879ab5 100644 --- a/test-suite/CMakeLists.txt +++ b/test-suite/CMakeLists.txt @@ -174,7 +174,6 @@ set(QL_TEST_SOURCES ) set(QL_TEST_HEADERS - forwardoption.hpp forwardrateagreement.hpp functions.hpp garch.hpp diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am index b47cfefa6ca..74df477e56c 100644 --- a/test-suite/Makefile.am +++ b/test-suite/Makefile.am @@ -174,7 +174,6 @@ QL_TEST_SRCS = \ zerocouponswap.cpp QL_TEST_HDRS = \ - forwardoption.hpp \ forwardrateagreement.hpp \ functions.hpp \ garch.hpp \ diff --git a/test-suite/forwardoption.cpp b/test-suite/forwardoption.cpp index 1d5a4cb600e..9ab88635e12 100644 --- a/test-suite/forwardoption.cpp +++ b/test-suite/forwardoption.cpp @@ -17,7 +17,8 @@ FOR A PARTICULAR PURPOSE. See the license for more details. */ -#include "forwardoption.hpp" +#include "speedlevel.hpp" +#include "toplevelfixture.hpp" #include "utilities.hpp" #include #include @@ -75,133 +76,6 @@ namespace { Real tol; // tolerance }; -} - - -void ForwardOptionTest::testValues() { - - BOOST_TEST_MESSAGE("Testing forward option values..."); - - /* The data below are from - "Option pricing formulas", E.G. Haug, McGraw-Hill 1998 - */ - ForwardOptionData values[] = { - // type, moneyness, spot, div, rate,start, t, vol, result, tol - // "Option pricing formulas", pag. 37 - { Option::Call, 1.1, 60.0, 0.04, 0.08, 0.25, 1.0, 0.30, 4.4064, 1.0e-4 }, - // "Option pricing formulas", VBA code - { Option::Put, 1.1, 60.0, 0.04, 0.08, 0.25, 1.0, 0.30, 8.2971, 1.0e-4 } - }; - - DayCounter dc = Actual360(); - Date today = Settings::instance().evaluationDate(); - - ext::shared_ptr spot(new SimpleQuote(0.0)); - ext::shared_ptr qRate(new SimpleQuote(0.0)); - Handle qTS(flatRate(today, qRate, dc)); - ext::shared_ptr rRate(new SimpleQuote(0.0)); - Handle rTS(flatRate(today, rRate, dc)); - ext::shared_ptr vol(new SimpleQuote(0.0)); - Handle volTS(flatVol(today, vol, dc)); - - ext::shared_ptr stochProcess( - new BlackScholesMertonProcess(Handle(spot), - Handle(qTS), - Handle(rTS), - Handle(volTS))); - - ext::shared_ptr engine( - new ForwardVanillaEngine(stochProcess)); - - for (auto& value : values) { - - ext::shared_ptr payoff(new PlainVanillaPayoff(value.type, 0.0)); - Date exDate = today + timeToDays(value.t); - ext::shared_ptr exercise(new EuropeanExercise(exDate)); - Date reset = today + timeToDays(value.start); - - spot->setValue(value.s); - qRate->setValue(value.q); - rRate->setValue(value.r); - vol->setValue(value.v); - - ForwardVanillaOption option(value.moneyness, reset, payoff, exercise); - option.setPricingEngine(engine); - - Real calculated = option.NPV(); - Real error = std::fabs(calculated - value.result); - Real tolerance = 1e-4; - if (error>tolerance) { - REPORT_FAILURE("value", payoff, exercise, value.s, value.q, value.r, today, value.v, - value.moneyness, reset, value.result, calculated, error, tolerance); - } - } -} - - -void ForwardOptionTest::testPerformanceValues() { - - BOOST_TEST_MESSAGE("Testing forward performance option values..."); - - /* The data below are the performance equivalent of the - forward options tested above and taken from - "Option pricing formulas", E.G. Haug, McGraw-Hill 1998 - */ - ForwardOptionData values[] = { - // type, moneyness, spot, div, rate,start, maturity, vol, result, tol - { Option::Call, 1.1, 60.0, 0.04, 0.08, 0.25, 1.0, 0.30, 4.4064/60*std::exp(-0.04*0.25), 1.0e-4 }, - { Option::Put, 1.1, 60.0, 0.04, 0.08, 0.25, 1.0, 0.30, 8.2971/60*std::exp(-0.04*0.25), 1.0e-4 } - }; - - DayCounter dc = Actual360(); - Date today = Settings::instance().evaluationDate(); - - ext::shared_ptr spot(new SimpleQuote(0.0)); - ext::shared_ptr qRate(new SimpleQuote(0.0)); - Handle qTS(flatRate(today, qRate, dc)); - ext::shared_ptr rRate(new SimpleQuote(0.0)); - Handle rTS(flatRate(today, rRate, dc)); - ext::shared_ptr vol(new SimpleQuote(0.0)); - Handle volTS(flatVol(today, vol, dc)); - - ext::shared_ptr stochProcess( - new BlackScholesMertonProcess(Handle(spot), - Handle(qTS), - Handle(rTS), - Handle(volTS))); - - ext::shared_ptr engine( - new ForwardPerformanceVanillaEngine( - stochProcess)); - - for (auto& value : values) { - - ext::shared_ptr payoff(new PlainVanillaPayoff(value.type, 0.0)); - Date exDate = today + timeToDays(value.t); - ext::shared_ptr exercise(new EuropeanExercise(exDate)); - Date reset = today + timeToDays(value.start); - - spot->setValue(value.s); - qRate->setValue(value.q); - rRate->setValue(value.r); - vol->setValue(value.v); - - ForwardVanillaOption option(value.moneyness, reset, payoff, exercise); - option.setPricingEngine(engine); - - Real calculated = option.NPV(); - Real error = std::fabs(calculated - value.result); - Real tolerance = 1e-4; - if (error>tolerance) { - REPORT_FAILURE("value", payoff, exercise, value.s, value.q, value.r, today, value.v, - value.moneyness, reset, value.result, calculated, error, tolerance); - } - } -} - - -namespace { - template