==============================================================================

TERM PROJECT: RISK & UNCERTAINTY IN VIETNAM MARKET (MSN, FPT, HPG)

==============================================================================

1. SETUP & DATA COLLECTION

——————————————————————————

required_packages <- c(“quantmod”, “PerformanceAnalytics”, “ggplot2”, “boot”, “corrplot”) for (pkg in required_packages) { if (!require(pkg, character.only = TRUE)) install.packages(pkg) library(pkg, character.only = TRUE) }

Tickers: Masan (Consumer), FPT (Tech), Hoa Phat (Steel)

tickers <- c(“MSN.VN”, “FPT.VN”, “HPG.VN”) start_date <- “2021-01-01”

Fetch Data (Suppress warnings for cleaner output)

data_env <- new.env() suppressWarnings( getSymbols(tickers, src = “yahoo”, from = start_date, env = data_env) )

Merge Adjusted Prices

prices <- do.call(merge, eapply(data_env, Ad)) colnames(prices) <- gsub(“.VN.Adjusted”, ““, colnames(prices))

Remove rows with missing data (Data Cleaning)

prices <- na.omit(prices)

Calculate Log Returns

returns <- na.omit(Return.calculate(prices, method = “log”))

——————————————————————————

2. DESCRIPTIVE STATISTICS & VISUALIZATION

——————————————————————————

A. Time Series Plot

plot(prices, main = “3-Year Price History: MSN, FPT, HPG”, legend.loc = “topleft”)

B. BoxPlots (To check for fat tails/outliers)

chart.Boxplot(returns, main = “Return Distributions (BoxPlot)”, colorset = c(“purple”, “green”, “orange”))

C. Correlation Matrix

cor_matrix <- cor(returns) print(“Correlation Matrix:”) print(cor_matrix) corrplot(cor_matrix, method = “number”, type = “upper”, title = “Correlations”, mar = c(0,0,2,0))

——————————————————————————

3. CER MODEL ESTIMATION (with Standard Errors)

——————————————————————————

We estimate Mean and Volatility, plus their Standard Errors.

estimate_cer <- function(x) { n <- length(x) mu_hat <- mean(x) sigma_hat <- sd(x) se_mu <- sigma_hat / sqrt(n) se_sigma <- sigma_hat / sqrt(2 * n) return(c(Mean = mu_hat, SD = sigma_hat, SE_Mean = se_mu, SE_SD = se_sigma)) }

cer_results <- apply(returns, 2, estimate_cer) print(“CER Model Estimates (Mean, SD, and SE):”) print(t(cer_results))

——————————————————————————

4. BOOTSTRAP VaR (Risk Management with Sampling Error)

——————————————————————————

We analyze HPG (usually the most volatile) to find the 95% Confidence Interval for VaR.

var_boot_fn <- function(data, indices) { sample_data <- data[indices] quantile(sample_data, probs = 0.05) }

set.seed(123) boot_results <- boot(data = as.numeric(returns[, “HPG”]), statistic = var_boot_fn, R = 1000)

print(“Bootstrap VaR Results (HPG):”) print(boot_results)

95% Confidence Interval

boot_ci <- boot.ci(boot_results, type = “perc”) print(“VaR 95% Confidence Interval:”) print(boot_ci)

Plot Sampling Distribution

plot(boot_results)