1 General directions for this Workshop

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.

2 Q Simple regression model

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

2.1 Data download

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
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!

2.2 Q Visualize the relationship

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

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)

# 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.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:

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)
## 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

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[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.

5 Data collection

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.

5.2 Download risk-free data from the FED

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)

5.3 Subsetting the risk-free dataset

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"]

5.4 Estimating the premium returns

Now you have to generate new variables (columns) for the premium returns for the stocks and the S&P 500. The premium returns will be equal to the returns minus the risk-free rat:

TSLA_Premr <- TSLA_r - rfrate
APPL_Premr <- APPL_r - rfrate
GSPC_Premr <- GSPC_r - rfrate

6 Q Visualize the relationship

  1. 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')

WHAT DOES THE PLOT TELL YOU? BRIEFLY EXPLAIN

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

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)

8 Reading

Read carefully: Basics of Linear Regression Models.

9 Quiz 4 and W4 submission

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:

  • Complete (100%): If you submit an ORIGINAL and COMPLETE HTML file with all the activities, with your notes, and with your OWN RESPONSES to questions
  • Incomplete (75%): If you submit an ORIGINAL HTML file with ALL the activities but you did NOT RESPOND to the questions and/or you did not do all activities and respond to some of the questions.
  • Very Incomplete (10%-70%): If you complete from 10% to 75% of the workshop or you completed more but parts of your work is a copy-paste from other workshops.
  • Not submitted (0%) Remember that you have to submit your .html file through Canvas BEFORE NEXT CLASS.