/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2004 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. */ #include "cliquetoption.hpp" #include "utilities.hpp" #include #include #include #include #include #include #include #include #include #include #include using namespace QuantLib; using namespace boost::unit_test_framework; #define REPORT_FAILURE(greekName, payoff, exercise, s, q, r, today, v, \ expected, calculated, error, tolerance) \ BOOST_FAIL(payoff->optionType() << " option:\n" \ << " spot value: " << s << "\n" \ << " moneyness: " << payoff->strike() << "\n" \ << " dividend yield: " << io::rate(q) << "\n" \ << " risk-free rate: " << io::rate(r) << "\n" \ << " reference date: " << today << "\n" \ << " maturity: " << exercise->lastDate() << "\n" \ << " volatility: " << io::volatility(v) << "\n\n" \ << " expected " << greekName << ": " << expected << "\n" \ << " calculated " << greekName << ": " << calculated << "\n"\ << " error: " << error << "\n" \ << " tolerance: " << tolerance); QL_BEGIN_TEST_LOCALS(CliquetOptionTest) void teardown() { Settings::instance().evaluationDate() = Date(); } QL_END_TEST_LOCALS(CliquetOptionTest) void CliquetOptionTest::testValues() { BOOST_MESSAGE("Testing Cliquet option values..."); Date today = Date::todaysDate(); DayCounter dc = Actual360(); boost::shared_ptr spot(new SimpleQuote(60.0)); boost::shared_ptr qRate(new SimpleQuote(0.04)); boost::shared_ptr qTS = flatRate(today, qRate, dc); boost::shared_ptr rRate(new SimpleQuote(0.08)); boost::shared_ptr rTS = flatRate(today, rRate, dc); boost::shared_ptr vol(new SimpleQuote(0.30)); boost::shared_ptr volTS = flatVol(today, vol, dc); boost::shared_ptr engine(new AnalyticCliquetEngine); boost::shared_ptr process( new BlackScholesMertonProcess(Handle(spot), Handle(qTS), Handle(rTS), Handle(volTS))); std::vector reset; reset.push_back(today + 90); Date maturity = today + 360; Option::Type type = Option::Call; Real moneyness = 1.1; boost::shared_ptr payoff( new PercentageStrikePayoff(type, moneyness)); boost::shared_ptr exercise( new EuropeanExercise(maturity)); CliquetOption option(process, payoff, exercise, reset, engine); Real calculated = option.NPV(); Real expected = 4.4064; // Haug, p.37 Real error = std::fabs(calculated-expected); Real tolerance = 1e-4; if (error > tolerance) { REPORT_FAILURE("value", payoff, exercise, spot->value(), qRate->value(), rRate->value(), today, vol->value(), expected, calculated, error, tolerance); } } QL_BEGIN_TEST_LOCALS(CliquetOptionTest) template void testOptionGreeks() { std::map calculated, expected, tolerance; tolerance["delta"] = 1.0e-5; tolerance["gamma"] = 1.0e-5; tolerance["theta"] = 1.0e-5; tolerance["rho"] = 1.0e-5; tolerance["divRho"] = 1.0e-5; tolerance["vega"] = 1.0e-5; Option::Type types[] = { Option::Call, Option::Put }; Real moneyness[] = { 0.9, 1.0, 1.1 }; Real underlyings[] = { 100.0 }; Rate qRates[] = { 0.04, 0.05, 0.06 }; Rate rRates[] = { 0.01, 0.05, 0.15 }; Integer lengths[] = { 1, 2 }; Frequency frequencies[] = { Semiannual, Quarterly }; Volatility vols[] = { 0.11, 0.50, 1.20 }; DayCounter dc = Actual360(); Date today = Date::todaysDate(); Settings::instance().evaluationDate() = today; boost::shared_ptr spot(new SimpleQuote(0.0)); boost::shared_ptr qRate(new SimpleQuote(0.0)); Handle qTS(flatRate(qRate, dc)); boost::shared_ptr rRate(new SimpleQuote(0.0)); Handle rTS(flatRate(rRate, dc)); boost::shared_ptr vol(new SimpleQuote(0.0)); Handle volTS(flatVol(vol, dc)); boost::shared_ptr process( new BlackScholesMertonProcess(Handle(spot), qTS, rTS, volTS)); for (Size i=0; i maturity( new EuropeanExercise(today + lengths[k]*Years)); boost::shared_ptr payoff( new PercentageStrikePayoff(types[i], moneyness[j])); std::vector reset; for (Date d = today + Period(frequencies[kk]); d < maturity->lastDate(); d += Period(frequencies[kk])) reset.push_back(d); boost::shared_ptr engine(new T); CliquetOption option(process, payoff, maturity, reset, engine); for (Size l=0; lsetValue(u); qRate->setValue(q); rRate->setValue(r); vol->setValue(v); Real value = option.NPV(); calculated["delta"] = option.delta(); calculated["gamma"] = option.gamma(); calculated["theta"] = option.theta(); calculated["rho"] = option.rho(); calculated["divRho"] = option.dividendRho(); calculated["vega"] = option.vega(); if (value > spot->value()*1.0e-5) { // perturb spot and get delta and gamma Real du = u*1.0e-4; spot->setValue(u+du); Real value_p = option.NPV(), delta_p = option.delta(); spot->setValue(u-du); Real value_m = option.NPV(), delta_m = option.delta(); spot->setValue(u); expected["delta"] = (value_p - value_m)/(2*du); expected["gamma"] = (delta_p - delta_m)/(2*du); // perturb rates and get rho and dividend rho Spread dr = r*1.0e-4; rRate->setValue(r+dr); value_p = option.NPV(); rRate->setValue(r-dr); value_m = option.NPV(); rRate->setValue(r); expected["rho"] = (value_p - value_m)/(2*dr); Spread dq = q*1.0e-4; qRate->setValue(q+dq); value_p = option.NPV(); qRate->setValue(q-dq); value_m = option.NPV(); qRate->setValue(q); expected["divRho"] = (value_p - value_m)/(2*dq); // perturb volatility and get vega Volatility dv = v*1.0e-4; vol->setValue(v+dv); value_p = option.NPV(); vol->setValue(v-dv); value_m = option.NPV(); vol->setValue(v); expected["vega"] = (value_p - value_m)/(2*dv); // perturb date and get theta Time dT = dc.yearFraction(today-1, today+1); Settings::instance().evaluationDate() = today-1; value_m = option.NPV(); Settings::instance().evaluationDate() = today+1; value_p = option.NPV(); Settings::instance().evaluationDate() = today; expected["theta"] = (value_p - value_m)/dT; // compare std::map::iterator it; for (it = calculated.begin(); it != calculated.end(); ++it) { std::string greek = it->first; Real expct = expected [greek], calcl = calculated[greek], tol = tolerance [greek]; Real error = relativeError(expct,calcl,u); if (error>tol) { REPORT_FAILURE(greek, payoff, maturity, u, q, r, today, v, expct, calcl, error, tol); } } } } } } } } } } } } QL_END_TEST_LOCALS(CliquetOptionTest) void CliquetOptionTest::testGreeks() { BOOST_MESSAGE("Testing Cliquet option greeks..."); QL_TEST_BEGIN testOptionGreeks(); QL_TEST_TEARDOWN } void CliquetOptionTest::testPerformanceGreeks() { BOOST_MESSAGE("Testing performance option greeks..."); QL_TEST_BEGIN testOptionGreeks(); QL_TEST_TEARDOWN } test_suite* CliquetOptionTest::suite() { test_suite* suite = BOOST_TEST_SUITE("Cliquet option tests"); suite->add(BOOST_TEST_CASE(&CliquetOptionTest::testValues)); suite->add(BOOST_TEST_CASE(&CliquetOptionTest::testGreeks)); suite->add(BOOST_TEST_CASE(&CliquetOptionTest::testPerformanceGreeks)); return suite; }