Bocconi Home

CARLO AMBROGIO FAVERO

Teaching > Course syllabus


THE EMPIRICAL APPLICATION OF FINANCE with R and Chat GPT (20135)


This course segment aims to illustrate the practical application of the Theory of Finance to real-life asset allocation problems. The course introduces  empirical modelling in finance, by illustrating its working and its historical evolution. The main focus is  on the hands-on implementation of this approach using actual data, utilizing specific models to exemplify its practicality. Through the course we shall also show how Chat GPT could be used to help in doing Empirical Finance with R.

In the course, we will delve into the process of translating financial theory into action on data through modeling. Basic models will be explored, and programming will emerge as an essential prerequisite for data manipulation. We will acquaint ourselves with the statistical software R and exhibit the application of theoretical concepts to financial data, illustrated by sample programs, exercises, and corresponding solutions.

Lectures will actively involve the demonstration and discussion of R codes. Attendees are expected to engage in real-time coding on their laptops during lectures. To facilitate this, two Teaching Assistants will be available for support during dedicated office hours.

Teaching Assistants:
Gabriele Carta, gabriele.carta@unibocconi.it, Office Hours: Tuesdays 18-20
Ruben Fernandez Fuertes, ruben.fernandez@phd.unibocconi.it

Evaluation Method:
Students will be offered two options for being evaluated.

  1. The first one is  written assignment. Students taking this option will have three days to complete a report based on a hands on application on real data using R. The application will involve downloading data from the web, using them through model specification, estimation and simulation to build portfolios and analyze their performance. The assigment is a group-work ( group of 5 students is the norm). After the assignment has been handed-in, a member of each group will be randomly chosen to discuss the assigment. The discussion will determine the final mark attributed to the assignment.  
  2. The second option is a question in the final exam that will evaluate participants' proficiency in applying R to put financial theory into practical application. This assessment will gauge comprehension levels of solution methodologies and application scenarios discussed throughout the course.

Assignment December 2023solution, Question(with answers) in the Final exam January 2023

The course is based on the following set of Lecture Notes: Favero C.A. and C.Tebaldi(2023) "Lectures on the Theory and Application of Modern Finance with R and ChatGPT", INDEX, PART1(Tebaldi), PART 2(Favero)
Slides Chapter 1Slides Chapter 2, Slides Chapter 3Slides Chapter 4Slides Chapter 5

Section 1: Empirical Models in  Finance 
1.Introduction 
2. The relevance of the distribution of future returns in finance. Some
Illustrative Examples. 
2.1 Standard Portfolio Theory
2.2 Risk Parity Portfolios 
3. Predicting the distribution of future returns: The Econometric Modelling
Process 
3.1 The Challenges of Financial Econometrics
4 Empirical Modelling of Asset Prices 
4.1 The view from the 1960s: Efficient Markets and CER 
4.1.1 Time-Series Implications 
4.1.2 Returns at different horizons
4.1.3 The Cross-Section of Returns
4.1.4 The Volatility of Returns 
4.1.5 Implications for Asset Allocation
5 Empirical Challenges to the traditional model 
5.1 The time-series evidence on expected returns
5.2 Anomalies 
5.3 The Cross-section Evidence on Expected Returns
5.4 The behaviour of returns at high-frequency: non-normality and heteroscedasticity
6 The Implications of the new evidence 
6.1 Asset Pricing with Predictable Returns

Useful links:
Risk Parity Portfolios in R 

Section 2: Asset Prices and Returns 
1. Introduction 
2. Returns 
2.1 Simple and log Returns
2.2 Statistical models of returns.Normal and log-normal distributions
2.3 Multi-period returns and annualized returns
2.4 Working with Returns
3. Stock and Bond Returns
3.1 Stock Returns and the dynamic dividend growth model
3.2 Bond Returns: Yields-to-Maturity, Duration and Holding Period Returns 
3.2.1 Zero-Coupon Bonds 
3.2.2 Coupon Bonds 
4 Going to the data with R 
4.1 Getting Started 
4.1.1 Ask Chat GPT

  • Using Section 1-2 of Torfs and Bauer Install R and R studio on your computer and learn how to run them
  • Your first code intro.R
  • learn  to clear the memory, set-up working directory, installing and running packages (libraries) 

EXERCISE 1 Write an R code that answers to all the ToDo points  in  Torfs P. and C. Bauer(2014) “A (very short) introduction to R” ,
SOLUTIONS FOR the Torfs-Brauer TO DO LIST

4.2 Data Objects in R 
4.2.1 Ask Chat GPT 

  • a code to illustrate data objects data_ob
  •   learn about all the different data-types and data structures(Vector, Matrices, Arrays, Data Frames,Lists) in R

4.3 Data Handling in R 
4.3.1 Importing Data into R

  • a code to illustrate downloading data with R from the web : the R version, the Rmd version
  •  getting data from https://finance.yahoo.com/ using quantmod
  •  getting data from  https://fred.stlouisfed.org/ using quantmod (in alternative you can use fredR)
  •  getting data from an URL, the case of COVID data for Italian Regions 
  •  getting data using API's 

Accessing APIs from R a tutorial , an R code for the tutorial
Accessing data from Github using an R code 
Github and Github Desktop A tutorial online 

  • a code to illustrate data handling: the R Version, the .Rmd versionThe data  (zipped files with data sets used by dH.r, to be placed in the same directory)
  • Importing and Exporting Data from different formats
  • setting-up dataframes, selecting data, transforming data
  • subsetting data, omitting na
  • extensible time series (xts)  objects

