Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

11

Transcript of Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Page 1: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341
Page 2: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Yves Hilpisch

Python for Finance

Page 3: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Python for Financeby Yves Hilpisch

Copyright © 2010 Yves Hilpisch. All rights reserved.

Printed in the United States of America.

Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions arealso available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected].

Editors: Brian MacDonald and Meghan BlanchetteProduction Editor: Copyeditor: FIX ME!Proofreader: FIX ME!

Indexer: FIX ME!Cover Designer: Karen MontgomeryInterior Designer: David FutatoIllustrator: Rebecca Demarest

November 2014: First Edition

Revision History for the First Edition:

2014-03-05: Early relese revision 1

See http://oreilly.com/catalog/errata.csp?isbn=9781491945285 for release details.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’ReillyMedia, Inc. !!FILL THIS IN!! and related trade dress are trademarks of O’Reilly Media, Inc.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademarkclaim, the designations have been printed in caps or initial caps.

While every precaution has been taken in the preparation of this book, the publisher and authors assumeno responsibility for errors or omissions, or for damages resulting from the use of the information containedherein.

ISBN: 978-1-491-94528-5

[?]

Page 4: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Table of Contents

Part I. Python and Finance

1. Why Python for Finance?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3What is Python? 3

Brief History of Python 5The Python Ecosystem 5Python User Spectrum 7The Scientific Stack 8

Technology in Finance 9Technology Spending 9Technology as Enabler 10Technology and Talent as Barriers to Entry 10Ever Increasing Speeds, Frequencies, Data Volumes 11The Rise of Real-Time Analytics 12

Python for Finance 13Finance and Python Syntax 13Efficiency and Productivity Through Python 17From Prototyping to Production 21

Conclusions 22Further Reading 22

2. Infrastructure and Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Python Deployment 26

Anaconda 26Wakari 32

Tools 33Python 34IPython 34Spyder 44

iii

Page 5: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Conclusions 46Further Reading 47

3. Introductory Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Implied Volatilities 49Monte Carlo Simulation 59

Pure Python 61Vectorization with NumPy 63Full Vectorization with Log Euler Scheme 65Graphical Analysis 66

Technical Analysis 68Conclusions 74Further Reading 75

Part II. Financial Analytics and Development

4. Data Types and Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Basic Data Types 80

Integers 80Floats 81Strings 83

Basic Data Structures 86Tuples 86Lists 87Excursion: Control Structures 89Excursion: Functional Programming 91Dicts 92Sets 93

NumPy Data Structures 94Arrays with Python Lists 95Regular NumPy Arrays 96Structured Arrays 100

Vectorization of Code 101Basic Vectorization 101Memory Layout 103

Conclusions 105Further Reading 105

5. Data Visualization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Two-Dimensional Plotting 107

One-Dimensional Data Set 108

iv | Table of Contents

Page 6: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Two-Dimensional Data Set 113Other Plot Styles 120

Financial Plots 1283d Plotting 132Conclusions 135Further Reading 135

6. Financial Time Series. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139pandas Basics 140

First Steps with DataFrame Class 140Second Steps with DataFrame Class 144Basic Analytics 148TimeSeries Class 151GroupBy Operations 152

Financial Data 154Regression Analysis 160High Frequency Data 169Conclusions 173Further Reading 174

7. Input-Output Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Basic I/O with Python 176

Writing Objects to Disk 176Reading and Writing Text Files 179SQL Databases 181Writing and Reading Numpy Arrays 183

I/O with pandas 185SQL Database 186From SQL to pandas 187Data as CSV File 190Data as Excel File 191

Fast I/O with PyTables 192Working with Tables 193Working with Compressed Tables 197Working with Arrays 198Out-of-Memory Computations 199

Conclusions 201Further Reading 202

8. Performance Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Python Paradigms and Performance 204Memory Layout and Performance 207

Table of Contents | v

Page 7: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Parallel Computing 209The Monte Carlo Algorithm 209The Sequential Calculation 210The Parallel Calculation 211Performance Comparison 214

Dynamic Compiling 215Introductory Example 215Binomial Option Pricing 216

