Research project - R programming / analytics

profileSkaur
Assignment4-Sukhmankaur.pdf

library(tidyquant)

library(ggplot2)

library(plotly)

library(timetk)

library(tidyverse)

tick <- c('AMZN', 'AAPL', 'NFLX', 'XOM', 'T')

price_data <- tq_get(tick,

from = '2014-01-01',

to = '2018-05-31',

get = 'stock.prices')

log_ret_tidy <- price_data %>%

group_by(symbol) %>%

tq_transmute(select = adjusted,

mutate_fun = periodReturn,

period = 'daily',

col_rename = 'ret',

type = 'log')

log_ret_xts <- log_ret_tidy %>%

spread(symbol, value = ret) %>%

tk_xts()

head(log_ret_xts)

#

cov_mat <- cov(log_ret_xts) * 252

print(round(cov_mat,4))

mean_ret <- colMeans(log_ret_xts)

print(round(mean_ret, 5))

wts <- runif(n = length(tick))

print(wts)

print(sum(wts))

wts <- wts/sum(wts)

print(wts)

sum(wts)

port_returns <- (sum(wts * mean_ret) + 1)^252 - 1

port_risk <- sqrt(t(wts) %*% (cov_mat %*% wts))

print(port_risk)

# Since Risk free rate is 0%

sharpe_ratio <- port_returns/port_risk

print(sharpe_ratio)

print(port_returns)

print(port_risk)

print(sharpe_ratio)

num_port <- 5000

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

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

}

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

colnames(all_wts) <- colnames(log_ret_xts)

# Combing all the values together

portfolio_values <- tk_tbl(cbind(all_wts, portfolio_values))

head(portfolio_values)

# Minimum variance portfolio

min_var <- portfolio_values[which.min(portfolio_values$Risk),]

# Tangency porfolio

max_sr <- portfolio_values[which.max(portfolio_values$SharpeRatio),]

# plot minimum variance portfolio

p <- min_var %>%

gather(AAPL:XOM, 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 tangency portfolio

p <- max_sr %>%

gather(AAPL:XOM, 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)

# plot random portfolios and visualize 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_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)

# global minimum variance

data(portfolio_values)

Rets <- returnseries(portfolio_values, method = "discrete", trim = TRUE)

PGMV(Rets)

# most diversified portfolio

data(portfolio_values)

Rets <- returnseries(portfolio_values, method = "discrete", trim = TRUE)

PMD(Rets)

# Equal risk contributed portfolio

data(portfolio_values)

Rets <- returnseries(portfolio_values, method = "discrete", trim = TRUE)

PMTD(Rets)

# Minimum Tail dependent portfolio

data(portfolio_values)

Rets <- returnseries(portfolio_values, method = "discrete", trim = TRUE)

PMTD(Rets)