# Introduction to Computational Finance and Financial Econometrics with R

*2022-02-25*

# Preface

Outline of preface (preliminary and incomplete). June 21, 2016.

I started teaching the course Introduction to Financial Econometrics at UW in 1998. Motivation was to teach more statistics and quantitative methods to economics majors. I found that combining statistics topics with finance applications was very effective and popular.

Early classes used Microsoft Excel as the main software tool (R was not around then). Experience with Excel was, and still is, in high demand by employers in the finance industry. However, Excel is not a good tool for doing statistics. In early 2000s I used Excel together with S-PLUS, as I was a consultant to Insightful at that time and I was developing the S+FinMetrics software and writing the book Modeling Financial Time Series with S-PLUS. This hybrid approach worked well but the difficulties of getting student licenses at the beginning of the quarter etc. was always a headache. In 2008, I made the switch to R and never looked back.

Many economics students lack exposure to programming languages (programming classes are typically not required outside of computer science majors) and find R a difficult language to learn and use. My approach to dealing with this reality was to provide all of the R code to do calculations in class and on homework assignments. In this way students did not have to worry about the coding aspect of the class and could concentrate on the concepts. I found that students learned to use R quite well this way – learning by doing – and most students were fairly proficient in R by the end of the course.

There are a lot of free resources on the web for learning R. In my Coursera course I partnered with DataCamp to provide a web-based learning environment for R tailored to the course material. I have found that this has substantially lowered the learning curve for many students. Student mostly use either a MAC or PC for their work (a few geeks use UNIX). I found that some things didn’t always work well across platforms (usually issues with the file system). In 2014, I required students to use Rstudio as the environment for using R. This make learning and working with R even easier. Another advantage of using Rstudio is that my lecture notes and homework assignments can be created using Rmarkdown.

Late 1990s was the beginning of the financial econometrics revolution. There was lots of interest in Garch and volatility modeling. Campbell, Lo and MacKinlay’s book was published. My course was one of the first undergraduate courses in the U.S. to focus on statistics and finance.

(Longer print book) Book is based on course notes for my advanced undergraduate course Introduction to Computational Finance and Financial Econometrics that I have taught at the University of Washington over the last 15 years. This course is taken mainly by senior economics majors who are pursuing the more quantitative BS in economics. These students typically have at least one year of calculus, intermediate microeconomics with some calculus (so they are exposed to optimization under constraints using Lagrange multipliers), and an introductory course in probability and statistics for economics majors. Some students have taken a course in linear algebra, some have taken an introductory econometrics course, and some have taken a financial economics course. I find that the typical introductory probability and statistics course is at a lower level than is needed for much of the material in my course. Ideally, students should have an intermediate probability and statistics course that uses calculus. As a result, I spend about two weeks at the beginning of the course doing a review of random variables and probability that uses basic calculus. A key part of the review is the manipulation of random variables, computing expectations of functions of random variables, etc. so that the students have the necessary tools to work with simple probabilistic models for asset returns. I also do a quick review of basic matrix algebra. Most introductory textbook treatments of portfolio theory show calculations for the simple case of two assets and then talk about how the calculations extend to the general case of many assets. Hence, students are not capable of implementing the theory for real world portfolios. I emphasize that with knowledge of just a little bit of matrix algebra (essentially matrix addition, multiplication and inversion) the general analysis of portfolio theory can be easily learned and implemented. Also, the mathematical analysis of multivariate distributions is greatly simplified using matrix algebra. I do a short introduction to time series concepts because the statistical analysis of asset returns involves using historical time series data. I define time series/stochastic process and the concepts of stationary and nonstationary time series and give simple examples of such processes that are commonly used in the modeling of financial time series. After the review material, I jump into descriptive statistical analysis of historical financial time series data. I emphasize that my approach to building models for asset returns is based on first looking at the historical data and studying its properties. I start by telling students to pretend that observed returns on assets are realizations from some unknown covariance stationary stochastic process. I review common graphical and numerical descriptive statistics (e.g., histograms, QQ-plots, sample mean, variance, correlation, etc.). The goal of descriptive statistical analysis is to compile a collection of “stylized facts” that can be used as building blocks for probabilistic models for asset returns. For example, I show that monthly asset returns are typically uncorrelated over time, have empirical distributions (histograms) that look like normal bell curves, have means close to zero, are positively correlated with other assets, etc. I also emphasize that some “stylized facts” of asset returns appear to stay the same for different investment horizons (daily, weekly, and monthly returns are uncorrelated) and some do not (e.g., daily and weekly returns have fatter tails than the normal and show evidence of nonlinear time dependence in higher moments)

(Shorter ebook). This book is based on my University of Washington sponsored Coursera course Introduction to Computational Finance and Financial Econometrics that has been running every quarter on Coursera since 2013. This Coursera course is based on the Summer 2013 offering of my University of Washington advanced undergraduate economics course of the same name. At the time, my UW course was part of a three course summer certificate in Fundamentals of Quantitative Finance offered by the Professional Masters Program in Computational Finance & Risk Management that was video-recorded and available for online students. An edited version of this course became the Coursera course. The popularity of the course encouraged me to convert the class notes for the course into a short book.

The book was typeset using Lyx with all of the R code examples embedded into the document and evaluated using knitr. I am extremely grateful to Yihui Xie for writing his wonderful book Dynamics Documents with R and knitr as this book would not have been possible without it. I am a firm believer in the “reproducible research paradigm”.

The R package IntroCompFinR contains all of the financial data (downloaded from finance.yahoo.com) used for the examples presented in the book as well as a number of R functions for portfolio and risk analysis. The package is freely available on R-forge and can be installed using `install.packages("IntroCompFinR", repos="http://R-Forge.R-project.org")`

.

There are now many good books written on the topic of computational finance and financial econometrics with examples in R at various levels of sophistication. Books written for advanced U.S. undergraduates include Carmona (2014) and Tsay (2012). Books written for Masters students in computational finance or financial engineering courses include Fan and Yao (20xx), Lai and Xing (2008), Ruppert and Matteson (2015) and Tsay (2010). All of these books assume the readers have a broader mathematical and statistical background than what I assume here. Hence, this book is truly an “introduction” to the methods of computational finance and financial econometrics and is appropriate for undergraduate economics and finance majors at Universities worldwide. Another feature that distinguishes this book from others is the extensive use of R and illustrated R code throughout the book. In this regard, this book is a “hand on” book in which the student can readily execute the R examples on their own computer while reading the book. This allows the student to also learn R as well as the concepts in the book.

Eric Zivot

Seattle, Washington

June 2016