| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 StatPro Italia srl |
| 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 "varianceoption.hpp" |
| 21 | #include "utilities.hpp" |
| 22 | #include <ql/experimental/varianceoption/varianceoption.hpp> |
| 23 | #include <ql/experimental/varianceoption/integralhestonvarianceoptionengine.hpp> |
| 24 | #include <ql/time/daycounters/actual360.hpp> |
| 25 | #include <ql/quotes/simplequote.hpp> |
| 26 | |
| 27 | using namespace QuantLib; |
| 28 | using namespace boost::unit_test_framework; |
| 29 | |
| 30 | void VarianceOptionTest::testIntegralHeston() { |
| 31 | |
| 32 | BOOST_TEST_MESSAGE("Testing variance option with integral Heston engine..." ); |
| 33 | |
| 34 | DayCounter dc = Actual360(); |
| 35 | Date today = Settings::instance().evaluationDate(); |
| 36 | |
| 37 | Handle<Quote> s0(ext::make_shared<SimpleQuote>(args: 1.0)); |
| 38 | Handle<YieldTermStructure> qTS; |
| 39 | ext::shared_ptr<SimpleQuote> rRate(new SimpleQuote(0.0)); |
| 40 | Handle<YieldTermStructure> rTS(flatRate(today, forward: rRate, dc)); |
| 41 | |
| 42 | Real v0 = 2.0; |
| 43 | Real kappa = 2.0; |
| 44 | Real theta = 0.01; |
| 45 | Real sigma = 0.1; |
| 46 | Real rho = -0.5; |
| 47 | |
| 48 | ext::shared_ptr<HestonProcess> process(new HestonProcess(rTS, qTS, s0, |
| 49 | v0, kappa, theta, |
| 50 | sigma, rho)); |
| 51 | ext::shared_ptr<PricingEngine> engine( |
| 52 | new IntegralHestonVarianceOptionEngine(process)); |
| 53 | |
| 54 | Real strike = 0.05; |
| 55 | Real nominal = 1.0; |
| 56 | Time T = 1.5; |
| 57 | Date exDate = today + int(360*T); |
| 58 | |
| 59 | ext::shared_ptr<Payoff> payoff(new PlainVanillaPayoff(Option::Call, |
| 60 | strike)); |
| 61 | |
| 62 | VarianceOption varianceOption1(payoff, nominal, today, exDate); |
| 63 | varianceOption1.setPricingEngine(engine); |
| 64 | |
| 65 | Real calculated = varianceOption1.NPV(); |
| 66 | Real expected = 0.9104619; |
| 67 | Real error = std::fabs(x: calculated-expected); |
| 68 | if (error>1.0e-7) { |
| 69 | BOOST_ERROR( |
| 70 | "Failed to reproduce variance-option price:" |
| 71 | << "\n expected: " << std::setprecision(7) << expected |
| 72 | << "\n calculated: " << std::setprecision(7) << calculated |
| 73 | << "\n error: " << error); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | v0 = 1.5; |
| 78 | kappa = 2.0; |
| 79 | theta = 0.01; |
| 80 | sigma = 0.1; |
| 81 | rho = -0.5; |
| 82 | |
| 83 | process = ext::make_shared<HestonProcess>( |
| 84 | args&: rTS, args&: qTS, args&: s0, args&: v0, args&: kappa, args&: theta, args&: sigma, args&: rho); |
| 85 | engine = ext::shared_ptr<PricingEngine>( |
| 86 | new IntegralHestonVarianceOptionEngine(process)); |
| 87 | |
| 88 | strike = 0.7; |
| 89 | nominal = 1.0; |
| 90 | T = 1.0; |
| 91 | exDate = today + int(360*T); |
| 92 | |
| 93 | payoff = ext::shared_ptr<Payoff>(new PlainVanillaPayoff(Option::Put, |
| 94 | strike)); |
| 95 | |
| 96 | VarianceOption varianceOption2(payoff, nominal, today, exDate); |
| 97 | varianceOption2.setPricingEngine(engine); |
| 98 | |
| 99 | calculated = varianceOption2.NPV(); |
| 100 | expected = 0.0466796; |
| 101 | error = std::fabs(x: calculated-expected); |
| 102 | if (error>1.0e-7) { |
| 103 | BOOST_ERROR( |
| 104 | "Failed to reproduce variance-option price:" |
| 105 | << "\n expected: " << std::setprecision(7) << expected |
| 106 | << "\n calculated: " << std::setprecision(7) << calculated |
| 107 | << "\n error: " << error); |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | test_suite* VarianceOptionTest::suite() { |
| 113 | auto* suite = BOOST_TEST_SUITE("Variance option tests" ); |
| 114 | |
| 115 | suite->add(QUANTLIB_TEST_CASE(&VarianceOptionTest::testIntegralHeston)); |
| 116 | return suite; |
| 117 | } |
| 118 | |
| 119 | |