/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2003 RiskMap 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 "compoundforward.hpp" #include "utilities.hpp" #include #include #include #include #include #include using namespace QuantLib; using namespace boost::unit_test_framework; QL_BEGIN_TEST_LOCALS(CompoundForwardTest) struct Datum { Integer n; TimeUnit units; Rate rate; }; Datum depositData[] = { { 3, Months, 4.557 }, { 6, Months, 4.496 }, { 9, Months, 4.490 } }; Datum swapData[] = { { 1, Years, 4.54 }, { 2, Years, 4.63 }, { 3, Years, 4.75 }, { 4, Years, 4.86 }, { 5, Years, 4.99 }, { 6, Years, 5.11 }, { 7, Years, 5.23 }, { 8, Years, 5.33 }, { 9, Years, 5.41 }, { 10, Years, 5.47 }, { 12, Years, 5.60 }, { 15, Years, 5.75 }, { 20, Years, 5.89 }, { 25, Years, 5.95 }, { 30, Years, 5.96 } }; // test-global variables Calendar calendar; Natural settlementDays; Date today, settlement; BusinessDayConvention convention; DayCounter dayCounter; Frequency frequency; Size deposits, swaps; std::vector rates; std::vector dates; boost::shared_ptr termStructure; void setup() { // data calendar = SouthAfrica(); settlementDays = 0; today = calendar.adjust(Date::todaysDate()); Settings::instance().evaluationDate() = today; settlement = calendar.advance(today,settlementDays,Days); convention = ModifiedFollowing; dayCounter = Actual365Fixed(); frequency = Semiannual; deposits = LENGTH(depositData); swaps = LENGTH(swapData); // market elements rates = std::vector(deposits+swaps); dates = std::vector(deposits+swaps); Size i; for (i=0; i( new CompoundForward(settlement,dates,rates, calendar,convention, frequency,dayCounter)); } void teardown() { Settings::instance().evaluationDate() = Date(); } QL_END_TEST_LOCALS(CompoundForwardTest) void CompoundForwardTest::testSuppliedRates() { BOOST_MESSAGE("Testing consistency of compound-forward curve " "with supplied rates..."); QL_TEST_BEGIN QL_TEST_SETUP Handle liborHandle = Handle(termStructure); Size i; // check swaps against original boost::shared_ptr index(new Jibar(Period(frequency), liborHandle)); for (i=0; idayCounter(), liborHandle); Rate expectedRate = swapData[i].rate/100, estimatedRate = swap.fairRate(); if (std::fabs(expectedRate-estimatedRate) > 1.0e-9) { BOOST_FAIL(swapData[i].n << " year(s) swap:\n" << std::setprecision(8) << " estimated rate: " << io::rate(estimatedRate) << "\n" << " expected rate: " << io::rate(expectedRate)); } } QL_TEST_TEARDOWN } void CompoundForwardTest::testConvertedRates() { BOOST_MESSAGE("Testing consistency of compound-forward curve " "with converted rates..."); QL_TEST_BEGIN QL_TEST_SETUP Handle liborHandle = Handle(termStructure); Size i; frequency = Quarterly; // check swaps against quarterly rates boost::shared_ptr index(new Jibar(Period(frequency), liborHandle)); for (i=0; idayCounter(), liborHandle); DayCounter tsdc = termStructure->dayCounter(); Rate expectedRate = termStructure->compoundForward(swap.maturityDate(), frequency); Rate estimatedRate = swap.fairRate(); if (std::fabs(expectedRate-estimatedRate) > 1.0e-9) { BOOST_FAIL(swapData[i].n << " year(s) swap:\n" << std::setprecision(8) << " estimated rate: " << io::rate(estimatedRate) << "\n" << " compound rate: " << io::rate(expectedRate)); } } QL_TEST_TEARDOWN } test_suite* CompoundForwardTest::suite() { test_suite* suite = BOOST_TEST_SUITE("Compound forward tests"); suite->add(BOOST_TEST_CASE(&CompoundForwardTest::testSuppliedRates)); suite->add(BOOST_TEST_CASE(&CompoundForwardTest::testConvertedRates)); return suite; }