Abstract
In this workshop we will learn the basics of simple regression models in the context of Finance. We will learn how to run a regression model for the Market Model and a regression model to estimate the Capital Asset Pricing Model (CAPM).You will work in RStudio. Create an R Notebook document to write whatever is asked in this workshop.
At the beginning of the R Notebook write Workshop 3 - Financial Econometrics II and your name (as we did in previous workshop).
You have to replicate all the steps explained in this workshop, and ALSO you have to do whatever is asked. Any QUESTION or any STEP you need to do will be written in CAPITAL LETTERS. For ANY QUESTION, you have to RESPOND IN CAPITAL LETTERS right after the question.
It is STRONGLY RECOMMENDED that you write your OWN NOTES as if this were your notebook. Your own workshop/notebook will be very helpful for your further study.
Keep saving your .Rmd file, and ONLY SUBMIT the .html version of your .Rmd file.
In a simple regression model is used to understand the linear relationship between two variables assuming that one variable, the independent variable (IV), can be used as a predictor of the other variable, the dependent variable (DV). In this part we illustrate a simple regression model with the Market Model.
The Market Model states that the expected return of a stock is given by its alpha coefficient (b0) plus its market beta coefficient (b1) multiplied times the market return. In mathematical terms:
\[ E[R_i] = α + β(R_M) \]
We can express the same equation using BO as alpha, and B1 as market beta:
\[ E[R_i] = β0 + β1(R_M) \]
We can estimate the alpha and market beta coefficient by running a simple linear regression model specifying that the market return is the independent variable and the stock return is the dependent variable. It is strongly recommended to use continuously compounded returns instead of simple returns to estimate the market regression model. The market regression model can be expressed as:
\[ r_{(i,t)} = β_0 + β_1*r_{(M,t)} + ε_t \]
Where:
\(ε_t\) is the error at time t. Thanks to the Central Limit Theorem, this error behaves like a Normal distributed random variable ∼ N(0, σε); the error term is expected to have mean=0 and a specific standard deviation (also called volatility).
\(r_{(i,t)}\) is the return of the stock i at time t.
\(r_{(M,t)}\) is the market return at time t
\(β0\) and \(β1\) are coefficients or constants
Now it’s time to use real data to better understand this model. Download monthly prices for Alfa (ALFAA.MX) and the IPCyC (^MXX) from Yahoo from January 2015 to Dec 2019. You must use ALSEA and the IPCyC to construct your own market model). You have to:
# load package quantmod
library(quantmod)
# Download the data
getSymbols(c("ALFAA.MX", "^MXX"), from="2015-01-01", to= "2019-12-31", periodicity="monthly", src="yahoo")
## [1] "ALFAA.MX" "^MXX"
# Calculate continuously returns for the stock and the market index
<- na.omit(diff(log(ALFAA.MX$ALFAA.MX.Adjusted))) #I dropped the na's
r_ALFAA # For the IPC:
<- na.omit(diff(log(MXX$MXX.Adjusted)))
r_MXX
# I merge them into the same object using the merge function:
<- merge(r_ALFAA, r_MXX)
all_rets
#I renamed the columns:
colnames(all_rets) <- c("ALFAA", "MXX")
# Take a look at your objects!
Do a scatter plot putting the IPCyC returns as the independent variable (X) and the stock return as the dependent variable (Y). We also add a line that better represents the relationship between the stock returns and the market returns.Type:
plot.default(x=all_rets$MXX,y=all_rets$ALFAA)
abline(lm(all_rets$ALFAA ~ all_rets$MXX),col='blue')
# As you see, I indicated that the Market returns goes in the X axis and
# Alfa returns in the Y axis.
# In the market model, the independent variable is the market returns, while
# the dependent variable is the stock return
Sometimes graphs can be deceiving. In this case, the range of X axis and Y axis are different, so it is better to do a graph where we can make both X and Y ranges with equal distance. We also add a line that better represents the relationship between the stock returns and the market returns. Type:
plot.default(x=all_rets$MXX,y=all_rets$ALFAA, xlim=c(-0.30,0.30) )
abline(lm(all_rets$ALFAA ~ all_rets$MXX),col='blue')
WHAT DOES THE PLOT TELL YOU? BRIEFLY EXPLAIN
Using the lm() function, run a simple regression model to see how the monthly returns of the stock are related with the market return. The first parameter of the function is the DEPENDENT VARIABLE (in this case, the stock return), and the second parameter must be the INDEPENDENT VARIABLE, also named the EXPLANATORY VARIABLE (in this case, the market return).
What you will get is called The Market Regression Model. You are trying to examine how the market returns can explain stock returns from Jan 2015 to Aug 2020.
Assign your market model to an object named “reg”.
# Run the regression with the lm function:
<- lm(r_ALFAA ~ r_MXX)
reg # The first variable is the Dependent variable (the stock return), and
# the variable after the ~ is the Independent variable or explanatory
# variable (the market return)
# I get the summary of the regression output into a variable
<- summary(reg)
sumreg# I display the main results of the regression:
sumreg
##
## Call:
## lm(formula = r_ALFAA ~ r_MXX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.097701 -0.036862 -0.004467 0.030768 0.146265
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.010262 0.006643 -1.545 0.128
## r_MXX 1.168891 0.187738 6.226 6.11e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.051 on 57 degrees of freedom
## Multiple R-squared: 0.4048, Adjusted R-squared: 0.3944
## F-statistic: 38.77 on 1 and 57 DF, p-value: 6.113e-08
We can calculate the main sums of squares of a regression model. In the Note “Basics of Linear Regression Models” you can remember what are these sums of squares.
For the sum of squares of total deviations from the mean of Y (SST), you can do the following:
Calculate a variable for the mean of the dependent variable Y (in this case, the stock return):
= mean(r_ALFAA) meanY
Calculate a variable with the squared deviations of each value of Y (stock returns) from its mean, and get the sum of these values:
# Calculate a vector for the squared deviations of each value of stock returns
# from its mean:
<- (r_ALFAA - meanY)^2
squared_deviations_1 # Now I get the sum of these squared deviations
= sum(squared_deviations_1)
SST SST
## [1] 0.2491152
For the sum of squares of the regression model (SSRM) you have to use the predicted values of the regression model, also called the fitted values of the model. These values are stored in the regression object reg we created with the lm function:
The fitted (predicted) values of the regression model are stored in the fitted.values attribute of the regression object:
= reg$fitted.values fittedY
Now you can get the SSRM with a similar process we followed to get the SST. Remember that you have to get the sum of squared deviations of each fitted value from the mean of Y.
# Calculate a vector for the squared deviations of each fitted value
# from the Y mean:
= (fittedY-meanY)^2
squared_deviations_2 # Sum these squared deviations to get SSRM
= sum(squared_deviations_2)
SSRM SSRM
## [1] 0.1008404
In a similar process, you can get the sum of squares for the errors (SSE). To get the SSE you have to get the sum of squares of the difference between the real values of Y (stock return) and the predicted values (fittedY).
You can compare if your calculations of sum of squares are correct by running the ANOVA function as follows:
anova(reg)
## Analysis of Variance Table
##
## Response: r_ALFAA
## Df Sum Sq Mean Sq F value Pr(>F)
## r_MXX 1 0.10084 0.100840 38.765 6.113e-08 ***
## Residuals 57 0.14827 0.002601
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
In the column Sum Sq you can see the SSRM and the SSE.
RESPOND TO THE FOLLOWING QUESTIONS:
1. What are the standard errors of the beta coefficients? (b0 and b1) What are they for?
2. What is the total sum of squares (SST) ? (provide the result, and explain the formula)
3. What is the sum of squared errors (SSE) ? (provide the result, and explain the formula)
4. What is the sum of squared regression differences (SSR) ? (provide the result and explain the formula)
5. What is the coefficient of determination of the regression (the R-squared)? (provide the result and explain the formula)
6. Interpret the results of the beta coefficients (b0 and b1) and their corresponding t-values and p-values with your own words.
7. Estimate an approximate 95% confidence interval for b0 and b1 and interpret them
The Capital Asset Pricing Model states that the expected return of a stock is given by the risk-free rate plus its beta coefficient multiplied by the market premium return. In mathematical terms:
\[ E[R_i] = R_f + β_1(R_M − R_f ) \]
We can express the same equation as:
\[ (E[R_i] − R_f ) = β_1(R_M − R_f ) \]
Then, we are saying that the expected value of the premium return of a stock is equal to the premium market return multiplied by its market beta coefficient. You can estimate the beta coefficient of the CAPM using a regression model and using continuously compounded returns instead of simple returns. However, you must include the intercept b0 in the regression equation:
\[ (r_i − r_f ) = β_0 + β_1(r_M − r_f ) + ε \]
Where ε ∼ N(0, \(σ_ε\)); the error is a random shock with an expected mean=0 and a specific standard deviation or volatility. This error represents the result of all factors that influence stock returns, and cannot be explained by the model (by the market).
In the market model, the dependent variable was the stock return and the independent variable was the market return. Unlike the market model, here the dependent variable is the difference between the stock return minus the risk-free rate (the stock premium return), and the independent variable is the premium return, which is equal to the market return minus the risk-free rate. Let’s run this model in r with a couple of stocks.
You can watch this VIDEO to get an idea of how to do it.
We first clean our environment and load the quantmod package:
# To clear our environment we use the remove function rm:
rm(list=ls())
# To avoid scientific notation for numbers:
options(scipen=999)
# load package quantmod
library(quantmod)
Download monthly stock data for Apple, Tesla and the S&P500 from 2014 to Dec, 2020 from Yahoo Finance using the getSymbols function and obtain continuously compounded returns for each.
getSymbols(c("AAPL", "^GSPC", "TSLA"), from="2014-01-01",
to="2020-12-01", periodicity="monthly", src="yahoo")
## [1] "AAPL" "^GSPC" "TSLA"
#I select only the adjusted prices of each stock and merge them together:
<- merge(AAPL$AAPL.Adjusted,GSPC$GSPC.Adjusted, TSLA$TSLA.Adjusted)
prices # Or I can do:
<- merge(Ad(AAPL), Ad(GSPC), Ad(TSLA))
prices
# I calculate continuously compounded returns to all columns of the
# price object:
<- na.omit(diff(log(prices$AAPL.Adjusted)))
APPL_r <- na.omit(diff(log(prices$GSPC.Adjusted)))
GSPC_r <- na.omit(diff(log(prices$TSLA.Adjusted)))
TSLA_r
# I use the na.omit() function to remove NA values (since the first month
# is not possible to calculate returns) and select only Adjusted columns.
Download the risk-free monthly rate for the US (6-month treasury bills), which is the TB6MS ticker:
getSymbols("TB3MS", src = "FRED")
## [1] "TB3MS"
This return is given in percentage and in annual rate. I divide it by 100 and 12 to get a monthly simple rate since I am using monthly rates for the stocks:
<-TB3MS/100/12 rfrate
Now I get the continuously compounded return from the simple return:
<- log(1+rfrate) rfrate
I used the formula to get cc reteurns from simple returns, which is applying the natural log of the growth factor (1+rfrate)
Unfortunately, when getSymbols brings data from the FED, it brings all historical values of the series, even though the end date is specified.
Then, I do a sub-setting of the risk-free rate dataset to keep only those months that are equal to the months I brought for the stocks:
<- rfrate["2014-02-01/2020-12-01"] rfrate
plot.default(x=GSPC_Premr, y=TSLA_Premr)
abline(lm(TSLA_Premr ~ GSPC_Premr),col='blue')
Sometimes graphs can be deceiving. In this case, the range of X axis and Y axis are different, so it is better to do a graph where we can make both X and Y ranges with equal distance. We also add a line that better represents the relationship between the stock returns and the market returns. Type:
plot.default(x=GSPC_Premr, y=TSLA_Premr, ylim=c(-0.5,0.5),xlim=c(-0.6,0.6))
abline(lm(TSLA_Premr ~ GSPC_Premr),col='blue')
WHAT DOES THE PLOT TELL YOU? BRIEFLY EXPLAIN
Use the premium returns to run the CAPM regression model for each stock.
We start with Tesla:
<-lm(TSLA_Premr ~ GSPC_Premr, na.action=na.omit)
Tesla_CAPM
# Note that I added the parameter na.action=na.omit to validate in case some
# of the return series have NA values. NA values will be omitted
# I apply the function summary to the Tesla_CAPM object to get the coefficients and the
# standard errors. I assign the result in the Tesla_s object
<-summary(Tesla_CAPM)
Tesla_s # The summary function, shows the results for the B1 and B0 coefficients, their
# residuals, t and p values.
# The first line shows the B0 coefficients
# The second, the coefficients for B1
Tesla_s
##
## Call:
## lm(formula = TSLA_Premr ~ GSPC_Premr, na.action = na.omit)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.34187 -0.08967 -0.02076 0.08358 0.42657
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.01884 0.01589 1.186 0.239
## GSPC_Premr 1.76090 0.38245 4.604 0.0000154 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1413 on 80 degrees of freedom
## Multiple R-squared: 0.2095, Adjusted R-squared: 0.1996
## F-statistic: 21.2 on 1 and 80 DF, p-value: 0.00001538
To do a rough estimate of the 95% confidence interval for B0:
<- Tesla_s$coefficients[1,1] - (2* Tesla_s$coefficients[1,2] )
minB0 <- Tesla_s$coefficients[1,1] + (2* Tesla_s$coefficients[1,2] )
maxBO
cat("The approx. B0 confidence interval goes from", minB0, "to", maxBO)
## The approx. B0 confidence interval goes from -0.01294225 to 0.05062803
To estimate the 95% confidence interval for B1:
<- Tesla_s$coefficients[2,1] - (2* Tesla_s$coefficients[2,2] )
minB1 <- Tesla_s$coefficients[2,1] + (2* Tesla_s$coefficients[2,2] )
maxB1
cat("The approx. B1 confidence interval goes from", minB1, "to", maxB1)
## The approx. B1 confidence interval goes from 0.9959955 to 2.525798
Follow the same procedure to get Apple’s CAPM and respond after you run your CAPM regression model for both stocks:
(a) INTERPRET THE RESULTS OF THE COEFFICIENTS (b0 and b1), THEIR STANDARD ERRORS, P-VALUES AND 95% CONFIDENCE INTERVALS.
(b) DO A QUICK RESEARCH ABOUT THE EFFICIENT MARKET HYPOTHESIS. BRIEFLY DESCRIBE WHAT THIS HYPOTHESIS SAYS.
(c) ACCORDING TO THE EFFICIENT MARKET HYPOTHESIS, WHAT IS THE EXPECTED VALUE OF b0 in the CAPM REGRESSION MODEL?
(d) ACCORDING TO YOUR RESULTS, IS TESLA SIGNIFICANTLY RISKIER THAN THE MARKET ? WHAT IS THE t-test YOU NEED TO DO TO RESPOND THIS QUESTION? Do the test and provide your interpretation. (Hint: Here you have to change the null hypothesis for b1: H0: b1=1; Ha=b1<>1)
Read carefully: Basics of Linear Regression Models.
Go to Canvas and respond Quiz 3 about Basics of Return and Risk. You will be able to try this quiz up to 3 times. Questions in this Quiz are related to concepts of the readings related to this Workshop. The grade of this Workshop will be the following: