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 (File -> New File -> R Notebook), where you have to write whatever is asked in this 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.
You have to keep saving your .Rmd file, and ONLY SUBMIT the .html version of your .Rmd file. Open R studio and open a new R-script. Write your Name and the Workshop name as comments at the top. Respond questions in CAPITAL LETTERS in your R-Script. Follow directions.
The simple linear 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)} = b_0 + b_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 \(ε_t\) 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
\(b_0\) and \(b_1\) are called regression coefficients
Now it’s time to use real data to better understand this model. Download monthly prices for Alfa (ALFAA.MX) and the Mexican market index IPCyC (^MXX) from Yahoo from January 2016 to Jan 2021.
We first load the quantmod package and download monthly price data for Alfa and the Mexican market index. We also merge both datasets into one:
# load package quantmod
library(quantmod)
# Download the data
getSymbols(c("ALFAA.MX", "^MXX"), from="2016-01-01", to= "2021-01-31", periodicity="monthly", src="yahoo")## [1] "ALFAA.MX" "^MXX"
We calculate continuously returns for both, Alfa and the IPCyC:
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:
# 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 returnSometimes 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=returns$MXX,y=returns$ALFAA, xlim=c(-0.30,0.30) )
abline(lm(returns$ALFAA ~ returns$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”.
reg <- lm(ALFAA ~ MXX, data=returns)
# Or by calling the return objects by itself:
reg <- lm(returns$ALFAA ~ returns$MXX)
summary(reg)##
## Call:
## lm(formula = returns$ALFAA ~ returns$MXX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.31927 -0.04455 -0.01824 0.03004 0.34653
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01541 0.01235 -1.248 0.217
## returns$MXX 1.90225 0.27196 6.995 2.99e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.09568 on 58 degrees of freedom
## Multiple R-squared: 0.4576, Adjusted R-squared: 0.4482
## F-statistic: 48.92 on 1 and 58 DF, p-value: 2.993e-09
Respond the following questions: 1. What are the standard errors of the beta coefficients? (b0 and b1) What are they for?
What is the total sum of squares (SST) ? (provide the result, and explain the formula)
What is the sum of squared errors (SSE) ? (provide the result, and explain the formula)
What is the sum of squared regression differences (SSR) ? (provide the result and explain the formula)
What is the coefficient of determination of the regression (the R-squared)? (provide the result and explain the formula)
Interpret the results of the beta coefficients (b0 and b1) and their corresponding p-values with your own words.
What are the t-values of the beta coefficients (b0 and b1) ?
Interpret the 95% confidence interval of the beta coefficients (b0 and b1)
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[Ri] = 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.
getSymbols(c("AAPL", "^GSPC", "TSLA"), from="2014-01-01",
to="2020-08-01", periodicity="monthly", src="yahoo")## [1] "AAPL" "^GSPC" "TSLA"
#I select only the adjusted prices of each stock and merge them together:
prices <- merge(AAPL$AAPL.Adjusted,GSPC$GSPC.Adjusted, TSLA$TSLA.Adjusted)
# Or I can do:
prices <- merge(Ad(AAPL), Ad(GSPC), Ad(TSLA))
# I calculate continuously compounded returns to all columns of the
# price object:
APPL_r <- na.omit(diff(log(prices$AAPL.Adjusted)))
GSPC_r <- na.omit(diff(log(prices$GSPC.Adjusted)))
TSLA_r <- na.omit(diff(log(prices$TSLA.Adjusted)))
# 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.## [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:
rfrate<-TB3MS/100/12
# Now I get the continuously compounded return from the simple return:
rfrate <- log(rfrate+1)
# Unfortunately, when getSymbols brings data from the FED, it brings all
# values of a series from the first date
# Then, I do a sub-setting of the risk-free rate zoo dataset to
# keep only those months that are equal to the months I brought for the stocks.
rfrate <- rfrate["2014-02-01/2020-08-01"]Tesla_CAPM <-lm(TSLA_Premr ~ GSPC_Premr)
# 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
Tesla_s <-summary(Tesla_CAPM)
# The summary funtion, shows the reults 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)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.30641 -0.07804 -0.01211 0.07983 0.42799
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.01623 0.01535 1.058 0.293513
## GSPC_Premr 1.34880 0.38456 3.507 0.000763 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1334 on 76 degrees of freedom
## Multiple R-squared: 0.1393, Adjusted R-squared: 0.128
## F-statistic: 12.3 on 1 and 76 DF, p-value: 0.0007635
# To do a raugh estimate of the 95% confidence interval for B0:
minB0 <- Tesla_s$coefficients[1,1] - (2* Tesla_s$coefficients[1,2] )
maxBO <- Tesla_s$coefficients[1,1] + (2* Tesla_s$coefficients[1,2] )
cat("The B0 confidence interval goes from", minB0, "to", maxBO)## The B0 confidence interval goes from -0.01445918 to 0.0469233
# To estimate the 95% confidence interval for B1:
minB1 <- Tesla_s$coefficients[2,1] - (2* Tesla_s$coefficients[2,2] )
maxB1 <- Tesla_s$coefficients[2,1] + (2* Tesla_s$coefficients[2,2] )
cat("The B1 confidence interval goes from", minB1, "to", maxB1)## The B1 confidence interval goes from 0.5796726 to 2.117923
Follow the same procedure to get Apple’s CAPM and respond after you run your CAPM regression model for both stocks:
INTERPRET THE RESULTS OF THE COEFFICIENTS (b0 and b1), THEIR STANDARD ERRORS, P-VALUES AND 95% CONFIDENCE INTERVALS. Before doing this, you have to learn or review about the CAPM model to better interpret your results. You also have to remember the basics of regression models.
DO A QUICK RESEARCH ABOUT THE EFFICIENT MARKET HYPOTHESIS. BRIEFLY DESCRIBE WHAT THIS HYPOTHESIS SAYS.
ACCORDING TO THE EFFICIENT MARKET HYPOTHESIS, WHAT IS THE EXPECTED VALUE OF b0 in the CAPM REGRESSION MODEL?
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)
Go to Canvas and do QUIZ 1. You will be able to try this quiz up to 2 times.
The grade of this Workshop will be the following:
If you submit an ORIGINAL and COMPLETE R file with your OWN RESPONSES, then the grade of your Workshop will be grade of your Quiz (the maximum of your attempts).
If you DO NOT submit file OR you submit a very, very similar version of another student’s Workshop, then your Workshop grade will be HALF of what you get in the Quiz.