R Programming and its Applications in Financial Mathematics Shuichi Ohsaki Chief Japan Rates Strategist, American Investment Bank, Tokyo, Japan Jori Ruppert-Felsot Equity Derivatives Trader, Tokyo, Japan Daisuke Yoshikawa Hokkai-Gakuen University, Sapporo, Hokkaido, Japan CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2017 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business International Standard Book Number-13: 978-1-4987-4799-8 (Hardback) Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Preface The importance of having basic knowledge of computational methods continues to increase for those working in the financial services industry. Computational finance theory has developed along with advancements in computing technology. The objective of this book is therefore to present an introduction to the various computing methods used in the financial service industry, via the R programming language. This book is intended for graduate students who are interested in computational finance, junior bankers, economists and traders working in the finance industry. We have several reasons why R has been used, rather than other available languages; e.g., C++, C#, Java, Python, EXCEL VBA, and MatLab, etc. First, R is available free and is an open source programming language. As such it is easily extensible through the addition of packages and has a large and active community of R users. And as a high-level scripting language, it is very easy to set up and to use. Second, R was developed for statistical computation and thus comes equipped with various packages which enable us to do regression analysis and statistical tests, etc. This reduces the time and effort required for implementation of many statistical methods and allows us to execute numerical experiments easily. Third, R comes equipped with a graphic capability to visualize large sets of data and the results of analysis. This is an attractive feature, particularly if we want to produce high-quality graphics for publication. Further, the basics that we learn here with R, are easily applicable to other languages, because many languages, like C# and Python have similar coding logic, albeit different syntax. We expect readers to write and execute the sample code introduced in this book. Following the sample code will help the readers’ understanding of the theory and improve their facility with R. Of course, incomplete understanding of theory may lead to codes which do not work well. However, the process of correcting an incomplete code is an important step in understanding the theory and improving one’s coding technique. We focus on two major aspects of computational finance theory: (1) statistical analysis of financial data, and (2) the valuation of financial instruments. We have divided the book into some parts. Section I of this book is devoted to statistical and time-series analysis, Section II covers the finance theory for the valuation of financial instruments and we focus on numerical methods for derivative pricing in Section III. This book was first published in Japanese, derived from sets of lectures given by the authors Ohsaki and Yoshikawa at the Graduate School of Management, Kyoto University in the Spring terms of 2008, 2009 and 2010. The lectures formed the basis of Section II and III, while Chapter 1 and Section I were written for the publication of the Japanese version of the book. Section 3.4.3 and Appendix B were added for the current English version of the book. We would like to thank the publishers, especially, the editor Vijay Primlani for his understanding and patience and Tetsuya Ishii who is the editor of the Japanese version for his understanding regarding the publication of the English version. We also thank Toshifumi Ikemori, Hideki Iwaki, Yoshihiko Uchida, and Zensho Yoshida for their helpful advice and support in completing this manuscript. Shuichi Ohsaki Jori Ruppert-Felsot Daisuke Yoshikawa Contents Preface v 1 Introduction to R Programming 1 1.1 Installation of R 2 1.2 Operators 6 1.3 Data structure 7 1.3.1 Scalar 9 1.3.2 Vector 11 1.3.3 Matrix 14 1.3.4 List 17 1.3.5 Data frame 20 1.3.6 Factor 21 1.3.7 Investigation of types and structures of data 22 1.4 Functions 22 1.5 Control statements 24 1.5.1 if statement 25 1.5.2 Iterative processing: for statement, while statement 27 1.6 Graphics 29 1.7 Reading and writing data 33 1.8 Reading program 34 1.9 Packages 36 SECTION I: STATISTICS IN FINANCE 2 Statistical Analysis with R 41 2.1 Basic statistics 41 2.2 Probability distribution and random numbers 46 2.3 Hypothesis testing 47 2.3.1 What is hypothesis testing? 47 2.3.2 t-Test of population mean 49 2.4 Regression Analysis 54 2.5 Yield curve analysis using principal component analysis 59 2.5.1 Yield curve 59 2.5.2 What is principal component analysis? 61 2.5.3 Example of principal component analysis using JGB 64 2.5.4 How to calculate the principal component analysis? 70 3 Time Series Analysis with R 73 3.1 Preparation of time series data 74 3.2 Before applying for models 77 3.3 The application of the AR model 80 3.3.1 Residual analysis 83 3.3.2 Forecasting 84 3.4 Models extended from AR 87 3.4.1 ARMA and ARIMA model 87 3.4.2 Vector autoregressive 91 3.4.3 GARCH model 97 3.4.4 Co-integration 103 3.5 Application of the time series analysis to finance: Pairs trading 107 SECTION II: BASIC THEORY OF FINANCE 4 Modern Portfolio Theory and CAPM 111 4.1 Mean-variance portfolio 113 4.2 Market portfolio 117 4.3 Derivation of CAPM 120 4.4 The extension of CAPM: Multi-factor model 121 4.4.1 Arbitrage Pricing Theory 121 4.4.2 Fama-French’s 3 factor model 125 4.5 The form of the efficient frontier 125 5 Interest Rate Swap and Discount Factor 129 5.1 Interest rate swap 129 5.2 Pricing of interest rate swaps and the derivation of 131 discount factors 5.3 Valuation of interest rate swaps and their risk 135 6 Discrete Time Model: Tree Model 139 6.1 Single period binomial model 140 6.1.1 Derivative pricing 141 6.1.2 Pricing by risk neutral measure 146 6.2 Multi period binomial model 149 6.2.1 Generalization to the multi period model 149 6.2.2 Pricing call options 152 6.3 Trinomial model 156 7 Continuous Time Model and the Black-Scholes Formula 161 7.1 Continuous rate of return 162 7.2 It oˆ’s lemma 165 7.3 The Black-Scholes formula 167 7.4 Implied volatility 170 SECTION III: NUMERICAL METHODS IN FINANCE 8 Monte Carlo Simulation 175 8.1 The basic concept of Monte Carlo simulation 176 8.2 Variance reduction method 179 8.2.1 Antithetic variates method 180 8.2.2 Moment matching method 182 8.3 Exotic options 184 8.4 Multi asset options 188 8.5 Control variates method 191 9 Derivative Pricing with Partial Differential Equations 199 9.1 The explicit method 202 9.2 The implicit method 205 APPENDIX A Optimization with R 213 A.1 Multi variate optimization problem 213 A.2 The efficient frontier by optimization problem 216 B Noise Reduction via Kalman Filter 221 B.1 Introduction to Kalman filter 222 B.2 Nonlinear Kalman filter 227 C The Other References on R 233 C.1 Information sources on R 233 C.2 R package on finance 234 References 235 Index 245 Chapter 1 Introduction to R Programming CONTENTS 1.1 InstallationofR ................................................... 2 1.2 Operators .......................................................... 6 1.3 Datastructure ...................................................... 7 1.3.1 Scalar ..................................................... 9 1.3.2 Vector .................................................... 11 1.3.3 Matrix .................................................... 14 1.3.4 List ....................................................... 17 1.3.5 Dataframe ................................................ 20 1.3.6 Factor ..................................................... 21 1.3.7 Investigationoftypesandstructuresofdata ................ 22 1.4 Functions .......................................................... 22 1.5 Controlstatements ................................................. 24 1.5.1 if-statement ............................................... 25 1.5.2 Iterativeprocessing:for-statement,while-statement ......... 27 1.6 Graphics ........................................................... 29 1.7 Readingandwritingdata ........................................... 33 1.8 Readingprogram .................................................. 34 1.9 Packages .......................................................... 36 The objective of this book is to learn the theory of finance using the R language,whichisaprogramminglanguagedevelopedspecificallyfornumerical 2 (cid:4) RProgrammingandItsApplicationsinFinancialMathematics calculation.Beforetheintroductionoffinancetheory,weneedtobuildthecases for using R. Thus, the first chapter is devoted to introducing R, its installation, andthebasicusageofR.ReadersalreadyfamiliarwithRprogrammingcanskip thischapter. 1.1 Installation of R LetusstartwiththeinstallationofR.1 Wecangetthebasicfilesforinstallation andpackagesfromthewebsiteoftheCRANproject(seeFigure1.1) http://www.r-project.org/index.html TogetanexefileforinstallationofR,clickdownload R(asofJuly2017). Then,thescreentransitstothefollowingonelikeFigure1.2.Chooseamirrorsite closetoyourlocation.Thus,forJapanwechoosehttps://cran.ism.ac.jp/. Then the screen transits to that like Figure 1.3. For Windows users, click “DownloadRforWindows”onthescreen“TheComprehensiveRArchiveNet- work”.IfyouarenotaWindowsuser,thenchoosetheappropriateoption. Figure1.1:Click“downloadR”onthepage“TheRProjectforStatisticalComputing” 1ThissectioncoverstheinstallationofRfromawindows-basedexecutablefile.Forinstallationon Macchoose“DownloadRfor(Mac)OSX”,andonLinuxchoose“DownloadRforLinux”(Figure1.3). Compilationfromsourcecodedependsontheoperationsystem.Here,wejustshowtheexamplebased onwindows-basedexecutablefile. IntroductiontoRProgramming (cid:4) 3 Figure1.2:Chooseamirrorsite Figure1.3:Click“DownloadRforWindows”onthescreen“TheComprehensiveRArchive Network”