O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Financial Instrument Pricing Using C++, 2nd Edition

Book Description

An integrated guide to C++ and computational finance

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

  1. Cover
  2. Title page
  3. Copyright page
  4. Chapter 1 A Tour of C++ and Environs
    1. 1.1 Introduction and Objectives
    2. 1.2 What is C++?
    3. 1.3 C++ As a Multiparadigm Programming Language
    4. 1.4 The Structure and Contents of this Book: Overview
    5. 1.5 A Tour of C++11: Black–Scholes and Environs
    6. 1.6 Parallel Programming in C++ and Parallel C++ Libraries
    7. 1.7 Writing C++ Applications; Where and How to Start?
    8. 1.8 For Whom is this Book Intended?
    9. 1.9 Next-Generation Design and Design Patterns in C++
    10. 1.10 Some Useful Guidelines and Developer Folklore
    11. 1.11 About the Author
    12. 1.12 The Source Code and Getting the Source Code
  5. Chapter 2 New and Improved C++ Fundamentals
    1. 2.1 Introduction and Objectives
    2. 2.2 The C++ Smart Pointers
    3. 2.3 Using Smart Pointers in Code
    4. 2.4 Extended Examples of Smart Pointers Usage
    5. 2.5 Move Semantics and Rvalue References
    6. 2.6 Other Bits and Pieces: Usability Enhancements
    7. 2.7 Summary and Conclusions
    8. 2.8 Exercises and Projects
  6. Chapter 3 Modelling Functions in C++
    1. 3.1 Introduction and Objectives
    2. 3.2 Analysing and Classifying Functions
    3. 3.3 New Functionality in C++: std::function<>
    4. 3.4 New Functionality in C++: Lambda Functions and Lambda Expressions
    5. 3.5 Callable Objects
    6. 3.6 Function Adapters and Binders
    7. 3.7 Application Areas
    8. 3.8 An Example: Strategy Pattern New Style
    9. 3.9 Migrating from Traditional Object-Oriented Solutions: Numerical Quadrature
    10. 3.10 Summary and Conclusions
    11. 3.11 Exercises and Projects
  7. Chapter 4 Advanced C++ Template Programming
    1. 4.1 Introduction and Objectives
    2. 4.2 Preliminaries
    3. 4.3 decltype Specifier
    4. 4.4 Life Before and After decltype
    5. 4.5 std::result_of and SFINAE
    6. 4.6 std::enable_if
    7. 4.7 Boost enable_if
    8. 4.8 std::decay()Trait
    9. 4.9 A Small Application: Quantities and Units
    10. 4.10 Conclusions and Summary
    11. 4.11 Exercises and Projects
  8. Chapter 5 Tuples in C++ and their Applications
    1. 5.1 Introduction and Objectives
    2. 5.2 An std::pair Refresher and New Extensions
    3. 5.3 Mathematical and Computer Science Background
    4. 5.4 Tuple Fundamentals and Simple Examples
    5. 5.5 Advanced Tuples
    6. 5.6 Using Tuples in Code
    7. 5.7 Other Related Libraries
    8. 5.8 Tuples and Run-Time Efficiency
    9. 5.9 Advantages and Applications of Tuples
    10. 5.10 Summary and Conclusions
    11. 5.11 Exercises and Projects
  9. Chapter 6 Type Traits, Advanced Lambdas and Multiparadigm Design in C++
    1. 6.1 Introduction and Objectives
    2. 6.2 Some Building Blocks
    3. 6.3 C++ Type Traits
    4. 6.4 Initial Examples of Type Traits
    5. 6.5 Generic Lambdas
    6. 6.6 How Useful will Generic Lambda Functions be in the Future?
    7. 6.7 Generalised Lambda Capture
    8. 6.8 Application to Stochastic Differential Equations
    9. 6.9 Emerging Multiparadigm Design Patterns: Summary
    10. 6.10 Summary and Conclusions
    11. 6.11 Exercises and Projects
  10. Chapter 7 Multiparadigm Design in C++
    1. 7.1 Introduction and Objectives
    2. 7.2 Modelling and Design
    3. 7.3 Low-Level C++ Design of Classes
    4. 7.4 Shades of Polymorphism
    5. 7.5 Is there More to Life than Inheritance?
    6. 7.6 An Introduction to Object-Oriented Software Metrics
    7. 7.7 Summary and Conclusions
    8. 7.8 Exercises and Projects
  11. Chapter 8 C++ Numerics, IEEE 754 and Boost C++ Multiprecision
    1. 8.1 Introduction and Objectives
    2. 8.2 Floating-Point Decomposition Functions in C++
    3. 8.3 A Tour of std::numeric_limits<T>
    4. 8.4 An Introduction to Error Analysis
    5. 8.5 Example: Numerical Quadrature
    6. 8.6 Other Useful Mathematical Functions in C++
    7. 8.7 Creating C++ Libraries
    8. 8.8 Summary and Conclusions
    9. 8.9 Exercises and Projects
  12. Chapter 9 An Introduction to Unified Software Design
    1. 9.1 Introduction and Objectives
    2. 9.2 Background
    3. 9.3 System Scoping and Initial Decomposition
    4. 9.4 Checklist and Looking Back
    5. 9.5 Variants of the Software Process: Policy-Based Design
    6. 9.6 Using Policy-Based Design for the DVM Problem
    7. 9.7 Advantages of Uniform Design Approach
    8. 9.8 Summary and Conclusions
    9. 9.9 Exercises and Projects
  13. Chapter 10 New Data Types, Containers and Algorithms in C++ and Boost C++ Libraries
    1. 10.1 Introduction and Objectives
    2. 10.2 Overview of New Features
    3. 10.3 C++ std::bitset<N> and Boost Dynamic Bitset Library
    4. 10.4 Chrono Library
    5. 10.5 Boost Date and Time
    6. 10.6 Forwards Lists and Compile-Time Arrays
    7. 10.7 Applications of Boost.Array
    8. 10.8 Boost uBLAS (Matrix Library)
    9. 10.9 Vectors
    10. 10.10 Matrices
    11. 10.11 Applying uBLAS: Solving Linear Systems of Equations
    12. 10.12 Summary and Conclusions
    13. 10.13 Exercises and Projects
  14. Chapter 11 Lattice Models Fundamental Data Structures and Algorithms
    1. 11.1 Introduction and Objectives
    2. 11.2 Background and Current Approaches to Lattice Modelling
    3. 11.3 New Requirements and Use Cases
    4. 11.4 A New Design Approach: A Layered Approach
    5. 11.5 Initial '101' Examples of Option Pricing
    6. 11.6 Advantages of Software Layering
    7. 11.7 Improving Efficiency and Reliability
    8. 11.8 Merging Lattices
    9. 11.9 Summary and Conclusions
    10. 11.10 Exercises and Projects
  15. Chapter 12 Lattice Models Applications to Computational Finance
    1. 12.1 Introduction and Objectives
    2. 12.2 Stress Testing the Lattice Data Structures
    3. 12.3 Option Pricing Using Bernoulli Paths
    4. 12.4 Binomial Model for Assets with Dividends
    5. 12.5 Computing Option Sensitivities
    6. 12.6 (Quick) Numerical Analysis of the Binomial Method
    7. 12.7 Richardson Extrapolation with Binomial Lattices
    8. 12.8 Two-Dimensional Binomial Method
    9. 12.9 Trinomial Model of the Asset Price
    10. 12.10 Stability and Convergence of the Trinomial Method
    11. 12.11 Explicit Finite Difference Method
    12. 12.12 Summary and Conclusions
    13. 12.13 Exercises and Projects
  16. Chapter 13 Numerical Linear Algebra: Tridiagonal Systems and Applications
    1. 13.1 Introduction and Objectives
    2. 13.2 Solving Tridiagonal Matrix Systems
    3. 13.3 The Crank-Nicolson and Theta Methods
    4. 13.4 The ADE Method for the Impatient
    5. 13.5 Cubic Spline Interpolation
    6. 13.6 Some Handy Utilities
    7. 13.7 Summary and Conclusions
    8. 13.8 Exercises and Projects
  17. Chapter 14 Data Visualisation in Excel
    1. 14.1 Introduction and Objectives
    2. 14.2 The Structure of Excel-Related Objects
    3. 14.3 Sanity Check: Is the Excel Infrastructure Up and Running?
    4. 14.4 ExcelDriver and Matrices
    5. 14.5 ExcelDriver and Vectors
    6. 14.6 Path Generation for Stochastic Differential Equations
    7. 14.7 Summary and Conclusions
    8. 14.8 Exercises and Projects
    9. 14.9 Appendix: COM Architecture Overview
    10. 14.10 An Example
    11. 14.11 Virtual Function Tables
    12. 14.12 Differences Between COM and Object-Oriented Paradigm
    13. 14.13 Initialising the COM Library
  18. Chapter 15 Univariate Statistical Distributions
    1. 15.1 Introduction, Goals and Objectives
    2. 15.2 The Error Function and its Universality
    3. 15.3 One-Factor Plain Options
    4. 15.4 Option Sensitivities and Surfaces
    5. 15.5 Automating Data Generation
    6. 15.6 Introduction to Statistical Distributions and Functions
    7. 15.7 Advanced Distributions
    8. 15.8 Summary and Conclusions
    9. 15.9 Exercises and Projects
  19. Chapter 16 Bivariate Statistical Distributions and Two-Asset Option Pricing
    1. 16.1 Introduction and Objectives
    2. 16.2 Computing Integrals Using PDEs
    3. 16.3 The Drezner Algorithm
    4. 16.4 The Genz Algorithm and the West/Quantlib Implementations
    5. 16.5 Abramowitz and Stegun Approximation
    6. 16.6 Performance Testing
    7. 16.7 Gauss–Legendre Integration
    8. 16.8 Applications to Two-Asset Pricing
    9. 16.9 Trivariate Normal Distribution
    10. 16.10 Chooser Options
    11. 16.11 Conclusions and Summary
    12. 16.12 Exercises and Projects
  20. Chapter 17 STL Algorithms in Detail
    1. 17.1 Introduction and Objectives
    2. 17.2 Binders and std::bind
    3. 17.3 Non-modifying Algorithms
    4. 17.4 Modifying Algorithms
    5. 17.5 Compile-Time Arrays
    6. 17.6 Summary and Conclusions
    7. 17.7 Exercises and Projects
    8. 17.8 Appendix: Review of STL Containers and Complexity Analysis
  21. Chapter 18 STL Algorithms Part II
    1. 18.1 Introduction and Objectives
    2. 18.2 Mutating Algorithms
    3. 18.3 Numeric Algorithms
    4. 18.4 Sorting Algorithms
    5. 18.5 Sorted-Range Algorithms
    6. 18.6 Auxiliary Iterator Functions
    7. 18.7 Needle in a Haystack: Finding the Right STL Algorithm
    8. 18.8 Applications to Computational Finance
    9. 18.9 Advantages of STL Algorithms
    10. 18.10 Summary and Conclusions
    11. 18.11 Exercises and Projects
  22. Chapter 19 An Introduction to Optimisation and the Solution of Nonlinear Equations
    1. 19.1 Introduction and Objectives
    2. 19.2 Mathematical and Numerical Background
    3. 19.3 Sequential Search Methods
    4. 19.4 Solutions of Nonlinear Equations
    5. 19.5 Fixed-Point Iteration
    6. 19.6 Aitken's Acceleration Process
    7. 19.7 Software Framework
    8. 19.8 Implied Volatility
    9. 19.9 Solvers in the Boost C++ Libraries
    10. 19.10 Summary and Conclusions
    11. 19.11 Exercises and Projects
    12. 19.12 Appendix: The Banach Fixed-Point Theorem
  23. Chapter 20 The Finite Difference Method for PDEs: Mathematical Background
    1. 20.1 Introduction and Objectives
    2. 20.2 General Convection–Diffusion–Reaction Equations and Black–Scholes PDE
    3. 20.3 PDE Preprocessing
    4. 20.4 Maximum Principles for Parabolic PDEs
    5. 20.5 The Fichera Theory
    6. 20.6 Finite Difference Schemes: Properties and Requirements
    7. 20.7 Example: A Linear Two-Point Boundary Value Problem
    8. 20.8 Exponentially Fitted Schemes for Time-Dependent PDEs
    9. 20.9 Richardson Extrapolation
    10. 20.10 Summary and Conclusions
    11. 20.11 Exercises and Projects
  24. Chapter 21 Software Framework for One-Factor Option Models
    1. 21.1 Introduction and Objectives
    2. 21.2 A Software Framework: Architecture and Context
    3. 21.3 Modelling PDEs and Finite Difference Schemes: What is Supported?
    4. 21.4 Several Versions of Alternating Direction Explicit
    5. 21.5 A Software Framework: Detailed Design and Implementation
    6. 21.6 C++ Code for PDE Classes
    7. 21.7 C++ Code for FDM Classes
    8. 21.8 Examples and Test Cases
    9. 21.9 Summary and Conclusions
    10. 21.10 Exercises and Projects
  25. Chapter 22 Extending the Software Framework
    1. 22.1 Introduction and Objectives
    2. 22.2 Spline Interpolation of Option Values
    3. 22.3 Numerical Differentiation Foundations
    4. 22.4 Numerical Greeks
    5. 22.5 Constant Elasticity of Variance Model
    6. 22.6 Using Software Design (GOF) Patterns
    7. 22.7 Multiparadigm Design Patterns
    8. 22.8 Summary and Conclusions
    9. 22.9 Exercises and Projects
  26. Chapter 23 A PDE Software Framework in C++11 for a Class of Path-Dependent Options
    1. 23.1 Introduction and Objectives
    2. 23.2 Modelling PDEs and Initial Boundary Value Problems in the Functional Programming Style
    3. 23.3 PDE Preprocessing
    4. 23.4 The Anchoring PDE
    5. 23.5 ADE for Anchoring PDE
    6. 23.6 Useful Utilities
    7. 23.7 Accuracy and Performance
    8. 23.8 Summary and Conclusions
    9. 23.9 Exercises and Projects
  27. Chapter 24 Ordinary Differential Equations and their Numerical Approximation
    1. 24.1 Introduction and Objectives
    2. 24.2 What is an ODE?
    3. 24.3 Classifying ODEs
    4. 24.4 A Palette of Model ODEs
    5. 24.5 Existence and Uniqueness Results
    6. 24.6 Overview of Numerical Methods for ODEs: The Big Picture
    7. 24.7 Creating ODE Solvers in C++
    8. 24.8 Summary and Conclusions
    9. 24.9 Exercises and Projects
    10. 24.9 Appendix
  28. Chapter 25 Advanced Ordinary Differential Equations and Method of Lines
    1. 25.1 Introduction and Objectives
    2. 25.2 An Introduction to the Boost Odeint Library
    3. 25.3 Systems of Stiff and Non-stiff Equations
    4. 25.4 Matrix Differential Equations
    5. 25.5 The Method of Lines: What is it and what are its Advantages?
    6. 25.6 Initial Foray in Computational Finance: MOL for One-Factor Black–Scholes PDE
    7. 25.7 Barrier Options
    8. 25.8 Using Exponential Fitting of Barrier Options
    9. 25.9 Summary and Conclusions
    10. 25.10 Exercises and Projects
  29. Chapter 26 Random Number Generation and Distributions
    1. 26.1 Introduction and Objectives
    2. 26.2 What is a Random Number Generator?
    3. 26.3 What is a Distribution?
    4. 26.4 Some Initial Examples
    5. 26.5 Engines in Detail
    6. 26.6 Distributions in C++: The List
    7. 26.7 Back to the Future: C-Style Pseudo-Random Number Generation
    8. 26.8 Cryptographic Generators
    9. 26.9 Matrix Decomposition Methods
    10. 26.10 Generating Random Numbers
    11. 26.11 Summary and Conclusions
    12. 26.12 Exercises and Projects
  30. Chapter 27 Microsoft .Net, C# and C++11 Interoperability
    1. 27.1 Introduction and Objectives
    2. 27.2 The Big Picture
    3. 27.3 Types
    4. 27.4 Memory Management
    5. 27.5 An Introduction to Native Classes
    6. 27.6 Interfaces and Abstract Classes
    7. 27.7 Use Case: C++/CLI as 'Main Language'
    8. 27.8 Use Case: Creating Proxies, Adapters and Wrappers for Legacy C++ Applications
    9. 27.9 'Back to the Future' Use Case: Calling C# Code from C++11
    10. 27.10 Modelling Event-Driven Applications with Delegates
    11. 27.11 Use Case: Interfacing with Legacy Code
    12. 27.12 Assemblies and Namespaces for C++/CLI
    13. 27.13 Summary and Conclusions
    14. 27.14 Exercises and Projects
  31. Chapter 28 C++ Concurrency, Part I Threads
    1. 28.1 Introduction and Objectives
    2. 28.2 Thread Fundamentals
    3. 28.3 Six Ways to Create a Thread
    4. 28.4 Intermezzo: Parallelising the Binomial Method
    5. 28.5 Atomics
    6. 28.6 Smart Pointers and the Thread-Safe Pointer Interface
    7. 28.7 Thread Synchronisation
    8. 28.8 When Should we use Threads?
    9. 28.9 Summary and Conclusions
    10. 28.10 Exercises and Projects
  32. Chapter 29 C++ Concurrency, Part II Tasks
    1. 29.1 Introduction and Objectives
    2. 29.2 Finding Concurrency: Motivation
    3. 29.3 Tasks and Task Decomposition
    4. 29.4 Futures and Promises
    5. 29.5 Shared Futures
    6. 29.6 Waiting on Tasks to Complete
    7. 29.7 Continuations and Futures in Boost
    8. 29.8 Pure Functions
    9. 29.9 Tasks versus Threads
    10. 29.10 Parallel Design Patterns
    11. 29.11 Summary and Conclusions
    12. 29.12 Quizzes, Exercises and Projects
  33. Chapter 30 Parallel Patterns Language (PPL)
    1. 30.1 Introduction and Objectives
    2. 30.2 Parallel Algorithms
    3. 30.3 Partitioning Work
    4. 30.4 The Aggregation/Reduction Pattern in PPL
    5. 30.5 Concurrent Containers
    6. 30.6 An Introduction to the Asynchronous Agents Library and Event-Based Systems
    7. 30.7 A Design Plan to Implement a Framework Using Message Passing and Other Approaches
    8. 30.8 Summary and Conclusions
    9. 30.9 Exercises and Projects
  34. Chapter 31 Monte Carlo Simulation, Part I
    1. 31.1 Introduction and Objectives
    2. 31.2 The Boost Parameters Library for the Impatient
    3. 31.3 Monte Carlo Version 1: The Monolith Program ('Ball of Mud')
    4. 31.4 Policy-Based Design: Dynamic Polymorphism
    5. 31.5 Policy-Based Design Approach: CRTP and Static Polymorphism
    6. 31.6 Builders and their Subcontractors (Factory Method Pattern)
    7. 31.7 Practical Issue: Structuring the Project Directory and File Contents
    8. 31.8 Summary and Conclusions
    9. 31.9 Exercises and Projects
  35. Chapter 32 Monte Carlo Simulation, Part II
    1. 32.1 Introduction and Objectives
    2. 32.2 Parallel Processing and Monte Carlo Simulation
    3. 32.3 A Family of Predictor–Corrector Schemes
    4. 32.4 An Example (CEV Model)
    5. 32.5 Implementing the Monte Carlo Method Using the Asynchronous Agents Library
    6. 32.6 Summary and Conclusions
    7. 32.7 Exercises and Projects
  36. Appendix 1 Multiple-Precision Arithmetic
    1. A1.1 Introduction and Objectives
    2. A1.2 Multiprecision Data Types
    3. A1.3 Initial Examples and Applications
    4. A1.4 Multiple Precision and Special Functions
    5. A1.5 Numerical Solution of Ordinary Differential Equations
    6. A1.6 Generating Random Numbers
    7. A1.7 Summary and Conclusions
    8. A1.8 Exercises and Projects
  37. Appendix 2 Computing Implied Volatility
    1. A2.1 Introduction and Objectives
    2. A2.2 Implied Volatility by Least-Squares Optimisation
    3. A2.3 Implied Volatility by Fixed-Point Iteration
    4. A2.4 Volatility by Homotopy and ODE Solvers
    5. A2.5 A Vector Space of Functions in C++
    6. A2.6 Multivariate Optimisation
    7. A2.7 Nonlinear Programming and Multivariable Optimisation
    8. A2.8 Nonlinear Least Squares
    9. A2.9 Summary and Conclusions
    10. A2.10 Exercises and Projects
  38. References
  39. Index
  40. EULA