CARLO AMBROGIO FAVERO

Lectures on the Theory and Application of Modern Finance with R and ChatGPT

Carlo A. Favero, Claudio Tebaldi 

 

These lecture notes are thought for Master courses  in Finance, Fintech and Quantitative Finance programmes. We fully subscribe to the philosophy that  Master students should be offered courses that are really at the cutting edge of the technologies and advances that are disrupting the financial industry and delve deep into topics such as A.I., machine learning, and their importance for Asset Management.  
in these notes the  illustration of the theory of Finance is paired with practical applications to real-life asset allocation problems. A hands-on approach is proposed  to construct and manipulate databases to build portfolios, assess their performance and manage their risk. 
The course begins with a section on the fundamentals on individual choice, to then move from individual choice to market valuation, covering the traditional Markowitz mean-variance approach, market-based asset pricing and Arbitrage-based pricing theory. 
 
Empirical modelling in finance is then introduced by illustrating its working and its historical evolution.
The translation of financial theory into action on data is driven by building predictive models for asset prices and returns. Basic models are explored, and programming emerges as an essential prerequisite for data manipulation. Readers can acquaint themselves with the statistical software R and exhibit the application of theoretical concepts to financial data, illustrated by sample programs, exercises, and corresponding solutions.

The main focus is on the hands-on implementation of this approach using actual data, utilizing specific models to exemplify its practicality. The text also  shows how Chat GPT could be used to help in doing Empirical Finance with R. The interaction with chat GPT is illustrated via live session  entitled "Ask Chat GPT", which provide  guidance to efficient interaction with Chat GPT to write codes designed for specific tasks and adapt and debug them, if necessary.  

The Lectures are meant to provide a background for flipped classrooms and interactive teaching  with the  demonstration and discussion of R codes, in which students are expected to engage in real-time coding  on their laptops and group discussions.

 

Chapter 1: Individual Choice 
1.1 Different paradigms for decision theory 
1.1.1 Decision theory under certainty 
1.1.2 Second: Decision theory under uncertainty 
1.1.3 Third: Decision theory under uncertainty, asymmetric informa-
tion and strategic interaction
1.2 A paradigm for rationality 
1.3 Decision theory under uncertainty 
1.4 Risk aversion
1.4.1 Quantifying risk aversion 
1.4.2 Certainty equivalent and risk premium
1.5 Insurance pricing principle
1.6 The Portfolio problem 
1.6.1 Two Securities and two states of the world
1.6.2 The case with N assets 
1.7 Subjective valuation
1.7.1 A numerical example 
1.8 Expected Utility reloaded 
1.8.1 Step 1: choice over binary actions
1.8.2 Step 2: Deriving the expected utility representation in a generic
state space with S states
1.9 Deviations from Expected Utility
1.9.1 Independence Axiom and the Allais Paradox 
1.9.2 The paradox of Ellsberg and the distinction between ambiguity
and risk
1.9.3 Preference for temporal resolution of uncertainty 
1.9.4 Anomalies observed by Khanemann and Tversky
1.9.5 Individual choice and collective decisions

Chapter 2: From Individual Choice to Market Pricing 
2.1 The Markowitz mean-variance approach
2.1.1 A bit of terminology
2.1.2 Minimum Variance Portfolio
2.1.3 Risk Parity Portfolios
2.1.4 The M-V model with two risky assets and no risk-free
2.1.5 Optimal allocation when a risk-free security is available
2.1.6 The general solution to the N risky assets and no risk free
2.1.7 The general solution in the presence of a risk-free security
2.1.8 The FOC for a mean-variance investor when a risk-free asset is
present.
2.2 Market-Based Asset Pricing
2.2.1 The Capital Asset Pricing Model
2.2.2 The Market Portfolio
2.2.3 The Capital Market Line 
2.2.4 The Security Market Line
2.2.5 SML and CML 
2.2.6 Market price as the present value of cash flows
2.2.7 The Zero-Beta CAPM (Black) 
2.2.8 The Roll’s Critique 
2.3 A structural interpretation 
2.4 Arbitrage-based Pricing Theory
2.4.1 The linear factor model 
2.4.2 Arbitrage Portfolios
2.4.3 The Ross (1973) arbitrage pricing argument
2.4.4 Economic Interpretation of the λ coefficients 
2.4.5 APT when the risk factors are portfolio excess returns
2.4.6 APT & CAPM 
2.4.7 APT tests and factor-mimicking portfolios
2.5 APT and risk-neutral valuation 
2.5.1 CAPM and SDF 

