QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdcevvanillaengine.cpp
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
24#include <ql/exercise.hpp>
25#include <ql/methods/finitedifferences/meshers/concentrating1dmesher.hpp>
26#include <ql/methods/finitedifferences/meshers/fdmcev1dmesher.hpp>
27#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
28#include <ql/methods/finitedifferences/operators/fdmcevop.hpp>
29#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
30#include <ql/methods/finitedifferences/solvers/fdm1dimsolver.hpp>
31#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
32#include <ql/methods/finitedifferences/utilities/fdmdiscountdirichletboundary.hpp>
33#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
34#include <ql/methods/finitedifferences/utilities/fdmtimedepdirichletboundary.hpp>
35#include <ql/pricingengines/vanilla/analyticcevengine.hpp>
36#include <ql/pricingengines/vanilla/fdcevvanillaengine.hpp>
37#include <ql/termstructures/yieldtermstructure.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:
62 const Time maturityTime_;
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}
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
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
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.