-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft3_markowitz.Rmd
77 lines (58 loc) · 2.85 KB
/
ft3_markowitz.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
title: "Markowitz Mean-Variance Model"
output:
html_document: default
html_notebook: default
---
## Basic Concepts
* Linear Programming and Quadratic Programming: https://wenku.baidu.com/view/72f1d6ad856a561253d36f1f.html
## Portfolio of Two Risky Assets
* `Portfolio`: the idea is to place a set of securities into a basket with weights representing amount of investment in each
* `Variance`: representing the volatility or the risky. So we are interested in how to minimize the risk. Below is the formula, where a/b are investment weights and X/Y are two investment returns:
> `Var(aX + bY) = E(aX + bY)^2 - E^2 (aX + bY) = a^2 Var(X) + b^2 Var(Y) + 2abCov(X, Y)`
```{r}
# Sample: how to find minimum variance of a portfolio.
## Suppose
# Make up the parameters
mu_d = .05 # debt has less return than equity
mu_e = .12
sigma_e = .30 # but equity has larger risk
sigma_d = .20
sigma_de = .003
w_d = seq(0, 1, .01) # generate 100 different weight combinations
mu_p = vector(length=length(w_d))
sigma_p = vector(length=length(w_d))
sr_p = vector(length=length(w_d))
for (u in 1:length(w_d)) {
mu_p[u] = mu_d * w_d[u] + mu_e * (1 - w_d[u])
sigma_p[u] = w_d[u]^2 * sigma_d^2 + 2 * w_d[u] * (1 - w_d[u]) * sigma_de + (1-w_d[u])^2 * sigma_e^2
sr_p[u] = mu_p[u] / sigma_p[u]
}
##################################
# Portfolio with minimum risk
##################################
par(mfrow=c(1, 2))
plot(sigma_p, w_d, type='l', ylab='w_d', col=6)
min_var_p_index = sigma_p == min(sigma_p) # get the minimum value of the portfolio
w_d[min_var_p_index] # weight of debt should be 0.7 to get the least risk
points(sigma_p[min_var_p_index], w_d[min_var_p_index]) # draw the point of minimum variance
text(sigma_p[min_var_p_index] + .03, w_d[min_var_p_index], paste('<-(', sigma_p[min_var_p_index], ',', w_d[min_var_p_index], ')'), cex=.75)
#####################################
# Plot risk as a function of return
#####################################
plot(sigma_p, mu_p, type='l', ylab='mu_p', col=2)
points(sigma_p[min_var_p_index], mu_p[min_var_p_index])
text(sigma_p[min_var_p_index] + 0.03, mu_p[min_var_p_index], paste('<-(', sigma_p[min_var_p_index], ',', mu_p[min_var_p_index], ')'), cex=.75)
#####################################
# Portfolio with optimal sharpe ratio
#####################################
optimal_p_index = sr_p == max(sr_p)
mu_p[optimal_p_index]
points(sigma_p[optimal_p_index], mu_p[optimal_p_index])
text(sigma_p[optimal_p_index] + 0.03, mu_p[optimal_p_index], paste('<-(', sigma_p[optimal_p_index], ',', mu_p[optimal_p_index], ')'), cex=.75)
```
### Summary
1. The efficient frontier is the entire cure of the rightmost plot
2. The minimum variance portfolio has a return and variance is the lower of the two selected points
3. The tangency portfolio which has the best sharpe ratio is the upper of the two selected points
## Quadratic Programming