Workshop 2, Investment Funds

Author

Alberto Dorantes, Ph.D.

Published

April 2, 2025

Abstract
In this workshop we learn the foundations of the Single-Index model, the Capital Asset Pricing Model (CAPM) and multi-factor models. In addition, we further explore how these models can help us define a market-based variance-covariance matrix for portfolio optimization.

1 Directions

Continue with the same Google Colab Notebook you used for Workshop 1.

You have to replicate this Workshop in Python and your results should be very similar to the results calculated here in R. You can use Gemini in Colab for code generation.

2 The single-index model

IN THIS SECTION, YOU DO NOT HAVE TO REPLICATE THE CODE INTO PYTHON; JUST READ AND UNDERSTAND THE THEORY USING THE EXAMPLES.

The Single index model is a factor model to better understand how an asset return is linearly related with an index return over time. This index is usually the market index, so we can learn how much an asset return is following or is related with the return of the market.

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 single index model as a simple regression model.

The single-index 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 B0 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.

2.1 Running a single-index model with real data

2.1.1 Data collection

We first load the quantmod package and download monthly price data for Tesla and the S&P500 market index. We also merge both datasets into one:

# load package quantmod
library(quantmod)

# Download the data
getSymbols(c("TSLA", "^GSPC"), from="2020-01-01", to= "2025-3-31", periodicity="monthly")
[1] "TSLA" "GSPC"
#Merge both xts-zoo objects into one dataset, but selecting only adjusted prices:

adjprices<-Ad(merge(TSLA,GSPC))

2.1.2 Return calculation

We calculate continuously returns for both, Tesla and the S&P500:

returns <- diff(log(adjprices)) 
#I dropped the na's:
returns <- na.omit(returns)

#I renamed the columns:
colnames(returns) <- c("TSLA", "GSPC")

2.1.3 Visualize the relationship

Do a scatter plot putting the S&P500 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=returns$GSPC,y=returns$TSLA)
abline(lm(returns$TSLA ~ returns$GSPC),col='blue')

# As you see, I indicated that the Market returns goes in the X axis and 
#   Tesla returns in the Y axis. 
# In the single-index model, the independent variable is the market returns, while
#   the dependent variable is the stock return

Sometimes graphs can be deceiving. Always check the Y sale and the X scale. In this case, the X goes from -0.10 to 0.10, while the Y scale goes from -0.20 to 0.40. Then, the real slope of the line should be steeper.

We can change the X scale so that both Y and X axis have similar ranges:

# I indicate that X axis goes from -0.7 to 0.7
plot.default(x=returns$GSPC,y=returns$TSLA, xlim=c(-0.7,0.7))
abline(lm(returns$TSLA ~ returns$GSPC),col='blue')

Now we see that the the market and stock returns have a similar scale. With this we can better appreciate their linear relationship.

WHAT DOES THE PLOT TELL YOU? BRIEFLY EXPLAIN

2.2 Running the single-index regression model

We can run the market regression model with the lm() function. 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 single-index regression model. You are trying to examine how the market returns can explain stock returns from Jan 2017 to Dec 2020.

Assign your single-index model to an object named “model1”:

model1 <- lm(TSLA ~ GSPC, data=returns)
summary(model1)

Call:
lm(formula = TSLA ~ GSPC, data = returns)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.33712 -0.07472 -0.00607  0.13124  0.39979 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.009675   0.021098   0.459    0.648    
GSPC        2.144888   0.398866   5.377 1.31e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1637 on 60 degrees of freedom
Multiple R-squared:  0.3252,    Adjusted R-squared:  0.314 
F-statistic: 28.92 on 1 and 60 DF,  p-value: 1.307e-06

3 CHALLENGE 1

YOU HAVE TO INTERPRET THIS MODEL. MAKE SURE YOU RESPOND THE FOLLOWING THE QUESTIONS:

- IS THE STOCK SIGNIFICANTLY OFFERING RETURNS OVER THE MARKET?

- IS THE STOCK SIGNIFICANTLY RISKIER THAN THE MARKET?

