Financial Computing with Python

Similar documents
Outline. GPU for Finance SciFinance SciFinance CUDA Risk Applications Testing. Conclusions. Monte Carlo PDE

Implied Volatility using Python s Pandas Library

Pricing Asian Options

2.1 Mathematical Basis: Risk-Neutral Pricing

Contents Critique 26. portfolio optimization 32

source experience distilled PUBLISHING BIRMINGHAM - MUMBAI

Monte Carlo Simulations

Numerical schemes for SDEs

AD in Monte Carlo for finance

Implementing Models in Quantitative Finance: Methods and Cases

Pricing with a Smile. Bruno Dupire. Bloomberg

Quantitative Investment: Research and Implementation in MATLAB

Computational Finance

Numerical software & tools for the actuarial community

Introducing LIST. Riccardo Bernini Head of Financial Engineering Enrico Melchioni Head of International Sales. March 2018

MATH6911: Numerical Methods in Finance. Final exam Time: 2:00pm - 5:00pm, April 11, Student Name (print): Student Signature: Student ID:

Heston Stochastic Local Volatility Model

Computational Finance Improving Monte Carlo

for Finance Python Yves Hilpisch Koln Sebastopol Tokyo O'REILLY Farnham Cambridge Beijing

PDE Project Course 1. Adaptive finite element methods

Barrier Option. 2 of 33 3/13/2014

NUMERICAL AND SIMULATION TECHNIQUES IN FINANCE

Monte Carlo Methods in Structuring and Derivatives Pricing

Financial Risk Modeling on Low-power Accelerators: Experimental Performance Evaluation of TK1 with FPGA

Math Option pricing using Quasi Monte Carlo simulation

Adjoint methods for option pricing, Greeks and calibration using PDEs and SDEs

Calibration Lecture 4: LSV and Model Uncertainty

EFFICIENT MONTE CARLO ALGORITHM FOR PRICING BARRIER OPTIONS

Review Direct Integration Discretely observed options Summary QUADRATURE. Dr P. V. Johnson. School of Mathematics

Multi-Asset Options. A Numerical Study VILHELM NIKLASSON FRIDA TIVEDAL. Master s thesis in Engineering Mathematics and Computational Science

Pricing Barrier Options under Local Volatility

The Pennsylvania State University. The Graduate School. Department of Industrial Engineering AMERICAN-ASIAN OPTION PRICING BASED ON MONTE CARLO

Short-time-to-expiry expansion for a digital European put option under the CEV model. November 1, 2017

Optimal Dam Management

Math Computational Finance Option pricing using Brownian bridge and Stratified samlping

Computer Exercise 2 Simulation

King s College London

Quasi-Monte Carlo for Finance

Chapter 15: Jump Processes and Incomplete Markets. 1 Jumps as One Explanation of Incomplete Markets

The Evaluation of American Compound Option Prices under Stochastic Volatility. Carl Chiarella and Boda Kang

Monte Carlo Methods for Uncertainty Quantification

Numerical Methods in Option Pricing (Part III)

Finite Element Method

Reinforcement Learning (1): Discrete MDP, Value Iteration, Policy Iteration

Math Computational Finance Barrier option pricing using Finite Difference Methods (FDM)

Sensitivity analysis for risk-related decision-making

Reinforcement Learning (1): Discrete MDP, Value Iteration, Policy Iteration

PDE Project Course 4. An Introduction to DOLFIN and Puffin

Monte Carlo Methods in Financial Engineering

Calibration and Simulation of Interest Rate Models in MATLAB Kevin Shea, CFA Principal Software Engineer MathWorks

Market Volatility and Risk Proxies

FX Smile Modelling. 9 September September 9, 2008

Monte Carlo Methods for Uncertainty Quantification

MONTE CARLO EXTENSIONS

MATH3075/3975 FINANCIAL MATHEMATICS TUTORIAL PROBLEMS

Valuation of performance-dependent options in a Black- Scholes framework

Risk Neutral Valuation

MSc in Financial Engineering

MASM006 UNIVERSITY OF EXETER SCHOOL OF ENGINEERING, COMPUTER SCIENCE AND MATHEMATICS MATHEMATICAL SCIENCES FINANCIAL MATHEMATICS.

Write legibly. Unreadable answers are worthless.

"Vibrato" Monte Carlo evaluation of Greeks

Math Computational Finance Double barrier option pricing using Quasi Monte Carlo and Brownian Bridge methods

Introduction to Sequential Monte Carlo Methods

MATH60082 Example Sheet 6 Explicit Finite Difference

Numerical Evaluation of American Options Written on Two Underlying Assets using the Fourier Transform Approach

MATH4143: Scientific Computations for Finance Applications Final exam Time: 9:00 am - 12:00 noon, April 18, Student Name (print):

