QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdcevvanillaengine.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2018 Klaus Spanderen
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/*! \file fdcevvanillaengine.hpp
21 \brief Finite-Differences pricing engine for the CEV model
22*/
23
24#include <ql/exercise.hpp>
38#include <utility>
39
40namespace QuantLib {
41
42 namespace {
43 class PriceAtBoundary {
44 public:
45 PriceAtBoundary(Time maturityTime,
46 ext::shared_ptr<StrikedTypePayoff> payoff,
47 ext::shared_ptr<YieldTermStructure> rTS,
48 ext::shared_ptr<CEVCalculator> calculator)
49 : maturityTime_(maturityTime), payoff_(std::move(payoff)),
50 calculator_(std::move(calculator)), rTS_(std::move(rTS)) {}
51
52 Real operator()(Real t) const {
53 const Time time2Expiry = std::max(1/365., maturityTime_ - t);
54 const DiscountFactor df =
55 rTS_->discount(maturityTime_) / rTS_->discount(t);
56
57 return df * calculator_->value(
58 payoff_->optionType(), payoff_->strike(), time2Expiry);
59 }
60
61 private:
63 const ext::shared_ptr<StrikedTypePayoff> payoff_;
64 const ext::shared_ptr<CEVCalculator> calculator_;
65 const ext::shared_ptr<YieldTermStructure> rTS_;
66 };
67 }
68
70 Real alpha,
71 Real beta,
72 Handle<YieldTermStructure> discountCurve,
73 Size tGrid,
74 Size xGrid,
75 Size dampingSteps,
76 Real scalingFactor,
77 Real eps,
78 const FdmSchemeDesc& schemeDesc)
79 : f0_(f0), alpha_(alpha), beta_(beta), discountCurve_(std::move(discountCurve)), tGrid_(tGrid),
80 xGrid_(xGrid), dampingSteps_(dampingSteps), scalingFactor_(scalingFactor), eps_(eps),
81 schemeDesc_(schemeDesc) {
82 registerWith(discountCurve_);
83 }
84
86
87 // 1. Mesher
88 const ext::shared_ptr<StrikedTypePayoff> payoff =
89 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
90 QL_REQUIRE(payoff, "non-striked payoff given");
91
92 const ext::shared_ptr<YieldTermStructure> rTS =
93 discountCurve_.currentLink();
94
95 const DayCounter dc = rTS->dayCounter();
96
97 const Date referenceDate = rTS->referenceDate();
98 const Date maturityDate = arguments_.exercise->lastDate();
99 const Time maturityTime = dc.yearFraction(referenceDate, maturityDate);
100
101 const ext::shared_ptr<Fdm1dMesher> cevMesher =
102 ext::make_shared<FdmCEV1dMesher>(
103 xGrid_,
104 f0_, alpha_, beta_,
105 maturityTime, eps_, scalingFactor_,
106 std::make_pair(payoff->strike(), 0.1));
107
108 const Real lowerBound = cevMesher->locations().front();
109 const Real upperBound = cevMesher->locations().back();
110
111 const ext::shared_ptr<FdmMesher> mesher =
112 ext::make_shared<FdmMesherComposite>(cevMesher);
113
114 // 2. Calculator
115 const ext::shared_ptr<FdmInnerValueCalculator> calculator =
116 ext::make_shared<FdmCellAveragingInnerValue>(payoff, mesher, 0);
117
118 // 3. Step conditions
119 const ext::shared_ptr<FdmStepConditionComposite> conditions =
121 DividendSchedule(), arguments_.exercise,
122 mesher, calculator,
123 referenceDate, dc);
124
125 // 4. Boundary conditions
126 FdmBoundaryConditionSet boundaries;
127
128 const PriceAtBoundary upperBoundPrice(
129 maturityTime, payoff, rTS,
130 ext::make_shared<CEVCalculator>(upperBound, alpha_, beta_));
131
132 boundaries.push_back(ext::make_shared<FdmTimeDepDirichletBoundary>(
133 mesher, ext::function<Real (Real)>(upperBoundPrice),
135
136 const Real delta = (1-2*beta_)/(1-beta_);
137 if (delta < 2.0) {
138 const Real terminalCashFlow = (*payoff)(lowerBound);
139
140 boundaries.push_back(
141 ext::make_shared<FdmDiscountDirichletBoundary>(
142 mesher, rTS, maturityTime, terminalCashFlow,
144 }
145
146 // 5. Solver
147 const FdmSolverDesc solverDesc = {
148 mesher, boundaries, conditions,
149 calculator, maturityTime, tGrid_, dampingSteps_
150 };
151
152 const ext::shared_ptr<FdmLinearOpComposite> op =
153 ext::make_shared<FdmCEVOp>(
154 mesher, discountCurve_.currentLink(), f0_, alpha_, beta_, 0);
155
156 const ext::shared_ptr<Fdm1DimSolver> solver =
157 ext::make_shared<Fdm1DimSolver>(solverDesc, schemeDesc_, op);
158
159 results_.value = solver->interpolateAt(f0_);
160 results_.delta = solver->derivativeX(f0_);
161 results_.gamma = solver->derivativeXX(f0_);
162 results_.theta = solver->thetaAt(f0_);
163 }
164}
Pricing engine for European vanilla options using a constant elasticity of variance (CEV) model.
const YieldTermStructure & discountCurve_
Definition: cashflows.cpp:418
const Instrument::results * results_
Definition: cdsoption.cpp:63
Concrete date class.
Definition: date.hpp:125
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
const Handle< YieldTermStructure > discountCurve_
void calculate() const override
FdCEVVanillaEngine(Real f0, Real alpha, Real beta, Handle< YieldTermStructure > discountCurve, Size tGrid=50, Size xGrid=400, Size dampingSteps=0, Real scalingFactor=1.0, Real eps=1e-4, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Douglas())
static ext::shared_ptr< FdmStepConditionComposite > vanillaComposite(const DividendSchedule &schedule, const ext::shared_ptr< Exercise > &exercise, const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< FdmInnerValueCalculator > &calculator, const Date &refDate, const DayCounter &dayCounter)
Shared handle to an observable.
Definition: handle.hpp:41
One-dimensional grid mesher concentrating around critical points.
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< CEVCalculator > calculator_
Finite-Differences pricing engine for the CEV model.
One-dimensional mesher for the CEV model.
FDM operator for the Constant Elasticity of Variance (CEV) model.
const Time maturityTime_
const ext::shared_ptr< YieldTermStructure > rTS_
discounted value on Dirichlet boundary conditions
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
FdmMesher which is a composite of Fdm1dMesher.
composite of fdm step conditions
time dependent Dirichlet boundary conditions
const ext::shared_ptr< Payoff > payoff_
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
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.
Real beta
Definition: sabr.cpp:200
Real alpha
Definition: sabr.cpp:200
Interest-rate term structure.