Portfolio analysis in R
Financial Mathematics 1
1 Portfolio returns
1.1 Problem 1:
Suppose you buy 500 shares of stock A, buy 300 shares of stock B, and short sell 100 shares of stock C. The corresponding prices of shares A, B, C: $70, $50, $90. Compute the weights of your portfolio.
# Define the vector values
values=c(500,300,100)
# Define the vector weights
weights=values/sum(values)
# Print the resulting weights
print(weights)## [1] 0.5555556 0.3333333 0.1111111
1.2 Problem 2:
Suppose on 10/12/2023, Anna buy 2000 shares of VFS and 3000 shares of TESLA at the close price. Compute the daily rate of return of her portfolio from 11/12/2023 to 20/12/2023.
## 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
## [1] "VFS"
## [1] "TSLA"
# Extract adjusted close prices
VFS_prices <- Cl(VFS)
Tesla_prices <- Cl(TSLA)
# Calculate daily returns
VFS_returns <- dailyReturn(VFS_prices)
Tesla_returns <- dailyReturn(Tesla_prices)
# Define the number of shares
VFS_shares <- 2000
Tesla_shares <- 3000
# Calculate the portfolio value for each day
portfolio_values <- VFS_shares * VFS_prices + Tesla_shares * Tesla_prices
# Calculate the daily portfolio returns
portfolio_returns <- dailyReturn(portfolio_values)
# Print the daily portfolio returns from 11/12/2023 to 20/12/2023
portfolio_returns['2023-12-11/2023-12-20']## daily.returns
## 2023-12-11 0.000000000
## 2023-12-12 -0.011779041
## 2023-12-13 0.010248189
## 2023-12-14 0.048643148
## 2023-12-15 0.012033416
## 2023-12-18 -0.006131959
## 2023-12-19 0.020946206
2 Mean and variance of a two-asset portfolio
2.1 Problem 1:
A person invests a portfolio buy buying 100 stock A and 400 stock B. The price for a stock A and a stock B are $40 and $20, respectively. Assume that the expected rate of return of stock A and B are 17%/year and 13%/year, respectively. In addition, the correlation between the stock returns of A and B is 0.7. Note that the volatility of stock and A and B are 30%/year and 20%/year, respectively.
- Compute the expected rate of return r of the portfolio.
# Define the parameters
investment_A <- 100 * 40
investment_B <- 400 * 20
total_investment <- investment_A + investment_B
weight_A <- investment_A / total_investment
weight_B <- investment_B / total_investment
weight_A## [1] 0.3333333
## [1] 0.6666667
expected_return_A <- 0.17
expected_return_B <- 0.13
# a) Calculate the expected return of the portfolio
expected_return_portfolio <- (weight_A * expected_return_A) + (weight_B * expected_return_B)
expected_return_portfolio## [1] 0.1433333
- Compute the variance of the portfolio.
# Define the parameters
volatility_A <- 0.3
volatility_B <- 0.2
correlation_AB <- 0.7
#Calculate the variance of the portfolio
variance_A <- volatility_A^2
variance_B <- volatility_B^2
covariance_AB <- correlation_AB * volatility_A * volatility_B
covariance_AB## [1] 0.042
portfolio_variance <- (weight_A^2 * variance_A) + (weight_B^2 * variance_B) + (2 * weight_A * weight_B * covariance_AB)
portfolio_variance## [1] 0.04644444
3 Global minimum variance portfolios
3.1 Problem 1:
Consider two assets \(S_{1}\) and \(S_{2}\) with the annual mean return \(\overline{r}_{1}=0.22\),\(\overline{r}_{2}=0.15\), the volatilities \(\sigma_{1}= 0.2\),\(\sigma_{2}= 0.3\). The correlation coefficient of the two assets is \(\rho = 0.2\). Assume the short sell is not permitted. Compute the weights of \(S_{1}\) and \(S_{2}\) that constitute a portfolio having the minimum variance.
Solution:
Since short selling is not permitted (meaning weights must be non-negative), we need to adjust the weights to the feasible region. If short selling were permitted, \(w_{1}\) would be greater than 1 and \(w_{2}\) would be negative. To respect the no short-selling constraint, we must set \(w_{1}= 1\) and \(w_{2}=0\) because \(w_{2}\) must be non-negative.
Therefore, the minimum variance portfolio with the constraint that short selling is not allowed has the weights: \(w_{1}=1\) and \(w_{2}=0\)
3.2 Problem 2:
Consider two assets \(S_{1}\) and \(S_{2}\) with the annual mean return \(\hat{r_{1}}=0.22\),\(\hat{r_{2}}=0.15\), the volatilities \(\sigma_{1}= 0.2\),\(\sigma_{2}= 0.3\). The correlation coefficient of the two assets is \(\rho = 0.7\). Assume the short sell is permitted. Compute the weights of \(S_{1}\) and \(S_{2}\) that constitute a portfolio having the minimum variance.
Solution:
Denote that:
- \(w_{1}\) is the weight of Assets \(S_{1}\)
- \(w_{2}= 1-w_{1}\) is the weight of Assets \(S_{2}\)
The covariance of portfolio is: \[Cov(S_{1}, S_{2})=\sigma_{12}= \rho\times\sigma_{1}\times\sigma_{2}=(0.7)(0.2)(0.3)= 0.042\]
The variance of portfolio is: \[ \begin{eqnarray} \sigma^{2}&=& w_{1}^{2}(\overline{r}_{1})^{2} + w_{2}^{2}(\overline{r}_{2})^{2} + 2w_{1}w_{2}\sigma_{12} \\&=& w_{1}^{2}(0.2)^{2} + w_{2}^{2}(0.3)^{2}+2w_{1}w_{2}(0.042) \end{eqnarray} \] The problem is optimal weight of the asset A and B: \[ \begin{array}{cl} Minimize & \sigma^{2}\\ s.t & w_{1}+w_{2}=1\\ \end{array} \] Since short selling is permitted, negative weights are allowed.
By using method of Larange multipliers, we obtain solution: \[w_{1}= \frac{\sigma_{2}^{2}-\sigma_{12}}{\sigma_{1}^{2}+\sigma_{2}^{2}-2\sigma_{12}}= \frac{(0.3)^{2}-0.042}{(0.2)^{2}+(0.3)^{2}-2(0.042)}= 1.04348,\\ w_{2}=1-w_{1}=1-1.04348= -0.04348\]
4 Efficient portfolios
4.1 Problem:
Consider two assets \(S_{1}\) and \(S_{2}\) with the annual mean return \(\hat{r_{1}}=0.22\),\(\hat{r_{2}}=0.15\), the volatilities \(\sigma_{1}= 0.2\),\(\sigma_{2}= 0.3\). The correlation coefficient of the two assets is \(\rho = 0.2\). Assume the short sell is not permitted. Compute the weights of S1 and S2 that constitute an efficient portfolio having the expected return \(r = 20\%\) and the lowest variance.
Solution:
Denote that:
- \(w_{1}\) is the weight of Assets \(S_{1}\)
- \(w_{2}= 1-w_{1}\) is the weight of Assets \(S_{2}\)
The variance between \(S_{1}\) and \(S_{2}\) is: \[Cov(S_{1}, S_{2})= \rho \times \sigma_{1} \times \sigma_{2}= (0.2)(0.2)(0.3)= 0.012\]
Now the covariance matrix is: \[\sum = \begin{bmatrix} \sigma_1^2 & \text{Cov}(S_1, S_2) \\ \text{Cov}(S_1, S_2) & \sigma_2^2 \end{bmatrix} = \begin{bmatrix} 0.04 & 0.012 \\ 0.012 & 0.09 \end{bmatrix}\]
The problem is to maxminimize the variance \((\sigma_{1}, \sigma_{2})\)for a given expected return \(r=0.2\). The formula for the variance of a portfolio \(\sigma_{p}^{2}\) with weight 1 (\(w_{1}\)) and (\(w_{2}\)) is \[ \begin{array}{cl} Minimize & \sigma^{2}=w_{1}^{2}\sigma_{1}^{2} + w_{2}^{2}\sigma_{2}^{2} + 2w_{1}w_{2}Cov(S_{1}, S_{2}) \\ s.t & r_{p}= w_{1}\hat{r_{1}}+\hat{w_{2}}r_{2}=0.2\\ & w_{1}+w_{2}=1 \end{array} \] Then the expectation of portfolio is calculated: \[\begin{eqnarray*} r_{p}= w_{1}\hat{r_{1}}+\hat{w_{2}}r_{2}=0.2 &\iff& w_{1}0.22+ w_{2}0.15= 0.2\\&\iff&w_{1}0.22+(1-w_{1})0.15=0.2\\&\iff& w_{1}= 0.7143 \end{eqnarray*}\] The weight of S_{1} w_{1} is: 0.7143 and the weight of S_{2} w_{2} is: \(w_{2}=1-w_{1}=0.2857\)
Therefore, the minimum variance of portforlio is given as: \[\begin{eqnarray*} \sigma^{2}&=&w_{1}^{2}\sigma_{1}^{2} + w_{2}^{2}\sigma_{2}^{2} + 2w_{1}w_{2}Cov(S_{1}, S_{2})\\&=& (0.7143)^{2}(0.2)+(0.2857)^{2}(0.3)+2(0.7134)(0.2857)(0.012)\\ &=& 0.1314 \end{eqnarray*}\]
5 Portfolio of multiple stocks
5.1 Problem 1:
Considering a portfolio including three stocks A, B, C. Compute the exected rate of return r of the portfolio.
| Asset_name | Share_number | price expected_return_rate |
|---|---|---|
| A | 100 | $30 |
| B | 400 | $20 |
| C | 200 | $50 |
- Calculate the total market value of the portfolio: \[Asset\ A= 100\times 30= 3000\\ Asset \ B = 400\times 20= 8000\\ Asset \ C = 200\times 50= 10000\]
Then, \[Total\ Asset = 3000+8000+10000= 21000\]
- Calculate the weight of each asset:
- Weight of Asset A: \[w_{A}=\frac{3000}{21000}=0.1429\]
- Weight of Asset B: \[w_{B}=\frac{8000}{21000}=0.3801\]
- Weight of Asset C: \[w_{C}=\frac{10000}{21000}=0.4762\]
- The expected rate of return \(r_{p}\) of the portfolio is: \[r_{p}= w_{A}\times r_{A}+ w_{B}\times r_{B} +w_{C}\times r_{C}= 0.1832 \]
5.2 Problem 2:
Consider three assets A, B, C. The annual mean returns of A, B, C are 17%, 5%, 8%, respectively. The volatilities (the standard deviation of mean returns) of A, B, C are 22%, 7%, 8%, respectively. The correlation matrix of A, B, C are given as: \[\begin{matrix} &A & B &C \\ A & 1.00 & -0.25 & -0.05 \\ B & -0.25 & 1.00 & 0.45 \\ C & -0.05 & 0.45 & 1.00 \\ \end{matrix}\]
We build a portfolio consisting of A, B, C with weights \(w_{A}=0.6\), \(w_{B}=0.15\), \(w_{B}=0.25\).
- Compute the covariance matrix of A, B, C. Using this formula, we compute the covariances: \[ Cov(A,B)= \rho_{AB}\sigma_{A}\sigma_{B}=(-0.25)(0.22)(0.07)= -0.00385\\ Cov(B,C)= \rho_{BC}\sigma_{B}\sigma_{C}=(0.45)(0.07)(0.08)= 0.00252\\ Cov(A,C)= \rho_{AC}\sigma_{A}\sigma_{C}=(-0.05)(0.22)(0.08)= 0.00088 \] \[\sum = \begin{bmatrix} 0.0484 & -0.00385 & 0.00088 \\ -0.00385& 0.0049 & 0.00252\\ 0.00088 & 0.00252 & 0.0064\\ \end{bmatrix}\]
- Compute the mean return of the portfolio.
The mean return of the portfolio \(\hat{r}_{p}\) is given by:
\[\begin{eqnarray*} \hat{r}_{p} &=& w_{A}\hat{r}_{A}+ w_{B}\hat{r}_{B} +w_{C}\hat{r}_{C}\\ &=& (0.6)(0.17)+ (0.15)(0.05)+ (0.25)(0.08)= 0.1295 \end{eqnarray*}\]
- Compute the variance return of the portfolio.
The variance of the portfolio \(\sigma_{p}^{2}\) is given by: \[\begin{eqnarray*} \hat{\sigma}^{2}_{p} &=& w_{A}^{2}\hat{\sigma}^{2}_{A}+ w_{B}^{2}\hat{\sigma}^{2}_{B} +w_{C}^{2}\hat{\sigma}^{2}_{C}\\ &=& (0.6^{2}\times0.0484)+(0.15^{2}\times0.0049)+(0.25^{2}\times0.0064)\\ &+&2(0.6)(0.15)(−0.00385)+2(0.6)(0.25)(−0.00088)+2(0.15)(0.25)(0.00252)\\ &=& 0.0172 \end{eqnarray*}\]
The standard deviation (volatility) of the portfolio is: \[\sigma_{p}= \sqrt{0.0172}= 0.1311\]