We can save the regression summary in another variable and display the regression coefficients:

s <-summary(model1)
s$coefficients
               Estimate Std. Error   t value     Pr(>|t|)
(Intercept) 0.009674799 0.02109814 0.4585616 6.482057e-01
GSPC        2.144887777 0.39886605 5.3774638 1.307475e-06

The beta0 coefficient is in the first row (the intercept), while the beta1 coefficient is in the second raw.

The regression coefficients will be in the first column of this matrix. The second column is the standard error of the coefficients, which are the standard deviation of the coefficients. The third column is the t-value of each coefficient, and finally their p-values in the fourth column.

We can get beta0, beta1, standard errors, t-values and p-values and store in other variables:

b0= s$coefficients[1,1]
b1= s$coefficients[2,1]

stderr_b0 = s$coefficients[1,2]
stderr_b1 = s$coefficients[2,2]

tval_b0 = s$coefficients[1,3]
tval_b1 = s$coefficients[2,3]

pvalue_b0 = s$coefficients[1,4]
pvalue_b1 = s$coefficients[2,4]

The reference [2,1] of the matrix s$coefficients means that I want to get the element of the matrix that is in the row #2 and the column #1. In this position we can find the beta1 of the model.

Then, any R matrix you can make reference as matrix[#row,#column] to get the value of any cell according to its position in row and column numbers.

We can also get the coefficients applying the function coef to the regression object:

coefs <- coef(model1)
coefs
(Intercept)        GSPC 
0.009674799 2.144887777 
b0 = coefs[1]
b1 = coefs[2]

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.

In this model there is NO b_0 coefficient since the CAPM assumes that b_0 should be zero since there should not be a stock that systematically offeres excess returns compared to the market returns. However, when we estimate the CAPM with historical data, it is very recommended to estimate the b_0 coefficient.

Then, the CAPM model used to estimate its beta coefficients is expressed as:

StockPremiumReturn_{i,t} = b_0 + b_1(MarketPremiumReturn_t) + ε_t

Where:

StockPremiumReturn_{i,t}=(E[R_i] − R_f)

MarketPremiumReturn_{i,t}=(R_M − R_f)

ε_t ∼ 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).

As in the Single-Index model, you can estimate the b_1 coefficient of the CAPM as:

b_1=\frac{cov(MarketPremiumReturn,StockPremiumReturn_i)}{var(MarketPremiumReturn)}

And the b_0 coefficient can be calculated as:

b_0 = \overline{StockPremiumReturns_i} - b_1*\overline{MarketPremiumReturns}

Remember that the bar over the variable means the arithmetic mean of the variable.

In the CAPM we use premium returns instead of returns for the calculation of the beta coefficients.

The interpretation of b_1 in the CAPM is the same than in the Single-Index Model, but we have to refer to premium returns instead of returns. Then, b_1 is a measure of market risk of the stock, while b_0 is a measure of excess return of the stock over the market.

If b_1=2.0, the means that the risk of the stock is the doble compared to the market. In other words, for each +1% of change in the market premium return, it is expected that the stock premium return will move in aboutg +2%.

The CAPM was developed based on Portfolio Theory. More specifically, CAPM was developed after the concept of Capital Market Line (CML). The CML was derived in the 1960’s from Markowitz Portfolio Theory (developed 1953). The main contributors to the idea of the CML and the development of the CAPM are Jack Treynor, William F. Sharpe, John Litner and Jan Mossin.

Remember that the CML is the set of efficient portfolios when the risk-free asset is added to the optimal portfolio of risky assets.

It is assumed that, if all participants of a financial market are risk-averse, and if they estimate the risk and correlations of assets returns in the same way, then the most optimal portfolio of risky assets that offer the highest premium return for each unit of risk is the Market Portfolio. In other words, the Market Portfolio is expected to have the highest possible Sharpe ratio. Remember that Sharpe ratio is a measure of how much expected return a portfolio offers for each unit of risk (1 unit of standard deviation). Then, the Sharpe ratio is estimated as:

