Mid-Test
Pemodelan dan Teori Risiko
| Kontak | : \(\downarrow\) |
| mugemisausan05@gmail.com | |
| https://www.instagram.com/saram.05/ | |
| RPubs | https://rpubs.com/sausanramadhani/ |
CASE STUDY 1
Consider a portfolio consisting of five stocks from different sectors in Indonesia and analyze the diversification benefits of combining stocks. Your answer should cover at least the following steps:
1.1 Downloading Stock Data
Use quantmod for downloading stock data
Data saham yang akan
dianalisis adalah sebagai berikut : * PT Astra International Tbk
(ASII.JK) : Sektor Otomotif dan Manufaktur Astra International
* PT
Bank Central Asia Tbk (BBCA.JK) : Sektor Perbankan
* PT
Telekomunikasi Indonesia Tbk (TLMK.JK) : Sektor Telekomunikasi
* PT
Indofood Sukses Makmur (INDF.JK) : Sektor Makanan dan Minuman
* PT
Adaro Energy Tbk (ADRO.JK) : Sektor Energi dan Pertambangan
library(quantmod)
library(plotly)
library(PerformanceAnalytics)
data_num1 <- c("ASII.JK", "BBCA.JK", "TLKM.JK", "INDF.JK", "ADRO.JK")
num1_labels <- c("Astra International", "Bank Central Asia", " Telekomunikasi Indonesia", "Indofood Sukses Makmur", "Adaro Energy")
start_date <- "2023-01-01"
end_date <- "2024-01-01"
getSymbols(data_num1, from = start_date, to = end_date, src = "yahoo", adjust = FALSE)## [1] "ASII.JK" "BBCA.JK" "TLKM.JK" "INDF.JK" "ADRO.JK"
1.2 Calculate Daily Return
Define a function calculate_returns() to calculate daily
returns from the closing prices of the stocks and apply this function to
each stock’s price series
1.3 Combine The Returns
Combine the returns of all stocks into a single data frame
1.4 Calculate The Correlation Matrix
Calculate the correlation matrix of returns using cor()
## ASII.JK.Close BBCA.JK.Close TLKM.JK.Close INDF.JK.Close
## ASII.JK.Close 1.00000000 0.12210027 0.11135901 0.02018793
## BBCA.JK.Close 0.12210027 1.00000000 0.14746890 0.11497019
## TLKM.JK.Close 0.11135901 0.14746890 1.00000000 -0.03662988
## INDF.JK.Close 0.02018793 0.11497019 -0.03662988 1.00000000
## ADRO.JK.Close 0.16497197 0.04255736 0.06603958 -0.05869760
## ADRO.JK.Close
## ASII.JK.Close 0.16497197
## BBCA.JK.Close 0.04255736
## TLKM.JK.Close 0.06603958
## INDF.JK.Close -0.05869760
## ADRO.JK.Close 1.00000000
1.5 Visualize The Correlation Matrix
Visualize the correlation matrix using corrplot()
1.6 Calculate The Portfolio’s Returns and SD
Calculate the portfolio’s returns and standard deviation based on
equal weights for each stock
num_data <- length(closing_prices)
weights <- rep(1/num_data, num_data)
port_returns <- rowSums(closing_prices * weights)
mean(port_returns)## [1] 23.73246
## [1] 0.7112171
It is known that the average portfolio return is 23.73% and the portfolio standard deviation is 0.71%.
1.7 Calculate The Diversification Ratio
Calculate the diversification ratio, which measures the extent to
which the portfolio’s risk is reduced due to diversification
## [1] 0.7112171
The diversification ratio value obtained is 0.7112171. This shows that the portfolio has a good level of diversification approaching the optimality of perfect diversification (1).
1.8 Define The Objective Function
Define the objective function and constraints for portfolio
optimization. Here, we aim to maximize the portfolio return subject to
the constraint that the sum of portfolio weights equals 1.
library(quadprog)
library(Matrix)
expected_ret <- c(0.085, 0.085, 0.085, 0.085, 0.085)
cov_matrix <- matrix(cormat, nrow = 5)
pd_D_mat <- nearPD(cov_matrix)
D_mat <- as.matrix(pd_D_mat$mat)
d_vec <- rep(0, length(expected_ret))
A_mat <- cbind(rep(1, length(expected_ret)), diag(length(expected_ret)))
b_vec <- c(1, d_vec)1.9 Find The Optimal Portfolio Weights
Find the optimal portfolio weights that maximize the portfolio return
given the covariance matrix and constraints.
output <- solve.QP(Dmat = D_mat, dvec = d_vec, Amat = A_mat, bvec = b_vec, meq = 1)
optimal_weights <- output$solution
optimal_weights## [1] 0.1675628 0.1626603 0.2030723 0.2490938 0.2176108
1.10 Visualize The Optimal Weight
Visualize the optimal weights assigned to each stock in the portfolio
viz_optimal_weights <- plot_ly(labels = num1_labels, values = optimal_weights, type = "pie", marker = list(colors = c("#B0C5A4", "#D37676", "#EBC49F", "#F1EF99", "#8C6A5D"))) %>%
layout(title = "Optimized Portfolio Weight", xaxis = list(title = ""), yaxis = list(title = ""))
viz_optimal_weightsJudging from the pie chart, the largest optimal portfolio weight results are found in Indofood Sukses Makmur shares at 24.9%. Then followed by 21.8%, namely Adaro Energy shares. The lowest occurred at Bank Central Asia at 16.3%.
1.11 Calculate The Expected Return & Volatility
Calculate the expected return and volatility of the optimized
portfolio using the optimal weights and covariance matrix
## [1] 0.085
optimal_portfolio_volatility <- sqrt(t(optimal_weights) %*%
cov_matrix %*%
optimal_weights)
optimal_portfolio_volatility## [,1]
## [1,] 0.500965
The calculation results show that the optimal portfolio calculated has optimal portfolio return of 8.5% and optimal portfolio volatility of 50.1%. These results provide an overview of the expected performance and risk of a portfolio that has been optimized according to the specified criteria.
CASE STUDY 2
Assume you are working as an Actuary at an Insurance Company. Your job is to define insurance claims data and analyze the risk associated with different scenarios. Consider explaining your answer in details as the following instructions:
2.1 Generate Insurance Claims Data
Generate insurance claims data for a hypothetical insurance company with 10000 policies over 14 years.
set.seed(123)
policies <- 10000
years <- 14
data_num2 <- data.frame(Policy_ID = rep(1:policies, each = years),
Year = rep(1:years, times = policies))
data_num22.3 Simulate Claim Frequencies
Simulate claim frequencies for each policy from a Poisson distribution with a mean of 0.1 (i.e., on average, each policy has 0.1 claims per year).
claim_frequency_mean <- 0.1
data_num2$Claim_Frequency <- rpois(n = nrow(data_num2), lambda = claim_frequency_mean)
data_num22.4 Simulate Claim Amounts
Simulate claim amounts for each policy and each year from a normal distribution with a mean of 5004 and a standard deviation of 2004.
claim_amount_mean <- 5004
claim_amount_sd <- 2004
data_num2$Claim_Amount <- rnorm(n = nrow(data_num2), mean = claim_amount_mean, sd = claim_amount_sd)
data_num22.5 Calculate Total Claim & Loss Ratios
Calculate total claim amounts per policy and then calculate loss ratios (total claims divided by premiums) to assess the risk associated with each policy.
data_num2$Total_Claim_Amount <- data_num2$Claim_Frequency * data_num2$Claim_Amount
data_num2$Loss_Ratio <- data_num2$Total_Claim_Amount / data_num2$Premium
head(data_num2)2.6 Plot A Histogram (Loss Ratios)
Plot a histogram of the loss ratios to visualize the distribution of risk.
hist(data_num2$Loss_Ratio, breaks = 30, col = "#A8CD9F", main = "Histogram Loss Ratios", xlab = "Loss Ratio", ylab = "Frequency")2.7 Calculate Summary Statistics
Calculate summary statistics including the mean, median, and 95th percentile of the loss ratios.
## [1] 0.2613787
## [1] 0
## 95%
## 1.540493
Based on the calculations above, the average value (mean) loss ratio is around 1.05. This shows that overall, the average proportion of claims to insurance premiums is around 1,05%. However, the median loss ratio value of 0 indicates that the distribution of loss ratios tends towards low values, even up to 0. This shows that most insurance policies have low loss ratios or even no claims. Meanwhile, the 95th percentile loss ratio is around 1.51, which shows that a small percentage of insurance policies have a fairly high loss ratio. Thus, even though the average loss ratio tends to be positive, in the distribution of insurance claims, most of the policies have a low loss ratio, and there are even policies that have a high loss ratio.