FE610 Stochastic Calculus for Financial Engineers. Stevens Institute of Technology

SYSM 6304: Risk and Decision Analysis Lecture 6: Pricing and Hedging Financial Derivatives

Lab12_sol. November 21, 2017

Pricing Variance Swaps under Stochastic Volatility Model with Regime Switching - Discrete Observations Case

New GPU Pricing Library

Using radial basis functions for option pricing

Introduction to Financial Mathematics

The Forward Kolmogorov Equation for Two Dimensional Options

Lecture outline. Monte Carlo Methods for Uncertainty Quantification. Importance Sampling. Importance Sampling

AMH4 - ADVANCED OPTION PRICING. Contents

American Equity Option Valuation Practical Guide

by Kian Guan Lim Professor of Finance Head, Quantitative Finance Unit Singapore Management University

MODELLING VOLATILITY SURFACES WITH GARCH

Getting Started with CGE Modeling

escript basics Lutz Gross School of Earth Sciences The University of Queensland 1/46 13/03/13

Accelerated Option Pricing Multiple Scenarios

SAQ KONTROLL AB Box 49306, STOCKHOLM, Sweden Tel: ; Fax:

Algorithmic Differentiation of a GPU Accelerated Application

Space-time adaptive finite difference method for European multi-asset options

Monte Carlo Methods for Uncertainty Quantification

The Black-Scholes PDE from Scratch

Forward Monte-Carlo Scheme for PDEs: Multi-Type Marked Branching Diffusions

Modelling for the Financial Markets with Excel

King s College London

Monte Carlo and Empirical Methods for Stochastic Inference (MASM11/FMSN50)

IEOR E4703: Monte-Carlo Simulation

CS 774 Project: Fall 2009 Version: November 27, 2009

- 1 - **** d(lns) = (µ (1/2)σ 2 )dt + σdw t

MFE Course Details. Financial Mathematics & Statistics

Fast Convergence of Regress-later Series Estimators

NEWCASTLE UNIVERSITY SCHOOL OF MATHEMATICS, STATISTICS & PHYSICS SEMESTER 1 SPECIMEN 2 MAS3904. Stochastic Financial Modelling. Time allowed: 2 hours

How to Implement Market Models Using VBA

Financial derivatives exam Winter term 2014/2015

Simple Robust Hedging with Nearby Contracts

Transcription:

Introduction to Financial Computing with Python Matthieu Mariapragassam

Why coding seems so easy? But is actually not Sprezzatura : «It s an art that doesn t seem to be an art» - The Book of the Courtier

Plan Introduction to programming languages Where do researchers and industry stand Fenics and Quantlib Libraries for PDEs and Financial Maths Python

How one writes code Maths problem Face another problem Look for a good method code it find an article

How one writes code It s easy to: Get lost And REINVENT the wheel!

MatLab (Octave, SciLab) R-project C++ Java C# Python SAS S The Usual Suspects

Different types of coding Tests Prototyping Enhanced- Prototyping Production ready code

Different types of coding Tests: Code as fast as possible and get a not too horrible result Matlab / Python / R / VBA

Different types of coding Prototyping: Transform an idea to code fast Reliable and accurate results Performance not a concern Matlab/ Python/ C#/ Java / R

Different type of coding Enhanced-Prototyping: Researchers belong here Scalability becomes more important Performance can be a concern, but is usually not Matlab / Python

Different type of coding Production ready code: Industry belong here Code is shared Scalability is of paramount importance Performance is a top concern Long-term goals C++ Eventually C# / Java

Example: Numerical Solution of a Partial Differential Equation We would like to : Solve the Forward Kolmogorov PDE for the density of the Black model Use the Finite Element method to handle efficiently Dirac delta initial value Compute Black-Scholes prices with the solution Be as efficient as possible

Example: Numerical Solution of a Partial Differential Equation The PDE Solver in Matlab: Black-box Lack of flexibility Don t handle Dirac IV It s quite slow Re-implementing is the only way in Matlab

Example: Numerical Solution of a Partial Differential Equation Approximate figure after implementation: A 1D problem takes 1M A 2D problem takes 10M A 3D problem takes 1H A 3D problem with good refinement takes 1D

Example: Numerical Solution of a Partial Differential Equation Conclusion: Acceptable for a 1D problem No scalability Lot of potential bugs What about external libraries?

Open Source External Libraries Pros No need to re-invent the wheel Robust Plenty of features Very fast and optimized when mature Transparent Scalable Usually coded in C++ only

Open Source External Libraries Cons Installing it can be tough Entry cost is usually very (very) high Documentation can be sparse Debugging is harder Building upon it requires to master it Usually coded in C++ Code can be over-engineered Getting lost in features