SharpRatio = \frac{(E[PortRet]-RiskFreeRate)}{SD(PortfolioReturn)}

When we add the risk-free rate to the tangency portfolio, then the set of efficient portfolios will NOT lie in the curved efficient frontier; the efficient portfolios will lie in the Capital Market Line.

In other words, the CML becomes the efficient frontier after adding the risk-free instrument to the tangent portfolio of risky assets.

The CML equation is:

E[PortRet]=RiskFreeRate+\left[\frac{(E[MarketReturn]-RiskFreeRate)}{SD(MarketReturn)}\right]PortfolioRisk

The ratio in the brackets is the Sharpe Ratio, which is the slope of the Capital Market Line when plotting PortfilioRisk in the X axis and Expected Portfolio Return in the Y axis.

Imagine we want to do a portfolio with 5 stocks. Then, we can estimate the expected return of each stock, and then estimate the efficient frontier and the optimal portfolio using Portfolio Theory.

[1] "TSLA" "AMZN" "AAPL" "JPM"  "WMT" 
Call:
globalMin.portfolio(er = ER, cov.mat = COV, shorts = FALSE)

Portfolio expected return:     0.01415781 
Portfolio standard deviation:  0.05107982 
Portfolio weights:
  TSLA   AMZN   AAPL    JPM    WMT 
0.0000 0.0639 0.0827 0.2410 0.6125 
Call:
tangency.portfolio(er = ER, cov.mat = COV, risk.free = rfree, 
    shorts = FALSE)

Portfolio expected return:     0.01533471 
Portfolio standard deviation:  0.05304073 
Portfolio weights:
  TSLA   AMZN   AAPL    JPM    WMT 
0.0080 0.0000 0.2647 0.1341 0.5932 

Assuming a risk-free rate = 3%, the efficient frontier, the tangency portfolio and the Capital Market Line for this portfolio would be:

The green line is the Capital Market Line (CML) for this example. We can see that the CML is more efficient than the efficient frontier (the blue curved line). There are some risk levels around 5% and 6% of portfolio risk where the portfolios that lie in the CML have higher expected returns compared to the blue efficient portfolios!

How can we interpret the CML? The different portfolios that lie in the CML are constructed with 2 assets.

  • The risk-free asset

  • The tangency portfolio

If we invest 100% if the risk-free asset and 0% in the tangency portfolio, then we are in the initial point of the CML (at the left); if we invest 0% in the risk-free and 100% in the tangency portfolio, then we are in the tangency point of the CML. Then, if we invest 50% in the risk-free and 50% in the tangency portfolio, we will be in the middle point of the CML between the risk-free portfolio and the tangency portfolio.

The CML portfolios that lie to the right of the tangency portfolio are portfolios that short the risk-free rate and then invest more money in the tangency portfolio (long the tangency and short the risk-free asset). This cannot be done in the real world since we cannot get a loan with a rate equal to the the risk-free rate!

Then, any point in the CML is a combination of weights between the tangency portfolio and the risk-free asset.

If we could create a portfolio considering ALL stocks in the US market and running the same optimization process we did, it is expected that the tangency portfolio will be the Market Portfolio with weights equal to the proportion of market capitalization of each stock compared to the total capitalization of the market. This will be true if we do the following assumptions:

  • All investors are risk-averse, but they are mean-variance optimizers. In other words, they are 100% rational (they prefer maximize return for each risk level)

  • All investors calculate in the same way the expected return of each asset, expected risk of each asset, and correlations of all pairs of assets (the covariance matrix)

Then, with these assumptions, if we keep adding stocks to our portfolio of 5 assets until we add ALL of them, then it is expected that the Tangency (optimal) portfolio will be the Market Portfolio.

With this idea of adding stocks to a portfolio, the idea of the CAPM was developed.

Once we add many stocks and estimate the tangency portfolio we will be close to the Market Portfolio.

In the next section, I describe the details of how the CAPM was developed from Portfolio Theory.

4.1 Derivation of the CAPM

Here I explain a mathematical derivation of the CAPM (see Sharpe 1964 for more details).

