pde modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z goal is to set up design software...
TRANSCRIPT
![Page 1: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/1.jpg)
PDE Models
![Page 2: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/2.jpg)
2
Goal is to set up design software frameworks
Determine scope (“what kinds of PDE
supported?”)
ISO 9126: Accuracy, Efficiency, Functionality
In the long term Maintainability paramount
(Creeping featuritis , evolving requirements)
Reusability, assembly from prebuilt libraries
(e.g. NAG, IMSL; Fortran)
PDE Models
![Page 3: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/3.jpg)
3
Types of PDEs in computational finance
Categories of PDEs
Finite difference methods for PDEs
Libraries, frameworks, applications in C++11
(Multiparadigm design patterns)
Challenges
![Page 4: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/4.jpg)
4
Equities (Black Scholes, Heston, Asian,…)
Interest rate (CIR, HW, Cheyette,…)
Fixed income (caps, floors, swap, swaptions,…)
Specials (UVM, Anchoring (Wilmott, Lewis and
Duffy))
Nonlinear PDEs (Hamilton-Jacobi-Bellman
(HJB))
Fokker-Planck, Kolmogorov
PDEs in Computational Finance
![Page 5: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/5.jpg)
5
D1: Degree of nonlinearity
D2: One-factor and multi-factor
D3: Domain (bounded, semi-infinite, infinite)
D4: Fixed, free and moving boundaries
D5: Known/unknown parameters
D6: Time-dependence, time-independence
D7: PDEs in conservative and non-conservative
forms
Dimensions of PDEs
![Page 6: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/6.jpg)
6
F1: Suitability for linear and nonlinear PDE
F2: One-step methods, multi-step methods
F3: Facility with multi-factor models
F4: Reusability of PDE and FDM models
F5: Using numerical libraries to promote
productivity
Finite Difference Methods
![Page 7: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/7.jpg)
7
L1: Hard-coded (e.g. Crank Nicolson for Black
Scholes PDE) (Duffy 2004)
L2: GOF design patterns (subtype
polymorphism, CRTP pattern) (Duffy 2006,
Duffy 2009)
L3: Layered approach (POSA pattern)
L4: Domain Architectures (incorporates designs
L1, L2, L3) (Duffy 2004, Duffy 2015)
How to write a FD Solver?
![Page 8: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/8.jpg)
8
A Word from Sponsor
![Page 9: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/9.jpg)
9
Which approach to use? (L1 to L4)
Each approach has its own level of difficulty
and consequences
“Mental model” varies between bottom-up
programming and top-down system
decomposition
Many implicit assumptions in developer’s head
System Architecture (1)
![Page 10: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/10.jpg)
10
Break a system into loosely-coupled and
cohesive components
Choose between task and data decomposition
Single Responsibility Principle (SRP)
Components have well-defined provides and
requires interfaces
Component internals can be implemented using
OOP and design patterns
System Decomposition
![Page 11: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/11.jpg)
11
Solution 1
main()
ConversionPreprocessor
ExcelDriver
Display
FDMDirectorUIBSPDE
Factory
uses uses
<<creates>>
FDM
Area 1 Area 2 Area 3
Area 4
![Page 12: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/12.jpg)
12
Separation of concerns (SRP)
Object-oriented approach
Based on PAC (Presentation-Abstraction-
Control) model
Not very flexible (nor maintainable)
Useful for throwaway prototypes
Remarks
![Page 13: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/13.jpg)
13
using namespace BlackScholesOneFactorIBVP;
// Assignment of functions
sigma = mySigma;
mu = myMu;
b = myB;
f = myF;
BCL = myBCL;
BCR = myBCR;
IC = myIC;
double T = 1.0; int J= 200; int N = 4000-1;
double Smax = 100.0;
FDMDirector fdir(Smax, T, J, N);
fdir.Start();
Code: Initialisation
![Page 14: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/14.jpg)
14
L1:
fdir.doit();
if (fdir.isDone() == false)
goto L1;
Vector<double, long> xresult(J+1, 1);
xresult[xresult.MinIndex()] = 0.0;
double h = Smax / (double) (J);
for (long j = xresult.MinIndex()+1;
j <= xresult.MaxIndex(); j++)
{
xresult[j] = xresult[j-1] + h;
}
printOneExcel(xresult, fdir.current(), string("Value"));
Code: State Machine
![Page 15: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/15.jpg)
15
OOP design pattern approach
Class hierarchies and subtype polymorphism
(virtual functions))
Variant: use CRTP
Patterns: Template Method, Bridge, State,
Mediator
Solution 2
![Page 16: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/16.jpg)
16
Model
![Page 17: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/17.jpg)
17
Maintainability issues due to inheritance
hierarchy
Possible performance issues
Reasonably stable (new schemes only need to
implement couple functions)
Restricted to linear convection-diffusion-
reaction PDE with Dirichlet boundary conditions
Object-oriented interfaces
Remarks
![Page 18: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/18.jpg)
18
class IBVP { // ‘Device-independent’ class
Range<double> xaxis; // Space interval
Range<double> taxis; // Time interval
IBVPImp* imp; // Bridge implementation
public:
// Coefficients of parabolic second order operator
double diffusion(double x, double t) const; // Second derivative
double convection(double x, double t) const;
double zeroterm(double x, double t) const;
double RHS(double x, double t) const;
// Boundary and initial conditions
double BCL(double t) const;
double BCR(double t) const;
double IC(double x) const;
double Constraint(double x) const;
};
Code: PDE Class
![Page 19: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/19.jpg)
19
class IBVPImp
{
public:
// Selector functions
// Coefficients of parabolic second order operator
virtual double diffusion(double x, double t) const = 0;
virtual double convection(double x, double t) const = 0;
virtual double zeroterm(double x, double t) const = 0;
virtual double RHS(double x, double t) const = 0;
// Boundary and initial conditions
virtual double BCL(double t) const = 0;
virtual double BCR(double t) const = 0;
virtual double IC(double x) const = 0;
virtual double Constraint(double x) const = 0;
};
Code Bridge Abstraction
![Page 20: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/20.jpg)
20
class BSIBVPImp : public IBVPImp
{
public:
Option* opt; // BS data
BSIBVPImp(Option& option);
double diffusion(double x, double t) const;
double convection(double x, double t) const;
double zeroterm(double x, double t) const;
double RHS(double x, double t) const;
// Boundary and initial conditions
double BCL(double t) const;
double BCR(double t) const;
double IC(double x) const;
double Constraint(double x) const;
};
Code Bridge Implementation
![Page 21: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/21.jpg)
21
class IBVPFDM
{ // Set of finite difference to solve scalar initial
// boundary value problems
protected:
IBVP* ibvp; // Pointer to 'parent'
// more
Mesher m;
public:
NumericMatrix<double, long>& result(); // The result of the calculation
Vector<double, long>& resultVector();
boost::tuple<Vector<double,long>, NumericMatrix<double, long>> ManagementInformation() const;
const Vector<double, long>& XValues() const; // Array of x values
const Vector<double, long>& TValues() const; // Array of time values
// Hook function for Template Method pattern
virtual void calculateBC() = 0; // Tells how to calculate sol. at n+1
virtual void calculate() = 0; // Tells how to calculate sol. at n+1
};
Code Base Class FDM
![Page 22: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/22.jpg)
22
class ADE: public IBVPFDM
{
private:
// Intermediate values
Vector<double, long> U;
Vector<double, long> V;
Vector<double, long> UOld;
Vector<double, long> VOld;
public:
ADE();
ADE(IBVP& source, long NSteps, long JSteps);
// Hook function for Template Method pattern
void calculateBC();
void calculate();
double fitting_factor(double x, double t);
};
Code ADE Solver
![Page 23: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/23.jpg)
23
Based on Domain Architectures and layering
principles
A domain architecture is a family of applications
(meta pattern)
Can be used as a reference model for several
similar kinds of applications
Used in Monte Carlo (C++ (V1) and C# (V2))
Solution 3
![Page 24: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/24.jpg)
24
Original Model
ProgressSystem
MCSolver
MCMediator
(Evolver)
Boost Signals
IProgress
IData
FDMVisitorPayoff
RNG
Boost Function
IPayoff
IRandom
IVisitor
Class Method
Boost Signals
Template Parameters
Boost Random
STL Algorithms
Multiple Systems
Multiple Systems
SDEICoeff
![Page 25: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/25.jpg)
25
Hybrid model (OOP, functional)
More focus on decomposition and narrow
interface design
No (less) class hierarchies needed
Support for both provides and requires
interfaces
Event notification using Boost signals2 (or .NET
delegates)
Remarks
![Page 26: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/26.jpg)
26
Revised Model
Builder
MIS
Pricer
Mediator
SdeSource RngFdm
msm
![Page 27: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/27.jpg)
C++ Classes for PDE
![Page 28: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/28.jpg)
2
Discussion of how to design PDEs in C++ (non-
OOP approach)
Structure, creation and behaviour (in an
application)
How can we use the new features in C++?
Let’s look at all the options!
C++ Classes for PDE
![Page 29: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/29.jpg)
3
A. Modular, non-OO
B. Class/struct-based
C. Signature-based
(It’s a design decision to decide which option or
combination of options to choose from)
Modelling Functions
![Page 30: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/30.jpg)
4
Decisions, Decisions
Functions
A
ModularFree FunctionsNamespaces
B C
Class-basedOOPCRTPTuples
Signature-basedFunction Type wrappersLambda FuntionsBinders
![Page 31: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/31.jpg)
5
Which Choice?
Maintainability Efficiencey Portability Interoperability
A 3 1 3 3
B 2 3 2 2
C 1 2 1 1
![Page 32: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/32.jpg)
6
C1: Function type wrappers
C2: Tuples
Combining C1 and C2 in different ways
Composition structure
Three-fold advantage a) group functions, b)
functions are signatures, c) functions with
tuples as input or output
Critical Components
![Page 33: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/33.jpg)
7
C++ does not support interfaces (compare C#,
Java)
Need some way to group functions into a single
entity
This entity can be tightly coupled or loosely
coupled
Use functional type wrappers (then entity
become a Bridge instance)
Grouping Functions
![Page 34: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/34.jpg)
8
template <typename T> class TwoFactorPde
{
public:
// U_t = a11U_xx + a22U_yy + b1U_x + b2U_y + cU_xy + dU + F;
// f = f(x,y,t)
boost::function<T (T,T,T)> a11;
boost::function<T (T,T,T)> a22;
boost::function<T (T,T,T)> c;
boost::function<T (T,T,T)> b1;
boost::function<T (T,T,T)> b2;
boost::function<T (T,T,T)> d;
boost::function<T (T,T,T)> F;
TwoFactorPde() {}
};
Ex: Functions in a Class
![Page 35: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/35.jpg)
9
double diffusion (double x)
{
double s = 0.2;
return 0.5 * s * s * x *x;
}
double convection (double x)
{
double r = 0.05;
return r*x;
}
std::tuple<FunctionTypeI, FunctionTypeI>
pde = std::make_tuple<FunctionTypeI, FunctionTypeI>
(diffusion, convection);
Ex: Tuples of Functions (1)
![Page 36: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/36.jpg)
10
typedef std::function<double (double)> FunctionTypeI;
// a u' + b u = g (a,b,g are scalar-valued functions)
typedef tuple<FunctionTypeI,FunctionTypeI,FunctionTypeI> RobinBC;
// Numerical approximation of Robin BC
// Input is a) boundary point x, b) mesh h, c) RobinBC
// Output is a tuple of 3 numbers
std::tuple<double,double,double>
discreteRobinBC(const RobinBC& bc, double x, double h)
{ // One-sided discretisation of continuous Robin BC
// Returns the coefficients
double a = std::get<0>(bc)(x) / h;
double b = std::get<1>(bc)(x) - a;
double c = std::get<2>(bc)(x);
return std::make_tuple<double,double,double> (a,b,c);
}
Ex: Tuples of Functions (2)
![Page 37: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/37.jpg)
11
Just like in mathematics
Applications to PDE when we perform
transformation to a unit interval, for example
We also need to transform the coefficients (and
derivatives) of the PDE
Most PDE solver use domain truncation
Function Composition
![Page 38: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/38.jpg)
12
// A class to model function composition
struct FunComposer
{
FunctionTypeI f_;
FunctionTypeI g_;
FunComposer(const FunctionTypeI& f,
const FunctionTypeI& g): f_(f), g_(g) {}
double operator () (double x) { return f_(g_(x));}
};
// Composition of functions
FunComposer fCom(diffusion, convection);
std::cout << fCom(100.0) << std::endl;
Examples (1)
![Page 39: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/39.jpg)
13
auto Compose = [](const FunctionType& f,
const FunctionType& g, double x, double t)-> double
{
return f(x)*g(t);
};
double My(double x)
{ return std::pow(x, 0.5);}
double Another(double x)
{ return std::pow(x, 2.0);}
std::cout << "II " << H(My, Another, 2.0, 2.0);
Examples (2)
![Page 40: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/40.jpg)
14
std::function (from
boost::function<> )
Similar to a .NET delegate type
Universal black box interface to many kinds of
functions in C++
Can handle various “developer styles”
‘Universal’ Function Type Wrappers
![Page 41: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/41.jpg)
15
Function Styles
Function Wrapper
Function pointers
(static) member function (STA::bind)
Function Object
Lambda Function
Boost signals2Black Box Proxy
![Page 42: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/42.jpg)
16
We need a defined process in order to structure
an application
(Class hierarchies and subtype polymorphism
are less critical to success these days …)
Layering is a common technique (Edsger
Dijkstra THE multiprogramming system)
In finance, e.g. RiskWatch
Choice: 3-layer versus 5-layer models
The Layers Pattern (POSA 1996)
![Page 43: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/43.jpg)
17
0: OS multiprogramming; processes,
semaphores,…
1: Allocating memory to processes(the pager)
2: Console – OS communication
3: All I/O between devices attached to the
computer
4: User programs. Compilation, execution,
printing user programs
5: The user!
THE (“Technische Hogeschool
Eindhoven”) 1966, 5 Layers
![Page 44: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/44.jpg)
18
In previous versions used class hierarchies,
procedural, GOF patterns
Maintenance issues (‘magic number 7, plus or
minus 2’)
Need a model that helps the developer stay in
control
And that allows us to manage projects
Example: Lattice Models (3 Layers)
![Page 45: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/45.jpg)
19
ADT and containers to hold (structured)
data
Generic (node types, axes)
Implementation (home-grown, Eigen,
Boost uBLAS)
Adapter containers (good information
hiding)
Objects and generics
Layer 1: Data Layer
![Page 46: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/46.jpg)
20
// The Node class contains the values of interest. LatticeType == 2 for binomial,3 for trinomial.
template <class Node, int LatticeType>
class Lattice
{ // Generic lattice class
// Implement as a full nested vector class
std::vector<std::vector<Node> > tree;
};
template <typename TimeAxis, typename Node, int LatticeType>
class GeneralisedLattice
{ // Generic lattice class
// Options
//std::vector<std::vector<Node> > tree;
//boost::unordered_map<TimeAxis, std::vector<Node> > tree;
std::map<TimeAxis, std::vector<Node> > tree;
};
Lattice ADTS
![Page 47: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/47.jpg)
21
Components use the ADTs from Layer 1
e.g. forward and backward induction algorithms
Algorithms (similar to GOF Strategy and Visitor
patterns)
Can use C++ 11 function wrapper to implement
behavioural ‘variations’
Generics and functional programming
Layer 2: Functions/Mechanisms
![Page 48: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/48.jpg)
22
template <class Node> void ForwardInduction
(Lattice<Node, TYPEB>& lattice,
const std::function<std::tuple<Node, Node>
(const Node& node)>& generator,
const Node& rootValue
)
template <class Node> Node BackwardInduction
(Lattice<Node, TYPEB>& lattice,
const std::function<Node
(const Node& upper,const Node& lower)>& generator,
const std::function<Node (const Node& node)>& endCond)
Algorithms
![Page 49: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/49.jpg)
23
Code to initialise the components in layers 1
and 2
“Creational patterns”
Lambda functions used as in-situ factories
Combine tuples and application objects in a
next generation Builder pattern
(Multiple entry points to the application network)
Layer 3: UI and Configuration
![Page 50: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/50.jpg)
24
// Payoff as a lambda function
double K = 100;
auto Payoff = [&K] (double S)-> double
{return std::max<double>(K - S, 0.0);};
// The early exercise constraint
auto AmericanAdjuster
= [&Payoff](double& V, double S)->void
{ // e.g. early exercise
V = std::max<double>(V, Payoff(S));
};
Customisation
![Page 51: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/51.jpg)
25
// Down-and-out put
double L = 20.0;
auto BarrierAmericanAdjuster = [&AmericanAdjuster, &L](double& V, double S)->void
{ // e.g. early exercise
if (S > L)
{
AmericanAdjuster(V,S);
}
else
{
V = 0.0;
}
};
double U = 100.0;
auto DoubleBarrierAmericanAdjuster = [&AmericanAdjuster, &L, &U](double& V, double S)->void
{ // down and out, up and out
if (S > L && S < U)
{
AmericanAdjuster(V,S);
}
else
{
V = 0.0;
}
};
Using Lambda Functions
![Page 52: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/52.jpg)
26
// Price a plain option
double res = LatticeMechanisms::BackwardInduction<double>
(lattice, algorithm, Payoff);
std::cout << "Plain Option price, BM classic #1: " << res;
// Price an early exercise option
double res2 = LatticeMechanisms::BackwardInduction<double>
(lattice2, algorithm, Payoff,
BarrierAmericanAdjuster);
std::cout << "Early execise option price" << res2;
Test Case
![Page 53: PDE Modelsquantlib.sourceforge.net/slides/qlws14/duffy.pdf · 2 z Goal is to set up design software frameworks z Determine scope ³ZKDWNLQGVRI3'( VXSSRUWHG"´ z ISO 9126: Accuracy,](https://reader034.vdocuments.us/reader034/viewer/2022042208/5eaafc54fbc6a64e096a588d/html5/thumbnails/53.jpg)
27
Idea: 5-Layer PDE/FDM Model
5
User
4
Vieuws (FDM, FEM, MC)
3
Ínternal Functionality’
2
Lattice/Pde/Sde/Ode
1
Specific Pdes etc.