Chapter 3 Empirical Tests of Asset Pricing Models 
3.1 The view from the 1960s: Efficient Markets and Constant Expected Returns
3.2 The cross-sectional evidence: CAPM verification 
3.3 The Frazzini and Pedersen (2014) BAB factor construction
3.4 Empirical tests of APT, anomalies and Fama and French (1993)
3.5 Direct construction of traded portfolios
3.6 Time-Series analysis of returns 
3.6.1 The behaviour of returns at high-frequency
3.6.2 A more realistic description
3.7 Time series anomalies 
3.8 Returns at different horizons and the Dynamic Dividend Growth model of Shiller (1981) 
3.9 Conditional Asset Pricing with Predictable Returns 
3.10 Predictive Models in Finance 

Chapter 4 From Theory to Practice 
4.1 The Econometric Modelling Process 
4.2 The Challenges for Financial Econometrics 
4.3 Returns
4.3.1 Simple and log Returns 
4.3.2 Statistical models for asset prices and returns
4.3.3 Multi-period returns and annualized returns
4.3.4 Working with Returns
4.4 Stock and Bond Returns 
4.4.1 Stock Returns and the dynamic dividend growth model
4.4.2 Bond Returns: Yields-to-Maturity and Holding Period Returns 
4.5 Going to the data with R 
4.5.1 Getting Started 
4.5.2 Data Objects in R 
4.5.3 Data Handling in R
4.5.4 Data Exploration and Graphics 
4.5.5 Interacting with Chat GPT 
4.6 Appendix: The Data

R CODES and EXERCISES 

The R Basics

  • Use 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

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 

Data Objects in R 

  • 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

Data Handling and pre-processing 

  • 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


 EXERCISE 2 An Introduction to Data HandlingSOLUTION 

Data Exploration and Graphics
 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

Programming and Control Flows with R  

  • 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

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

 

Chapter 5 The Constant Expected Return model 
5.1 Model Specification
5.1.1 Stocks for the long run
5.2 Model Estimation .
5.2.1 Parameters Estimation in a linear model 
5.3 Model Simulation 
5.4 The CER model at work with R 
5.4.1 Asset Allocation with the CER 
5.4.2 Model Simulation: backtesting and VaR 

R CODES and EXERCISES 
 Asset Allocation with the CER, an illustrative code 
 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,

Useful links:
Risk Parity Portfolios in R 


Chapter 6 Factor Models 
6.1 Time-Series Representation
6.2 Cross-Sectional representation
6.3 Factor-based Portfolios and Factor Exposures 
6.4 Asset Allocation with the CER and the CAPM in R 
6.5 Validating Factor Models 
6.5.1 Which Factors ?
6.6 Factor Models with Predictability
6.6.1 An illustration with R 

R CODES and EXERCISES 

Asset Allocation with the CER and the CAPM in R , code
Factor Models with Predictability, an illustration with R, code 

References
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 

Chapter 7 Models for Risk Measurement 
7.1 Risk Measurement
7.1.1 Value at Risk (VaR)
7.2 VaR without predictability 
7.2.1 VaR with the CER 
7.2.2 VaR with the CAPM
7.3 The Evidence from high-frequency data
7.4 A general model for high-frequency data
7.4.1 GARCH Models for Heteroscedasticity
7.4.2 GARCH Properties
7.4.3 GARCH Forecasting 
7.4.4 Testing for GARCH 
7.5 Estimation of GARCH Models 
7.5.1 Quasi MLE Estimation 
7.6 From GARCH to VaR 
7.6.1 GARCH with factors 
7.7 Measuring risk: an illustration with R
7.8 Backtesting VaR
7.8.1 Unconditional Coverage Testing 
7.8.2 Independence Testing 
7.8.3 Conditional Coverage Testing 
7.8.4 Backtesting VaR in R  

R CODES and EXERCISES 

Measuring risk with and without predictability an illustration with R, code 
Backtesting VaR in R, code

 

 

 

 

Modificato il 18/11/2024