This complete guide to C++ and computational finance is a follow-up and major extension to Daniel J. Duffy's 2004 edition of Financial Instrument Pricing Using C++. Both C++ and computational finance have evolved and changed dramatically in the last ten years and this book documents these improvements. Duffy focuses on these developments and the advantages for the quant developer by:
- Delving into a detailed account of the new C++11 standard and its applicability to computational finance.
- Using de-facto standard libraries, such as Boost and Eigen to improve developer productivity.
- Developing multiparadigm software using the object-oriented, generic, and functional programming styles.
- Designing flexible numerical algorithms: modern numerical methods and multiparadigm design patterns.
- Providing a detailed explanation of the Finite Difference Methods through six chapters, including new developments such as ADE, Method of Lines (MOL), and Uncertain Volatility Models.
- Developing applications, from financial model to algorithmic design and code, through a coherent approach.
- Generating interoperability with Excel add-ins, C#, and C++/CLI.
- Using random number generation in C++11 and Monte Carlo simulation.
Duffy adopted a spiral model approach while writing each chapter of Financial Instrument Pricing Using C++ 2e: analyse a little, design a little, and code a little. Each cycle ends with a working prototype in C++ and shows how a given algorithm or numerical method works. Additionally, each chapter contains non-trivial exercises and projects that discuss improvements and extensions to the material.
This book is for designers and application developers in computational finance, and assumes the reader has some fundamental experience of C++ and derivatives pricing.
HOW TO RECEIVE THE SOURCE CODE
Once you have purchased a copy of the book please send an email to the author dduffyATdatasim.nl requesting your personal and non-transferable copy of the source code. Proof of purchase is needed. The subject of the mail should be “C++ Book Source Code Request”. You will receive a reply with a zip file attachment.
Table of Contents
CHAPTER 1 A Tour of C++ and Environs 1
1.1 Introduction and Objectives 1
1.2 What is C++? 1
1.3 C++ as a Multiparadigm Programming Language 2
1.4 The Structure and Contents of this Book: Overview 4
1.5 A Tour of C++11: Black-Scholes and Environs 6
1.6 Parallel Programming in C++ and Parallel C++ Libraries 12
1.7 Writing C++ Applications; Where and How to Start? 14
1.8 For whom is this Book Intended? 16
1.9 Next-Generation Design and Design Patterns in C++ 16
1.10 Some Useful Guidelines and Developer Folklore 17
1.11 About the Author 18
1.12 The Source Code and Getting the Source Code 19
CHAPTER 2 New and Improved C++ Fundamentals 21
2.1 Introduction and Objectives 21
2.2 The C++ Smart Pointers 21
2.3 Using Smart Pointers in Code 23
2.4 Extended Examples of Smart Pointers Usage 30
2.5 Move Semantics and Rvalue References 34
2.6 Other Bits and Pieces: Usability Enhancements 39
2.7 Summary and Conclusions 52
2.8 Exercises and Projects 52
CHAPTER 3 Modelling Functions in C++ 59
3.1 Introduction and Objectives 59
3.2 Analysing and Classifying Functions 60
3.3 New Functionality in C++: std::function<> 64
3.4 New Functionality in C++: Lambda Functions and Lambda Expressions 65
3.5 Callable Objects 69
3.6 Function Adapters and Binders 70
3.7 Application Areas 75
3.8 An Example: Strategy Pattern New Style 75
3.9 Migrating from Traditional Object-Oriented Solutions: Numerical Quadrature 78
3.10 Summary and Conclusions 81
3.11 Exercises and Projects 82
CHAPTER 4 Advanced C++ Template Programming 89
4.1 Introduction and Objectives 89
4.2 Preliminaries 91
4.3 decltype Specifier 94
4.4 Life Before and After decltype 101
4.5 std::result_of and SFINAE 106
4.6 std::enable_if 108
4.7 Boost enable_if 112
4.8 std::decay()Trait 114
4.9 A Small Application: Quantities and Units 115
4.10 Conclusions and Summary 118
4.11 Exercises and Projects 118
CHAPTER 5 Tuples in C++ and their Applications 123
5.1 Introduction and Objectives 123
5.2 An std:pair Refresher and New Extensions 123
5.3 Mathematical and Computer Science Background 128
5.4 Tuple Fundamentals and Simple Examples 130
5.5 Advanced Tuples 130
5.6 Using Tuples in Code 133
5.7 Other Related Libraries 138
5.8 Tuples and Run-Time Efficiency 140
5.9 Advantages and Applications of Tuples 142
5.10 Summary and Conclusions 143
5.11 Exercises and Projects 143
CHAPTER 6 Type Traits, Advanced Lambdas and Multiparadigm Design in C++ 147
6.1 Introduction and Objectives 147
6.2 Some Building Blocks 149
6.3 C++ Type Traits 150
6.4 Initial Examples of Type Traits 158
6.5 Generic Lambdas 161
6.6 How Useful will Generic Lambda Functions be in the Future? 164
6.7 Generalised Lambda Capture 171
6.7.1 Living Without Generalised Lambda Capture 173
6.8 Application to Stochastic Differential Equations 174
6.9 Emerging Multiparadigm Design Patterns: Summary 178
6.10 Summary and Conclusions 179
6.11 Exercises and Projects 179
CHAPTER 7 Multiparadigm Design in C++ 185
7.1 Introduction and Objectives 185
7.2 Modelling and Design 185
7.3 Low-Level C++ Design of Classes 190
7.4 Shades of Polymorphism 199
7.5 Is there More to Life than Inheritance? 206
7.6 An Introduction to Object-Oriented Software Metrics 207
7.7 Summary and Conclusions 210
7.8 Exercises and Projects 210
CHAPTER 8 C++ Numerics, IEEE 754 and Boost C++ Multiprecision 215
8.1 Introduction and Objectives 215
8.2 Floating-Point Decomposition Functions in C++ 219
8.3 A Tour of std::numeric_limits 221
8.4 An Introduction to Error Analysis 223
8.5 Example: Numerical Quadrature 224
8.6 Other Useful Mathematical Functions in C++ 228
8.7 Creating C++ Libraries 231
8.8 Summary and Conclusions 239
8.9 Exercises and Projects 239
CHAPTER 9 An Introduction to Unified Software Design 245
9.1 Introduction and Objectives 245
9.1.1 Future Predictions and Expectations 246
9.2 Background 247
9.3 System Scoping and Initial Decomposition 251
9.4 Checklist and Looking Back 259
9.5 Variants of the Software Process: Policy-Based Design 260
9.6 Using Policy-Based Design for the DVM Problem 268
9.7 Advantages of Uniform Design Approach 273
9.8 Summary and Conclusions 274
9.9 Exercises and Projects 275
CHAPTER 10 New Data Types, Containers and Algorithms in C++ and Boost C++ Libraries 283
10.1 Introduction and Objectives 283
10.2 Overview of New Features 283
10.3 C++ std::bitset and Boost Dynamic Bitset Library 284
10.4 Chrono Library 288
10.5 Boost Date and Time 301
10.6 Forwards Lists and Compile-Time Arrays 306
10.7 Applications of Boost.Array 311
10.8 Boost uBLAS (Matrix Library) 313
10.9 Vectors 316
10.10 Matrices 318
10.11 Applying uBLAS: Solving Linear Systems of Equations 322
10.12 Summary and Conclusions 330
10.13 Exercises and Projects 331
CHAPTER 11 Lattice Models Fundamental Data Structures and Algorithms 333
11.1 Introduction and Objectives 333
11.2 Background and Current Approaches to Lattice Modelling 334
11.3 New Requirements and Use Cases 335
11.4 A New Design Approach: A Layered Approach 335
11.5 Initial ‘101’ Examples of Option Pricing 347
11.6 Advantages of Software Layering 349
11.7 Improving Efficiency and Reliability 352
11.8 Merging Lattices 355
11.9 Summary and Conclusions 357
11.10 Exercises and Projects 357
CHAPTER 12 Lattice Models Applications to Computational Finance 367
12.1 Introduction and Objectives 367
12.2 Stress Testing the Lattice Data Structures 368
12.3 Option Pricing Using Bernoulli Paths 372
12.4 Binomial Model for Assets with Dividends 374
12.5 Computing Option Sensitivities 377
12.6 (Quick) Numerical Analysis of the Binomial Method 379
12.7 Richardson Extrapolation with Binomial Lattices 382
12.8 Two-Dimensional Binomial Method 382
12.9 Trinomial Model of the Asset Price 384
12.10 Stability and Convergence of the Trinomial Method 385
12.11 Explicit Finite Difference Method 386
12.12 Summary and Conclusions 389
12.13 Exercises and Projects 389
CHAPTER 13 Numerical Linear Algebra: Tridiagonal Systems and Applications 395
13.1 Introduction and Objectives 395
13.2 Solving Tridiagonal Matrix Systems 395
13.3 The Crank-Nicolson and Theta Methods 406
13.4 The ADE Method for the Impatient 411
13.5 Cubic Spline Interpolation 415
13.6 Some Handy Utilities 427
13.7 Summary and Conclusions 428
13.8 Exercises and Projects 429
CHAPTER 14 Data Visualisation in Excel 433
14.1 Introduction and Objectives 433
14.2 The Structure of Excel-Related Objects 433
14.3 Sanity Check: Is the Excel Infrastructure Up and Running? 435
14.4 ExcelDriver and Matrices 437
14.5 ExcelDriver and Vectors 444
14.6 Path Generation for Stochastic Differential Equations 448
14.7 Summary and Conclusions 459
14.8 Exercises and Projects 459
14.9 Appendix: COM Architecture Overview 463
14.10 An Example 468
14.11 Virtual Function Tables 471
14.12 Differences between COM and Object-Oriented Paradigm 473
14.13 Initialising the COM Library 474
CHAPTER 15 Univariate Statistical Distributions 475
15.1 Introduction, Goals and Objectives 475
15.2 The Error Function and Its Universality 475
15.3 One-Factor Plain Options 478
15.4 Option Sensitivities and Surfaces 488
15.5 Automating Data Generation 491
15.6 Introduction to Statistical Distributions and Functions 499
15.7 Advanced Distributions 504
15.8 Summary and Conclusions 511
15.9 Exercises and Projects 511
CHAPTER 16 Bivariate Statistical Distributions and Two-Asset Option Pricing 515
16.1 Introduction and Objectives 515
16.2 Computing Integrals Using PDEs 516
16.3 The Drezner Algorithm 521
16.4 The Genz Algorithm and the West/Quantlib Implementations 521
16.5 Abramowitz and Stegun Approximation 525
16.6 Performance Testing 528
16.7 Gauss-Legendre Integration 529
16.8 Applications to Two-Asset Pricing 531
16.9 Trivariate Normal Distribution 536
16.10 Chooser Options 543
16.11 Conclusions and Summary 545
16.12 Exercises and Projects 546
CHAPTER 17 STL Algorithms in Detail 551
17.1 Introduction and Objectives 551
17.2 Binders and std::bind 554
17.3 Non-modifying Algorithms 557
17.4 Modifying Algorithms 567
17.5 Compile-Time Arrays 575
17.6 Summary and Conclusions 576
17.7 Exercises and Projects 576
17.8 Appendix: Review of STL Containers and Complexity Analysis 583
CHAPTER 18 STL Algorithms Part II 589
18.1 Introduction and Objectives 589
18.2 Mutating Algorithms 589
18.3 Numeric Algorithms 597
18.4 Sorting Algorithms 601
18.5 Sorted-Range Algorithms 604
18.5.5 Merging 608
18.6 Auxiliary Iterator Functions 609
18.7 Needle in a Haystack: Finding the Right STL Algorithm 612
18.8 Applications to Computational Finance 613
18.9 Advantages of STL Algorithms 613
18.10 Summary and Conclusions 614
18.11 Exercises and Projects 614
CHAPTER 19 An Introduction to Optimisation and the Solution of Nonlinear Equations 617
19.1 Introduction and Objectives 617
19.2 Mathematical and Numerical Background 618
19.3 Sequential Search Methods 619
19.4 Solutions of Nonlinear Equations 620
19.5 Fixed-Point Iteration 622
19.6 Aitken’s Acceleration Process 623
19.7 Software Framework 623
19.8 Implied Volatility 632
19.9 Solvers in the Boost C++ Libraries 632
19.10 Summary and Conclusions 633
19.11 Exercises and Projects 633
19.12 Appendix: The Banach Fixed-Point Theorem 636
CHAPTER 20 The Finite Difference Method for PDEs: Mathematical Background 641
20.1 Introduction and Objectives 641
20.2 General Convection-Diffusion-Reaction Equations and Black-Scholes PDE 641
20.3 PDE Preprocessing 64520.3.2 Reduction of PDE to Conservative Form 646
20.4 Maximum Principles for Parabolic PDEs 649
20.5 The Fichera Theory 650
20.6 Finite Difference Schemes: Properties and Requirements 654
20.7 Example: A Linear Two-Point Boundary Value Problem 655
20.8 Exponentially Fitted Schemes for Time-Dependent PDEs 659
20.9 Richardson Extrapolation 663
20.10 Summary and Conclusions 665
20.11 Exercises and Projects 666
CHAPTER 21 Software Framework for One-Factor Option Models 669
21.1 Introduction and Objectives 669
21.2 A Software Framework: Architecture and Context 669
21.3 Modelling PDEs and Finite Difference Schemes: What is Supported? 670
21.4 Several Versions of Alternating Direction Explicit 671
21.5 A Software Framework: Detailed Design and Implementation 673
21.6 C++ Code for PDE Classes 674
21.7 C++ Code for FDM Classes 679
21.8 Examples and Test Cases 690
21.9 Summary and Conclusions 693
21.10 Exercises and Projects 694
CHAPTER 22 Extending the Software Framework 701
22.1 Introduction and Objectives 701
22.2 Spline Interpolation of Option Values 701
22.3 Numerical Differentiation Foundations 704
22.4 Numerical Greeks 710
22.5 Constant Elasticity of Variance Model 715
22.6 Using Software Design (GOF) Patterns 715
22.7 Multiparadigm Design Patterns 720
22.8 Summary and Conclusions 721
22.9 Exercises and Projects 721
CHAPTER 23A PDE Software Framework in C++11 for a Class of Path-Dependent Options 727
23.1 Introduction and Objectives 727
23.2 Modelling PDEs and Initial Boundary Value Problems in the Functional Programming Style 728
23.3 PDE Preprocessing 731
23.4 The Anchoring PDE 732
23.5 ADE for Anchoring PDE 739
23.6 Useful Utilities 746
23.7 Accuracy and Performance 748
23.8 Summary and Conclusions 750
23.9 Exercises and Projects 751
CHAPTER 24 Ordinary Differential Equations and their Numerical Approximation 755
24.1 Introduction and Objectives 755
24.2 What is an ODE? 755
24.3 Classifying ODEs 756
24.4 A Palette of Model ODEs 757
24.5 Existence and Uniqueness Results 760
24.6 Overview of Numerical Methods for ODEs: The Big Picture 763
24.7 Creating ODE Solvers in C++ 770
24.8 Summary and Conclusions 776
24.9 Exercises and Projects 776
24.10 Appendix 778
CHAPTER 25 Advanced Ordinary Differential Equations and Method of Lines 781
25.1 Introduction and Objectives 781
25.2 An Introduction to the Boost Odeint Library 782
25.3 Systems of Stiff and Non-stiff Equations 791
25.4 Matrix Differential Equations 796
25.5 The Method of Lines: What is it and what are its Advantages? 799
25.6 Initial Foray in Computational Finance: MOL for One-Factor Black-Scholes PDE 801
25.7 Barrier Options 806
25.8 Using Exponential Fitting of Barrier Options 808
25.9 Summary and Conclusions 808
25.10 Exercises and Projects 809
CHAPTER 26 Random Number Generation and Distributions 819
26.1 Introduction and Objectives 819
26.2 What is a Random Number Generator? 820
26.3 What is a Distribution? 821
26.4 Some Initial Examples 825
26.5 Engines in Detail 827
26.6 Distributions in C++: The List 830
26.7 Back to the Future: C-Style Pseudo-Random Number Generation 831
26.8 Cryptographic Generators 833
26.9 Matrix Decomposition Methods 833
26.10 Generating Random Numbers 845
26.11 Summary and Conclusions 848
26.12 Exercises and Projects 849
CHAPTER 27 Microsoft .Net, C# and C++11 Interoperability 853
27.1 Introduction and Objectives 853
27.2 The Big Picture 854
27.3 Types 858
27.4 Memory Management 859
27.5 An Introduction to Native Classes 861
27.6 Interfaces and Abstract Classes 861
27.7 Use Case: C++/CLI as ‘Main Language’ 862
27.8 Use Case: Creating Proxies, Adapters and Wrappers for Legacy C++ Applications 864
27.8.1 Alternative: SWIG (Simplified Wrapper and Interface Generator) 871
27.9 ‘Back to the Future’ Use Case: Calling C# Code from C++11 872
27.10 Modelling Event-Driven Applications with Delegates 876
27.11 Use Case: Interfacing with Legacy Code 886
27.12 Assemblies and Namespaces for C++/CLI 889
27.13 Summary and Conclusions 895
27.14 Exercises and Projects 896
CHAPTER 28 C++ Concurrency, Part I Threads 899
28.1 Introduction and Objectives 899
28.2 Thread Fundamentals 900
28.3 Six Ways to Create a Thread 903
28.4 Intermezzo: Parallelising the Binomial Method 909
28.5 Atomics 916
28.6 Smart Pointers and the Thread-Safe Pointer Interface 924
28.7 Thread Synchronisation 926
28.8 When should we use Threads? 929
28.9 Summary and Conclusions 929
28.10 Exercises and Projects 930
CHAPTER 29 C++ Concurrency, Part II Tasks 935
29.1 Introduction and Objectives 935
29.2 Finding Concurrency: Motivation 936
29.3 Tasks and Task Decomposition 937
29.4 Futures and Promises 941
29.5 Shared Futures 945
29.6 Waiting on Tasks to Complete 948
29.7 Continuations and Futures in Boost 950
29.8 Pure Functions 952
29.9 Tasks versus Threads 953
29.10 Parallel Design Patterns 953
29.11 Summary and Conclusions 955
29.12 Quizzes, Exercises and Projects 955
CHAPTER 30 Parallel Patterns Language (PPL) 961
30.1 Introduction and Objectives 961
30.2 Parallel Algorithms 962
30.3 Partitioning Work 967
30.4 The Aggregation/Reduction Pattern in PPL 971
30.5 Concurrent Containers 977
30.6 An Introduction to the Asynchronous Agents Library and Event-Based Systems 978
30.7 A Design Plan to Implement a Framework Using Message Passing and Other Approaches 986
30.8 Summary and Conclusions 989
30.9 Exercises and Projects 990
CHAPTER 31 Monte Carlo Simulation, Part I 993
31.1 Introduction and Objectives 993
31.2 The Boost Parameters Library for the Impatient 995
31.3 Monte Carlo Version 1: The Monolith Program (‘Ball of Mud’) 1000
31.4 Policy-Based Design: Dynamic Polymorphism 1003
31.5 Policy-Based Design Approach: CRTP and Static Polymorphism 1011
31.6 Builders and their Subcontractors (Factory Method Pattern) 1013
31.7 Practical Issue: Structuring the Project Directory and File Contents 1014
31.8 Summary and Conclusions 1016
31.9 Exercises and Projects 1017
CHAPTER 32 Monte Carlo Simulation, Part II 1023
32.1 Introduction and Objectives 1023
32.2 Parallel Processing and Monte Carlo Simulation 1023
32.3 A Family of Predictor-Corrector Schemes 1033
32.4 An Example (CEV Model) 1038
32.5 Implementing the Monte Carlo Method Using the Asynchronous Agents Library 1041
32.6 Summary and Conclusions 1047
32.7 Exercises and Projects 1050
Appendix 1: Multiple-Precision Arithmetic 1053
Appendix 2: Computing Implied Volatility 1075
References 1109
Index 1117