import dữ liệu

vn30 <- read.csv("D:/BAI TAP/MOHINHDINHGIATAISANTAICHINH1/VN30.csv")

thống kê mô tả

library(psych)
d_table <- describe(vn30)
print(d_table)
##        vars   n    mean    sd  median trimmed   mad     min     max  range
## Date*     1 250  125.50 72.31  125.50  125.50 92.66    1.00  250.00 249.00
## VN30      2 250 1283.21 51.41 1293.25 1288.83 41.34 1131.64 1362.89 231.25
## lnVN30    3 250    7.16  0.04    7.16    7.16  0.03    7.03    7.22   0.19
##         skew kurtosis   se
## Date*   0.00    -1.21 4.57
## VN30   -0.91     0.24 3.25
## lnVN30 -0.99     0.41 0.00

kiểm định tính dừng

library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
adf_test <- adf.test(vn30$lnVN30, alternative = "stationary")
print(adf_test)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  vn30$lnVN30
## Dickey-Fuller = -3.3215, Lag order = 6, p-value = 0.0681
## alternative hypothesis: stationary

tính log return và bỏ đi phần NA

lnreturn <- diff(log(vn30$VN30))
lnreturn <- na.omit(lnreturn)

ước lượng độ lệch chuẩn

stdev <- sd(lnreturn,na.rm = TRUE)

ước lượng giá trị kỳ vọng

mu <- mean(lnreturn,na.rm=TRUE)

ước lượng độ dao động

sigma <- stdev/sqrt(250/250)

số bước mô phỏng

n <- length(vn30$lnVN30) # số ngày mô phỏng
T <- 1 # kỳ hạn
dt <- T/n # khoảng thời gian giữa các bước

mô phỏng

simulated_lnprice <- numeric(n)
simulated_lnprice[1] <- vn30$lnVN30[n]  # Giá bắt đầu là giá cuối cùng trong chuỗi dữ liệu

mu <- as.numeric(mu)
sigma <- as.numeric(sigma)
dt <- as.numeric(dt)

set.seed(123) 

for (i in 2:n) {
  simulated_lnprice[i] <- simulated_lnprice[i-1] * exp((mu - 0.5 * sigma^2) * dt + sigma * sqrt(dt) * rnorm(1))
}



simulated <- data.frame(date = rep(NA, 250), lnprice = rep(NA, 250)) # tạo dataframe rỗng với 2 cột và 250 dòng
simulatedlnprice <- as.data.frame(simulated_lnprice) # tạo một dataframe với 1 cột là chuỗi simulated_lnprice
colnames(simulatedlnprice) <- c("lnprice") # đổi tên cột thành lnprice

simulated$lnprice <- simulatedlnprice$lnprice #thêm cột lnprice vào simulated
simulated$date <- vn30$Date # thêm ngày
simulated$price <- exp(simulated$lnprice) # ước lượng được giá trị của chuỗi VN30 nhờ lnprice
simulated$difference <- ((abs(simulated$price) - abs(vn30$VN30))/abs(vn30$VN30))*100 # tính toán độ chênh lệch giữa mô phỏng và thực tế

đưa dữ liệu về dạng ngày

vn30$Date <- as.Date(vn30$Date, format = "%m/%d/%Y")
simulated$date <- as.Date(simulated$date, format = "%m/%d/%Y")

đồ thị giá vn30 mô phỏng

ggplot(simulated, aes(x = date, y = price, group = 1)) +
  geom_line(color = "navy") +
  labs(title = "Giá VN30 mô phỏng",x = "Ngày", y = "Giá") +
  theme_classic() +
  theme(text = element_text(family = "Times New Roman"), plot.title = element_text(hjust = 0.5)) +
  scale_x_date(date_labels = "%b-%Y")

đồ thị giá vn30 thực tế

ggplot(vn30, aes(x = Date, y = VN30, group = 1)) +
  geom_line(color = "red")+
  labs(title = "Gi VN30 thc t", x = "Ngy", y = "Gi") +
  theme_classic() +
  theme(text = element_text(family = "Times New Roman"), plot.title = element_text(hjust = 0.5)) +
  scale_x_date(date_labels = "%b-%Y")

đồ thị mức chênh lệch của giá mô phỏng so với thực tế

ggplot(simulated, aes(x = date, y = difference, group = 1)) +
  geom_line(color = "darkslategray") +
  labs(title = "Độ lệch của giá mô phỏng so với thực tế", x = "Ngày", y = "Độ chênh lệch (%)") +
  theme_classic() +
  theme(text = element_text(family = "Times"), plot.title = element_text(hjust = 0.5)) +
  scale_x_date(date_labels = "%b-%Y") +
  ylim(min(simulated$difference) - 1, max(simulated$difference) + 1)
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## not found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## not found in Windows font database
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database