WORKSHOP 4
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[Ri]=α+β(RM)
We can express the same equation using BO as alpha, and B1 as market beta:
E[Ri]=β0+β1(RM)
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)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
# Download the data
getSymbols(c("ALFAA.MX", "^MXX"), from="2015-01-01", to= "2019-12-31", periodicity="monthly", src="yahoo")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "ALFAA.MX" "^MXX"
# Calculate continuously returns for the stock and the market index
r_ALFAA <- na.omit(diff(log(ALFAA.MX$ALFAA.MX.Adjusted))) #I dropped the na's
# For the IPC:
r_MXX <- na.omit(diff(log(MXX$MXX.Adjusted)))
# I merge them into the same object using the merge function:
all_rets <- merge(r_ALFAA, r_MXX)
#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')
#WE CAN SEE A POSITIVE RELATIONSHIP, THE SCALE OF X AND Y ARE DIFFERENT, YOU COULD CHANGE IT.
# 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')
# QUESTION ONE# WHAT DOES THE PLOT TELL YOU? BRIEFLY EXPLAIN #WE CHANGED THE JUMPS OF THE X AND Y SCALE, WE CAN STILL APRECIATE THE POSITIVE RELATIONSHIP BETWEEN THE MARKET AND THE STOCK, SINCE THE POINTS HAVE AN INCREASING TENDENCY. THE SLOPE OF THE LINE IS = TO 1, THE ANGLE FORMED IS ABOUT 45ª. WHEN THE MARKET MOVES THE STOCK MOVES.
2.3 Q Running the market regression model 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:
reg <- lm(r_ALFAA ~ r_MXX)
# 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)
#
#IN THIS CASE THE MARKET RETURN IS EXPLANING THE STOCK RETURN
#THE DEPENDENT VARIABLE IS THE STOCK RETURN
# I get the summary of the regression output into a variable
sumreg<- summary(reg)
# 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):
meanY = mean(r_ALFAA)
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:
squared_deviations_1 <- (r_ALFAA - meanY)^2
# Now I get the sum of these squared deviations
SST = sum(squared_deviations_1)
SST
## [1] 0.2491151
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:
fittedY = reg$fitted.values
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:
squared_deviations_2 = (fittedY-meanY)^2
# Sum these squared deviations to get SSRM
SSRM = sum(squared_deviations_2)
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)
In the column Sum Sq you can see the SSRM and the SSE.
THE EQUATION CAN BE EXPRESSED AS: E(r_alfa)= b0 + b1 = r_MXX
What are the standard errors of the beta coefficients? (b0 and b1) What are they for? #THE BETA COEFFICIENT CAN BE FOUND IN THE “ESTIMATE” COLUM OF THE “COEFFICIENTS” WE CAN SEE THE STD. ERROR, T-VALUE AND PVALUE OF EACH BETA COEFFICIENT. #B0 IS THE Y INTERSECTION POINT OF THE LINE,IT IS THE EXPECTED VALUE OF Y WHEN THE MARKET RETURN IS ZERO, IT IS A MEASURE OF SYSTEMATIC RETURNS OVER THE MARKET, IN THIS CASE IT IS 1%, B0=0.01. STD ERROR (B0)= 0.00664. # B1 IS THE SLOPE. B1 IS THE PROPORTION IN WHICH A RETURN CHANGES AS THE MAKET CHANGES. IN THIS CASE THE B1 IS A MEASURE FOR THE MARKET RISK OF THE STOCK, B1=1.17 MEANING THAT THE STOCK IS EAQUALY SENSITIVE TO THE MARKET. STD ERROR (B1)=0.18774 # THE STANDARD ERROR OF A VARIABLE IS THE STANDARD DEVIATION OF THE VARIABLE OF STUDY, IN THIS CASE B0 AND B1. #THE STANDARD ERRORS OF THIS VARIABLES ARE THE VARIABILITY, THEY ALWAYS BEHAVE SIMILAR TO A NORMAL DISTRIBUTED VARIABLE, LIKE A t-STUDENT DISTRIBUTION.
What is the total sum of squares (SST) ? (provide the result, and explain the formula) #THE TOTAL SUM OF SQUARES (SST) IS THE DISTANCE BETWEEN EACH DATA POINT AND THE MEAN SQUARED AND THEN THE SUM OF THOSE RESULTS.
What is the sum of squared regression differences (SSR) ? (provide the result and explain the formula)
sumsquares <- anova(reg)
sumsquares
What is the sum of squared errors (SSE) ? (provide the result, and explain the formula) #E(RA)=B0+B1(RM), IT IS A LINEAR REGRESSION. Ei=RAi-E(RAi), ALL THE POINTS IN THE GRAPH HAVE AN ERROR, THE SUM OF THE ERRORS IS ZERO WHEN THE LINE IS OPTIMAL.
What is the coefficient of determination of the regression (the R-squared)? (provide the result and explain the formula) #IT IS THE CHANGE IN THE RETURNS OF THE STOCK, AS THE MARKET CHANCES.
Interpret the results of the beta coefficients (b0 and b1) and their corresponding t-values and p-values with your own words. # WE HAVE A POSITIVE RELATIONSHIP BETWEEN THE STOCK RETURNS AND TEH MARKET.
Estimate an approximate 95% confidence interval for b0 and b1 and interpret them #BETA 0= -0.01026 : STD EROOR (B0)= 0.00664 #MINIMUM VALUE OF 95% C.I. (B0) = -0.01026 - 20.00664 #MAX VALUE OF 95% C.I. (B0) = -0.01026 + 20.00664 #THE 95% C.I. = (-0.02535 . . 0.003 )
3 Q Estimating the CAPM model for a stock # 4 The CAPM model 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]=Rf+β1(RM−Rf)
We can express the same equation as:
(E[Ri]−Rf)=β1(RM−Rf)
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:
(ri−rf)=β0+β1(rM−rf)+ε
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.
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)
#5.1 Download stock data 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:
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.
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:
rfrate<-TB3MS/100/12
Now I get the continuously compounded return from the simple return:
rfrate <- log(1+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 <- rfrate["2014-02-01/2020-12-01"]
Do a scatter plot putting the S&P500 premium returns as the independent variable (X) and Tesla premium return as the dependent variable (Y). We also add a line that better represents the relationship between the stock returns and the market returns:
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')
# QUESTION## WHAT DOES THE PLOT TELL YOU? BRIEFLY EXPLAIN #IT HAS A POSITIVE RETURN, LINEAR AND ALL OF THE RETURNS ARE FOCUSED FROM -0.2 TO 0.2 APROX. 7 Q Estimating the CAPM model for a stock Use the premium returns to run the CAPM regression model for each stock.
We start with Tesla:
Tesla_CAPM <-lm(TSLA_Premr ~ GSPC_Premr, na.action=na.omit)
# 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 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:
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 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:
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 approx. B1 confidence interval goes from", minB1, "to", maxB1)
## The approx. B1 confidence interval goes from 0.9959955 to 2.525798
#(a) INTERPRET THE RESULTS OF THE COEFFICIENTS (b0 and b1), THEIR STANDARD ERRORS, P-VALUES AND 95% CONFIDENCE INTERVALS.
DO A QUICK RESEARCH ABOUT THE EFFICIENT MARKET HYPOTHESIS. BRIEFLY DESCRIBE WHAT THIS HYPOTHESIS SAYS. # THIS HYPOTHES STATES THAT SHARE PRICES REFLECT ALL INFORMATION AND CONSISTENT ALPHA GENERATION IS IMPOSSIBLE. STOCKS TRADE AT THEUR FAIR MARKET VALUR ON EXCHANGES, INVESTORS BENEFIT FROM INVESTING IN A LOW COST PORTFOLIO.
ACCORDING TO THE EFFICIENT MARKET HYPOTHESIS, WHAT IS THE EXPECTED VALUE OF b0 in the CAPM REGRESSION MODEL? # B0 SHOULD BE ZERO.