I start developing the equation for the Capital Market Line. From Portfolio Theory, we know that the market portfolio is the tangency portfolio and is the most optimal portfolio (with the assumptions I mentioned earlier).

The first idea to come up to the CML is that we can construct a portfolio of 2 assets: the risk-free asset and the market portfolio. Then we can describe the return of this portfolio R as follows:

P=wR_{f}+\left(1-w\right)R_{m}

Following Portfolio Theory, we can estimate the variance and risk of this portfolio R as:

VAR[P]=w^{2}VAR(R_{f})+(1-w)^{2}VAR(R_{m})+2w(1-w)COV(R_{f},R_{m})

Since the R_f is the risk-free rate asset, then by definition it has no variability (it is constant) so its variance and its covariance with the market return must be zero. Then, the variance of the portfolio P can be written as:

VAR(P)=(1-w)^2VAR(R_m)

Taking the squared root of both sides to get the portfolio risk:

PortfolioRisk=SD(P)=(1-w)SD(R_m)

Then the risk of the portfolio will be reduced as the % allocated to the risk-free asset (w) increases; if w=1 then the risk becomes zero since 100% is invested in the risk-free asset. If w=0, then the risk becomes the risk of the market.

To simplify the notation, I will use the Greek letter for standard deviation ($\sigma$) and variance $\sigma^{2}$:

\sigma_{p}=(1-w)\sigma_{_{m}}

Then the return and risk of the portfolio P depends on w. Then I can estimate how much the return and risk changes for a change in w, and then estimate how much the portfolio return changes when the portfolio risk changes. To do this, I can take the partial derivative of the portfolio return and risk equations with respect to w, and then divide both derivatives to get the rate of change in return for a change in risk:

\frac{\delta\sigma_{p}}{\delta w}=-\sigma_{m}

\frac{\delta P}{\delta w}=R_{f}-R_{m}

Now dividing both derivatives I get the marginal change of portfolio return for any change in portfolio risk:

\frac{\delta R}{\delta\sigma_{p}}=\frac{(R_{m}-R_{f})}{\sigma_{m}}

We arrive to the slope of the Capital Market Line, which is also called the Sharpe Ratio. The numerator is the market premium return and the denominator is the risk of the market. Then the Sharpe Ratio measures how much the market premium return changes for each unit of change in the market risk.

Since the CML states a linear relation between the portfolio return (Y axis) and portfolio risk (X axis), we can get the CML equation considering that the point where the line crosses the Y axis (the portfolio return) is the risk-free return:

CML=E[P]=R_f+\frac{(R_{m}-R_{f})}{\sigma_{m}}\sigma_p

Now, with a similar logic, we can think in a portfolio composed of 2 assets:

R_i: A risky asset i

R_m: The market portfolio

Then, we assign w% to the asset i and (1-w)% to the market return. Then, this new portfolio can be written as:

P=wR_{i}+\left(1-w\right)R_{m}

Following Portfolio Theory, we can estimate the variance and risk of this portfolio R as:

VAR[P]=w^{2}VAR(R_{i})+(1-w)^{2}VAR(R_{m})+2w(1-w)COV(R_{i},R_{m})

Using the notation of sigma squared for the variances, I can re-write this formula as:

\sigma^{2}_{p}=w^2\sigma^{2}_{i}+(1-w)^{2}\sigma^2_{m}+2w(1-w)\sigma_{im}

Where \sigma_{im} is the covariance between the stock return and the market return.

Then the portfolio risk is the squared root of its variance:

\sigma_{p}=\left[w^2\sigma^{2}_{i}+(1-w)^{2}\sigma^2_{m}+2w(1-w)\sigma_{im}\right]^{\frac{1}{2}}

Following the same rational than in the case of the portfolio of the risk-free and the market assets, I take the partial derivatives of return and risk with respect to the weight w , and then divide these 2 derivatives to get the marginal change of return for any change in portfolio risk:

\frac{\delta P}{\delta w}=R_i-R_m

Now I do the same for portfolio risk:

