Modeling Derivatives Applications in Matlab, C++, and Excel
NJ • New York • London • San Francisco • Toronto • Sydney Tokyo • Singapore • Hong Kong • Cape Town • Madrid Paris • Milan • Munich • Amsterdam
. C++.Modeling Derivatives Applications in Matlab. and Excel
An Imprint of PEARSON EDUCATION Upper Saddle River.
C++. Publishing as FT Press Upper Saddle River.S. No part of this book may be reproduced. Pearson Education Canada. Pte. Pte. Company and product names mentioned herein are the trademarks or registered trademarks of their respective owners. Ltd. Modeling Credit Derivatives.com. For more information.S. I. please contact International Sales at international@pearsoned. Pearson Education North Asia. Corporate and GovernmentSales.. de C.L692E45 2006 005. paper) 1. Pearson Education Australia PTY. Printed in the United States of America First Printing December. p. All rights reserved. Ltd.Vice President.V. C++. Ltd. Pricing Models in Matlab. For sales outside the U. cm. Editor-in-Chief: Tim Moore Executive Editor: Jim Boyd Editorial Assistant: Susie Abraham Development Editor: Russ Hall Associate Editor-in-Chief and Director of Marketing: Amy Neidlinger Cover Designer: Chuti Prasertsith Managing Editor: Gina Kanouse Senior Project Editor: Lori Lyons Copy Editor: Water Crest Publishing Indexer: Christine Karpeles Compositor: Lori Hughes Manufacturing Buyer: Dan Uhrig © 2007 by Pearson Education. without permission in writing from the publisher. 2006 ISBN: 0-13-196259-0 Pearson Education LTD.382-3419.5’7–dc22 2006023492
. Limited. o Pearson Education—Japan Pearson Education Malaysia. S. and Excel. in any form or by any means. 1968Modeling Derivatives Applications in Matlab. Pearson Educati` n de Mexico. New Jersey 07458 FT Press offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. 2. please contact U. and Excel / Justin London. Justin. Ltd. ISBN 0-13-196259-0 (hardback : alk. Pearson Education Singapore.4.com. Library of Congress Cataloging-in-Publication Data London. Inc. HF5548. corpsales@pearsontechgroup.A. Title. 1-800.
Milton and Evelyn London. Leon and Leslie. and my sister. my parents.v
To the memory of my grandparents. Joanna.
Modiﬁed Duration.7 Bootstrap Method 1. and DV01 Hedging Bond Portfolios 1.11 Swap Pricing in Matlab Using Term Structure Analysis 1.C ONTENTS
Preface Acknowledgments About the Author 1 SWAPS AND FIXED INCOME INSTRUMENTS 1.1 Eurodollar Futures 1.3 Computing Treasury Bill Prices and Yields in Matlab 1.13 Bermudan Swaption Pricing in Matlab Endnotes
xv xix xxi 1 2 3 6 7 10 11 11 12 14 14 15 18 20 24 25 28 30 33 43 45 50 61 65
.10 General Swap Pricing in Matlab Description 1.9 Bootstrapping in Excel 1.4 Hedging Debt Positions Hedging a Future 91-Day T-Bill Investment with T-Bill Call Short Hedge: Managing the Maturity Gap Maturity Gap and the Carrying Cost Model Managing the Maturity Gap with Eurodollar Put Short Hedge: Hedging a Variable-Rate Loan 1.5 Bond and Swap Duration.12 Swap Valuation in C++ 1.8 Bootstrapping in Matlab 1.6 Term Structure of Rates 1.2 Treasury Bills and Bonds Hedging with T-Bill Futures Long Futures Hedge: Hedging Synthetic Futures on 182-Day T-Bill 1.
7 Collateralized Mortgage Obligations (CMOs) 3.6 Using Copulas in Excel Endnotes 3 MORTGAGE-BACKED SECURITIES 3.1 Prepayment Models 3.9 Planned Amortization Classes (PACS) 3.1 Structure of CDOs Cash Flow CDOs Market Value CDOs Balance Sheet Cash Flows CDOs Arbitrage CDOs Arbitrage Market Value CDOs
67 67 69 69 71 73 74 74 76 76 77 77 82 86 87 91 93 95 98 100 111 126 131 137 146 149 151 151 153 160 163 164 165 166 166 166 166
.3 MBS Pricing and Quoting 3.4 Prepayment Risk and Average Life of MBS 3.3 Archimedean Copulae 2. and Roncalli Method e Mashal and Zeevi Method 2.5 MBS Pricing Using Monte Carlo in C++ 3.5 Numerical Results for Calibrating Real-Market Data Bouy` .10 Principal. Nikeghbali.4 Calibrating Copulae Exact Maximum Likelihood Method (EML) The Inference Functions for Margins Method (IFM) The Canonical Maximum Likelihood Method (CML) 2.11 Interest Rate Risk 3.2 Classes of Copula Functions Multivariate Gaussian Copula Multivariate Student’s T Copula 2. Riboulet.8 CMO Implementation in C++ 3.12 Dynamic Hedging of MBS The Multivariable Density Estimation Method Endnotes 4 COLLATERALIZED DEBT OBLIGATIONS 4.1 Deﬁnition and Basic Properties of Copula Functions 2.viii
2 COPULA FUNCTIONS 2.6 Matlab Fixed-Income Toolkit for MBS Valuation 3. Durrelman.2 Numerical Example of Prepayment Model 3.and Interest-Only Strips 3.
8 4.2 Synthetic CDOs Fully Funded Synthetic CDOs Partially and Unfunded Funded Synthetic CDOs Balance Sheet Management with CDS The Distribution of Default Losses on a Portfolio CDO Equity Tranche CDO Equity Tranche Performance The CDO Embedded Option The Price of Equity Using Moody’s Binomial Expansion Technique to Structure Synthetic CDOs Correlation Risk of CDO Tranches 4.2 5.5 5.5
Arbitrage Cash Flow CDOs Credit Enhancement in Cash Flow Transactions Credit Enhancement in Market Value Transactions: Advance Rates and the Over-Collateralization Test Minimum Net Worth Test Transaction Characteristics 4.12 CDO Tranche Pricing Pricing Equation Simulation Algorithm CDO Pricing in Matlab CDO Pricing in C++ CDO2 Pricing Fast Loss Calculation for CDOs and CDO2 s Fast Algorithm for Computing CDO Tranche Loss in Matlab Endnotes 5 CREDIT DERIVATIVES 5.8 CDS Day Counting Conventions General Valuation of Credit Default Swaps Hazard Rate Function Poisson and Cox Processes Valuation Using a Deterministic Intensity Model Hazard Rate Function Calibration Credit Curve Construction and Calibration
167 167 167 169 171 171 177 179 181 181 186 186 187 188 189 193 196 197 197 199 208 216 216 218 220 223 224 226 226 228 229 232 235 248
4.3 5.1 Credit Default Swaps 5.10 4.6 4.7 4.6 5.4 5.11 4.4 4.3 4.9 4.7 5.
11 5.10 Pricing Weather Options in C++ Endnotes 7 ENERGY AND POWER DERIVATIVES 7.7 Pricing Weather Derivatives Model Framework Pricing a Heating Degree Day Option 6.5 Volatility Estimation 6.9 Time-Series Weather Forecasting 6.2 Weather Contracts CME Weather Futures 6.2 Electricity Pricing Models Modeling the Price Process One-Factor Model Estimating the Deterministic Component Estimation of the Stochastic Process for the One-Factor Models Two-Factor Model
.6 Mean-Reversion Parameter Estimation 6.4 Parameter Estimation 6.12
Credit Basket Default Swaps Pricing Generation of Correlated Default Stopping Times Sampling from Elliptical Copulae The Distribution of Default Arrival Times Basket CDS Pricing Algorithm Credit Basket Pricing in Matlab Credit Basket Pricing in C++ Credit Linked Notes (CLNs) CLNs with Collateralized Loan or Bond Obligations (CLOs or CBOs) Pricing Tranched Credit Linked Notes Regulatory Capital Endnotes
249 250 250 252 252 255 264 291 295 296 296 297 299 300 303 303 306 308 309 310 310 311 312 312 313 316 318 328 330 333 334 336 336 337 340 341 342
6 WEATHER DERIVATIVES 6.8 Historical Burn Analysis 6.3 Modeling Temperature Noise Process Mean-Reversion 6.x
5.1 Electricity Markets 7.9
5.1 Weather Derivatives Market 6.10 5.
14 7. and Penalty Functions Swing Option Pricing in Matlab LSM Simulation Results Upper and Lower Boundaries Exercise Strategies The Threshold of Early Exercise Interplay Between Early Exercise and Option Value Pricing of Energy Commodity Derivatives Cross-Commodity Spread Options Model 1 Model 2 Model 3 Jump Diffusion Pricing Models Model 1a: Afﬁne Mean-Reverting Jump-Diffusion Process Model 1b Model 2a: Time-Varying Drift Component Model 2b: Time-Varying Version of Model 1b Stochastic Volatility Pricing Models Model 3a: Two-Factor Jump-Diffusion Afﬁne Process with Stochastic Volatility Model Parameter Estimation ML-CCF Estimators ML-MCCF Estimators Spectral GMM Estimators Simulation Parameter Estimation in Matlab Energy Commodity Models Natural Gas Natural Gas Markets Natural Gas Spot Prices Gas Pricing Models One-Factor Model Two-Factor Model Calibration
344 345 346 348 351 352 352 354 356 358 360 362 362 364 365 366 368 368 369 370 372 372 372 373 375 376 379 383 385 385 387 387 389 390 390 391 393
7.4 7.13 7.11
7.6 7.3 7.10
Swing Options The Longstaff-Schwartz Algorithm for American and Bermudan Options The LSM Algorithm Extension of Longstaff-Schwartz to Swing Options General Case: Upswings.8
7 Using the Calibrated Model to Price Options Daily Strike Options Monthly Strike Options Spark Spread Options 8.18
One-Factor Model Calibration Two-Factor Model Calibration Natural Gas Pricing in Matlab Natural Gas and Electricity Swaps Generator End User Endnotes
393 394 398 398 400 401 402
8 PRICING POWER DERIVATIVES: THEORY AND MATLAB IMPLEMENTATION 8.1 Introduction 8.4 Commercial Real Estate-Backed Securitization (CREBS)—Singapore’s Experience 9.3 Concepts of Securitizing Real Estate Cash Flows 9.10 Summary Endnotes References 9 COMMERCIAL REAL ESTATE ASSET-BACKED SECURITIES 9.3 Traditional Valuation Approaches Are Problematic for Power 8.8 Option Valuation Methodology Splitting the (Finite) Difference: Daily Strike and Monthly Strike Options Matlab Implementation for a Monthly Strike Option Spark Spread Options Matlab Implementation of Spark Spread Option Valuation 8.6 Model Calibration 8.5 Structure of a Typical CREBS A CREBS Case by Visor Limited
407 407 409 410 413 415 419 423 423 423 424 424 424 426 434 434 439 443 443 445 447 447 449 450 452 456 457
.2 Power Markets 8.9 Results 8.4 Fundamentals-Based Models 8.2 Motivations for Asset-Backed Securitization 9.1 Introduction 9.xii
7.5 The PJ Model—Overview 8.17 7.
1 BDT Modeling in Matlab A.5 Matlab Excel Link Example A.Contents
9.4 HJM Pricing in Matlab Description Syntax Arguments Examples Creating an HJM Volatility and Pricing Model A.3 Black-Karasinski Trees in Matlab A.9 9.2 Hull-White Trees in Matlab A.6 Two-Factor HJM Model Implementation in Matlab Endnotes B CHAPTER 7 CODE REFERENCES INDEX
Pricing of CREBS Swaps and Swaptions The Cash Flow Swap Structure for CREBS Valuation of CREBS Using a Swap Framework Basic Swap Valuation Framework Pricing of Credit Risks for CREBS Using the Proposed Swap Model Modeling Default Risks in the CREBS Swap Numerical Analysis of Default Risks for a Typical CREBS Monte Carlo Simulation Process Input Parameters Analysis of Results Matlab Code for the Numerical Analysis Summary Endnotes
459 459 459 460 460 461 461 463 463 464 465 467 470 470 473 473 478 486 490 492 493 493 493 494 497 500 501 503 543 555
A INTEREST RATE TREE MODELING IN MATLAB A.
The purpose of the book is to teach readers how to properly develop and implement derivatives applications so that they can adapt the code for their own use as they develop their own applications. the implementation of these models in C++ and Matlab (two widely used languages for implementing and building derivatives models) has made programming skills in these languages important for practitioners to have. C++. C++. Modeling Derivatives Applications in Matlab. In addition. and Excel is the ﬁrst book to cover in detail important derivatives pricing models for credit derivatives (for example. the theory underlying the models. and Excel. Craig Pirrong. and increasingly important weather. C++. mortgagebacked securities (MBSs). collateralized-debt obligations (CDOs). power.
. Throughout this book. and energy derivatives using Matlab. numerous examples are given using Matlab. as well as the code implementations. Moreover. Readers will beneﬁt from both the mathematical derivations of the models. and Excel. swaps. Examples using actual real-time Bloomberg data show how these models work in practice.P REFACE
Given the explosive growth in new ﬁnancial derivatives such as credit derivatives. hundreds of ﬁnancial institutions now market these complex instruments and employ thousands of ﬁnancial and technical professionals needed to model them accurately and effectively. The best way to learn is to follow the examples and run the code. the use of Excel is also important as many trading desks use Excel as a front-end trading application. but is written and based on the proprietary work of its author. credit default swaps and credit-linked notes). asset-backed securities (ABSs). The chapters cover the following topics: • Chapter 1: Swaps and ﬁxed income securities • Chapter 2: Copulas and copula methodologies • Chapter 3: Mortgage-backed securities • Chapter 4: Collateralized-debt obligations • Chapter 5: Credit derivatives • Chapter 6: Weather derivatives • Chapter 7: Energy and power derivatives • Chapter 8: Also covers model implementations for energy derivatives using Matlab. professor of ﬁnance and director of the Global Energy Management Institute at the University of Houston. ﬁxed income securities.
trading. the work and models developed and written by various leading practitioners and experts for certain topics are provided and incorporated throughout the book. although some two-dimensional arrays are used as well. but it also incorporates important work contributions from leading practitioners in the industry. the work of Galiani (2003) is discussed in the chapter on copulas and credit derivatives. The valuable work for energy derivatives of Doerr (2002). and hedging using C++. provides automatic memory allocation of memory as data is used if no array sizes are predeﬁned. Xiang (2004). numerous individuals named in the acknowledgments contributed useful code throughout the book. Data can be added or removed from an object and the object will dynamically expand or reduce the amount of memory space as needed. the C++ code provided could perhaps be more modular as with some of the routines in building interest rate trees. In Chapter 8. and Excel.g. The emphasis throughout the book has been to provide working implementations for the reader to adapt. The work of Picone (2004) is discussed in the chapter on collateralized-debt obligations. In Chapter 9. professor in the Department of Real Estate Finance at the National University of Singapore. The book emphasizes how to implement and code complex models for pricing. Matlab. for instance. a two-dimensional alternating implicit difference (ADI) ﬁnite difference scheme for pricing energy derivatives. the predeﬁned array sizes may turn out not to be large enough. use of array template classes (that can handle multiple dimensions) and vectors (of vectors) in the Standard Template Library in C++ are more efﬁcient because they are dynamic and only use as much memory as is needed. the book does provide some discussions and helpful tips for building efﬁcient models. For instance. a matrix manipulation language.
In order to provide different perspectives to readers and provide as much useful information as possible. All data in Matlab are treated as matrix objects. Tien-Foo Sing discusses using Monte Carlo to price asset-based securities. which is written and is based on the proprietary work of its author.
. not only does this book cover complex derivatives models and provide all of the code (which can be downloaded using a secure ID code from the companion Web site at www. Use of a predeﬁned two-dimensional array. Craig Pirrong discusses the Pirrong-Jermayakan model. Tien-Foo Sing.com/title/0131962590). Although two-dimensional arrays are easy to deﬁne. a single number is treated as a 1 x 1 array.ftpress. However.xvi
• Chapter 9: Commercial real-estate backed securities (a type of asset-backed security). e. is not the most efﬁcient way to allocate memory since it is ﬁxed in size.) Efﬁciency and modularity are important design goals in building robust object-oriented code. Moreover. memory allocation for data structures is always an issue when developing a model that requires use and storage of multi-dimensional data. Matlab. A lot of memory may be unutilized and wasted if you do not know how large the structure needs to be to store the actual data. In some cases in this book. and Xu (2004) is given. The work of Johnson (2004) is discussed in the chapters on ﬁxed-income instruments and mortgage-backed securities. Thus.. The book does not focus on design patterns or best coding practices (these issues may be discussed in subsequent editions of the book. For instance. On the other hand. Such structures are used in the book.
there may still be a few.
.ftpress. NOTE: The code ﬁles are Copyrighted © 2006 by Justin London and the contributors thereof. inevitably in a book of this length and complexity. go to www.Preface
While every effort has been made to catch all typos and errors in the book. please send us an inquiry via the Contact Us page at http://www. All rights reserved. this book will give you the foundation to develop. You will need a valid email address and the access code that is printed inside the envelope located at the back of the book. Hopefully. If you have any questions about online registration or downloading the code. build. and test your own models while saving you a great deal of development time through use of pre-tested robust code. Any corrections will be posted on the Web site.ftpress.com/title/0131962590 and follow the on-screen instructions. To register online.
SUPPLEMENTAL F ILES ON THE W EB SITE
To download the code in this book. you must ﬁrst register online. Unauthorized reproduction or distribution is prohibited.com/about/contact us/.
Special thanks to the following people for their code and work contributions to this book: Ahsan Amin Sean Campbell Francis Diebold Uwe Doerr Stefano Galiani Michael Gibson Stafford Johnson Jochen Meyer Dominic Picone Craig Pirrong Eduardo Schwartz Tien Foo Sing Liuren Wu Lei Xiong James Xu
.S. and mathematics. in economics and mathematics. London is the founder of a global online trading and ﬁnancial technology company. and an M.A. Illinois.A BOUT THE AUTHOR
Justin London has developed ﬁxed-income and equity models for trading companies and his own quantitative consulting ﬁrm. He has analyzed and managed bank corporate loan portfolios using credit derivatives in the Asset Portfolio Group of a large bank in Chicago. London holds a B. an M. A graduate of the University of Michigan. computer science.A. in ﬁnancial engineering. as well as advised several banks in their implementation of derivative trading systems. in applied economics.