Optimasi Portofolio
Tugas Portofolio Saham
| *Kontak | : \(\downarrow\)* |
| clara.evania@student.matanauniversity.ac.id | |
| https://www.instagram.com/claraevania/ | |
| RPubs | https://rpubs.com/claradellaevania/ |
**
DATA SAHAM
Untuk Data yang digunakan, saya memakai data 5 saham untuk membangun portofolio yaitu
- BCA = Bank Central Asia Tbk PT = bbca.jk = BBCA.JK
- Telkom = Telkom Indonesia (Persero) Tbk PT = TLKM.JK
- BRI = Bank Rakyat Indonesia (Persero) Tbk PT = BBRI.JK
- Astra = Astra International Tbk PT = ASII.JK
- Unilever = Unilever Indonesia Tbk PT = UNVR.JK
library(tidyquant)
library(plotly)
library(timetk)tick = c('BBCA.JK','TLKM.JK','BBRI.JK','ASII.JK','UNVR.JK')
pricedata = tq_get(tick,
from = '2020-01-01',
to = '2022-12-09',
get = 'stock.prices')
pricedataPengembalian
Setelah menginput 5 data saham, kita dapat menghitung pengembalian harian untuk beberapa saham ini dengan menggunakan pengembalian logaritmik yang bertujuan untuk memastikan data stasioner.
log_ret_tidy <- pricedata %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = 'daily',
col_rename = 'ret',
type = 'log')
#log_ret_tidy$ret<-round(log_ret_tidy$ret,4)
library(DT) # print data dalam tabel
datatable(log_ret_tidy)Kita dapat menggunakan fungsi spread() untuk mengubah datatable menjadi format lebar dan menggunakan xts() untuk mengubahnya menjadi objek time series.
library(tidyr)
log_ret_xts = log_ret_tidy %>%
spread(symbol, value = ret) %>%
tk_xts()
datatable(log_ret_xts)Rata-rata Pengembalian
mean_ret <- colMeans(log_ret_xts)
print ( round (mean_ret, 4))## ASII.JK BBCA.JK BBRI.JK TLKM.JK UNVR.JK
## -1e-04 4e-04 3e-04 1e-04 -7e-04
Matriks Kovariansi
cov_mat <- cov(log_ret_xts) * 252
print(round(cov_mat,4))## ASII.JK BBCA.JK BBRI.JK TLKM.JK UNVR.JK
## ASII.JK 0.1397 0.0531 0.0696 0.0516 0.0409
## BBCA.JK 0.0531 0.0831 0.0640 0.0446 0.0346
## BBRI.JK 0.0696 0.0640 0.1460 0.0539 0.0372
## TLKM.JK 0.0516 0.0446 0.0539 0.1087 0.0325
## UNVR.JK 0.0409 0.0346 0.0372 0.0325 0.1300
Penerapan Metode Portofolio
Dalam menerapkan metode portofolio,langkah-langkah yang hrtus dilakukan untuk membentuk suatu portofolio adalah dengan
- Bobot Acak
- Rata-Rata Pengembalian Aset
- Risiko Portofolio
- Bobot Portofolio
wts <- runif(n = length(tick))
wts <- wts/sum(wts)
port_returns <- (sum(wts * mean_ret) + 1)^252 - 1
port_risk <- sqrt(t(wts) %*% (cov_mat %*% wts))
sharpe_ratio <- port_returns/port_risknum_port <- 5000
all_wts <- matrix(nrow = num_port, ncol = length(tick))
port_returns <- vector('numeric', length = num_port)
port_risk <- vector('numeric', length = num_port)
sharpe_ratio <- vector('numeric', length = num_port)for (i in seq_along(port_returns)) {
wts <- runif(length(tick))
wts <- wts/sum(wts)
all_wts[i,] <- wts
port_ret <- sum(wts * mean_ret)
port_ret <- ((port_ret + 1)^252) - 1
port_returns[i] <- port_ret
port_sd <- sqrt(t(wts) %*% (cov_mat %*% wts))
port_risk[i] <- port_sd
sr <- port_ret/port_sd
sharpe_ratio[i] <- sr
}portfolio_values <- tibble(Return = port_returns,
Risk = port_risk,
SharpeRatio = sharpe_ratio)
all_wts <- tk_tbl(all_wts)
colnames(all_wts) <- colnames(log_ret_xts)
portfolio_values <- tk_tbl(cbind(all_wts, portfolio_values))
datatable(portfolio_values)Variansi Minimum
library(forcats)
min_var <- portfolio_values[which.min(portfolio_values$Risk),]
p <- min_var %>%
gather(ASII.JK:UNVR.JK, 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 = 'Aset',
y = 'Bobot',
title = "Bobot Portofolio dengan Variansi Minimum") +
scale_y_continuous(labels = scales::percent) +
theme(legend.position = "none" )
ggplotly(p)Berdasarkan dengan data bahwa didapatkan Mayoritas Portofolio diinvestasikan pada saham TELKOM dan Bank Central Asia.
Portofolio Tangensi
max_sr <- portfolio_values[which.max(portfolio_values$SharpeRatio),]
p <- max_sr %>%
gather(ASII.JK:UNVR.JK, 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 = 'Aset',
y = 'Bobot',
title = "Bobot Portofolio Tangensi (Maksimum Sharpe Ratio)") +
scale_y_continuous(labels = scales::percent) +
theme(legend.position = "none" )
ggplotly(p)Portofolio dengan Ratio Sharpe tertinggi hanya memiliki sedikit investasi pada saham Unilever dan Astra International Bank Republik Indonesia dan Bank Central Asia
Batas Efisien Portofolio
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 = 'Risiko Tahunan',
y = 'Pengembalian Tahunan',
title = "Optimasi Portofolio & Perbatasan yang Efisien") +
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.25, y = 0.05, label = "Portofolio Tangensi") +
annotate('text', x = 0.274, y = 0.15, label = "Portofolio Varians minimum") +
annotate(geom = 'segment', x = 0.273, xend = 0.273, y = 0.09,
yend = 0.14, color = 'red', arrow = arrow(type = "open")) +
annotate(geom = 'segment', x = 0.2417, xend = 0.2417, y = 0.002,
yend = 0.04, color = 'red', arrow = arrow(type = "open"))
ggplotly(p)