How to Implement Market Models Using VBA
How to Implement Market Models Using VBA FRANÇOIS GOOSSENS
This edition first published 2015 2015 François Goossens Registered office John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex, PO19 8SQ, United Kingdom For details of our global editorial offices, for customer services and for information about how to apply for permission to reuse the copyright material in this book please see our website at www.wiley.com. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, except as permitted by the UK Copyright, Designs and Patents Act 1988, without the prior permission of the publisher. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Designations used by companies to distinguish their products are often claimed as trademarks. All brand names and product names used in this book are trade names, service marks, trademarks or registered trademarks of their respective owners. The publisher is not associated with any product or vendor mentioned in this book. Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with the respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. It is sold on the understanding that the publisher is not engaged in rendering professional services and neither the publisher nor the author shall be liable for damages arising herefrom. If professional advice or other expert assistance is required, the services of a competent professional should be sought. Library of Congress Cataloging-in-Publication Data is Available ISBN 978-1-118-96200-8 (hardback) ISBN 978-1-118-96198-8 (ebk) ISBN 978-1-118-96199-5 (ebk) ISBN 978-1-119-06583-8 Cover Design: Wiley Top Image: istock.com/pixel_dreams; Bottom Image: istock.com/awstok Set in 10/12pt Times by Laserwords Private Limited, Chennai, India Printed in Great Britain by TJ International Ltd, Padstow, Cornwall, UK
Contents Preface Acknowledgements Abbreviations About the Author ix xi xiii xv CHAPTER 1 The Basics of VBA Programming 1 1.1 Getting started 1 1.2 VBA objects and syntax 2 1.2.1 The object-oriented basic syntax 3 1.2.2 Using objects 3 1.3 Variables 5 1.3.1 Variable declaration 5 1.3.2 Some usual objects 7 1.3.3 Arrays 9 1.4 Arithmetic 10 1.5 Subroutines and functions 13 1.5.1 Subroutines 14 1.5.2 Functions 15 1.5.3 Operations on one-dimensional arrays 16 1.5.4 Operations on two-dimensional arrays (matrices) 16 1.5.5 Operations with dates 19 1.6 Custom objects 21 1.6.1 Types 21 1.6.2 Classes 22 1.7 Debugging 24 1.7.1 Error handling 24 1.7.2 Tracking the code execution 25 v
vi CONTENTS CHAPTER 2 Mathematical Algorithms 29 2.1 Introduction 29 2.2 Sorting lists 29 2.2.1 Shell sort 29 2.2.2 Quick sort 32 2.3 Implicit equations 34 2.4 Search for extrema 36 2.4.1 The Nelder-Mead algorithm 36 2.4.2 The simulated annealing 40 2.5 Linear algebra 43 2.5.1 Matrix inversion 44 2.5.2 Cholesky decomposition 46 2.5.3 Interpolation 48 2.5.4 Integration 57 2.5.5 Principal Component Analysis 60 CHAPTER 3 Vanilla Instruments 67 3.1 Definitions 67 3.2 Fixed income 67 3.2.1 Bond market 68 3.2.2 Interbank market 72 3.3 Vanilla derivatives 75 3.3.1 Forward contracts 75 3.3.2 Swaps 77 3.3.3 Bond futures 81 3.4 Options basics 84 3.4.1 Brownian motion 84 3.4.2 Ito integral 85 3.4.3 Ito formula 86 3.4.4 Black Scholes basic model 89 3.4.5 Risk-neutral probability 90 3.4.6 Change of probability 90 3.4.7 Martingale and numeraires 92 3.4.8 European-style options pricing 94 3.5 First generation exotic options 95 3.5.1 Barrier options 95 3.5.2 Quanto options 102 CHAPTER 4 Numerical Solutions 105 4.1 Finite differences 105 4.1.1 Generic equation 105 4.1.2 Implementation 106
Contents vii 4.2 Trees 112 4.2.1 Binomial trees 112 4.2.2 Trinomial trees 116 4.3 Monte-Carlo scenarios 116 4.3.1 Uniform number generator 117 4.3.2 From uniform to Gaussian numbers 127 4.4 Simulation and regression 129 4.5 Double-barrier analytical approximation 134 CHAPTER 5 Monte-Carlo Pricing Issues 139 5.1 Multi-asset simulation 139 5.1.1 The correlations issue 139 5.1.2 The Gaussian case 139 5.1.3 Exotics 143 5.2 Discretization schemes 146 5.3 Variance reduction techniques 147 5.3.1 Antithetic variates 147 5.3.2 Importance sampling 148 5.3.3 Control variates 153 CHAPTER 6 Yield Curve Models 163 6.1 Short rate models 163 6.1.1 Introduction 163 6.1.2 Hull and White one-factor model 164 6.1.3 Gaussian two-factor model 180 6.1.4 Hull and White two-factor model 203 6.2 Forward rate models 204 6.2.1 Generic Heath-Jarrow-Morton 205 6.2.2 LMM (LIBOR market model) 216 CHAPTER 7 Stochastic Volatilities 233 7.1 The Heston model 234 7.1.1 Code 234 7.1.2 A faster algorithm 239 7.1.3 Calibration 248 7.2 Barrier options 254 7.2.1 Numerical results 257 7.2.2 Code 257 7.3 Asian-style options 260 7.4 SABR model 264 7.4.1 Caplets 264 7.4.2 Code 265
viii CONTENTS CHAPTER 8 Interest Rate Exotics 267 8.1 CMS swaps 267 8.1.1 Code 269 8.2 Cancelable swaps 272 8.2.1 Code 272 8.2.2 Tree approximation 276 8.3 Target redemption note 281 8.3.1 Code 282 Bibliography 287 Index 289
Preface Graduate students and practitioners coming to the area of quantitative finance may be daunted by the abstruseness of stochastic matters, coupled with the austerity of scientific programming languages. Learning these disciplines is a challenge that some are reluctant to tackle. Then, to get some hands-on expertise of complex valuation issues with an easy-to-learn programming language, without delving too deeply into the theory, proves to be an attractive alternative: this is what this book invites you to do. For this purpose, VBA has been chosen for its accessible code and its connection with Excel and its easy-to-use spreadsheet format. What sort of financial applications may be run in VBA? Contrary to common belief, you can run a lot of complex pricings with VBA, almost as fast as in C++ even if VBA is not going to compete with C++ as the engine of big risk management systems. This book will not specifically address the computing performance topic: for a comprehensive approach to this see, e.g., Webber (2011). However, one thing to remember is that, not only in terms of accuracy, but also in terms of computational cost, VBA is an efficient tool to get fast and in-depth understanding skills on complex derivatives. How this book can help you To complete a pricing project from scratch is a road paved with challenging steps. If you are holding this book it means you are already a VBA developer, or you intend to become one, and you have some taste for quantitative matters: it is already a good start. Guiding you through the technical obstacles and making you familiar with the means to fix them is the ambition of this book. The quicker you complete your first Monte-Carlo algorithm or tree-based pricing program, the more self-confidence you get, if you are a newcomer in applications coding. To strengthen this self-confidence, no aspect of quantitative issues must remain in the shadow: you will probably notice that barrier option valuation theory and yield curve modeling are developed in detail, for they illustrate best the key notions of stochastic calculus. Knowledge prerequisites No VBA knowledge is required. The essentials presented in this book should suffice. You must have basic notions of analysis (continuity, derivability, integration) and linear algebra basics (matrix calculus). In the capital markets domain, you must be familiar with vanilla derivatives (forward contracts, European-style options) and, preferably, have some knowledge of the common risk management issues, such as delta or gamma positions. As regards the stochastic calculus theory, this book helps you to apply theory with a practical approach. ix
x PREFACE Structure of the book Chapter 1 delivers the essential syntax and vocabulary elements to start coding algorithms in VBA; some emphasis is put on matricial calculus, as it occupies a prominent place in numerical algorithms and Monte-Carlo scenarios. Chapter 2 introduces common algorithms that fix unavoidable numerical problems in the course of a pricing algorithm. In fact, every valuation program will encounter at least one interpolation or optimization hurdle at some stage of the algorithm. Chapter 3 gives the reader an opportunity to check his knowledge of the capital market fundamentals. For complete VBA beginners, it also provides some training exercises to put into practice VBA techniques, such as loops. In a second part, one key aspect of the stochastic calculus theory, the change of probability measure is addressed. This technique is implemented via barrier options pricing. Chapter 4 deals with numerical solutions that are called in aid when no analytical solution is available: to put it bluntly, it fixes 99% of the problems. This is the core material of the book, since every pricing algorithm refers more or less to one of the recipes presented in this chapter. Chapter 5 deals with classes of assets that are valued using Monte-Carlo simulation methods. It covers multi-asset and path-dependent instruments. Variance reduction techniques are logically investigated in this chapter. Chapter 6 addresses widely used yield curve models and the critical calibration issues. It introduces first Hull & White and Gaussian short rate models, secondly Heath-Jarrow-Morton and LIBOR market forward rate curve models. Chapter 7: as a widespread standard stochastic volatility model, Heston s is here presented in detail. Some recipes to tackle exotic pricings using this model are developed. In addition, one paragraph is devoted to SABR. Chapter 8: in this chapter, our yield curve modelings are put into application to implement numerical algorithms aimed at some "standard" interest rate exotics: CMS Swaps, Cancelable Swaps, and Target Redemption Notes. Solutions resulting from different models or numerical methods will be compared.
Acknowledgements My thanks go first to the staff of Wiley for their patience and assistance in the unenviable task of correcting the manuscript. I would also like to thank the practitioners and principal lecturers at the Paris Dauphine and Paris-Sorbonne universities who provided time from their busy schedules to review or comment on this book. Advice and encouragement given by Sofiane Aboura especially have been of great help in completing the work. Last but not least, I would like to express my gratitude to the authors, some are mentioned in the bibliography, who succeeded in making quantitative finance an appealing matter: I owe them an incurable taste for financial algorithms. xi
Abbreviations ATM BGM BS CMS HJM IRS LMM OTC RNG RV TARN At-the-money (options) Benhamou/Gobet/Miri approximation method (Heston model) Black Scholes Constant Maturity Swap Heath-Jarrow-Morton model Interest Rate Swap LIBOR Market Model Over-The-Counter Random Number Generator Random Variable TArget Redemption Note xiii
About the Author François Goossens has 12 years experience in Java and VBA programming of pricing algorithms. As a consultant, he currently trains students and young practitioners in computational finance through VBA coding. Prior to that he ran, over 15 years, interest-rates and equity related trading desks with Credit Lyonnais and Ixis, and was strongly involved in exotic derivatives management. François graduated from École Centrale in Paris. xv