QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdornsteinuhlenbeckvanillaengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2016 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#include <ql/exercise.hpp>
21#include <ql/experimental/finitedifferences/fdornsteinuhlenbeckvanillaengine.hpp>
22#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
23#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp>
24#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
25#include <ql/methods/finitedifferences/operators/fdmornsteinuhlenbeckop.hpp>
26#include <ql/methods/finitedifferences/solvers/fdm1dimsolver.hpp>
27#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
28#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
29#include <ql/processes/ornsteinuhlenbeckprocess.hpp>
30#include <ql/termstructures/yieldtermstructure.hpp>
31#include <utility>
32
33namespace QuantLib {
34
35 namespace {
36 class FdmOUInnerValue : public FdmInnerValueCalculator {
37 public:
38 FdmOUInnerValue(ext::shared_ptr<Payoff> payoff,
39 ext::shared_ptr<FdmMesher> mesher,
40 Size direction)
41 : payoff_(std::move(payoff)), mesher_(std::move(mesher)), direction_(direction) {}
42
43
44 Real innerValue(const FdmLinearOpIterator& iter, Time) override {
45 const Real s = mesher_->location(iter, direction_);
46 return (*payoff_)(s);
47 }
48
49 Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override {
50 return innerValue(iter, t);
51 }
52
53 private:
54 const ext::shared_ptr<Payoff> payoff_;
55 const ext::shared_ptr<FdmMesher> mesher_;
56 const Size direction_;
57 };
58 }
59
61 ext::shared_ptr<OrnsteinUhlenbeckProcess> process,
62 const ext::shared_ptr<YieldTermStructure>& rTS,
63 Size tGrid,
64 Size xGrid,
65 Size dampingSteps,
66 Real epsilon,
67 const FdmSchemeDesc& schemeDesc)
68 : process_(std::move(process)), rTS_(rTS), explicitDividends_(false),
69 tGrid_(tGrid), xGrid_(xGrid),
70 dampingSteps_(dampingSteps), epsilon_(epsilon), schemeDesc_(schemeDesc) {
72 registerWith(rTS);
73 }
74
76 ext::shared_ptr<OrnsteinUhlenbeckProcess> process,
77 const ext::shared_ptr<YieldTermStructure>& rTS,
78 DividendSchedule dividends,
79 Size tGrid,
80 Size xGrid,
81 Size dampingSteps,
82 Real epsilon,
83 const FdmSchemeDesc& schemeDesc)
84 : process_(std::move(process)), rTS_(rTS),
85 dividends_(std::move(dividends)), explicitDividends_(true),
86 tGrid_(tGrid), xGrid_(xGrid),
87 dampingSteps_(dampingSteps), epsilon_(epsilon), schemeDesc_(schemeDesc) {
89 registerWith(rTS);
90 }
91
93
94 // dividends will eventually be moved out of arguments, but for now we need the switch
95 QL_DEPRECATED_DISABLE_WARNING
97 QL_DEPRECATED_ENABLE_WARNING
98
99 // 1. Mesher
100 const ext::shared_ptr<StrikedTypePayoff> payoff =
101 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
102
103 const DayCounter dc = rTS_->dayCounter();
104 const Date referenceDate = rTS_->referenceDate();
105
106 const Time maturity = dc.yearFraction(
107 referenceDate, arguments_.exercise->lastDate());
108
109 const ext::shared_ptr<Fdm1dMesher> equityMesher(
111 xGrid_, process_, maturity, 1, epsilon_));
112
113 const ext::shared_ptr<FdmMesher> mesher (
114 new FdmMesherComposite(equityMesher));
115
116 // 2. Calculator
117 const ext::shared_ptr<FdmInnerValueCalculator> calculator(
118 new FdmOUInnerValue(payoff, mesher, 0));
119
120 // 3. Step conditions
121 const ext::shared_ptr<FdmStepConditionComposite> conditions =
123 passedDividends, arguments_.exercise,
124 mesher, calculator,
125 referenceDate, dc);
126
127 // 4. Boundary conditions
128 const FdmBoundaryConditionSet boundaries;
129
130 // 5. Solver
131 FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
132 maturity, tGrid_, dampingSteps_ };
133
134 const ext::shared_ptr<FdmOrnsteinUhlenbeckOp> op(
135 new FdmOrnsteinUhlenbeckOp(mesher, process_, rTS_, 0));
136
137 const ext::shared_ptr<Fdm1DimSolver> solver(
138 new Fdm1DimSolver(solverDesc, schemeDesc_, op));
139
140 const Real spot = process_->x0();
141
142 results_.value = solver->interpolateAt(spot);
143 results_.delta = solver->derivativeX(spot);
144 results_.gamma = solver->derivativeXX(spot);
145 results_.theta = solver->thetaAt(spot);
146 }
147}
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
FdOrnsteinUhlenbeckVanillaEngine(ext::shared_ptr< OrnsteinUhlenbeckProcess >, const ext::shared_ptr< YieldTermStructure > &rTS, Size tGrid=100, Size xGrid=100, Size dampingSteps=0, Real epsilon=0.0001, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Douglas())
const ext::shared_ptr< YieldTermStructure > rTS_
const ext::shared_ptr< OrnsteinUhlenbeckProcess > process_
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)
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
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.