Cover image for Python for Finance

Book Description

The financial industry has adopted Python at a tremendous rate recently, with some of the largest investment banks and hedge funds using it to build core trading and risk management systems. This hands-on guide helps both developers and quantitative analysts get started with Python, and guides you through the most important aspects of using Python for quantitative finance.

Table of Contents

  1. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. Safari® Books Online
    4. How to Contact Us
    5. Acknowledgments
  2. I. Python and Finance
    1. 1. Why Python for Finance?
      1. What Is Python?
        1. Brief History of Python
        2. The Python Ecosystem
        3. Python User Spectrum
        4. The Scientific Stack
      2. Technology in Finance
        1. Technology Spending
        2. Technology as Enabler
        3. Technology and Talent as Barriers to Entry
        4. Ever-Increasing Speeds, Frequencies, Data Volumes
        5. The Rise of Real-Time Analytics
      3. Python for Finance
        1. Finance and Python Syntax
        2. Efficiency and Productivity Through Python
          1. Shorter time-to-results
          2. Ensuring high performance
        3. From Prototyping to Production
      4. Conclusions
      5. Further Reading
    2. 2. Infrastructure and Tools
      1. Python Deployment
        1. Anaconda
        2. Python Quant Platform
      2. Tools
        1. Python
        2. IPython
          1. From shell to browser
          2. Basic usage
          3. Markdown and LaTeX
          4. Magic commands
          5. System shell commands
        3. Spyder
      3. Conclusions
      4. Further Reading
    3. 3. Introductory Examples
      1. Implied Volatilities
      2. Monte Carlo Simulation
        1. Pure Python
        2. Vectorization with NumPy
        3. Full Vectorization with Log Euler Scheme
        4. Graphical Analysis
      3. Technical Analysis
      4. Conclusions
      5. Further Reading
  3. II. Financial Analytics and Development
    1. 4. Data Types and Structures
      1. Basic Data Types
        1. Integers
        2. Floats
        3. Strings
      2. Basic Data Structures
        1. Tuples
        2. Lists
        3. Excursion: Control Structures
        4. Excursion: Functional Programming
        5. Dicts
        6. Sets
      3. NumPy Data Structures
        1. Arrays with Python Lists
        2. Regular NumPy Arrays
        3. Structured Arrays
      4. Vectorization of Code
        1. Basic Vectorization
        2. Memory Layout
      5. Conclusions
      6. Further Reading
    2. 5. Data Visualization
      1. Two-Dimensional Plotting
        1. One-Dimensional Data Set
        2. Two-Dimensional Data Set
        3. Other Plot Styles
      2. Financial Plots
      3. 3D Plotting
      4. Conclusions
      5. Further Reading
    3. 6. Financial Time Series
      1. pandas Basics
        1. First Steps with DataFrame Class
        2. Second Steps with DataFrame Class
        3. Basic Analytics
        4. Series Class
        5. GroupBy Operations
      2. Financial Data
      3. Regression Analysis
      4. High-Frequency Data
      5. Conclusions
      6. Further Reading
    4. 7. Input/Output Operations
      1. Basic I/O with Python
        1. Writing Objects to Disk
        2. Reading and Writing Text Files
        3. SQL Databases
        4. Writing and Reading NumPy Arrays
      2. I/O with pandas
        1. SQL Database
        2. From SQL to pandas
        3. Data as CSV File
        4. Data as Excel File
      3. Fast I/O with PyTables
        1. Working with Tables
        2. Working with Compressed Tables
        3. Working with Arrays
        4. Out-of-Memory Computations
      4. Conclusions
      5. Further Reading
    5. 8. Performance Python
      1. Python Paradigms and Performance
      2. Memory Layout and Performance
      3. Parallel Computing
        1. The Monte Carlo Algorithm
        2. The Sequential Calculation
        3. The Parallel Calculation
        4. Performance Comparison
      4. multiprocessing
      5. Dynamic Compiling
        1. Introductory Example
        2. Binomial Option Pricing
      6. Static Compiling with Cython
      7. Generation of Random Numbers on GPUs
      8. Conclusions
      9. Further Reading
    6. 9. Mathematical Tools
      1. Approximation
        1. Regression
          1. Monomials as basis functions
          2. Individual basis functions
          3. Noisy data
          4. Unsorted data
          5. Multiple dimensions
        2. Interpolation
      2. Convex Optimization
        1. Global Optimization
        2. Local Optimization
        3. Constrained Optimization
      3. Integration
        1. Numerical Integration
        2. Integration by Simulation
      4. Symbolic Computation
        1. Basics
        2. Equations
        3. Integration
        4. Differentiation
      5. Conclusions
      6. Further Reading
    7. 10. Stochastics
      1. Random Numbers
      2. Simulation
        1. Random Variables
        2. Stochastic Processes
          1. Geometric Brownian motion
          2. Square-root diffusion
          3. Stochastic volatility
          4. Jump diffusion
        3. Variance Reduction
      3. Valuation
        1. European Options
        2. American Options
      4. Risk Measures
        1. Value-at-Risk
        2. Credit Value Adjustments
      5. Conclusions
      6. Further Reading
    8. 11. Statistics
      1. Normality Tests
        1. Benchmark Case
        2. Real-World Data
      2. Portfolio Optimization
        1. The Data
        2. The Basic Theory
        3. Portfolio Optimizations
        4. Efficient Frontier
        5. Capital Market Line
      3. Principal Component Analysis
        1. The DAX Index and Its 30 Stocks
        2. Applying PCA
        3. Constructing a PCA Index
      4. Bayesian Regression
        1. Bayes’s Formula
        2. PyMC3
        3. Introductory Example
        4. Real Data
      5. Conclusions
      6. Further Reading
    9. 12. Excel Integration
      1. Basic Spreadsheet Interaction
        1. Generating Workbooks (.xls)
        2. Generating Workbooks (.xslx)
        3. Reading from Workbooks
        4. Using OpenPyxl
        5. Using pandas for Reading and Writing
      2. Scripting Excel with Python
        1. Installing DataNitro
        2. Working with DataNitro
          1. Scripting with DataNitro
          2. Plotting with DataNitro
          3. User-defined functions
      3. xlwings
      4. Conclusions
      5. Further Reading
    10. 13. Object Orientation and Graphical User Interfaces
      1. Object Orientation
        1. Basics of Python Classes
        2. Simple Short Rate Class
        3. Cash Flow Series Class
      2. Graphical User Interfaces
        1. Short Rate Class with GUI
        2. Updating of Values
        3. Cash Flow Series Class with GUI
      3. Conclusions
      4. Further Reading
    11. 14. Web Integration
      1. Web Basics
        1. ftplib
        2. httplib
        3. urllib
      2. Web Plotting
        1. Static Plots
        2. Interactive Plots
        3. Real-Time Plots
          1. Real-time FX data
          2. Real-time stock price quotes
      3. Rapid Web Applications
        1. Traders’ Chat Room
        2. Data Modeling
        3. The Python Code
          1. Imports and database preliminaries
          2. Core functionality
        4. Templating
        5. Styling
      4. Web Services
        1. The Financial Model
        2. The Implementation
      5. Conclusions
      6. Further Reading
  4. III. Derivatives Analytics Library
    1. 15. Valuation Framework
      1. Fundamental Theorem of Asset Pricing
        1. A Simple Example
        2. The General Results
      2. Risk-Neutral Discounting
        1. Modeling and Handling Dates
        2. Constant Short Rate
      3. Market Environments
      4. Conclusions
      5. Further Reading
    2. 16. Simulation of Financial Models
      1. Random Number Generation
      2. Generic Simulation Class
      3. Geometric Brownian Motion
        1. The Simulation Class
        2. A Use Case
      4. Jump Diffusion
        1. The Simulation Class
        2. A Use Case
      5. Square-Root Diffusion
        1. The Simulation Class
        2. A Use Case
      6. Conclusions
      7. Further Reading
    3. 17. Derivatives Valuation
      1. Generic Valuation Class
      2. European Exercise
        1. The Valuation Class
        2. A Use Case
      3. American Exercise
        1. Least-Squares Monte Carlo
        2. The Valuation Class
        3. A Use Case
      4. Conclusions
      5. Further Reading
    4. 18. Portfolio Valuation
      1. Derivatives Positions
        1. The Class
        2. A Use Case
      2. Derivatives Portfolios
        1. The Class
        2. A Use Case
      3. Conclusions
      4. Further Reading
    5. 19. Volatility Options
      1. The VSTOXX Data
        1. VSTOXX Index Data
        2. VSTOXX Futures Data
        3. VSTOXX Options Data
      2. Model Calibration
        1. Relevant Market Data
        2. Option Modeling
        3. Calibration Procedure
      3. American Options on the VSTOXX
        1. Modeling Option Positions
        2. The Options Portfolio
      4. Conclusions
      5. Further Reading
    6. A. Selected Best Practices
      1. Python Syntax
      2. Documentation
      3. Unit Testing
    7. B. Call Option Class
    8. C. Dates and Times
      1. Python
      2. NumPy
      3. pandas
  5. Index
  6. About the Author
  7. Colophon
  8. Copyright