\frac{\delta\sigma_{p}}{\delta w}=\frac{1}{2}\left[\frac{2w\sigma_{i}^{2}+2(1-w)(-1)\sigma_{m}^{2}+\sigma_{im}(2)-4w\sigma_{im}}{\sigma_{p}}\right]

Simplifying:

\frac{\delta\sigma_{p}}{\delta w}=\frac{w\sigma_{i}^{2}+(w-1)\sigma_{m}^{2}+\sigma_{im}-2w\sigma_{im}}{\sigma_{p}}

When w=0 I am considering only the Market Portfolio, so the portfolio p becomes the market portfolio. Then, I evaluate this derivative when w=0 to see the rate of change of portfolio return with respect to weight:

\frac{\delta\sigma_{p}}{\delta w}\mid_{w=0}=\frac{\sigma_{im}-\sigma_{m}^{2}}{\sigma_{m}}

Now dividing both derivatives I get the instantaneous rate of change of return with respect to portfolio risk at the Market portfolio:

\frac{\delta P}{\delta\sigma_{p}}\mid_{w=0}=\frac{(R_i-R_m)\sigma_{m}}{\sigma_{im}-\sigma_{m}^{2}}

The rate of change of return with respect of portfolio risk must be equal to the Sharpe Ratio since this portfolio is the market return (w=0). Then, I make this equation equal to the Sharpe Ratio and do some algebra to get the return of the stock in terms of the return of the market:

SharpeRatio=\frac{(R_{m}-R_{f})}{\sigma_{m}}

Then:

\frac{(R_{m}-R_{f})}{\sigma_{m}}=\frac{(R_i-R_m)\sigma_{m}}{\sigma_{im}-\sigma_{m}^{2}}

Moving terms to leave R_i alone we get:

R_{i}=R_{f}+\frac{\sigma_{im}}{\sigma_{m}^{2}}(R_{m}-R_{f})

The ratio of covariance divided by the variance of the market return is actually the beta coefficient of the stock, which is a measure of market risk of the stock.

We arrive to the Capital Asset Pricing Formula!

Then, when a market there is equilibrium (supply equals demand), the CAPM equation should hold. The CAPM states that the expected return of a risky assets is equal to the risk-free rate plus the market premium return multiplied (scaled) by stock beta. The stock beta measures the market risk of a stock, which is how much on average the premium return of the stock changes for each change in the market premium return.

4.2 Estimation of the CAPM beta

We can estimate the beta of each stock using a simple linear regression with historical cc returns.

Moving the risk-free rate to the left of the CAPM equation, we can express the CAPM as:

(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 single-index 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.

According to the market efficiency hypothesis, it is expected that the β_0 coefficient is zero, since it is assumed that there is no asset or financial instrument that systematically outperforms the market. The market efficient hypothesis states that all information of a stock that is released to the market is assimilated immediately by all market participants, which will react accordingly and will make the stock to price correctly without systematically beating the market.

Although the theory states that β_0 must be zero, we have to include it in the model, and, if the theory is true, then we expect that the p-vaue of beta0 to be non-significant. According to the market efficient hypothesis, if β_0 is estimated, it is expected that, if it is not zero (that will always be the case), it will not be statistically significant! In other words, although it will be non-zero, since its p-value should be much greater than 0.05, then it is like assuming that β_0 is zero since it could be negative, zero or positive.

Let’s do an exercise to estimate the CAPM.

4.3 Data collection

We load the quantmod package:

# To avoid scientific notation for numbers: 
options(scipen=999)

# load package quantmod
library(quantmod)

4.4 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="2020-01-01", 
           to="2025-03-31", periodicity="monthly", src="yahoo")
[1] "AAPL" "GSPC" "TSLA"
#I select only the adjusted prices of each stock and merge them together:
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.

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

4.6 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["2020-02-01/2025-03-31"]

4.7 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

5 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

6 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.33395 -0.07460 -0.00468  0.12916  0.39606 

Coefficients:
            Estimate Std. Error t value   Pr(>|t|)    