Open Source External Libraries: Fenics FEniCS has an extensive list of features for automated, efficient solution of differential equations, including automated solution of variational problems, automated error control and adaptivity, a comprehensive library of finite elements, high performance linear algebra and many more.

Open Source External Libraries: Fenics Coded in C++ Can handle up to 3 dimensions Specific BC or Subdomain equations Can handle very complex problems Can either be called from C++ or Python

Introduction to Python and Library Interfacing

Python Interpreted language Object-Oriented Syntax and Readability are key A Real programming language Fast when vectorized Better than MatLab when not Slower than C++ Packages are not as good as in Matlab

Python For Matrices, rely on NumPy:

Python For numerical toolbox, rely on SciPy: constants: physical optimize: optimization algorithms cluster: hierarchical clustering, vector quantization signal: signal processing tools fftpack: Discrete Fourier Transform algorithms sparse: sparse matrix and related algorithms integrate: numerical integration routines spatial: KD-trees, nearest neighbors, distance functions interpolate: interpolation tools io: data input and output special: special functions lib: Python wrappers to external libraries stats: statistical functions linalg: linear algebra routines weave: tool for writing C/C++ code misc: miscellaneous utilities (e.g. image reading/writing) ndimage: various functions for multi-dimensional image processing optimize: optimization algorithms including linear programming

Driving Example: Pricing a Call Option under Black-Scholes with: Pure Python libraries SciPy/NumPy and Integration of the payoff x density Use Fenics C++ Lib to solve the Fokker-Plank equation and integrate payoff x density Use QuantLib C++ Lib (SWIG) and price with Monte-Carlo

Results comes first: Driving Example:

Fenics FEM Library Fenics use UFL to define a PDE under weak form Fokker-Planck equation of for the density of S under Black: φ t xφ + r q x 1 2 x 2 σ 2 φ 2 x = 0 Initial value: φ s, 0 = δ(s = s 0 ) Boundary conditions: φ 0, t = φ, t = 0

Fenics FEM Library The space Ω on which we want to solve the PDE is Ω = [0; [ Let s assume no rates or dividends. Let v be a test function. The weak formulation of the PDE is: Ω φ t vdω+ Ω 1 2 x2 σ 2 φ v dω+ x x Ω 1 x 2 σ 2 2 x v φ dω = 0 x The dirac delta IV condition becomes: Ωφ t = 0 vdω = v(s0)

Fenics FEM Library Full implicit time scheme: We note φ n = φ(t n ) Ω φ t = 0 vdω = v(s 0 ) Ω φ n 1 vdω + dt Ω 2 x2 σ 2 φn v dω+ x x Ω Ω φn 1 vdω 1 x 2 σ 2 2 x v φn dω = x

Fenics FEM Library

Scipy Toolbox Exercise: Price a Call Option with the Scipy toolbox s0=100.; K= 95; T=1.0; vol = 0.20

Scipy Toolbox Idea: Using SciPy Integration combined with the log-normal density Use the documentation tool of Spyder The density function f of S T under the Black-Scholes model E S T K + = 0 xf x dx ln S t = ln S 0 σ2 t 2 + σw t Density is log-normal with scale factor e ln S 0 σ2 t 2 and shape factor σ

QuantLib FinEng Library It s a full C++ solution Very over-engineered It makes it s beauty, for a geek Entry cost is high, very high: In Python it s okay It s a powerfull toolbox!

QuantLib FinEng Library Exercise: Price a Call Option with the QuantLib Monte-Carlo Engine Use Sobol sequences s0=100.; K= 95; T=1.0; vol = 0.20

QuantLib FinEng Library Steps: 1. Access the SWIG mappings between C++/Python: https://github.com/lballabio/quantlib/tree/master/quantlib-swig/swig 1. Define the Vanilla contract 2. Define the Random Number Generator 3. Define the Path Generator 4. Write the Monte-Carlo Loop 5. Compute Mean and StdDev using Numpy Use the debugger to check the values

QuantLib FinEng Library Exercise 2: Define a StochasticProcess python class Define a LocalVolatilityProcess deriving from it Price an Up-and-Out Call option with QuantLib MC engine and compare LocalVol and Black-Scholes prices. s0=100.; K= 80;B=120; T=1.0; volbs = 0.20 localvol = ax 2 + bx + c, such that c b2 4a = 0.18 Extrapolated flat after ± 6 Black-Scholes stddev Test limit cases to validate the pricer

Conclusion Python is the perfect Glue: Interface C++ libraries Use Native Python libraries Identify bottlenecks and compile them in C Best of both worlds! Python is a real Programming OO language! Open, Free and Multi-Platform!

Questions? THANKS