First lets load our packages
Next lets select a few stocks to build our portfolios.
We will choose the following 4 stocks 2(诺思兰德) 1(同辉信息) 7(华岭股份) 5(微创光电)
Read the price data.
##选取投资组合对象
#2(诺思兰德) 1(同辉信息) 7(华岭股份) 5(微创光电)
##读取数据
price <-read.csv("D:/桌面/投资组合管理作业/data.csv")
#修改数据格式
price[,1] = as.POSIXct(price[,1])
price = xts(price[,-1], order.by=price[,1])
##绘制走势图
names(price)<-c("2","1","7","5")
dygraph(price)
Next we will calculate the daily returns for these stocks. We will use the logarithmic returns.
pr_daily <- to.daily(price, indexAt = "lastof", OHLC = FALSE)
#计算回报
rt_daily <- na.omit(Return.calculate(pr_daily, method = "log"))
dygraph(rt_daily)
Next let’s calculate the mean daily returns for each asset. Calculate the covariance matrix for all these stocks. We will annualize it by multiplying by 252.
mean_ret <- colMeans(rt_daily)
print(round(mean_ret, 4))
## 2 1 7 5
## -0.0023 -0.0043 -0.0021 -0.0024
cov_mat <- cov(rt_daily) * 252
print(round(cov_mat,4))
## 2 1 7 5
## 2 0.2788 0.1010 0.0290 0.0482
## 1 0.1010 0.3079 0.0353 0.0611
## 7 0.0290 0.0353 0.1414 0.0222
## 5 0.0482 0.0611 0.0222 0.0667
To calculate the portfolio returns and risk (standard deviation) we will us need
# Calculate the random weights
tick <- c("2","1","7","5")
wts <- runif(n = length(tick))
wts <- wts/sum(wts)
print(wts)
## [1] 0.0991012 0.4265314 0.2730689 0.2012985
# Calculate the portfolio returns
port_returns <- (sum(wts * mean_ret) + 1)^252 - 1
port_risk <- sqrt(t(wts) %*% (cov_mat %*% wts))
sharpe_ratio <- port_returns/port_risk
We have everything we need to perform our optimization. All we need now is to run this code on 10000 random portfolios. For that we will use a for loop.
Before we do that, we need to create empty vectors and matrix for storing our values.
# Creating a matrix to store the weights
num_port <- 10000
# Creating a matrix to store the weights
all_wts <- matrix(nrow = num_port,
ncol = length(tick))
# Creating an empty vector to store
# Portfolio returns
port_returns <- vector('numeric', length = num_port)
# Creating an empty vector to store
# Portfolio Standard deviation
port_risk <- vector('numeric', length = num_port)
# Creating an empty vector to store
# Portfolio Sharpe Ratio
sharpe_ratio <- vector('numeric', length = num_port)
Next lets run the for loop 5000 times.
for (i in seq_along(port_returns)) {
wts <- runif(length(tick))
wts <- wts/sum(wts)
# Storing weight in the matrix
all_wts[i,] <- wts
# Portfolio returns
port_ret <- sum(wts * mean_ret)
port_ret <- ((port_ret + 1)^252) - 1
# Storing Portfolio Returns values
port_returns[i] <- port_ret
# Creating and storing portfolio risk
port_sd <- sqrt(t(wts) %*% (cov_mat %*% wts))
port_risk[i] <- port_sd
# Creating and storing Portfolio Sharpe Ratios
# Assuming 0% Risk free rate
sr <- port_ret/port_sd
sharpe_ratio[i] <- sr
}
All the heavy lifting has been done and now we can create a data table to store all the values together.
# Storing the values in the table
portfolio_values <- tibble(Return = port_returns,
Risk = port_risk,
SharpeRatio = sharpe_ratio)
# Converting matrix to a tibble and changing column names
all_wts <- tk_tbl(all_wts)
## Warning in tk_tbl.data.frame(as.data.frame(data), preserve_index, rename_index,
## : Warning: No index to preserve. Object otherwise converted to tibble
## successfully.
colnames(all_wts) <- colnames(pr_daily)
# Combing all the values together
portfolio_values <- tk_tbl(cbind(all_wts, portfolio_values))
## Warning in tk_tbl.data.frame(cbind(all_wts, portfolio_values)): Warning: No
## index to preserve. Object otherwise converted to tibble successfully.
We have the weights in each asset with the risk and returns along with the Sharpe ratio of each portfolio.
Next lets look at the portfolios that matter the most.
min_var <- portfolio_values[which.min(portfolio_values$Risk),]
max_r <- portfolio_values[which.max(portfolio_values$Return),]
max_sr <- portfolio_values[which.max(portfolio_values$SharpeRatio),]
Plot the weights of each portfolio. First with the minimum variance portfolio.
p <- min_var %>%
gather(1:4, key = Asset,
value = Weights) %>%
mutate(Asset = as.factor(Asset)) %>%
ggplot(aes(x = fct_reorder(Asset,Weights), y = Weights, fill = Asset)) +
geom_bar(stat = 'identity') +
theme_minimal() +
labs(x = 'Assets', y = 'Weights', title = "Minimum Variance Portfolio Weights") +
scale_y_continuous(labels = scales::percent)
ggplotly(p)
Plot the weights of each portfolio. Next lets look at the maximum variance portfolio.
p <- max_r %>%
gather(1:4, key = Asset,
value = Weights) %>%
mutate(Asset = as.factor(Asset)) %>%
ggplot(aes(x = fct_reorder(Asset,Weights), y = Weights, fill = Asset)) +
geom_bar(stat = 'identity') +
theme_minimal() +
labs(x = 'Assets', y = 'Weights', title = "Maximum Return Portfolio Weights") +
scale_y_continuous(labels = scales::percent)
ggplotly(p)
Last lets look at the tangency portfolio or the the portfolio with the highest sharpe ratio.
p <- max_sr %>%
gather(1:4, key = Asset,
value = Weights) %>%
mutate(Asset = as.factor(Asset)) %>%
ggplot(aes(x = fct_reorder(Asset,Weights), y = Weights, fill = Asset)) +
geom_bar(stat = 'identity') +
theme_minimal() +
labs(x = 'Assets', y = 'Weights', title = "Tangency Portfolio Weights") +
scale_y_continuous(labels = scales::percent)
ggplotly(p)
Finally lets plot all the random portfolios and visualize the efficient frontier.
p <- portfolio_values %>%
ggplot(aes(x = Risk, y = Return, color = SharpeRatio)) +
geom_point() +
theme_classic() +
scale_y_continuous(labels = scales::percent) +
scale_x_continuous(labels = scales::percent) +
labs(x = 'Annualized Risk',
y = 'Annualized Returns',
title = "Portfolio Optimization & Efficient Frontier") +
geom_point(aes(x = Risk,
y = Return), data = min_var, color = 'red') +
geom_point(aes(x = Risk,
y = Return), data = max_r, color = 'red')+
geom_point(aes(x = Risk,
y = Return), data = max_sr, color = 'red') #+
#annotate('text', x = 0.20, y = 0.42, label = "Tangency Portfolio") +
#annotate('text', x = 0.18, y = 0.01, label = "Minimum variance portfolio") +
#annotate(geom = 'segment', x = 0.14, xend = 0.135, y = 0.01,
# yend = 0.06, color = 'red', arrow = arrow(type = "open")) +
#annotate(geom = 'segment', x = 0.22, xend = 0.2275, y = 0.405,
# yend = 0.365, color = 'red', arrow = arrow(type = "open"))
ggplotly(p)