| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2010 Adrian O' Neill |
| 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 <ql/experimental/variancegamma/fftvanillaengine.hpp> |
| 21 | #include <ql/exercise.hpp> |
| 22 | #include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp> |
| 23 | #include <complex> |
| 24 | |
| 25 | namespace QuantLib { |
| 26 | |
| 27 | FFTVanillaEngine::FFTVanillaEngine( |
| 28 | const ext::shared_ptr<GeneralizedBlackScholesProcess>& process, Real logStrikeSpacing) |
| 29 | : FFTEngine(process, logStrikeSpacing) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | std::unique_ptr<FFTEngine> FFTVanillaEngine::clone() const |
| 34 | { |
| 35 | ext::shared_ptr<GeneralizedBlackScholesProcess> process = |
| 36 | ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(r: process_); |
| 37 | return std::unique_ptr<FFTEngine>(new FFTVanillaEngine(process, lambda_)); |
| 38 | } |
| 39 | |
| 40 | void FFTVanillaEngine::precalculateExpiry(Date d) |
| 41 | { |
| 42 | ext::shared_ptr<GeneralizedBlackScholesProcess> process = |
| 43 | ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(r: process_); |
| 44 | |
| 45 | dividendDiscount_ = |
| 46 | process->dividendYield()->discount(d); |
| 47 | riskFreeDiscount_ = |
| 48 | process->riskFreeRate()->discount(d); |
| 49 | |
| 50 | DayCounter rfdc = process->riskFreeRate()->dayCounter(); |
| 51 | t_ = rfdc.yearFraction(d1: process->riskFreeRate()->referenceDate(), d2: d); |
| 52 | |
| 53 | ext::shared_ptr<BlackConstantVol> constVol = ext::dynamic_pointer_cast<BlackConstantVol> |
| 54 | (r: *(process->blackVolatility())); |
| 55 | QL_REQUIRE(constVol, "Constant volatility required" ); |
| 56 | Real vol = constVol->blackVol(t: 0.0, strike: 0.0); |
| 57 | var_ = vol*vol; |
| 58 | } |
| 59 | |
| 60 | std::complex<Real> FFTVanillaEngine::complexFourierTransform(std::complex<Real> u) const |
| 61 | { |
| 62 | std::complex<Real> i1(0, 1); |
| 63 | |
| 64 | Real s = process_->x0(); |
| 65 | |
| 66 | std::complex<Real> phi = std::exp(z: i1 * u * (std::log(x: s) - (var_ * t_) / 2.0) |
| 67 | - (var_ * u * u * t_) / 2.0); |
| 68 | phi = phi * std::pow(x: dividendDiscount_/ riskFreeDiscount_, y: i1 * u); |
| 69 | return phi; |
| 70 | } |
| 71 | |
| 72 | Real FFTVanillaEngine::discountFactor(Date d) const |
| 73 | { |
| 74 | ext::shared_ptr<GeneralizedBlackScholesProcess> process = |
| 75 | ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(r: process_); |
| 76 | return process->riskFreeRate()->discount(d); |
| 77 | } |
| 78 | |
| 79 | Real FFTVanillaEngine::dividendYield(Date d) const |
| 80 | { |
| 81 | ext::shared_ptr<GeneralizedBlackScholesProcess> process = |
| 82 | ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(r: process_); |
| 83 | return process->dividendYield()->discount(d); |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |