R Programming and Its Applications in Financial Mathematics 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 p, p, A SCIENCE PUBLISHERS BOOK A SCIENCE PUBLISHERS BOOK 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 No claim to original U.S. Government works Printed on acid-free paper Version Date: 20170119 International Standard Book Number-13: 978-1-4987-4799-8 (Hardback) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let uCsR kCn Powre ssso we may rectify in any future reprint. Taylor & Francis Group E60x0ce0p Bt raosk epne rSmouitntedd P uanrkdwera yU N.SW. C, oSpuyitrei g3h0t0 Law, no part of this book may be reprinted, reproduced, transmitted, oBro cuat iRliazteodn i, nF La n3y3 4fo8r7m-2 7b4y2 any electronic, mechanical, or other means, now known or hereafter invented, includ- ing photocopying, microfilming, and recording, or in any information storage or retrieval system, without written ©pe 22rm0011i78s sbiyo nT afryolomr t&h eF rpaunbcliiss hGerrosu.p, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business For permission to photocopy or use material electronically from this work, please access www.copyright.com N(hott cpl:a//imww tow o.croigpiynraigl hUt..Sco. Gmo/v) eorrn cmonetnatc wt tohrek sCopyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety Pofr iunsteerds .o Fno arc oidr-gfarneeiz paatipoenrs that have been granted a photocopy license by the CCC, a separate system of payment Vhaesr sbioeenn D aartrea:n 22g00e11d87.00111094 ITnrtaedrneamtiaornka lN Sotatnicdea:r Pdr Boodoukc tN ourm cobrepr-o1r3a:t 99e77 n88a--11m--44e9s9 88m77-a-46y76 b90e99 -t-8r8a (dHeamrdabrkacsk o)r registered trademarks, and are used only for identification and explanation without intent to infringe. This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or theL cibonrasreyq uoefn Ccoesn gorf etshse Cir autsaelo. Tgihneg a‑iunt‑hPoursb laincdat piounb lDisahtears have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to Names: Liu, Jian (Chemical engineer), editor. | Jiang, San Ping, editor. publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let Title: Mesoporous materials for advanced energy storage and conversion us know so we may rectify in any future reprint. technologies / editors, Jian Liu, Department of Chemical Engineering, Faculty of Science and Engineering, Curtin University, Perth, WA, Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, Australia, San Ping Jiang, Fuels and Energy Technology Institute & or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, includ- Department of Chemical Engineering, Curtin University, Perth, WA, ing photocopying, microfilming, and recording, or in any information storage or retrieval system, without written Australia. permission from the publishers. Description: Boca Raton, FL : CRC Press, Taylor & Francis Group, 2017. | For permSisesriioens: Ato spciheontcoec poupby liosrh eursse b omoakt e|r Iinacl leuldeecst rboinblicioagllrya pfhroicma l trheifse rwenocreks, please access www.copyright.com (http://wwanwd.c ionpdyerxi.ght.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923Id, e9n7t8i-f7ie5r0s-: 8L4C0C0N. C 2C0C16 i0s 4a2 n5o0t9-|f oISr-BpNro 9fi7t8 o1r4g9a8n7i4z7a9ti9o8n (thhaartd pbraocvki d: easlk l.i cpeanpseers) a|nd registration for a variety of users. FISoBr Nor 9g7a8n1iz4a9t8io74n8s 0t1h8a t( eh-abvoeo kb)een granted a photocopy license by the CCC, a separate system of payment has been aSrurbajnegcetsd: .LCSH: Electric batteries--Materials. | Fuel cells--Materials. | Solar cells--Materials. | Mesoporous materials. TrademaCrkla Nssoitfiiccaet:i Porno: dLuCcCt oTrK co29rp0o1r .aMte4 n7 a2m01e7s m| DayD bCe 6t2ra1d.3e1m/2a4rk2s4 0o2r 8re4g--isdtce2r3ed trademarks, and are used only for identificaLtiCon r eacnodr de xapvlaainlaabtiloe na tw hittthposu:/t/ licnctnen.lto tco.g ionvf/r2in01g6e.042509 Visit the Taylor & Francis WLebib sriatrey a otf Congress Cataloging‑in‑Publication Data http://www.taylorandfrancis.com Names: Liu, Jian (Chemical engineer), editor. | Jiang, San Ping, editor. and the CTRitCle P: Mreessso Wpoerbo suist em aatterials for advanced energy storage and conversion http://wwtewc.hcnrcoplorgeisess. c/o emditors, Jian Liu, Department of Chemical Engineering, Faculty of Science and Engineering, Curtin University, Perth, WA, Australia, San Ping Jiang, Fuels and Energy Technology Institute & Department of Chemical Engineering, Curtin University, Perth, WA, Australia. Description: Boca Raton, FL : CRC Press, Taylor & Francis Group, 2017. | Series: A science publishers book | Includes bibliographical references and index. Identifiers: LCCN 2016042509| ISBN 9781498747998 (hardback : alk. paper) | ISBN 9781498748018 (e-book) Subjects: LCSH: Electric batteries--Materials. | Fuel cells--Materials. | Solar cells--Materials. | Mesoporous materials. Classification: LCC TK2901 .M47 2017 | DDC 621.31/24240284--dc23 LC record available at https://lccn.loc.gov/2016042509 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 vi < R Programming and Its Applications in Financial Mathematics 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 viii < R Programming and Its Applications in Financial Mathematics 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 Contents n ix 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
Description: