UAS Optimasi

Tugas UAS Optimasi


*Kontak : \(\downarrow\)*
Email
Instagram https://www.instagram.com/m_naufalardiansyah/
RPubs https://rpubs.com/muhamad_naufal/

*Library

library(lpSolve)
## Warning: package 'lpSolve' was built under R version 4.2.1
library(tidyquant)
## Warning: package 'tidyquant' was built under R version 4.2.2
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Warning: package 'PerformanceAnalytics' was built under R version 4.2.2
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(plotly)
## Warning: package 'plotly' was built under R version 4.2.2
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(timetk)
## Warning: package 'timetk' was built under R version 4.2.2
library(DT)
## Warning: package 'DT' was built under R version 4.2.1
library(tidyr)
library(forcats)
## Warning: package 'forcats' was built under R version 4.2.1

**

Buatlah contoh kasus Optimisasi Program Linear dalam Industri atau bisnis!

contoh Kasus

Sebuah bisnis kecil menjual dua produk, yaitu Produk 1 dan Produk 2. Setiap ton Produk 1 menghabiskan 30 jam kerja, dan setiap ton Produk 2 menghabiskan 20 jam kerja. Bisnis ini memiliki maksimum 2700 jam kerja untuk periode yang dipertimbangkan. Sedangkan untuk jam kerja mesin, setiap ton Produk 1 dan 2 masing-masing menghabiskan 5 dan 10 jam kerja mesin. Ada 850 jam mesin yang tersedia.

Setiap ton Produk 1 menghasilkan laba 20 dollar , sedangkan Produk 2 menghasilkan 60 dollar untuk setiap ton yang terjual. Untuk alasan teknis, perusahaan harus memproduksi minimal 95 ton secara total di antara kedua produk. Kita perlu mengetahui berapa ton Produk 1 dan 2 yang harus diproduksi untuk memaksimalkan total laba.

Variabel

maka diketahui untuk fungsi kendala: 1. x jumlah ton yang diproduksi dan dijual dari Produk 1 2. y jumlah ton yang diproduksi dan dijual dari produk 2

fungsi tujuan

Koefisien biaya dari variabel-variabel ini masing-masing adalah 20 dan 60. Oleh karena itu, fungsi tujuan didefinisikan dengan mengalikan setiap variabel dengan koefisien biaya yang sesuai.

\[\begin{align*} maxZ=20x+60y \end{align*}\]

fungsi kendala

Kendala Jam Kerja membuat bahwa jumlah total jam kerja yang digunakan pada Produk 1 dan Produk 2, yang sama dengan 30x+20y, adalah kurang atau sama dengan 2.700 jam.

Kendala yang sama, membuat jam kerja bahwa jumlah total jam kerja mesin 5x+10y kurang atau sama dengan 850.

Kendala yang membuat bahwa total unit yang diproduksi dan dijual x+y lebih besar atau sama dengan 95

perhitungan

Menyatukan semua ini, dan mempertimbangkan bahwa variabel-variabel keputusan adalah non negatif, LP yang memaksimalkan keuntungan adalah:

\[\begin{align*} x & \geq 0\\ y & \geq 0\\ 30x+20y & \leq 2700\\ 5x+10y & \leq 850\\ x+y & \geq 95 \end{align*}\]
tuj = c(20,60)
var = matrix(c(30, 20, 5, 10, 1, 1), nrow = 3, byrow = TRUE)
arah = c("<=", "<=", ">=")
bat = c(2700, 850, 95)

hasil

lp("max", tuj, var, arah, bat)
## Success: the objective function is 4900
lp("max", tuj, var, arah, bat)$solution
## [1] 20 75

maka nilai maksimum keuntungan yang dapat diperoleh sebesar 4900 dollar

dengan:

\[\begin{align*} x = 20\\ y = 75 \end{align*}\]

Buatlah contoh kasus Optimisasi Program NonLinear dalam Industri atau bisnis!

f <- function(x, y) {
        x^2 + y^2 + 3* x * y
}
n <- 30
xpts <- seq(-1.5, 1.5, len = n)
ypts <- seq(-1.5, 1.5, len = n)
gr <- expand.grid(x = xpts, y = ypts)
feval <- with(gr, matrix(f(x, y), nrow = n, ncol = n))
par(mar = c(5, 4, 1, 1))
contour(xpts, ypts, feval, nlevels = 20, xlab = "x", ylab = "y")
points(-1, -1, pch = 19, cex = 2)
abline(h = -1)

Buatlah contoh kasus Optimisasi Portofolio dengan memilih 5 saham terbaik di Indonesia dengan cara melakukan analisis data keuangan terlebih dahulu pada saham LQ45!

data

tick <- c('BBCA.JK', 'GGRM.JK', 'JSMR.JK', 'INDF.JK', 'BBRI.JK')
price_data <- tq_get(tick,
                     from = '2020-01-01',
                     to = Sys.Date(),
                     get = 'stock.prices')
head(price_data)

pengembalian

log_ret_tidy <- price_data %>%
  group_by(symbol) %>%
  tq_transmute(select = adjusted,
               mutate_fun = periodReturn,
               period = 'daily',
               col_rename = 'ret',
               type = 'log')

datatable(log_ret_tidy)
log_ret_xts <- log_ret_tidy %>%
  spread(symbol, value = ret) %>%
  tk_xts()
## Warning: Non-numeric columns being dropped: date
## Using column `date` for date_var.
datatable(log_ret_xts)

Rata-rata pengembalian

mean_ret <- colMeans(log_ret_xts)
print(round(mean_ret, 4))
## BBCA.JK BBRI.JK GGRM.JK INDF.JK JSMR.JK 
##  0.0004  0.0003 -0.0014  0.0000 -0.0008

Matriks Kovariansi

cov_mat <- cov(log_ret_xts) * 252
print(round(cov_mat,4))
##         BBCA.JK BBRI.JK GGRM.JK INDF.JK JSMR.JK
## BBCA.JK  0.0824  0.0632  0.0348  0.0326  0.0471
## BBRI.JK  0.0632  0.1445  0.0449  0.0459  0.0611
## GGRM.JK  0.0348  0.0449  0.1318  0.0479  0.0437
## INDF.JK  0.0326  0.0459  0.0479  0.0974  0.0388
## JSMR.JK  0.0471  0.0611  0.0437  0.0388  0.1618

Penerapan Metode 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_risk

num_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)
## 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(log_ret_xts)

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.
datatable(portfolio_values)

Variansi Minimum

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

p <- min_var %>%
  gather(BBCA.JK:JSMR.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)

Portofolio Tangensi

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

p <- max_sr %>%
  gather(BBCA.JK:JSMR.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)

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 = 'black') +
  geom_point(aes(x = Risk, y = Return), data = max_sr, color = 'red') +
  annotate('text', x = 0.25, y = 0.10, label = "Portofolio Tangensi") +
  annotate('text', x = 0.255, y = -0.05, label = "Portofolio Varians minimum")
  

ggplotly(p)