QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analyticvariancegammaengine.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4Copyright (C) 2010 Adrian O' Neill
5
6This file is part of QuantLib, a free-software/open-source library
7for financial quantitative analysts and developers - http://quantlib.org/
8
9QuantLib is free software: you can redistribute it and/or modify it
10under the terms of the QuantLib license. You should have received a
11copy 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
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/exercise.hpp>
27#include <utility>
28
29namespace QuantLib {
30
31 namespace {
32
33 class Integrand {
34 public:
35 Integrand(ext::shared_ptr<StrikedTypePayoff> payoff,
36 Real s0,
37 Real t,
38 Real riskFreeDiscount,
39 Real dividendDiscount,
40 Real sigma,
41 Real nu,
42 Real theta)
43 : payoff_(std::move(payoff)), s0_(s0), t_(t), riskFreeDiscount_(riskFreeDiscount),
44 dividendDiscount_(dividendDiscount), sigma_(sigma), nu_(nu), theta_(theta) {
45 omega_ = std::log(1.0 - theta_ * nu_ - (sigma_ * sigma_ * nu_) / 2.0) / nu_;
46 // We can precompute the denominator of the gamma pdf (does not depend on x)
47 // shape = t_/nu_, scale = nu_
48 GammaFunction gf;
49 gammaDenom_ = std::exp(gf.logValue(t_ / nu_)) * std::pow(nu_, t_ / nu_);
50 }
51
52 Real operator()(Real x) const {
53 // Compute adjusted black scholes price
54 Real s0_adj = s0_ * std::exp(theta_ * x + omega_ * t_ + (sigma_ * sigma_ * x) / 2.0);
55 Real vol_adj = sigma_ * std::sqrt(x / t_);
56 vol_adj *= std::sqrt(t_);
57
58 BlackScholesCalculator bs(payoff_, s0_adj, dividendDiscount_, vol_adj, riskFreeDiscount_);
59 Real bsprice = bs.value();
60
61 // Multiply by gamma distribution
62 Real gamp = (std::pow(x, t_ / nu_ - 1.0) * std::exp(-x / nu_)) / gammaDenom_;
63 Real result = bsprice * gamp;
64 return result;
65 }
66
67 private:
68 ext::shared_ptr<StrikedTypePayoff> payoff_;
78 };
79 }
80
81
82 VarianceGammaEngine::VarianceGammaEngine(ext::shared_ptr<VarianceGammaProcess> process,
83 Real absoluteError)
84 : process_(std::move(process)), absErr_(absoluteError) {
85 QL_REQUIRE(absErr_ > 0, "absolute error must be positive");
87 }
88
90
92 "not an European Option");
93
94 ext::shared_ptr<StrikedTypePayoff> payoff =
95 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
96 QL_REQUIRE(payoff, "non-striked payoff given");
97
98 DiscountFactor dividendDiscount =
99 process_->dividendYield()->discount(
100 arguments_.exercise->lastDate());
101 DiscountFactor riskFreeDiscount =
102 process_->riskFreeRate()->discount(arguments_.exercise->lastDate());
103
104 DayCounter rfdc = process_->riskFreeRate()->dayCounter();
105 Time t = rfdc.yearFraction(process_->riskFreeRate()->referenceDate(),
106 arguments_.exercise->lastDate());
107
108 Integrand f(payoff,
109 process_->x0(),
110 t, riskFreeDiscount, dividendDiscount,
111 process_->sigma(), process_->nu(), process_->theta());
112
113 Real infinity = 15.0 * std::sqrt(process_->nu() * t);
114 Real target = absErr_*1e-4;
115 Real val = f(infinity);
116 while (std::abs(val)>target){
117 infinity*=1.5;
118 val = f(infinity);
119 }
120 // the integration is split due to occasional singularities at 0
121 Real split = 0.1;
122 GaussKronrodNonAdaptive integrator1(absErr_, 1000, 0);
123 Real pvA = integrator1(f, 0, split);
124 GaussLobattoIntegral integrator2(2000, absErr_);
125 Real pvB = integrator2(f, split, infinity);
126 results_.value = pvA + pvB;
127 }
128
129}
Real dividendDiscount_
Real riskFreeDiscount_
Analytic Variance Gamma option engine for vanilla options.
Black-Scholes formula calculator class.
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Integral of a 1-dimensional function using the Gauss-Kronrod methods.
Integral of a one-dimensional function.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
ext::shared_ptr< Exercise > exercise
Definition: option.hpp:65
ext::shared_ptr< Payoff > payoff
Definition: option.hpp:64
ext::shared_ptr< VarianceGammaProcess > process_
VarianceGammaEngine(ext::shared_ptr< VarianceGammaProcess >, Real absoluteError=1e-5)
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Option exercise classes and payoff function.
const ext::shared_ptr< Payoff > payoff_
Gamma distribution.
integral of a one-dimensional function using the adaptive Gauss-Lobatto integral
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Rate
interest rates
Definition: types.hpp:70
Real theta
Real sigma
ext::shared_ptr< QuantLib::Payoff > payoff
Integral of a 1-dimensional function using the Gauss-Kronrod method.
Definition: any.hpp:35
STL namespace.
Real nu
Definition: sabr.cpp:200
Integral of a one-dimensional function using segment algorithm.