(Intercept)  0.01199    0.02091   0.573      0.569    
GSPC_Premr   2.16725    0.39795   5.446 0.00000101 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1633 on 60 degrees of freedom
Multiple R-squared:  0.3308,    Adjusted R-squared:  0.3196 
F-statistic: 29.66 on 1 and 60 DF,  p-value: 0.000001011

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.02983747 to 0.05382031

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 1.371346 to 2.963162

Follow the same procedure to get Apple’s CAPM and respond after you run your CAPM regression model for both stocks:

7 CHALLENGE 2

** INTERPRET THE RESULTS OF THE COEFFICIENTS (b0 and b1), THEIR STANDARD ERRORS, P-VALUES AND 95% CONFIDENCE INTERVALS.**

8 CHALLENGE 3

You have to find the optimal portfolio of a 4 US stocks with 2 methods:

  • Estimate the Variance-Covariance matrix according to the Markowitz Portfolio Theory

  • Estimate the Variance-Covariance matrix according to the Single-Index Model. In this case you have to use the Single-index model to estimate the individual Expected return of each stock, so you have to make an assumption about the expected return of the market.

    The 4 stocks must be from different industries and will be announced in class. You have to use historical monthly data (3 to 5 years of data).

    You have to do the following:

  • Estimate the efficient frontier for both methods

  • Estimate the optimal portfolio for both methods for a risk-free rate of 4% annual rate

  • Compare the 2 methods and their results. Explain the differences and why do you think the results are different? Which method would you prefer and why?

9 Multi-Factor Models

After the CAPM was introduced, it received strong critiques by researchers and analysts since there were some stocks that had positive abnormal returns that were not explained by the CAPM. In other words, several stocks had alpha coefficients significantly positive for a long time. These and other cases are considered anomalies since the CAPM does not explain the excess return of all assets all the time. In other words, there should be other sources of systematic risk, not only the market risk.

In 1993, the 3-factor (Fama&French) model was proposed. This factor model starts with the CAPM model and adds 2 other sources of systematic risk. The sources of systematic risk are called factors.

The FF 3-factor model includes the following factors:

  • Market Factor

  • Value Factor

  • Size Factor

The value factor refers that firms with high book-to-market value usually get higher future returns compared to firms with low book-to-market value. The book-to-market value of a stock is the ratio:

BTM_t =\frac{bookvalue_t}{marketvalue_t}

The book value at any accounting period t is the accounting value of a stock, which can be estimated as the difference between total assets minus total liabilities:

bookvalue_t=totalassets_t-totalliabilities_t

Then, if both values are equal, BTM =1. Most current active firms have BTM that are much less than 1, so the market value must be always greater than the book value. This difference can be explained by intangible assets such as competitive advantage, quality of products/services, innovation, reputation, etc.

The market value of a stock at any period t is the product of stock price times the number of shares outstanding:

marketvalue_t=(stockprice_t)(sharesoutstanding)

The value factor is estimated “artificially” by creating 2 portfolios over time: one with firms with high BMV and firms with low BMV. The portfolio returns are subtracted each period to get the difference in returns between the portfolio with high BMV and the portfolio with low BMV.

It is supposed that this is a factor that measure how firms with high BMV usually recover and surprise with excess returns in the near future.

The size factor refers that usually small firms get high future returns compared to big firms. This factor is also calculated “virtually” by creating a portfolio with small firms and a portfolio with big firms over time, and then get the difference between these 2 portfolios. This difference is considered the SIZE factor.

Then, now the 3 factors, market, value and size are calculated with excess returns. The market is calculated as the market return minus the risk-free return; the value factor is calculated as the difference between the portfolio of high BMV portfolio returns minus low BMV portfolio returns overtime. The size factor is calculated as the difference of returns between the portfolio with SMALL firms and the portfoio with BIG firms.

9.1 Estimatig 3-factor FF model with real data

The FF factors are constantly estimated for the US and other international markets. These calculations are public and can be downloaded from a web page (https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html)

We use the frechdata R library to automatically get the FF factors from this page:

library(dplyr)
library(frenchdata)

