1 General directions for this Workshop

You will work in RStudio. Create an R Notebook document to write whatever is asked in this workshop.

You have to solve CHALLENGE exercises.

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 Challenge I - Writing a function for the CAPM

In the previous workshop we learned how to write a function for the market regression model. Now you have to write a function that estimates the CAPM for a stock, and it has to return:

  • beta coefficients (beta0 and beta1),
  • standard errors of beta coefficients,
  • minimim value of the 95% confidence interval for beta0 and beta1
  • maximum value of the 95% confidence interval for beta0 and beta1
  • the # of valid (non-missing) observations used to run the regression.

I will first do a review of the CAPM model. I also recommend you to read Basics of Portfolio theory - Part III, where I explain how the CAPM was developed.

A CAPM model can be estimated running a simple regression model using the market premium as the independent variable, and the individual asset premium as the dependent variable:

\[[r_{i(t)}-r_{f}]=\hat{\alpha}+\hat{\beta}[r_{M(t)}-r_{f}]+\varepsilon_{t}\] According to the CAPM and the efficient market hypothesis, the expected value of the constant in this regression model should be zero. Why? because if we find an individual asset that has a significant positive alpha coefficient when running the CAPM regression, then this individual asset is expected to over perform the market. According to the efficient market hypothesis, the most efficient or more optimal portfolio in a financial market is the market portfolio. If there is an individual asset with a positive and significant alpha, it is expected that sooner or later (sooner more than later), its alpha will tend to zero. However, in reality, the efficient market hypothesis does not always hold.

Let’s write an R function to estimate a CAPM regression model given a stock return, a market return, and a risk-free rate vectors. The function has to return a vector with several regression results.

Here are the specifications of the function:

  1. Your function has to receive 3 parameters: 1) the continuously compounded (cc) return of a stock, 2) the risk-free rate in continuously compounded, and 2) the cc returns of the market.

  2. Using the lm function, run a simple regression model according to the market model. Remember that the dependent variable must the stock premium return, and the independent variable must be the market premium returns.

  3. Your function must return a vector with 9 values. The structure of this vector is illustrated in the following example:

b0 se(b0) min(b0) max(b0) b1 se(b1) min(b1) max(b1) # Non-missing
0.01 0.02 -0.03 0.05 1.3 0.10 1.1 1.5 35

The non-missing observations can be calculated using the attriute df.residual of the regression model. You just have to add 2 to this value since the degrees of freedom of the regression residual is N-2, where N is the number of non-missing observations.

Test your function with at least 2 Mexican stocks (select any stocks you want), and use the correct market index. Display the vector results. For 1 stock respond to the following questions: * Is the stock offering returns systematically over the market? Explain * Is the stock significantly riskier than the market? Explain

You have to bring data from Yahoo Finace for the stock and the Mexican market, and bring the CETES data form the FRED database (ticker for CETES: INTGSTMXM193N)

3 Challenge II - Running the CAPM for several stocks with a loop using your function

Using the Excel file InputTickers.xlsx posted in the course site, you have to bring monthly stock prices, and a monthly series for the market index. Bring data from Jan 2018 to Dec 2021. In one Sheet you can find the stock tickers, and in the other Sheet you find the market ticker.

Write the corresponding code to estimate the CAPM models of all stocks. The specifications of the program are the following:

  1. Write a loop to run the a market regression model for each stock.

  2. You must use the function you defined in the previous part to run the CAPM models.

  3. Make sure your program runs with any number of tickers specified in the Excel file and it is validated in case the ticker does not exist in Yahoo. I recommend you to start testing your program with few tickers (2 or 3), so create another excel file with only 2 or 3 stocks.

  4. Your program must create a Matrix with the results of the models for all stocks. The structure of this matrix is the following:

MATRIX RESULTS:

STOCK b0 se(b0) min(b0) max(b0) b1 se(b1) min(b1) max(b1) # Non-missing
ALFA 0.01 0.02 -0.03 0.05 1.3 0.10 1.1 1.5 35
…. ..

Use the stock names as the rownames of the matrix.

Then, each row will be the CAPM results for each stock, and each column will have specific values of the betas, std errors, 95%C.I. and the # of non-missing values used in the regression. Note that it is possible that some stocks might not have prices/returns for some periods.

4 Challenge III - Selection of stocks based on market regressions

  1. Write a code to select 5 stocks according to the following criteria:
  • Select 10 stocks those with the highest minimum value of the 95% C.I. of \(b_{0}\)

  • Out of these 10 stocks, select 5 stocks that have the least market risk

5 Challenge IV - Optimize your portfolio

With these 5 stocks, and using the xts-zoo objects of these stocks (data from 2018 to 2021), calculate the optimal portfolio considering an annual risk-free rate of 2% annual (convert this to monthly since your dataset is monthly).

Bring price data of these 5 tickers, but now from Jan 2022 to date. Using the optimal weights and these months, calculate the holding-period return of this portfolio. Report your results.

Bring data of the market index from Jan 2021 to date. Calculate the holding-period return of the market. Which HPR was better, the one from the market or the HPR of your optimal portfolio?

6 Respond the Quiz about Regression models

Go to Canvas and respond the Quiz about Regression models.

Before you do the Quiz for this Workshop, be ready to respond any of the following questions (you do not have to respond them for this workshop):

  1. What are the coefficients \(b_{0}\) and \(b_{1}\) in the model?

  2. What are the standard errors of the coefficients? (\(b_{0}\) and \(b_{1}\)) What are they? What are they for?

  3. How you interpret the results of the coefficients (\(b_{0}\) and \(b_{1}\)) and p-values? 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.

  4. What is the efficient market hypothesis?

  5. According to the CAPM model and the efficient market hypothesis , what is the expected value of \(b_{0}\) in the CAPM regression model?You can watch my Youtube video [https://www.youtube.com/watch?v=B2SmeZ6AS_E&feature=youtu.be||HERE] to help you out with this part

To better understand this workshop, re-read/review the last part of my Note: Portfolio Theory - Part III where I introduce the CAPM model (you can also review Chapter 8 about the Capital Asset Pricing Model of the book: Finance: Capital Markets, Financial Management, and Investment Management , Frank J. Fabozzi Series). Also, you have to read my Note “Basics of Regression Analysis”.