/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2002, 2003, 2004 Ferdinando Ametrano Copyright (C) 2002, 2003 RiskMap srl Copyright (C) 2003, 2004, 2005 StatPro Italia srl Copyright (C) 2007 Affine Group Limited 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. */ /*! \file binomialengine.hpp \brief Binomial option engine */ #ifndef quantlib_binomial_engine_hpp #define quantlib_binomial_engine_hpp #include #include #include #include #include #include #include #include namespace QuantLib { //! Pricing engine for vanilla options using binomial trees /*! \ingroup vanillaengines \test the correctness of the returned values is tested by checking it against analytic results. \todo Greeks are not overly accurate. They could be improved by building a tree so that it has three points at the current time. The value would be fetched from the middle one, while the two side points would be used for estimating partial derivatives. */ template class BinomialVanillaEngine : public VanillaOption::engine { public: BinomialVanillaEngine(Size timeSteps) : timeSteps_(timeSteps) {} void calculate() const; private: Size timeSteps_; }; // template definitions template void BinomialVanillaEngine::calculate() const { boost::shared_ptr process = boost::dynamic_pointer_cast( this->arguments_.stochasticProcess); QL_REQUIRE(process, "Black-Scholes process required"); DayCounter rfdc = process->riskFreeRate()->dayCounter(); DayCounter divdc = process->dividendYield()->dayCounter(); DayCounter voldc = process->blackVolatility()->dayCounter(); Real s0 = process->stateVariable()->value(); Volatility v = process->blackVolatility()->blackVol( arguments_.exercise->lastDate(), s0); Date maturityDate = arguments_.exercise->lastDate(); Rate r = process->riskFreeRate()->zeroRate(maturityDate, rfdc, Continuous, NoFrequency); Rate q = process->dividendYield()->zeroRate(maturityDate, divdc, Continuous, NoFrequency); Date referenceDate = process->riskFreeRate()->referenceDate(); // binomial trees with constant coefficient Handle flatRiskFree( boost::shared_ptr( new FlatForward(referenceDate, r, rfdc))); Handle flatDividends( boost::shared_ptr( new FlatForward(referenceDate, q, divdc))); Handle flatVol( boost::shared_ptr( new BlackConstantVol(referenceDate, v, voldc))); boost::shared_ptr payoff = boost::dynamic_pointer_cast(arguments_.payoff); QL_REQUIRE(payoff, "non-plain payoff given"); Time maturity = rfdc.yearFraction(referenceDate, maturityDate); boost::shared_ptr bs( new GeneralizedBlackScholesProcess( process->stateVariable(), flatDividends, flatRiskFree, flatVol)); // adjust the bermudan exercise times according to the tree steps TimeGrid grid(maturity, timeSteps_); for (Size k=0; k tree(new T(bs, maturity, timeSteps_, payoff->strike())); boost::shared_ptr > lattice( new BlackScholesLattice(tree, r, maturity, timeSteps_)); DiscretizedVanillaOption option(arguments_); option.initialize(lattice, maturity); // Partial derivatives calculated from various points in the // binomial tree (Odegaard) // Rollback to third-last step, and get underlying price (s2) & // option values (p2) at this point option.rollback(grid[2]); Array va2(option.values()); QL_ENSURE(va2.size() == 3, "Expect 3 nodes in grid at second step"); Real p2h = va2[2]; // high-price Real s2 = lattice->underlying(2, 2); // high price // Rollback to second-last step, and get option value (p1) at // this point option.rollback(grid[1]); Array va(option.values()); QL_ENSURE(va.size() == 2, "Expect 2 nodes in grid at first step"); Real p1 = va[1]; // Finally, rollback to t=0 option.rollback(0.0); Real p0 = option.presentValue(); Real s1 = lattice->underlying(1, 1); // Calculate partial derivatives Real delta0 = (p1-p0)/(s1-s0); // dp/ds Real delta1 = (p2h-p1)/(s2-s1); // dp/ds // Store results results_.value = p0; results_.delta = delta0; results_.gamma = 2.0*(delta1-delta0)/(s2-s0); //d(delta)/ds results_.theta = blackScholesTheta(process, results_.value, results_.delta, results_.gamma); } } #endif