Static Compiling with Cython 221Generation of Random Numbers on GPUs 224Conclusions 228Further Reading 228

9. Mathematical Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Approximation 231

Regression 232Interpolation 244

Convex Optimization 247Global Optimization 249Local Optimization 250Constrained Optimization 251

Integration 253Numerical Integration 255Integration by Simulation 256

Symbolic Computation 256Basics 257Equations 258Integration 259Differentiation 260

Conclusions 261Further Reading 262

10. Stochastics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256Random Numbers 264Simulation 269

Random Variables 269Stochastic Processes 272Variance Reduction 286

Valuation 289European Options 289American Options 293

Risk Measures 296

vi | Table of Contents

Page 8: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Value-at-Risk 296Credit Value Adjustments 300

Conclusions 303Further Reading 303

11. Statistics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305Normality Tests 306

Benchmark Case 307Real World Data 315

Portfolio Optimization 321The Data 321The Basic Theory 323Portfolio Optimizations 326Efficient Frontier 329Capital Market Line 331

Principal Component Analysis 334DAX Index and its 30 Stocks 335Applying PCA 335Constructing a PCA Index 336

Bayesian Regression 339Bayes’ Formula 340PyMC3 341Introductory Example 341Real Data 345

Conclusions 353Further Reading 354

12. Excel Integration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357Basic Spreadsheet Interaction 358

Generating Workbooks (xls) 359Generating Workbooks (xslx) 360Reading from Workbooks 362Using OpenPyXL 364Using pandas for Reading and Writing 366

Scripting Excel with Python 369Installing DataNitro 369Working with DataNitro 370

Conclusions 377Further Reading 378

13. Object Orientation and Graphical User Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379Object Orientation 379

Table of Contents | vii

Page 9: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Basics of Python Classes 380Simple Short Rate Class 385Cash Flow Series Class 389

Graphical User Interfaces 391Short Rate Class with GUI 391Updating of Values 394Cash Flows Series Class with GUI 396

Conclusions 399Further Reading 399

14. Web Integration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Web Basics 402

ftplib 402httplib 405urllib 406

Web Plotting 408Static Plots 408Interactive Plots 411Real-Time Plots 414

Rapid Web Applications 421Traders’ Chat Room 423Data Modeling 423The Python Code 424Templating 431Styling 436

Web Services 438The Financial Model 439The Implementation 440

Conclusions 447Further Reading 448

Part III. Derivatives Analytics Library

15. Valuation Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451Fundamental Theorem of Asset Pricing 451

A Simple Example 452The General Results 453

Risk-Neutral Discounting 454Modelling and Handling Dates 454Constant Short Rate 456

Market Environments 458

viii | Table of Contents

Page 10: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

Conclusions 461Further Reading 462

16. Simulation of Financial Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463Random Number Generation 464Generic Simulation Class 466Geometric Brownian Motion 469

The Simulation Class 469A Use Case 471

Jump Diffusion 473The Simulation Class 474A Use Case 476

Square-Root Diffusion 478The Simulation Class 478A Use Case 480

Conclusions 482Further Reading 482

17. Derivatives Valuation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485Generic Valuation Class 485European Exercise 489

The Valuation Class 490A Use Case 491

American Excercise 497Least-Squares Monte Carlo 497The Valuation Class 499A Use Case 500

Conclusions 503Further Reading 504

18. Portfolio Valuation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505Derivatives Positions 506

The Class 506A Use Case 508

Derivatives Portfolios 509The Class 509A Use Case 513

Conclusions 517Further Reading 518

19. Volatility Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521The VSTOXX Data 522

Table of Contents | ix

Page 11: Python for Finance - Hilpisch · Bayes’ Formula 340 PyMC3 341 Introductory Example 341

VSTOXX Index Data 522VSTOXX Futures Data 523VSTOXX Options Data 525

Model Calibration 527Relevant Market Data 527Option Modelling 528Calibration Procedure 530

American Options on the VSTOXX 535Modelling Option Positions 535The Options Portfolio 537

Conclusions 538Further Reading 538

A. Selected Best Practices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541

B. Call Option Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549

x | Table of Contents