Learning Modern C++ for Finance

Book description

This practical book demonstrates why C++ is still one of the dominant production-quality languages for financial applications and systems. Many programmers believe that C++ is too difficult to learn. Author Daniel Hanson demonstrates that this is no longer the case, thanks to modern features added to the C++ Standard beginning in 2011.

Financial programmers will discover how to leverage C++ abstractions that enable safe implementation of financial models. You’ll also explore how popular open source libraries provide additional weapons for attacking mathematical problems. C++ programmers unfamiliar with financial applications also benefit from this handy guide.

  • Learn C++ basics from a modern perspective: syntax, inheritance, polymorphism, composition, STL containers, and algorithms
  • Dive into newer features and abstractions including functional programming using lambdas, task-based concurrency, and smart pointers
  • Implement basic numerical routines in modern C++
  • Understand best practices for writing clean and efficient code

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Navigating This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. 1. An Overview of C++
    1. C++ and Quantitative Finance
      1. C++11: The Modern Era Is Born
      2. Open Source Mathematical Libraries
      3. Some Myths about C++
      4. Compiled Versus Interpreted Code
    2. The Components of C++
      1. C++ Language Features
      2. The C++ Standard Library
    3. Some New Language Features Since C++11
      1. The auto Keyword
      2. Range-Based for Loops
      3. The using Keyword
      4. Uniform Initialization
      5. Formatting Output
      6. Class Template Argument Deduction
      7. Enumerated Constants and Scoped Enumerations
      8. Lambda Expressions
    4. Mathematical Operators, Functions, and Constants in C++
      1. Standard Arithmetic Operators
      2. Mathematical Functions in the Standard Library
      3. Mathematical Special Functions
      4. Standard Library Mathematical Constants
    5. Naming Conventions
    6. Summary
    7. Further Resources
  3. 2. Writing User-Defined Classes with Modern C++ Features
    1. A Black-Scholes Class
      1. Representing the Payoff
      2. Writing the Class Declaration
      3. Writing the Class Implementation
      4. Using a Functor for Root Finding: Implied Volatility
    2. Move Semantics and Special Member Functions
      1. Data Members and Performance Considerations
      2. An Introduction to Move Semantics
      3. Initialization of Constructor Arguments with std::move(.)
      4. Anonymous Temporary Objects and Move Semantics
      5. Return Value Optimization
      6. Default Constructor
    3. Three-Way Comparison Operator (Spaceship Operator)
    4. Lambda Expressions and User-Defined Class Members
    5. Summary
    6. Additional References
  4. 3. Inheritance, Polymorphism, and Smart Pointers
    1. Polymorphism
    2. Resource Ownership with Raw Pointers
      1. Using Clone Methods
      2. Creating an Instance of OptionInfo
      3. Preventing Shallow Copy
      4. Implementing the OptionInfo Destructor
      5. Pricing an Option
      6. Implementing Copy Operations
      7. The (Old) Rule of Three
    3. Introducing Smart Pointers
      1. Unique Pointers
      2. Shared Pointers
    4. Managing Resources with Unique Pointers
      1. Just Move It
      2. Using the Result in a Pricing Model
      3. If Copy Operations Are Required
    5. Summary
    6. Further Resources
  5. 4. The Standard Template Library Part I: Containers and Iterators
    1. Templates
      1. Using Function Templates
      2. Using Class Templates
      3. Compiling Template Code
    2. STL Containers
      1. Sequential Containers
      2. Associative Containers
    3. STL Iterators
      1. Using auto to Reduce Verbosity
      2. Using Constant Iterators
      3. Iterators or Indices?
      4. Iterators on Associative Containers
    4. Summary
    5. Further Resources
  6. 5. The Standard Template Library Part II: Algorithms and Ranges
    1. STL Algorithms
      1. A First STL Algorithm Example
      2. A First Example with Ranges
      3. Some Commonly Used Algorithms
      4. Function Objects as Auxiliary Functions
      5. Class Member Functions as Auxiliary Functions
      6. Locating, Sorting, Searching, Copying, and Moving Elements
      7. Numeric Algorithms
    2. Range Views, Range Adaptors, and Functional Programming
      1. Range Views
      2. Chaining for Functional Composition
      3. Views, Containers, and Range-Based for Loops
    3. Summary
    4. Additional References
  7. 6. Random Number Generation and Concurrency
    1. Distributional Random Number Generation
      1. Introducing Engines and Distributions
      2. Generating Random Normal Draws
      3. Using Other Distributions
    2. Shuffling
    3. Monte Carlo Option Pricing
      1. A Review of Monte Carlo Option Pricing
      2. Generating Random Equity Price Scenarios
      3. Calculating the Option Price
      4. Pricing Path-Dependent Options
    4. Concurrency and Parallelism
      1. Parallel Algorithms from the Standard Library
      2. Task-Based Concurrency
      3. Concluding Remarks on async and future
    5. Summary
    6. Further Resources
  8. 7. Dates and Fixed Income Securities
    1. Representation of a Date
    2. Serial Representation
    3. Accessor Functions for Year, Month, and Day
      1. Checking the Validity of a Date
      2. Checking Leap Years and Last Day of the Month
      3. Identifying Weekdays and Weekends
      4. Adding Years, Months, and Days
    4. A Date Class Wrapper
      1. Class Declaration
      2. Class Implementation
    5. Day Count Basis
    6. Yield Curves
      1. Deriving a Yield Curve from Market Data
      2. Discount Factors
      3. Calculating Forward Discount Factors
      4. Implementing a Yield-Curve Class
      5. Implementing a Linearly Interpolated Yield Curve Class
    7. A Bond Class
      1. Bond Payments and Valuation
      2. Designing a Bond Class
      3. Implementing the Bond Class
    8. A Bond Valuation Example
    9. Summary
    10. Additional Reference
  9. 8. Linear Algebra
    1. Lazy Evaluation and Expression Templates
      1. Lazy Evaluation
      2. Expression Templates
    2. The Eigen Linear Algebra Library
      1. Eigen Matrices and Vectors
      2. Matrix and Vector Math Operations
      3. STL Compatibility
    3. Matrix Decompositions and Applications
      1. Fund Tracking with Multiple Regression
      2. Correlated Random Equity Paths and the Cholesky Decomposition
      3. Yield-Curve Dynamics and Principal Component Analysis
    4. Future Directions: Linear Algebra in the Standard Library
      1. mdspan
      2. BLAS Interface
    5. Summary
    6. Further Resources
  10. 9. The Boost Libraries
    1. Mathematical Constants
    2. Statistical Distributions
      1. Probability Functions
      2. Drawdown Example, Revisited
      3. Random Number Generation with Boost Distributions
    3. MultiArray
      1. A Simple Two-Dimensional MultiArray
      2. Binomial Lattice Option Pricing
      3. Accumulators
      4. Max and Min Example
      5. Mean and Variance
      6. Rolling Mean and Variance
    4. Trading Indicator Examples
    5. Summary
    6. Further Reading
  11. 10. Modules and Concepts
    1. Modules
      1. Standard Library Header Units
      2. Templates in Modules
      3. import Versus #include
      4. Declarations in Module Interfaces
      5. Separating Declarations from Implementation
      6. Namespaces
      7. Partitions
    2. Concepts
      1. Defining Concepts
      2. Defining Concepts with Multiple Conditions
      3. Standard Library Concepts
    3. Summary
  12. A. Virtual Default Destructor
  13. B. Object Slicing
  14. C. Implementation of Move Special Member Functions
  15. D. Resolving Conflicts in the Initialization of a vector
  16. E. valarray and Matrix Operations
    1. Arithmetic Operators and Math Functions
    2. valarray as a Matrix Proxy
  17. Index
  18. About the Author

Product information

  • Title: Learning Modern C++ for Finance
  • Author(s): Daniel Hanson
  • Release date: November 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098100803