QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdornsteinuhlenbeckvanillaengine.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) 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>
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_;
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), tGrid_(tGrid), xGrid_(xGrid),
69 dampingSteps_(dampingSteps), epsilon_(epsilon), schemeDesc_(schemeDesc) {
70 registerWith(process_);
71 registerWith(rTS);
72 }
73
75 ext::shared_ptr<OrnsteinUhlenbeckProcess> process,
76 const ext::shared_ptr<YieldTermStructure>& rTS,
77 DividendSchedule dividends,
78 Size tGrid,
79 Size xGrid,
80 Size dampingSteps,
81 Real epsilon,
82 const FdmSchemeDesc& schemeDesc)
83 : process_(std::move(process)), rTS_(rTS),
84 dividends_(std::move(dividends)), tGrid_(tGrid), xGrid_(xGrid),
85 dampingSteps_(dampingSteps), epsilon_(epsilon), schemeDesc_(schemeDesc) {
86 registerWith(process_);
87 registerWith(rTS);
88 }
89
91
92 // 1. Mesher
93 const ext::shared_ptr<StrikedTypePayoff> payoff =
94 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
95
96 const DayCounter dc = rTS_->dayCounter();
97 const Date referenceDate = rTS_->referenceDate();
98
99 const Time maturity = dc.yearFraction(
100 referenceDate, arguments_.exercise->lastDate());
101
102 const ext::shared_ptr<Fdm1dMesher> equityMesher(
104 xGrid_, process_, maturity, 1, epsilon_));
105
106 const ext::shared_ptr<FdmMesher> mesher (
107 new FdmMesherComposite(equityMesher));
108
109 // 2. Calculator
110 const ext::shared_ptr<FdmInnerValueCalculator> calculator(
111 new FdmOUInnerValue(payoff, mesher, 0));
112
113 // 3. Step conditions
114 const ext::shared_ptr<FdmStepConditionComposite> conditions =
116 dividends_, arguments_.exercise,
117 mesher, calculator,
118 referenceDate, dc);
119
120 // 4. Boundary conditions
121 const FdmBoundaryConditionSet boundaries;
122
123 // 5. Solver
124 FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
125 maturity, tGrid_, dampingSteps_ };
126
127 const ext::shared_ptr<FdmOrnsteinUhlenbeckOp> op(
128 new FdmOrnsteinUhlenbeckOp(mesher, process_, rTS_, 0));
129
130 const ext::shared_ptr<Fdm1DimSolver> solver(
131 new Fdm1DimSolver(solverDesc, schemeDesc_, op));
132
133 const Real spot = process_->x0();
134
135 results_.value = solver->interpolateAt(spot);
136 results_.delta = solver->derivativeX(spot);
137 results_.gamma = solver->derivativeXX(spot);
138 results_.theta = solver->thetaAt(spot);
139 }
140}
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
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)
const DefaultType & t
Option exercise classes and payoff function.
const ext::shared_ptr< YieldTermStructure > rTS_
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
FdmMesher which is a composite of Fdm1dMesher.
Ornstein Uhlenbeck process.
One-dimensional grid mesher.
composite of fdm step conditions
const ext::shared_ptr< FdmMesher > mesher_
const Size direction_
const ext::shared_ptr< Payoff > payoff_
Finite-Differences Ornstein Uhlenbeck vanilla option engine.
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
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.
Ornstein-Uhlenbeck process.
Interest-rate term structure.