Workshop 1, Investment Funds
1 General directions for the Workshops
You have to use a new Google Colab to do all the Workshops related to this part of Module 2. Module 2 is related to portfolio theory and financial models (CAPM, Factor models, etc).
You have to read the workshop, take your notes in your Colab, and also DO the CHALLENGES You have to submit ONLY the link of your Google Colab. You must share your notebook with me: cdorante@tec.mx (as Editor)
2 Portfolio Theory - Review
2.1 What is a Portfolio
A portfolio is a set of 2 or more financial assets. A financial asset can be of any type such as stock, bond, derivative, commodity. The portfolio owner needs to decide how much money allocate to each individual asset. The main advantage of financial portfolios is the possibility to diversify non-systematic (idiosyncratic) risk, while maintaining an expected rate of return.
In this section we review practical easy examples of 2-asset and 3-asset portfolios with the purpose to review portfolio theory and portfolio optimization.
It is recommended that you review the lecture notes posted in the course site where portfolio theory, matrix algebra, and portfolio optimization are explain in more detail.
The main contribution of portfolio theory to Finance is the method to estimate the expected risk and return of a portfolio. Before we review this method, we review how to estimate the expected return and risk of an asset.
Let’s start with an example of a 2-asset portfolio. Imagine we want to invest in a portfolio with the following composition:
WalMart: 70%
Tesla: 30%
We start reviewing the expected return of each asset:
2.2 Estimation of expected return of an asset
The most simple way to estimate expected return of an asset is by the geometric mean of historical returns. Other sophisticated methods to estimate expected stock return are the single-index model, the CAPM regression model, ARIMA models.
The geometric mean of historical returns is the period return needed so that holding an investment over several compounding periods, we get a final a final holding return of the stock.
The mathematical formula of geometric mean return is:
GeomMean(R)=\sqrt[N]{(1+R_{1})(1+R_{2})...(1+R_{N})}-1
Where R_t is the historical return for period t. In this formula we have N historical periods (can be months)
Another easier way to calculate geometric mean of returns is to calculate the arithmetic mean of continuously compounded returns, and then convert the result to simple return by applying the exponential function:
GeomMean(R)=e^{\bar{r}}-1
Where \bar{r} is the arithmetic mean of historical returns:
\bar{r}=\frac{r_{1}+r_{2}+...+r_{N}}{N}
Remember that continuously compounded return can be calculated from prices as follows:
r_t=log(price_t)-log(price_{t-1})
The the return of a stock today is the difference between the natural logarithm of the current price minus the natural logarithm of the price of the previous period.
Let’s practice with an excercise using real data.
3 CHALLENGE 1
Using real monthly stock price data for Walmart and Tesla from Jan 2020 to March 2025, calculate the following:
Holding period return (HPR) for both stocks (total return in the whole period)
Expected monthly return, and corresponding expected annual return of WalMart and Tesla.
3.1 Estimation of expected risk of an asset
A simple way to calculate the expected risk of an asset is through the standard deviation of historical returns.
Remember that the variance is the square of the standard deviation. Then, we start calculating the variance of historical returns.
3.1.1 Variance
Variance of any variable is the arithmetic mean of squared deviations. A squared deviation is the value resulting from subtracting the value of the variable minus its mean, and the square the value.
Why we square the deviations? This is a trick to convert the negative deviations into positive so that we can measure how much each value of the variable moves or deviates from its mean.
The mean of returns is estimated as:
\bar{r} =\frac{r_{1}+r_{2}+...+r_{N}}{N}
The variance is estimated as:
VAR(r)=\frac{(r_{1}-\bar{r})^{2}+(r_{2}-\bar{r})^{2}+...+(r_{N}-\bar{r})^{2}}{N}
Variance is a measure of dispersion. The higher the variance, the more dispersed the values are from its mean. It is hard to interpret the magnitude of variance. That is the reason why we need to calculate standard deviation, which is basically the squared root of the variance.
3.1.2 Standard deviation - volatility
Standard deviation of a variable is the squared root of the variance of the variable:
SD(r)=\sqrt{VAR(r)}
Then, the standard deviation of returns can be interpreted as a standardized average distance from each value of the variable from its mean.
The standard deviation of returns is called volatility. Then, volatility of returns tells us an idea of how much on average (above or below) the period returns move from its historical mean.
Depending on the granularity of the data you use, you calculate the corresponding granularity for variance and standard deviation, but you can calculate the corresponding variance and standard deviation for higher levels of granularity using the granularity you have in the data.
For example, if you have monthly data of returns of a stock, you can calculate the monthly variance using the previous formula, and you can estimate a corresponding annual variance using monthly variance as follows:
To calculate annual variance of returns is like calculating 12-month variance. According to probability theory, the variance of a sum is the sum of variance. Then:
If VAR(r) is the estimated variance of monthly returns, then the annual variance is the 12-month variance:
12monthVAR = VAR(r) + VAR(r) + ... + VAR(r) = 12 * VAR(r)
Then, the 12-month standard deviation of returns using monthly returns is:
12monthSD = \sqrt{12monthVAR}=\sqrt{12*VAR(r)}=\sqrt{12}SD(r)
Then, when estimating volatility from a lower granularity to a higher granularity, you have to multiply the lower-granularity volatility times the squared root of the number of periods in the higher-granularity.
Let’s calculate variance and volatility (expected risk) of WalMart and Tesla:
4 CHALLENGE 2
Using the historical monthly data for Walmart and Tesla from 2020, do the following:
Calculate the variance of returns for Walmart and Tesla
Calculate the expected monthly and annual volatility (risk) of both stocks
Now that we have simple measures for expected return and risk of 1 asset, we move to methods to estimate the expected return of a portfolio.
4.1 Historical Sharpe ratio of a portfolio
The Sharpe ratio is a standardized measure of portfolio premium return after considering its volatility.
A premium return is the return above the risk-free rate. In Mexico the risk-free rate is the CETES; in the US, the risk-free rate is the Treasury Bills.
Then, the Sharpe ratio tells us how much portfolio returns (above the risk free rate) we can expect for each percent point of volatility. Let’s see the formula:
SharpeRatio=\frac{(PortfolioReturn-riskfreeRate)}{PortfolioVolatility}
5 Calculating expected portfolio return
Up to now we have calculated expected return and risk of 1 asset using historical mean and standard deviation of historical cc returns.
Here we review how to estimate future expected portfolio returns and risk based on Portfolio Theory developed by Harry Markowitz.
5.1 Expected portfolio return
The expected portfolio return is the weighted average of the individual expected return of the stocks of the portfolio.
Imagine we have a 2-stock portfolio composed as follows:
WalMart: 70%
Tesla: 30%
5.1.1 Method 1: weighted average using a sum of products
We use the weights (%) allocated for each asset to calculate the weighted average as the portfolio expected return:
ER(P)=w_1*E(R_1)+w_2E(R_2)
5.1.2 Method 2: weighted average using matrix algebra
Another way to calculate the expected return of a portfolio is using matrix algebra. This is a very useful method when we have many assets in the portfolio since it is very easy to compute.
If you do not remember how to multiply matrices, it is strongly recommended to review this (you can read Note 2 of Portfolio Theory).
Matrix multiplication is used to compute sum of products or sum of multiplications.
For example, the way to estimate the expected return of our portfolio is the following:
ERPort=t\left(W\right)*ER
t(W) is the transpose of the matrix W.
The transposed of a matrix or a vector is the conversion of the rows by columns and columns by rows. Transposing is like rotating a vector or a matrix 90 degrees:
t\left(\left[\begin{array}{c}w_1\\w_2\end{array}\right]\right)=\left[\begin{array}{cc}w_1 & w_2\end{array}\right]
Then:
ERPort=\begin{bmatrix}w_{1} & w_{2}\end{bmatrix}*\begin{bmatrix}ER_{1}\\ ER_{2}\end{bmatrix}=w_1*ER_1+w_2*ER_2
6 CHALLENGE 3
Assume a portfolio of 30% Tesla and 70% Walmart, and using monthly stock price data from Jan 2020 to March 2025:
Calculate the expected monthly return of the portfolio using the 2 methods (sum of products and matrix multiplication). Make sure you get the same result
Calculate the corresponding annual expected return of this portfolio
Do a) and b), but now change the weights 70% Walmart and 30% Tesla
With this multiplication we got the same expected portfolio return as above.
6.1 Expected portfolio risk
Before we calculate the expected portfolio risk we need to understand what is the expected portfolio variance.
According to Portfolio Theory, the expected variance of a 2-asset portfolio returns is given by:
VAR(PortReturns)=w_{1}^{2}VAR(r_{1})+w_{2}^{2}VAR(r_{2})+2w_{1}w_{2}COV(r_{1},r_{2})
r_1 refers to returns of stock 1, and r_2 refers to returns of stock 2.
COV(r_1,r_2) is the covariance of return 1 and return 2.
Check the lecture note Basics of Portfolio Theory-Note 1 to understand why this is the way to estimate the expected variance of a 2-asset portfolio.
It is worth to remember what is covariance.
6.1.1 What is covariance of 2 stock returns?
The covariance of 2 stock returns is the arithmetic mean of the product return deviations. A deviation is the difference between the stock return in a period t and its mean. Here is the formula:
COV(r_{1},r_{2})=\frac{(r_{(1,1)}-\bar{r_{1}})(r_{(2,1)}-\bar{r_{2}})+(r_{(1,2)}-\bar{r_{1}})(r_{(2,2)}-\bar{r_{2}})+(r_{(1,3)}-\bar{r_{1}})(r_{(2,3)}-\bar{r_{2}})+...}{N}
Were:
r_{(1,1)} is the return of stock 1 in the period 1
r_{(2,1)} is the return of stock 2 in the period 1
Then:
r_{(i,j)} is the return of stock i in the period j
\bar{r_{1}} is the average return of stock 1
\bar{r_{2}} is the average return of stock 2
Then, in the numerator we have a sum of product deviations. Each product deviation is the deviation of the stock 1 return multiplied by the deviation of the stock 2 return.
The covariance is a measure of linear relationship between 2 variables. If covariance between stock return 1 and stock return 2 is positive this means that both stock returns are positively related. In other words, when stock 1 return moves up it is likely that stock 2 return moves up and vice-versa; both returns move in the same direction (not always, but mostly).
If covariance is negative that means that stock 1 return is negatively related to stock 2 return; when stock 1 return moves up it is likely that stock 2 return moves down.
Covariance can be a negative or positive number (it is not limited to any number). It is very difficult to interpret the magnitude of covariance. It is much more intuitive if we standardize covariance.
The standardization of the covariance is called correlation.
6.1.2 What is correlation of 2 stock returns?
Correlation of 2 stock returns is also a measure of linear relationship between both returns. The difference compared to covariance is that the possible values of correlation is between -1 and +1, and the correlation gives us a percentage of linear relationship.
Correlation between 2 returns is the covariance between the 2 returns divided by the product of standard deviation of return 1 and standard deviation of return 2.
We calculate correlation as follows:
CORR(r_{1},r_{2})=\frac{COV(r_{1},r_{2})}{SD(r_{1})SD(r_{2})}
Correlation has the following possible values:
-1<=CORR(r_{1},r_{2})<=+1
6.2 Expected variance of a portfolio
Now we can calculate the expected variance of our portfolio according to the previous formula:
6.2.1 Method 1: using sum of products
We calculate the variance of a portfolio of 2 assets as the sum of products:
VAR(PortReturns)=w_{1}^{2}VAR(r_{1})+w_{2}^{2}VAR(r_{2})+2w_{1}w_{2}COV(r_{1},r_{2})
When we have more assets in a portfolio, this formula becomes quite large and complicated to compute since we have to includ all possible pairs of terms of covariances. For example, for a portfolio of 10 stocks, we need to calculate about 100 terms and the sum them all!
When we have a portfolio of many assets, matrix algebra can be very useful. Let’s see.
6.2.2 Method 2: using matrix algebra
Another faster method to get the expected variance of a portfolio is by multiplying the following matrices:
t\left(W\right)*COV*W
Where:
W=\begin{bmatrix}w_{1}\\ w_{2} \end{bmatrix}
And COV is the Variance-Covariance matrix, which has the return variances in the diagonal and the pair correlations in the non-diagonal:
COV=\begin{bmatrix}VAR(r_{2}) & COV(r_{1},r_{2})\\ COV(r_{2},r_{1}) & VAR(r_{1}) \end{bmatrix} We can easily calculate the variance-covariance matrix or the correlation matrix in any computer programming language (Python, R) or Excel.
6.3 Expected risk of a portfolio
To get the expected risk of a portfolio, we simply get the squared root of the expected variance:
PortRisk=SD(PortReturns)=\sqrt{VAR(PortReturns)}
7 CHALLENGE 4
Using the same data of Tesla and Walmart and a portfolio of 30% Tesla and 70% Walmart, do the following:
Calculate the expected monthly variance of the portfolio
Calculate the expected monthly standard deviation of the portfolio
Calculate the expected annual risk of the portfolio
Assuming a risk-free rate of 3% annual, calculate the Sharpe Ratio of this portfolio
7.1 Drivers of Portfolio Diversification
Financial portfolios allow an investor to diversify the risk. The main drivers of portfolio diversification are:
N - the number of financial assets. The more the financial assets, the higher the expected diversification.
Combination of pair of correlations between asset returns. The less the correlation of the pair of assets, the more the diversification.
The first driver is just given by N, the number of assets.
The second driver can actually be manipulated by changing the weights allocated for each asset.
In the following section we illustrate how changing asset weights actually change both, the expected return of the portfolio and the expected portfolio volatility.
8 Risk-return space - set of feasible portfolios
Weight combination determines both expected risk and expected return of a portfolio. Let’s do an example with the same 2-asset portfolio:
8.1 Frontier of 2-asset portfolios
Let’s create a set of 11 portfolios where we change the weight assigned to the first stock from 0 to 1 changing by 0.10. We can create a matrix of weights, where each column represents one portfolio:
W=\begin{bmatrix}0 & 0.1 & 0.2 & 0.3 & 0.4 & 0.5 & 0.6 & 0.7 & 0.8 & 0.9\\ 1 & 0.9 & 0.8 & 0.7 & 0.6 & 0.5 & 0.4 & 0.3 & 0.2 & 0.1 \end{bmatrix}
9 CHALLENGE 5
Using the advantage of matrix algebra calculate:
Expected annual return of these 11 portfolios
Expected annual risk of these 11 portfolios
Do plot to show these 11 portfolios to see how Expected return changes with different portfolio risk (X= portfolio risk, y= portfolio expected return)
Report your interpretation of this plot with your words
9.1 Portfolios of more than 2 assets
With the previous challenge we can see how asset weight combination in a portfolio actually change the expected return and risk of a portfolio!
This plot is called the frontier of feasible portfolios of 2 assets.
Interestingly, for portfolios with 3 or more assets, the possibilities of feasible portfolios will not be a nice frontier like this. The feasible portfolios will be inside the frontier. In other words, for these portfolios some feasible portfolios will not be efficient since there will be other feasible portfolios with the same level of expected risk, but with higher expected return.
Let’s do an example of 4-asset portfolios.
10 CHALLENGE 6
You have to add Oracle (ORCL) and Freeport-McMoran, Inc (FCX) to your portfolio of WMT and TSLA.
Do the data management to come up with a dataset with historical monthly returns for the 4 stocks.
Then, create a matrix to represent 1,000 portfolios where each portfolio has random weights for each of the 4 assets. Each portfolio has to be represented in 1 column of this matrix, so you will end up with a matrix of 4 rows and 1,000 columns.
Calculate the expected return and expected risk for each of these 1,000 portfolios using matrix algebra
Do a plot to show the 1,000 portfolios to see how portfolio expected return change with changes in portfolio risk. (X= portfolio risk; Y=portfolio return)
Which portfolios are more efficient and why? Respond with your own words
10.1 Portfolio efficient frontier
We can see that for each level of expected risk there are many possible portfolios. The best portfolios will be those that lie in the frontier of this feasible portfolios.
The problem we have when we have 3 or more assets in a portfolio is that we need to find the efficient frontier to get only those portfolios that maximize the expected return for each level of expected risk.
Here is where we need to find optimization algorithms to do this.
In the lecture note Basics of Portfolio Theory - Part III I explain what type of optimization methods can be used.
Fortunately, we can use Python or R packages that already do these complicated optimization algorithms.
11 Illustrating the effect of correlation
With the following challenge, you will learn the role of correlation of pairs of assets in the construction of portfolios.
12 CHALLENGE 7
You have to do the following:
- Download monthly price data for WMT and TSLA from Yahoo from Jan 2020 to date. Calculate cc returns
- Calculate the expected returns and store them in a vector
- Calculate the Variance-Covariance matrix of these 2 stock returns
- Create a vector wa for simulating different weights for WMT. Create this vector with sequential values from 0 to 1. Create the vector wb, that will be the complement of wa (1-wa) that represent the % invested in TSLA.
- Then, with these weight vectors of 11 elements you have to estimate the expected return, variance and expected standard deviation of each of these 11 portfolios.
- Do a scatter plot for the portfolio frontier, so that you can see the relationship between risk and return of these 11 portfolios.
- You have to simulate 5 portfolio frontiers using the weight vectors with positive weights. Each of these portfolio frontiers will have a hypothetical correlation between WMT and TSLA. Create the first portfolio frontier with a hypothetical correlation of -1, then the 2nd portfolio frontier with a correlation of -0.5, then the 3rd with a correlation of 0, then the 4rd frontier with a correlation of 0.5, and finally, the 5th frontier with a correlation of 1. Graph the 5 frontiers in one plot. What do you observe? Explain with your own words.
With the final plot you can better appreciate the “magic” of diversification when the correlation between the assets change from a very high positive correlation to a low positive correlation, up to a very high negative correlation.
If both assets are perfectly correlated (correlation=+1), I can see that the risk-reward relationship is actually linear with positive trend, as expected. The reason is that making a portfolio with 2 assets that one correlates exactly than the other is like investing in only in the first assets since there is no diversification at all.
If the correlation starts to go down, then I can see how the straight line is transformed to a horizontal parabola. The lowest the correlation, the more the diversification. When the horizontal parabola is more pronounced towards the left is when we have more diversification since we can find portfolios with less expected risk.
When correlation is exactly -1 we can see like a triangle where I can see a portfolio with portfolio equal to zero. This happens when the weights of the assets is 0.50 for both. This case is similar to the case of a ruleta where I bet 50% of my money in even numbes and 50% in odd numbers. Then, for sure I have no risk since I will always win or lose the same amount of money.
I see this phenomenon in the Markowitz formula to estimate the expected variance of a portfolio. Remember the formula in the case of 2 assets returns (ra and rb):
VAR(P) = wa^2 * VAR(r_a) + wb^2 * VAR(r_b) + 2*wa*wb*COV(r_a,r_b)
Since correlation is the standardized version of the covariance, then I can also express portfolio variance as:
VAR(P) = wa^2 * VAR(r_a) + wb^2 * VAR(r_b) + 2*wa*wb*CORR(r_a,r_b)*\sigma_{ra}*\sigma_{rb}
Then I can clearly see that when CORRELATION is negative, then the variance of the portfolio will be reduced.
13 Portfolio optimization
Out of all feasible portfolios we might be interested in the following:
The portfolio with the least expected risk of all - The Global Minimum Variance Portfolio (GMV)
The efficient frontier - all portfolios that offer the highest expected return for any level of expected risk
The tangent/optimal portfolio - The portfolio with the highest Sharpe Ratio
You have to read Basics of Portfolio Theory - Part III to review how these portfolios can be estimated.
14 CHALLENGE 8
Using monthly stock data of the 4 stocks you worked in this workshop, find:
The Global-minimum-variance portfolio. What is its expected return and risk, and what is the weight combination?
The Optimal portfolio assuming an annual risk-free rate = 3%
The efficient frontier. Do a plot of this frontier and interpret.
15 Adding a risk-free asset to the optimal portfolio
A very important discovery after portfolio theory was developed by Harry Makowitz is that when we add the risk-free instrument to the optimal (tangent) portfolio composed of stocks, then the new efficient frontier becomes the Capital Market Line (instead of the hyperbola), which is more efficient that the previous efficient frontier (the hyperbola).
Then, an investor can play between the tangent portfolio and the risk-free rate to move in the CML. If an investor has a middle-level aversion to risk, he/she might select to allocate 50% to the risk-free asset and the rest 50% in the tangent portfolio, locating the portfolio in a mid-point in the CML between the risk-free rate and the tangent portfolio.
15.1 Assumptions of Portfolio Theory
The main assumption of portfolio theory is that all investors behave as rational participants all the time. In other words, they always maximize return and minimize risk using the available information disclosed to the market in a rational way. In other words, they act with no emotions, no fear and they always understand what happen in the market.