After the 3-factor FF model, they proposed other 2 extra factors, so now we can use up to 5 factors to model stock returns. These extra factors are profitability and investment pattern.

I download the historic series for the 5 factors for the US market:

ff5 =
  download_french_data("Fama/French 5 Factors (2x3)")

The ff5 R object is a list of 3 datsets. The first dataset contains monthly historical returns of the factors. Then, I get the first dataset:

ff5data = ff5$subsets$data[[1]]
# I see the most current values of the factors:
tail(ff5data)
# A tibble: 6 × 7
    date `Mkt-RF`   SMB   HML   RMW   CMA    RF
   <dbl>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 202407     1.24  8.28  5.74  0.22  0.43  0.45
2 202408     1.61 -3.65 -1.13  0.85  0.86  0.48
3 202409     1.74 -1.02 -2.59  0.04 -0.26  0.4 
4 202410    -0.97 -0.88  0.89 -1.38  1.03  0.39
5 202411     6.51  4.78 -0.05 -2.62 -2.17  0.4 
6 202412    -3.17 -3.87 -2.95  1.82 -1.1   0.37

Now I download historic stock prices of a company to run the FF 3-factor model:

Bajo datos de precios de 1 empresa para hacer mi modelo de 3 factores:

library(quantmod)
getSymbols(Symbols=c("WMT"), periodicity="monthly",from="2020-01-01", to="2025-03-31")
[1] "WMT"

I calculate monthly returns:

ret = diff(log(Ad(WMT)))
names(ret)=c("rwmt")

I do some data management to put the historical data into 1 dataset before I run the multiple regression model:

library(xts)
library(lubridate)
ff5data$m = ym(ff5data$date) 
# I change the dataset to an xts time-series dataset:
ff5data.xts <- xts(x = ff5data, order.by = ff5data$m)
# I merge both datasets:
dataw = merge(ff5data.xts,ret)

dataw = dataw["2020-01-01/"]

The return data of the FF factors are in percentage, so I have to do the right transformation. I can multply times 100 the stock returns:

dataw$rwmt = dataw$rwmt * 100
dataw$rmktp = dataw$Mkt.RF
# I calculate the stock premium returns:
dataw$rwmtp =dataw$rwmt - dataw$RF

I run the models:

# CAMP:
modcapm = lm(rwmtp ~ rmktp, data=dataw, na.action=na.omit )
summary(modcapm)

Call:
lm(formula = rwmtp ~ rmktp, data = dataw, na.action = na.omit)

Residuals:
     Min       1Q   Median       3Q      Max 
-18.0679  -2.2474   0.4662   2.5483  10.9001 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   0.8603     0.6491   1.325 0.190377    
rmktp         0.4870     0.1169   4.165 0.000107 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 4.891 on 57 degrees of freedom
  (4 observations deleted due to missingness)
Multiple R-squared:  0.2333,    Adjusted R-squared:  0.2199 
F-statistic: 17.35 on 1 and 57 DF,  p-value: 0.0001066
# FF3 factor model:
modff3 = lm(rwmtp ~ rmktp + SMB + HML, data=dataw, na.action=na.omit )
summary(modff3)

Call:
lm(formula = rwmtp ~ rmktp + SMB + HML, data = dataw, na.action = na.omit)

Residuals:
     Min       1Q   Median       3Q      Max 
-16.5373  -1.9814   0.4658   2.2031   9.1029 

Coefficients:
            Estimate Std. Error t value   Pr(>|t|)    
(Intercept)   0.7487     0.6193   1.209     0.2319    
rmktp         0.5744     0.1180   4.870 0.00000983 ***
SMB          -0.3953     0.1996  -1.981     0.0526 .  
HML          -0.1691     0.1414  -1.196     0.2369    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 4.641 on 55 degrees of freedom
  (4 observations deleted due to missingness)
Multiple R-squared:  0.334, Adjusted R-squared:  0.2977 
F-statistic: 9.195 on 3 and 55 DF,  p-value: 0.00005006

10 CHALLENGE 4

You have to interpret the FF model in detail