4.3.2 Pre-Processing Data
 EXERCISE 2 An Introduction to Data HandlingSOLUTION 

4.4 Data Exploration and Graphics
4.4.1 Ask Chat GPT 

  • an illustrative code : the R version, the database in .XLSX format 
  • clear the memory, set-up working directory, install and load the relevant packages
  • load the data and create a time-series object and a data frame
  • data-transformation 
  • graphical and descriptive data analysis

4.5 Programming and Control Flows with R: Interacting with Chat GPT  

  • a code to illustrate programming prog.R
  • if-else statements, using switch, loops, functions in R
  • Interacting with Chat GPT on the efficient frontier: a basic code for the efficient frontier with two and three assets effront.R, a more advanced code, after some interaction with Chat GPT

4.6 Dynamic Documents with R Markdown
Dynamic Documents with R Markdown: build a report with all results and comments
a code to illustrate R Markdown Example.Rmd

 References
An online introduction to R
Torfs Brauer "A Very Short Intro to R" 
An introduction to R Markdown
Writing R-codes in Finance with Chat-GPT
James, Witten, Habstie and Tibshirani (2011) An introduction to Statistical Learning with Applications to R, Springer,  Ch 1-2 
Singh AK and DE Allen(2017) R in Finance and Economics. A Beginners Guide, World Scientific Publishing, Ch 1,2,3,4
Codes for data objects, data handling, and programming are modified versions of the codes provided by the two autors and downloadable at http://www.rforresearch.com/r-in-finance-economics
Heiss F. (2016)  Using R for introductory Econometrics,  
Yihui Xie, Dynamic Documents with R and Knitr,  Chapman and Hall 
Regenstein J.K.(2019) Reproducible Finance with R. Code Flows and Shiny Apps for Portfolio Analysis, CRC Press 
Sceuch C., S. Voigt, P.Heiss (2023) Tidy Finance with R., CRC Press

Section 3: The Modelling Process at work: the Constant Expected Return (CER) model
3.1    Introduction
3.2    Model Specification: the Constant Expected Return Model
3.2.1 Stocks for the long run 
3.2    Model Estimation
3.2.1 Parameters Estimation in a linear model
3.2.2 OLS in the CER
3.2.3 From one-period to multi-period returns in the CER
3.3    Model Simulation: Monte-Carlo and Bootstrap Methods
3.4    The CER model at work with R
3.4.1 Asset Allocation with the CER, an illustrative code 
3.4.2 Model Simulation with the CER: backtesting and VaR an illustrative code

EXERCISE 3: Minimum Variance vs Tangency Portfolios, solution 
References
Diethelm Würtz, Tobias Setz, Yohan Chalabi, William Chen, Andrew Ellis
Rmetrics eBooks 2009, NEW: Update 2015 
The Complete Guide to Portfolio Optimization in R,

Section 4: Factor Models For Asset Prices And Returns
4.1 Introduction: Factor Models and Reduction in Dimensionality 
4.2 Factor Models:Time-Series Representation 
4.3 Factor Models: Cross-Sectional representation 
4.4 Factor-based Portfolios and Factor Exposures 
4.5 A single factor model:The CAPM 
4.5.1 Asset Allocation with the CER and the CAPM in R , code
4.6 Validating Factor Models
4.6.1 Which Factors ?
4.7 Factor Models with Predictability 
4.7.1 An illustration with R, code 

References
Ang. A (2014) Asset Management: a Systematic Approach to Factor Investing, Oxford University Press
Fama, Eugene F., and James D. MacBeth.(1973) "Risk, return, and equilibrium: Empirical tests." Journal of Political Economy 81.3 (1973): 607-636.
Fama, Eugene F., and K. French(1993) "Common Risk Factors in the Returns of Stocks and Bonds" Journal of Financial Economics 33 : 3-56.
Fama, E. F., & French, K. R. (2015). A five-factor asset pricing model. Journal of Financial Economics116(1), 1-22.
ZIVOT and WANG(2006) Modelling Financial Time-Series with S-Plus, Springer , in particular Chapter 15 "Factor Models for Asset Returns" with an illustrative Rcode 
Kenneth R. French Data Library 

Section 5: Models for Risk Measurement
1. Risk Measurement 
1.1 Value at Risk (VaR) 
2 VaR without predictability 
2.1 VaR with the CER 
2.2 VaR with the CAPM
3 The Evidence from high-frequency data 
4 A general model for high-frequency data
4.1 Generalized Autoregressive Conditional Heteroskedastic (GARCH) Models 
4.2 GARCH Properties 
4.3 GARCH Forecasting 
4.4 Testing for GARCH 
5 Estimation of GARCH Models 
5.1 Quasi MLE Estimation
6 From GARCH to VaR 
6.1 GARCH with factors 
7 Measuring risk with and without predictability an illustration with R, code 
8 Backtesting VaR 
8.1 Unconditional Coverage Testing
8.2 Independence Testing 
8.3 Conditional Coverage Testing
8.4 Backtesting VaR in R, code
9 Beyond GARCH:Threshold (GJR) GARCH model, and Multivariate GARCH
References
Christoffersen, Peter. Elements of financial risk management. Academic press, 2011.

Last update 26/01/2024



In this section