binomialengine.hpp
来自「有很多的函数库」· HPP 代码 · 共 168 行
HPP
168 行
/* -*- 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
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
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 <ql/methods/lattices/binomialtree.hpp>
#include <ql/methods/lattices/bsmlattice.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
#include <ql/pricingengines/vanilla/discretizedvanillaoption.hpp>
#include <ql/pricingengines/greeks.hpp>
#include <ql/processes/blackscholesprocess.hpp>
#include <ql/termstructures/yieldcurves/flatforward.hpp>
#include <ql/termstructures/volatilities/blackconstantvol.hpp>
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 T>
class BinomialVanillaEngine : public VanillaOption::engine {
public:
BinomialVanillaEngine(Size timeSteps)
: timeSteps_(timeSteps) {}
void calculate() const;
private:
Size timeSteps_;
};
// template definitions
template <class T>
void BinomialVanillaEngine<T>::calculate() const {
boost::shared_ptr<GeneralizedBlackScholesProcess> process =
boost::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
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<YieldTermStructure> flatRiskFree(
boost::shared_ptr<YieldTermStructure>(
new FlatForward(referenceDate, r, rfdc)));
Handle<YieldTermStructure> flatDividends(
boost::shared_ptr<YieldTermStructure>(
new FlatForward(referenceDate, q, divdc)));
Handle<BlackVolTermStructure> flatVol(
boost::shared_ptr<BlackVolTermStructure>(
new BlackConstantVol(referenceDate, v, voldc)));
boost::shared_ptr<PlainVanillaPayoff> payoff =
boost::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
QL_REQUIRE(payoff, "non-plain payoff given");
Time maturity = rfdc.yearFraction(referenceDate, maturityDate);
boost::shared_ptr<StochasticProcess1D> 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<arguments_.stoppingTimes.size(); ++k) {
arguments_.stoppingTimes[k] =
grid.closestTime(arguments_.stoppingTimes[k]);
}
boost::shared_ptr<T> tree(new T(bs, maturity, timeSteps_,
payoff->strike()));
boost::shared_ptr<BlackScholesLattice<T> > lattice(
new BlackScholesLattice<T>(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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?