# Replicating SW Section 15.3 calculations
# Load dataset, download from SW website
library(readxl)
<- read_excel("us_macro_quarterly.xlsx",
gdpgr col_types = c("skip", "numeric", "skip",
"skip", "skip"))
library(xts)
# Make a date variable
$date <- as.yearqtr(1955 + seq(0, 63*4-1)/4)
gdpgr# Create xts object
<- as.xts(gdpgr, frequency = 4)
gdpgr # Create GDP growth variable
$Y <- 400*diff(log(gdpgr$GDPC1))
gdpgrlibrary(dyn)
# Estimate AR(1) over the specified time period
<- dyn$lm(Y ~ lag(Y, 1), data=gdpgr['1962-01/2017-07'])
res1 # Estimate AR(2) over the specified time period
<- dyn$lm(Y ~ lag(Y, 1:2), data=gdpgr['1962-01/2017-07'])
res2 # Summary tables
summary(res1)
summary(res2)
library(lmtest)
# Testing whether beta1=0
# Standard errors are based on conditional homoscedasticity
coeftest(res1)
library(sandwich)
# Standard errors are robust to conditional heteroscedasticity
coeftest(res1, vcov. = vcovHC)
# Standard errors are robust to conditional heteroscedasticity and serial correlation (autocorrelation)
# More details in SW Section 16.4
# Using defaults
coeftest(res1, vcov. = vcovHAC)
Welcome to the ECOFMET Webpage!
Shortened course syllabi
What you see in this webpage is a shortened version of the course syllabi for Advanced Financial Econometrics (ECOFMET) available in Animospace.
Basic information
You are enrolled in either ECOFMET V24 or V25.
The prerequisite is ECOMETR.
We meet for ECOFMET
- V24 Tuedays 1430-1600 face-to-face and Fridays 1430-1600 synchronously (unless otherwise informed)
- V25 Tuedays 1615-1745 face-to-face and Fridays 1615-1745 synchronously (unless otherwise informed)
We have two websites: one at Animospace and one here. I maintain a course diary here.
Course description
Within the context of financial economic theory, this course combines interesting and rigorous elements of microeconometrics and time series econometrics. The microeconometrics component consists of estimation and testing frameworks used in understanding individual behavior. Topics include selected models for cross-section data (e.g. discrete choice modeling, selection models, basic GMM estimation and testing frameworks for optimization-based problems, linear panel data models for financial and economic outcomes, etc). The time series econometrics component, which is appropriately called macroeconometrics, includes methods that are quite prominent in cyclical analysis (e.g. autoregressive and moving average models) as well as other time series models (vector autoregression models and their structural variants).
Main textbooks
We will be using the following textbooks (abbreviated as SDAFE and SW in this syllabus):
Ruppert, D. & Matteson, D. S. (2015). Statistics and Data Analysis for Financial Engineering: With R Examples (2nd ed.). Springer.
Stock, J. H. & Watson, M. W. (2020). Introduction to Econometrics (4th ed.). Pearson.
Course outline
Below is a tentative course outline and we may have to adjust, depending on the circumstances.
Topics | Readings |
---|---|
Returns | SDAFE Chapter 2 |
Exploratory Data Analysis | SDAFE Sections 4.1 to 4.5 |
Modeling Univariate Distributions | SDAFE Chapter 5, Sections 19.1, 19.2.1, 19.2.2 |
Resampling | SDAFE Chapter 6 and Section 19.3 |
Multivariate Statistical Models | SDAFE Chapter 7 |
Time Series Models: Basics | SDAFE Chapter 12, SW Chapter 15 |
Time Series Models: Further Topics | SDAFE Chapter 13, SW Chapter 16 |
Regression: Advanced Topics | SDAFE Chapter 11, SW Chapters 9 to 14 |
Cointegration | SDAFE Chapter 15, SW Chapter 17 |
GARCH Models | SDAFE Chapter 14 and Section 19.4, SW Chapter 17 |
References
The following references have physical copies available at the DLSU Library Reserve Section, 8th Floor of the Henry Sy Hall and most are also available for digital borrowing through the Internet Archive.
Angrist, J. D. & Pischke, J. (2015). Mastering ’Metrics: The Path from Cause to Effect. Princeton University Press. Borrow from the Internet Archive.
Wooldridge, J. W. (2010). Econometric Analysis of Cross Section and Panel Data (2nd ed.). The MIT Press.
Enders, W. (2009). Applied Econometric Time Series (3rd ed.). Wiley.
Angrist, J. D. & Pischke, J. (2008). Mostly Harmless Econometrics: An Empiricist’s Companion. Princeton University Press.
Mills, T. C. & Markellos, R. N. (2008). The Econometric Modeling of Financial Time Series (3rd ed.). Cambridge University Press.
Rachev, S. T., Mittnik, S., Fabozzi, F. J., Focardi, S. M., & Jašić, T. (2007). Financial Econometrics: From Basics to Advanced Modeling Techniques. Wiley.
Tsay, R. S. (2005). Analysis of Financial Time Series (2nd ed.). Wiley. Borrow from the Internet Archive.
Alexander, C. (2001). Market Models: A Guide to Financial Data Analysis. Borrow from the Internet Archive.
Gourieroux, C. & Jasiak, J. (2001). Financial Econometrics: Problems, Models, and Methods. Princeton University Press. Borrow from Internet Archive.
Campbell, J. Y., Lo, A. W., & MacKinlay, A. C. (1997). The Econometrics of Financial Markets. Princeton University Press. Borrow from Internet Archive.
The following references are NOT physically available at the DLSU Library but they are available for digital borrowing through the Internet Archive.
Wooldridge, J. W. (2016). Introductory Econometrics: A Modern Approach (6th ed.). CENGAGE Learning. Borrow from the Internet Archive.
Fan, J. & Yao, Q. (2015). The Elements of Financial Econometrics. Beijing Science Press. Borrow from Internet Archive.
Tsay, R. S. (2010). Analysis of Financial Time Series (3rd ed.). Wiley. Borrow from the Internet Archive.
Wang, P. (2009). Financial Econometrics. Routledge. Borrow from the Internet Archive.
Enders, W. (2004). Applied Econometric Time Series (2nd ed.). Wiley. Borrow from Internet Archive.
Wooldridge, J. W. (2002). Econometric Analysis of Cross Section and Panel Data (1st ed.). The MIT Press. Borrow from the Internet Archive.
Mills, T. C. (1999). The Econometric Modeling of Financial Time Series (2nd ed.). Cambridge University Press. Borrow from the Internet Archive.
The following references are more recent (meaning they come from the past five years) and are mainly articles about the bigger picture related to the course:
Bollerslev, T. (2023). The story of GARCH: A personal odyssey. Journal of Econometrics, 234 Supplement: 96-100. https://doi-org.dlsu.idm.oclc.org/10.1016/j.jeconom.2023.01.015.
Blundell, R. & Bond, S. (2023). Initial conditions and Blundell-Bond estimators. Journal of Econometrics, 234 Supplement: 101-110. https://doi-org.dlsu.idm.oclc.org/10.1016/j.jeconom.2023.01.020.
Diebold, F. X. & Yilmaz, K. (2023). On the past, present, and future of the Diebold–Yilmaz approach to dynamic network connectedness. Journal of Econometrics, 234 Supplement: 115-120. https://doi-org.dlsu.idm.oclc.org/10.1016/j.jeconom.2023.01.021.
Scheuch, C., Voigt, S., & Weiss, P. (2023). Tidy Finance with R (1st ed.). Chapman and Hall/CRC. https://doi.org/10.1201/b23237.
Graham, J. R. (2022), Presidential Address: Corporate Finance and Reality. The Journal of Finance, 77: 1975-2049. https://doi.org/10.1111/jofi.13161.
The following are a subset of the references we may be using for the collaborative class project.
Creamer, J., & Warren, L. (2024). Unbanked and impoverished? Exploring banking and poverty interactions over time. Journal of Consumer Affairs, 1–32. https://doi.org/10.1111/joca.12576. Access ungated copy here.
Bekaert, G., Harvey, C. R., & Mondino, T. (2023). Emerging Equity Markets in a Globalized World. SSRN Working Paper. https://dx.doi.org/10.2139/ssrn.2344817.
Debuque-Gonzales, M. & Corpus, J. P. P. (2021) Understanding and Measuring Financial Inclusion in the Philippines. PIDS Discussion Paper DP 2021-37.
Karlan, D., Mullainathan, S., & Roth, B. N. (2019). Debt Traps? Market Vendors and Moneylender Debt in India and the Philippines. American Economic Review: Insights, 1 (1): 27-42. https://www.aeaweb.org/articles/pdf/doi/10.1257/aeri.20180030
Because of accreditation requirements, I am compelled to brush aside my modesty and refer to the following materials which may still be relevant for the current course:
Pua, A. A. Y. (2024). Econometrics. Visit online textbook draft.
Fritsch, M., Pua, A. A. Y., & Schnurbus, J. (2024). Teaching Advanced Topics in Econometrics using an Introductory Textbook: The Case of Dynamic Panel Data Methods. Working Paper.
Fritsch, M., Pua, A. A. Y., & Schnurbus, J. (2024) Seven Examples to Better Illustrate the Method of Moments for Business, Economics, and Finance Undergraduates. Working Paper.
Course requirements
There are group-based and individual-based requirements for the course. You may be asked to provide a self-evaluation and a group evaluation, as we go along. These evaluations are excellent ways to gain first-hand knowledge regarding your own and your classmates’ attitudes and behaviors.
Collaborative class projects (30%): This course requirement is the core research component of the course.
Individual long exams 1, 2, and 3 (each weighted 18%, 24%, 28%, respectively): These requirements are timed and scheduled in advance.
- The exams are conducted face to face.
- They are tentatively set on 2024-05-28, 2024-07-09, and 2024-07-30.
- These individual exams are expected to be comprehensive. This means that the second long exam may feature topics from the first long exam. In addition, the third long exam may feature topics from the first and second long exams.
Class policies
Be guided by the student handbook regarding conduct and attendance. Additional policies may be given in class, if necessary. The biggest hurdle of any student is the inability to read, comprehend, and follow instructions. Make sure to keep yourself informed.
Refer to full syllabus for exam policy, late homework policy, and other policies related to online sessions.
Collaborative efforts
Emerging market equity returns
Errata for SDAFE, 2nd edition
Google doc for maintaining an errata file here (require organization log-in credentials to view and comment)
Course diary
Suspension of classes, make-up materials
- Video recordings of Long Exam 01 and 02 solutions (already shared, July 12 asynchronous lecture)
- Video recordings of the ideas from SW Section 15.3 and testing return predictability (already shared, August 2 asynchronous lecture which replaced the suspended class on July 26)
- Stock and Watson 4th edition student resources
- Code for replicating calculations in SW Section 15.3
- Code to test whether PSEi monthly returns are predictable
# Load dataset
# Pay attention to format of original xls
library(readxl)
<- read_excel("~/Documents/ecometf/ecofmet-V24-V25/PSEi.xls", skip = 2)
PSEi # Get a sense of the data
head(PSEi)
tail(PSEi)
is.na(PSEi$Close),]
PSEi[<- PSEi[which(!is.na(PSEi$Close)),]
PSEi_clean # Transform to xts object for use in quantmod
<- as.Date(PSEi_clean$Date)
dates <- PSEi_clean$Close
prices library(quantmod)
<- xts(prices, dates)
ts # Remove entries which will create issues with monthly return calculations
<- ts['1990-03/2008-06']
ts.monthly # log returns
<- monthlyReturn(ts.monthly, type="log")
ret.monthly # Data risk free rates
# https://www.bsp.gov.ph/Statistics/Financial%20System%20Accounts/sdir.xls
# starts from Jan 1975
# HAVE TO ADJUST THE PATH
<- read_excel("~/Documents/ecometf/ts-data/sdir-2024-05.xls", sheet = "MONTHLY", skip = 5)
sdir_2024_05 <- sdir_2024_05[!(sdir_2024_05$...2 %in% as.character(seq(1975, 2024))), ]
sdir_2024_05 <- sdir_2024_05[3:595, c(2, seq(5, 49, by = 2), 77)]
sdir_2024_05 names(sdir_2024_05) <- c("month", "tbill91", "tbill182", "tbill364", "tbillall",
"timedep30_45", "timedep46_60", "timedep61_90", "timedep91_180", "timedep181_365", "timedep366_730", "timedep731", "timedep_l_1yr", "timedep_g_1yr", "timedep_l_2yr", "timedepall",
"sav_dep_rate", "bank_avg_lend_rate", "bank_lend_rate_high", "bank_lend_rate_low", "mnl_ref_rate_60", "mnl_ref_rate_90", "mnl_ref_rate_180", "mnl_ref_rate_all", "ibcl_rate")
<- as.data.frame(sapply(sdir_2024_05, as.numeric))
sdir_2024_05 $date <- as.yearmon(1975 + seq(0, 49*12-1+5)/12)
sdir_2024_05$month <- NULL
sdir_2024_05<- as.xts(sdir_2024_05, frequency = 12)
cleaned <- cleaned$timedep30_45
tdep.monthly # Adjust time variable to merge correctly
time(ret.monthly) <- as.yearmon(1990 +(3:222)/12)
# Merge series
<- merge.xts(ret.monthly, tdep.monthly)
merged <- dyn$lm(monthly.returns ~ lag(monthly.returns, 1), data = merged['1990-04/2008-07'])
res library(lmtest)
coeftest(res, vcov. = vcov)
library(sandwich)
coeftest(res, vcov. = vcovHC)
coeftest(res, vcov. = vcovHAC)
# Compute excess returns
$excess.ret <- merged$monthly.returns-log(1+merged$timedep30_45/(12*100))
merged# testing predictability of monthly excess returns
<- dyn$lm(excess.ret ~ lag(excess.ret, 1), data = merged['1990-04/2008-07'])
res library(lmtest)
coeftest(res, vcov. = vcov)
library(sandwich)
coeftest(res, vcov. = vcovHC)
coeftest(res, vcov. = vcovHAC)
<- dyn$lm(excess.ret ~ lag(excess.ret, 1:2), data = merged['1990-04/2008-07'])
res library(car)
linearHypothesis(res,
c("lag(excess.ret, 1:2)1", "lag(excess.ret, 1:2)2"),
rhs = 0,
test = "Chisq",
vcov. = vcov)
library(sandwich)
linearHypothesis(res,
c("lag(excess.ret, 1:2)1", "lag(excess.ret, 1:2)2"),
rhs = 0,
test = "Chisq",
vcov. = vcovHC)
linearHypothesis(res,
c("lag(excess.ret, 1:2)1", "lag(excess.ret, 1:2)2"),
rhs = 0,
test = "Chisq",
vcov. = vcovHAC)
Lecture 15, 2024-07-19, online
- What happens to the sample mean outside of the IID case?
- Motivation for considering AR(1) models
- Best prediction and forecasting
- Notes for V24, notes for V25
Lecture 14, 2024-07-16, face-to-face
- Recap of the big picture for what makes the analysis of time series very different
- Understanding the concept of stationarity (SW Key Concept 15.3) and how dependence is completely different from stationarity
- Visualizations to understand stationarity and why it may be difficult to distinguish stationary and nonstationary time series when you only have one sample
### Process 1: IID N(1,4)
<- 50
n <- ts(rnorm(n, 1, 2))
xt plot(xt, ylim=c(-5,7))
# run after the first part
par(new=TRUE)
<- ts(rnorm(n, 1, 2))
xt plot(xt, ylim=c(-5,7))
### Process 2: MA(1)
<- 50
n <- 0.5
theta <- ts(rnorm(n))
zt <- zt+theta*lag(zt,-1)
xt plot(xt, ylim=c(-3,3))
# run after the first part
par(new=TRUE)
<- ts(rnorm(n))
zt <- zt+theta*lag(zt,-1)
xt plot(xt, ylim=c(-3,3))
### Process 3: AR(1)
<- arima.sim(n = 50, list(order=c(1,0,0), ar = 0.98), innov = rnorm(50))
xt plot(xt, ylim=c(-10,10))
# run after first part
<- arima.sim(n = 50, list(order=c(1,0,0), ar = 0.98), innov = rnorm(50))
xt plot(xt, ylim=c(-10,10))
### Process 4: Random walk
<- 50
n <- ts(cumsum(rnorm(n)))
xt plot(xt, ylim=c(-10, 10))
# run after running the first part
par(new=TRUE)
<- ts(cumsum(rnorm(n)))
xt plot(xt, ylim=c(-10, 10))
Long exam 02, 2024-07-09
Lecture 13, 2024-07-05, online
- Continuing slides for time series: Recapping major ideas
- Why is the case where \(\beta_1^*=1\) and \(\beta_1^*<1\) for the AR(1) model \(Y_t=\beta_0^*+\beta_1^*Y_{t-1}+u_t\) is very different? Why does it matter for practice?
- Thinking about how to determine if returns are predictable
- Notes for V24, Notes for V25
Lecture 12, 2024-06-14, online
- How to use Chapters 2, 4, and 5 to assess market risk: Sections 19.1 to 19.2, Slides as of 2024-06-13
- Move on to effect of using time series data when running linear regressions: Slides as of 2024-06-11
Lecture 11, 2024-06-11, face-to-face
- Wrap up Chapter 5
- Code in book versus code shared in this website
- Discussed other use cases for maximum likelihood
- Introduced the method of moments
Lecture 10, 2024-06-07, online
Discussed location-scale transformations of \(t_{\nu}\) and the GED, incorporating asymmetries for symmetric distributions
Maximum likelihood as an algorithm: obtaining estimates (Section 5.9), standard errors (Section 5.10), confidence intervals (Section 5.10), choosing models (Section 5.12), testing (Section 5.11)
Rough theoretical ideas as to why we expect MLE to work: quadraticity of the log-likelihood is the crucial reason, code below demonstrates for a simple example
# Draw random numbers from N(1, 4)
<- 5
n <- 1
mu <- 4 # change this to 1/4 and 40 if you want to look at the curvature of the log-likelihood
sigma.sq <- rnorm(n, mu, sqrt(sigma.sq))
y # Set up MINUS the log-likelihood (reused code from example)
# BUT sigma.sq is known, rather than a parameter to be estimated
<- function(par)
mlnl
{sum(-dnorm(y, mean = par, sd = sqrt(sigma.sq), log = TRUE))
}# New part where I want a plot of the log-likelihood for mu
# Place a grid of values for mu, adjust length.out if you wish
<- seq(-10, 10, length.out = 1000)
mu.val # Compute the log-likelihood at every value in mu.val
# The MINUS sign is to enable me to display the log-likelihood rather than the negative of the log-likelihood
<- -sapply(mu.val, mlnl)
log.like # Create a plot: vertical axis are the log-likelihood values, horizontal axis are the values of mu
# type = "l" is to connect the dots, try removing it
# fix the vertical axis for a nicer effect
plot(mu.val, log.like, type = "l", ylim = c(-200, 0))
# Draw a vertical line at the MLE for mu
# Make sure the line is colored blue-ish and dotted
abline(v = nlm(mlnl, 0)$estimate, col = "#0072B2", lty = "dotted")
# Draw a vertical line at mu, hence v = mu
# Make sure the line is colored orange-ish and is dotted
abline(v = mu, col = "#D55E00", lty = "dotted")
# Draw a curve representing the expected log-likelihood
curve(-n/2*log(2*pi)-n/2*log(sigma.sq)-n/2-n/(2*sigma.sq)*(1-x)^2, add = TRUE, col = "#CC79A7", lty = "dashed", lwd = 3)
Application to daily returns for the PSEi
Code to fit \(t_{\nu}^{\mathrm{std}}(\mu,\sigma^2)\), GED with parameters \(\mu\), \(\sigma^2\), \(\nu\), skewed \(t_{\nu}^{\mathrm{std}}(\mu,\sigma^2)\) with additional parameter \(\xi\): report estimates, standard errors, minimized negative log-likelihoods, AIC, BIC, check fit with Q-Q plot
# Load dataset
# Pay attention to format of original xls
library(readxl)
<- read_excel("PSEi.xls", skip = 2)
PSEi # Get a sense of the data
head(PSEi)
tail(PSEi)
is.na(PSEi$Close),]
PSEi[<- PSEi[which(!is.na(PSEi$Close)),]
PSEi_clean # Transform to xts object for use in quantmod
<- as.Date(PSEi_clean$Date)
dates <- PSEi_clean$Close
prices library(quantmod)
<- xts(prices, dates)
ts <- dailyReturn(ts, type="log", leading = FALSE)
ret.daily # Adapted from Ruppert and Matteson
# Estimating parameters of t-distribution
# starting values
<- c(mean(ret.daily$daily.returns), sd(ret.daily$daily.returns), 4)
start # package for standardized t-distribution
library(fGarch)
# negative log-likelihood
<- function(theta)
loglik
{-sum(dstd(ret.daily$daily.returns, mean = theta[1], sd = theta[2], nu = theta[3], log = TRUE))
}<- optim(start, loglik, hessian = TRUE)
mle # parameter estimates
$par
mle# minimized value of negative log-likelihood
$value
mle# Inverse of Fisher information
<- solve(mle$hessian)
invFishInfo # standard errors
sqrt(diag(invFishInfo))
# AIC
<- 2*mle$value+2*length(mle$par)
aic_t # BIC
<- 2*mle$value+log(length(ret.daily$daily.returns))*length(mle$par)
bic_t c(aic_t, bic_t)
# qq plots from scratch based on Ruppert and Matteson
= length(ret.daily$daily.returns)
n = (1:n)/(n+1)
grid # as.numeric here is important, without it will produce no plot
qqplot(sort(as.numeric(ret.daily$daily.returns)),
qstd(grid,mean=mle$par[1], sd=mle$par[2], nu=mle$par[3]),
xlab="Data",ylab="t-quantiles",
main="(a) t model")
# reference line
abline(0,1,col="red",lwd=2)
# an alternative possibility using qqPlot which may be easier
::qqPlot(sort(as.numeric(ret.daily$daily.returns)), distribution = "std", mean=mle$par[1], sd=mle$par[2], nu=mle$par[3])
car# Repeat for standardized generalized error distribution
library(fGarch)
<- function(theta)
loglik
{-sum(dged(ret.daily$daily.returns, mean = theta[1], sd = theta[2], nu = theta[3], log = TRUE))
}<- c(mean(ret.daily$daily.returns), sd(ret.daily$daily.returns), 2)
start <- optim(start, loglik, hessian = TRUE)
mle $par
mle$value
mle<- solve(mle$hessian)
invFishInfo sqrt(diag(invFishInfo))
<- 2*mle$value+2*length(mle$par)
aic_ged <- 2*mle$value+log(length(ret.daily$daily.returns))*length(mle$par)
bic_ged c(aic_ged, bic_ged)
::qqPlot(sort(as.numeric(ret.daily$daily.returns)), distribution = "ged", mean=mle$par[1], sd=mle$par[2], nu=mle$par[3])
car# Repeat for skewed standardized t-distribution
library(fGarch)
<- function(theta)
loglik
{-sum(dsstd(ret.daily$daily.returns, mean = theta[1], sd = theta[2], nu = theta[3], xi = theta[4], log = TRUE))
}<- c(mean(ret.daily$daily.returns), sd(ret.daily$daily.returns), 4, 2)
start <- optim(start, loglik, hessian = TRUE)
mle $par
mle$value
mle<- solve(mle$hessian)
invFishInfo sqrt(diag(invFishInfo))
<- 2*mle$value+2*length(mle$par)
aic_st <- 2*mle$value+log(length(ret.daily$daily.returns))*length(mle$par)
bic_st c(aic_st, bic_st)
::qqPlot(sort(as.numeric(ret.daily$daily.returns)), distribution = "sstd", mean=mle$par[1], sd=mle$par[2], nu=mle$par[3], xi=mle$par[4]) car
- Code to conduct a likelihood ratio test of the null \(\xi=1\) (meaningful in our context as a way to determine whether the data provides evidence to support the hypothesis of symmetry for the distribution of daily returns)
# Estimate the model without imposing the null (unrestricted/unconstrained model)
library(fGarch)
<- function(theta)
loglik
{-sum(dsstd(ret.daily$daily.returns, mean = theta[1], sd = theta[2], nu = theta[3], xi = theta[4], log = TRUE))
}<- c(mean(ret.daily$daily.returns), sd(ret.daily$daily.returns), 4, 2)
start # Remove hessian=TRUE to save computational time
<- optim(start, loglik)
mle <- mle$value ## Negative log-likelihood at the optimum found
nllu # Estimate the model imposing the null (restricted/constrained model)
library(fGarch)
<- function(theta)
loglik
{-sum(dsstd(ret.daily$daily.returns, mean = theta[1], sd = theta[2], nu = theta[3], xi = 1, log = TRUE))
}# Change starting point to reflect that onyl 3 parameters are estimated
<- c(mean(ret.daily$daily.returns), sd(ret.daily$daily.returns), 4)
start # Remove hessian=TRUE to save computational time
<- optim(start, loglik)
mle <- mle$value ## Negative log-likelihood at the optimum found
nllc # likelihood ratio LR test statistic
<- 2*(-nllu-(-nllc))
test.statistic # significance level 0.05
# critical value from chi-squared with 1 df, because we have 1 restriction
<- qchisq(0.95, df = 1)
critical # decision
> critical # if TRUE reject null, if FALSE fail to reject null
test.statistic # report p-value
<- pchisq(test.statistic, df = 1, lower.tail = FALSE)
p.val p.val
Students should work on the examples found in Section 5.14.
Reading assignment for next meeting: Sections 19.1 to 19.2, Chapter 12
Labs and practice exercises: Lab 5.19.2 Problems 6 and 7, Lab 5.19.3, Exercises 11 to 13
Long exam 01, 2024-06-04, face-to-face
What to bring for ECOMATH exam:
- Pens and extra pens
- Non-programmable scientific calculator
- University ID with picture
Coverage:
- Chapters 2, 4 (specifically Sections 4.1 to 4.4), and 5 (specifically Sections 5.1, 5.2, 5.4.2, 5.5 (only \(t_{\nu}\), discrete mixtures), 5.6 (only what GEDs are), 5.9)
- Updated practice exercises for Chapter 5 are Exercises 2, 7, 8, 9. The style of questioning found in Labs 5.19.2 and 5.19.3 and Exercises 11 and 12 may be relevant.
- Review how to calculate expected values and variances (along with using their properties) even just for the discrete case.
- And whatever we have done in class until May 31.
Lecture 9, 2024-05-31, online
- Maximum likelihood estimation as an algorithm: MLE by hand and using R
- Why could we not use the \(t_{\nu}\) distribution right away as a model for returns data?
- Code used in class
# Generate simulated data from N(1, 4)
# 1 = popn mean
# 4 = popn variance
<- 5
n <- 1
mu <- 4
sigma.sq # Artificial data
<- rnorm(n, mu, sqrt(sigma.sq))
y # Only get to see and use y from here on
# Use fitdistr
library(MASS)
fitdistr(y, "normal")
mean sd
2.3566398 1.5829661
(0.7079240) (0.5005778)
# Use nlm()
# Set up MINUS the log-likelihood
<- function(par)
mlnl
{sum(-dnorm(y, mean = par[1], sd = sqrt(par[2]), log = TRUE))
}# nlm() is one of the numerical optimization routines available in R
# Print out things completely
# Try changing the starting points for yourself
<- nlm(mlnl, c(1,4), hessian = TRUE, print.level = 2) output
iteration = 0
Step:
[1] 0 0
Parameter:
[1] 1 4
Function Value
[1] 10.77684
Gradient:
[1] -1.69579908 -0.05410171
iteration = 1
Step:
[1] 1.69579908 0.05410171
Parameter:
[1] 2.695799 4.054102
Function Value
[1] 9.710163
Gradient:
[1] 0.4182932 0.2180144
iteration = 2
Step:
[1] -0.3229366 -0.1411842
Parameter:
[1] 2.372863 3.912918
Function Value
[1] 9.606537
Gradient:
[1] 0.02073124 0.22971721
iteration = 3
Step:
[1] -0.1067777 -0.2839884
Parameter:
[1] 2.266085 3.628929
Function Value
[1] 9.54894
Gradient:
[1] -0.1247667 0.2116594
iteration = 4
Step:
[1] -0.1628645 -1.2786754
Parameter:
[1] 2.103220 2.350254
Function Value
[1] 9.464752
Gradient:
[1] -0.53912994 -0.09945711
iteration = 5
Step:
[1] 0.1995725 0.4251879
Parameter:
[1] 2.302793 2.775442
Function Value
[1] 9.406431
Gradient:
[1] -0.09700411 0.08657624
iteration = 6
Step:
[1] 0.06161348 -0.24024040
Parameter:
[1] 2.364406 2.535201
Function Value
[1] 9.391424
Gradient:
[1] 0.01531961 0.01142023
iteration = 7
Step:
[1] -0.005896888 -0.032759406
Parameter:
[1] 2.358509 2.502442
Function Value
[1] 9.3912
Gradient:
[1] 0.003737883 -0.001334322
iteration = 8
Step:
[1] -0.001970372 0.003488168
Parameter:
[1] 2.356539 2.505930
Function Value
[1] 9.391195
Gradient:
[1] -1.987399e-04 5.944937e-05
iteration = 9
Parameter:
[1] 2.356639 2.505782
Function Value
[1] 9.391195
Gradient:
[1] 3.693459e-08 3.906057e-07
Relative gradient close to zero.
Current iterate is probably solution.
output
$minimum
[1] 9.391195
$estimate
[1] 2.356639 2.505782
$gradient
[1] 3.693459e-08 3.906057e-07
$hessian
[,1] [,2]
[1,] 1.995385e+00 -9.292058e-05
[2,] -9.292058e-05 3.979971e-01
$code
[1] 1
$iterations
[1] 9
# Use optim()
# Try changing the starting points for yourself
<- optim(c(1,4), mlnl, hessian=TRUE) output.optim
Try to modify the code above to directly estimate the population standard deviation rather than the population variance so that the results of
nlm()
,optim()
, andMASS::fitdistr()
will match.
Lecture 8, 2024-05-28, face-to-face
Move on to Chapter 5
- A lot of “named” univariate distributions out there with links to each other: Most of the distributions found in Chapter 5 are not even listed there.
- Quick introduction to likelihood functions
- Properties of MLE: Skip “Some asymptotic tools”.
- Some computational examples: Skip LM Case Study 5.2.1 and LM Example 5.2.3.
Reading assignment for Chapter 5: Focus on Sections 5.1 to 5.6, 5.9 to 5.12, 5.14 (skip Example 5.16), 5.16 (return also to Section 4.5 on boxplots)
Labs for Chapter 5: Focus on 5.19.2 (skip Problems 8 and 9), 5.19.3
Exercises for Chapter 5: 2, 4a, 4b, 5a, 5b, 5c, 7 to 13
Lecture 7, 2024-05-24, online
Wrap up Chapter 4
- Continue PSEi analysis
- Psaradakis and Vávra (2018): Monte Carlo comparison of normality tests designed for time series data
Code used in class, shown below is for monthly returns. Adjust accordingly for daily returns.
# Load dataset
# Pay attention to format of original xls
library(readxl)
<- read_excel("PSEi.xls", skip = 2)
PSEi # Get a sense of the data
head(PSEi)
tail(PSEi)
is.na(PSEi$Close),]
PSEi[<- PSEi[which(!is.na(PSEi$Close)),]
PSEi_clean # Transform to xts object for use in quantmod
<- as.Date(PSEi_clean$Date)
dates <- PSEi_clean$Close
prices library(quantmod)
<- xts(prices, dates)
ts # Remove entries which will create issues with monthly return calculations
<- ts['1990-03/2008-06']
ts.monthly <- monthlyReturn(ts.monthly, type="log")
ret.monthly # Density estimate for monthly returns
plot(density(ret.monthly$monthly.returns))
<- seq(-.4, 0.4, length=1000)
grid # Parametric fit using normal distribution
lines(grid, dnorm(grid, mean=mean(ret.monthly$monthly.returns), sd=sd(ret.monthly$monthly.returns)), col = "red", lty = 2)
# Use a robust estimator of spread
lines(grid, dnorm(grid, mean=mean(ret.monthly$monthly.returns), sd=mad(ret.monthly$monthly.returns)), col = "blue", lty = 3)
# Q-Q plot with normal as reference distribution (all default options)
# as.numeric shows up here to convert the xts object into an which qqnorm could understand
# try removing as.numeric() to see what happens
qqnorm(as.numeric(ret.monthly$monthly.returns), datax = TRUE)
qqline(as.numeric(ret.monthly$monthly.returns), datax = TRUE)
# Q-Q plot with t as reference distribution
# Require df as input
# sorting was adopted from the book (but does not seem to be necessary)
<- sort(as.numeric(ret.monthly$monthly.returns))
s_monthlyRet <- length(s_monthlyRet)
n # Grid to represent the cumulative probabilities (or how many percent full of water is the vase)
<- (1:n)/(n+1)
grid = 4 # feel free to change this, needed for next line
df # Compare with t with df=df as reference distribution
# qt() gives quantiles of the t distribution require df as input
qqplot(s_monthlyRet, qt(grid,df=df), xlab="monthly returns", ylab=paste("Quantiles of t-distribution with df=", df))
# Make own reference line
<- lm(qt(c(.25,.75),df = df) ~ quantile(s_monthlyRet,c(.25,.75)))
lmfit abline(lmfit)
# Alternative from car package with confidence bands
# But does not conform with convention in Ruppert and Matteson
# specifically for datax = TRUE
library(car)
# compare with normal as reference distribution
qqPlot(s_monthlyRet, dist="norm", ylab = "monthly returns")
# compare with t distribution, adjust df accordingly
= 4
df qqPlot(s_monthlyRet, dist="t", df=df, ylab = "monthly returns")
Previewed Sections 5.1 to 5.3, and a bit of maximum likelihood
Labs for Chapter 4: All are included but there is code for Problem 6 which require some knowledge of Chapter 5.
Exercises for Chapter 4: 1a to 1d, 2, 3, 4, 5, 6
Lecture 6, 2024-05-21, face-to-face
- Recapped connections among Chapters 2, 4, and 5
- Focused on the following population objects: cdf, population quantile
- Focused on the following sample objects: empirical cdf, sample quantiles, order statistics
- Water-filling analogy for normal probability plots (resulting patterns of plots for distributions which differ from the normal)
Lecture 5, 2024-05-17, face-to-face ONLINE
Continue working on Chapter 4
- Why bother estimating a density?
- Why a histogram would be useful visualization even in the time series case? Can we use it all the time?
- Introduce density estimation: You have already been doing it with histograms.
- Some details about density estimation: meaning of the formula, kernel function, bandwidth choice
- A preview of how to choose the bandwidth (at least when thinking about the histogram as a density estimator)
- Bart Simpson density: Check page 2 of these notes. If interested, you can look into the theoretical details of density estimation.
Some real data examples
- The Distribution of Country Equity Returns
- Daily data from Philippine Stock Exchange Index (1990-02-28 to 2008-07-24), download here
# Load dataset (make sure working directory has the file)
# Pay attention to format of original xls
# Pay attention to the packages
library(readxl)
<- read_excel("PSEi.xls", skip = 2)
PSEi # Get a sense of the data
head(PSEi)
tail(PSEi)
is.na(PSEi$Close),]
PSEi[<- PSEi[which(!is.na(PSEi$Close)),]
PSEi_clean # Transform to xts object for use in quantmod
<- as.Date(PSEi_clean$Date)
dates <- PSEi_clean$Close
closing library(quantmod)
<- xts(closing, dates)
ts # Remove entries which will create issues with monthly return calculations
<- ts['1990-03/2008-06']
ts.monthly <- monthlyReturn(ts.monthly, type="log")
ret.monthly <- dailyReturn(ts, type="log", leading = FALSE)
ret.daily # Using histogram as a density estimator
hist(ret.daily$daily.returns, freq = FALSE)
# Using density() with Gaussian kernel and automatic bandwidth
plot(density(ret.daily$daily.returns))
Reading assignment for next meeting: Chapter 4 (skip Sections 4.3.3, 4.6 to 4.8), Sections 5.1 to 5.5
Practice exercises: Codes used to generate figures in the book, Chapter 4 Lab Problems 1, 2, 9, 10
Lecture 4, 2024-05-14, face-to-face ONLINE
Work on lognormal distributions and Exercise 8
Discuss the purpose of Chapter 4. Connect to idea of stationarity in Chapter 12.
Reading assignment for next meeting: Chapter 4 (skip Sections 4.3.3, 4.6 to 4.8)
Lecture 3, 2024-05-10, online
Wrap up Chapter 2
- R Lab: Section 2.4.3 and related implementation for Exercise 1
- Exercises in class: 1, 7c, 7d
- Practice exercises: 2, 3, 4, 5, 6, 7a, 7b
- For a challenge, try Exercises 9a, 9c, 10, 11 and R Lab Section 2.4.2
- Advice: When doing the labs and exercises, ask why these were made part of the book.
Code used in class (with slight changes compared to the live demo): Thanks to Carlos Conda for pointing out what could be wrong with my initial live attempt!
# Calculating normal probabilities using R (to the left, see help for others)
pnorm(log(0.99), mean = 0.001, sd = 0.015)
# Modified code from Ruppert and Matteson
# Changed = to <-
# How to answer Exercise 1 with a computer alone
set.seed(2012) # Comment out if needed
<- 1 # Number of holding periods
n <- 1e4 # Number of replications
nsim <- numeric(nsim) # Storage
indicator for (i in 1:nsim)
{<- rnorm(n,0.001,0.015) # Simulated data on daily log returns, fixed a gross mistake here
logr <- c(1000,1000*exp(cumsum(logr))) # initial wealth, plus path of wealth
wealth <- (wealth[(n+1)] <= 990) # check final wealth whether it is below threshold
indicator[i]
}mean(indicator) # estimate of the exact probability that final wealth is below 990 dollars
Reading assignment for next meeting: Chapter 4 (skip Sections 4.3.3, 4.6 to 4.8)
Lecture 2, 2024-05-07, face-to-face ONLINE
Continue Chapter 2
- Returns in various guises
- Why bother with log returns? When are they most appropriate?
- What is the point of computing \(k\)-period log-returns? Why obtain its distribution?
- Connection to random walks
Reading assignment for next meeting: Chapter 2, Chapter 4 (skip Sections 4.6 to 4.8)
Lecture 1, 2024-05-03, online
Course administration:
- Deadline for dropping course: May 8 with 90% refund, May 15 with 50% refund, May 29 with no refund
- Deadline for withdrawal from course: May 30 to June 29 with no refund
Advice:
Broad sense of what financial econometrics looks like about 10 years ago
Why study returns? How to compute returns? Why have a model for the distribution of returns?
Reading assignment for next meeting: Chapter 2