Dữ liệu thu thập là giá đóng cửa có điều chỉnh của mã chứng khoán FLC trong khoảng thời gian từ ngày 06/07/2016 đến 21/04/2017 được thu thập từ trang chủ của Vndirect:
Chia tập dữ liệu thành 2 phần là train và test theo tỷ lệ 9:1. Trong đó tập train sẽ chiếm 90% số quan sát ngày đầu tiên và test chiếm 10% số quan sát những ngày cuối cùng. Tập train được sử dụng để hồi qui model và tập test được dùng để kiểm định tính chính xác của model.
## PRICE
## 2016-07-06 6.0
## 2016-07-07 6.1
## 2016-07-08 6.0
## 2016-07-11 5.9
## 2016-07-12 5.9
## 2016-07-13 6.0
## [1] "6 quan sat dau tien test data"
## PRICE
## 2017-04-14 7.58
## 2017-04-17 7.55
## 2017-04-18 7.80
## 2017-04-19 7.49
## 2017-04-20 7.52
## 2017-04-21 7.64
## [1] "Kich thuoc mau test data"
## [1] 20
## [1] "6 quan sat dau tien cua train data"
## PRICE
## 2017-03-16 7.62
## 2017-03-17 7.65
## 2017-03-20 7.71
## 2017-03-21 8.24
## 2017-03-22 8.24
## 2017-03-23 8.20
## [1] "Kich thuoc mau train data"
## [1] 180
Bước 1: Tiền xử lý dữ liệu
Sử dụng kĩ thuật logarit để chuyển hóa chuỗi giá đóng cửa thành chuỗi lợi suất của FLC và loại bỏ các giá trị null value.
## [1] "Chuoi loi suat thuc te theo ngay"
## RETURN
## 2017-04-14 0.036269926
## 2017-04-17 -0.003965636
## 2017-04-18 0.032576170
## 2017-04-19 -0.040554936
## 2017-04-20 0.003997340
## 2017-04-21 0.015831465
Bước 2: Kiểm định tính dừng của chuỗi lợi suất
Kiểm định ADF về tính dừng của chuỗi
##
## Augmented Dickey-Fuller Test
##
## data: data.xts
## Dickey-Fuller = -6.1443, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
Kết quả p-value = 0.01 cho thấy chuỗi lợi suất có tính dừng. Vậy bậc sai phân của chuỗi lợi suất trong ARIMA là d = 0.
Bước 3: Tìm bậc của trung bình trượt và bậc của tự hồi qui
Đồi thị của hệ số tự tương quan ACF và hệ số tự tương quan riêng phần PACF như bên dưới:
Dựa vào đồ thị ACF và PACF ta có thể thấy chuỗi lợi suất FLC có tự tương quan bậc 3 và trung bình trượt bậc 2
Vậy model ARIMA thích hợp để dự báo cho chuỗi lợi suất này là ARIMA(3,0,2)
Bước 4: Xây dựng model ARIMA
## [1] 180
## RETURN
## 2017-03-23 -0.00486619
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6764 -1.2141 0.1961 -1.4855 0.8611
## s.e. 0.1681 0.2001 0.0846 0.1595 0.1797
##
## sigma^2 estimated as 0.001022: log likelihood = 362.12, aic = -712.25
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001540075 0.03197148 0.02359469 NaN Inf 0.7726391
## ACF1
## Training set -0.005689595
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 180 -0.01351853 -0.07618148 0.04914441
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6715 -1.2075 0.1951 -1.4802 0.8545
## s.e. 0.1833 0.2179 0.0857 0.1774 0.2024
##
## sigma^2 estimated as 0.001017: log likelihood = 364.62, aic = -717.25
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001573969 0.03188893 0.02349659 NaN Inf 0.7735715
## ACF1
## Training set -0.005849982
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 181 -0.009830674 -0.07233182 0.05267047
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6498 -1.1815 0.1929 -1.4562 0.8263
## s.e. 0.2835 0.3330 0.0964 0.2886 0.3312
##
## sigma^2 estimated as 0.001016: log likelihood = 366.77, aic = -721.55
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001721851 0.0318724 0.0234914 NaN Inf 0.7742533
## ACF1
## Training set -0.007207717
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 182 0.002125982 -0.06034277 0.06459473
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6374 -1.1686 0.1919 -1.4418 0.8109
## s.e. 0.3109 0.3596 0.1021 0.3160 0.3513
##
## sigma^2 estimated as 0.001011: log likelihood = 369.27, aic = -726.53
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001762292 0.03179256 0.02339665 NaN Inf 0.7743577
## ACF1
## Training set -0.008207293
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 183 0.004806297 -0.05750598 0.06711858
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6217 -1.1425 0.1863 -1.4270 0.7857
## s.e. 0.2600 0.3043 0.0957 0.2601 0.2919
##
## sigma^2 estimated as 0.001009: log likelihood = 371.46, aic = -730.92
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0016289 0.03176626 0.0233839 NaN Inf 0.773618 -0.008228125
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 184 -0.0006593134 -0.06292004 0.06160141
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6116 -1.1279 0.1852 -1.415 0.7681
## s.e. 0.2413 0.2746 0.0956 0.237 0.2531
##
## sigma^2 estimated as 0.001004: log likelihood = 373.95, aic = -735.9
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001579423 0.03168758 0.0232855 NaN Inf 0.7729241
## ACF1
## Training set -0.008604783
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 185 0.003250902 -0.05885561 0.06535741
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 2.0289 -1.3713 0.2827 -1.9086 0.9999
## s.e. 0.0702 0.1299 0.0709 0.0266 0.0274
##
## sigma^2 estimated as 0.0009268: log likelihood = 380.92, aic = -749.85
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001321776 0.03044407 0.02263067 NaN Inf 0.7534514
## ACF1
## Training set -0.006227163
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 186 0.01752146 -0.04244415 0.07748708
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 2.0287 -1.3712 0.2833 -1.9088 1.0000
## s.e. 0.0700 0.1296 0.0706 0.0264 0.0273
##
## sigma^2 estimated as 0.0009218: log likelihood = 383.48, aic = -754.97
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001330916 0.03036151 0.02253281 NaN Inf 0.7518033
## ACF1
## Training set -0.006158049
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 187 0.02172899 -0.03807316 0.08153115
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6303 -1.1490 0.1894 -1.4343 0.7876
## s.e. 0.2323 0.2776 0.0904 0.2305 0.2696
##
## sigma^2 estimated as 0.0009892: log likelihood = 381.44, aic = -750.87
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001635818 0.03145212 0.02302467 NaN Inf 0.7704962
## ACF1
## Training set -0.008075105
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 188 -0.000332825 -0.06197784 0.06131219
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6383 -1.1643 0.1914 -1.4434 0.805
## s.e. 0.2587 0.3086 0.0913 0.2605 0.307
##
## sigma^2 estimated as 0.000986: log likelihood = 383.78, aic = -755.55
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001506278 0.03140043 0.02303587 NaN Inf 0.7710231
## ACF1
## Training set -0.00683028
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 189 -0.009517244 -0.07106096 0.05202647
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 2.0334 -1.3696 0.2760 -1.9089 0.9999
## s.e. 0.0697 0.1292 0.0702 0.0272 0.0281
##
## sigma^2 estimated as 0.0009195: log likelihood = 390.03, aic = -768.05
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0009312524 0.03032261 0.02251702 NaN Inf 0.7567416
## ACF1
## Training set -0.001164984
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 190 0.01289998 -0.04681578 0.07261575
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 2.0351 -1.3711 0.2759 -1.9089 0.9999
## s.e. 0.0694 0.1288 0.0700 0.0273 0.0283
##
## sigma^2 estimated as 0.0009152: log likelihood = 392.55, aic = -773.11
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0008768605 0.03025152 0.02243538 NaN Inf 0.7556953
## ACF1
## Training set -0.001301873
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 191 0.01339244 -0.04618205 0.07296693
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6590 -1.1871 0.1939 -1.4664 0.8313
## s.e. 0.2171 0.2608 0.0844 0.2175 0.2624
##
## sigma^2 estimated as 0.0009729: log likelihood = 391.16, aic = -770.33
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001405024 0.03119201 0.02287257 NaN Inf 0.7712696
## ACF1
## Training set -0.005130702
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 192 -0.003207876 -0.06434309 0.05792734
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 2.0458 -1.3753 0.2730 -1.9096 1.0000
## s.e. 0.0698 0.1299 0.0705 0.0322 0.0334
##
## sigma^2 estimated as 0.0009303: log likelihood = 395.17, aic = -778.34
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0003327668 0.03050073 0.02260607 NaN Inf 0.7613271
## ACF1
## Training set 0.002580121
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 193 -0.005214542 -0.06527928 0.05485019
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6892 -1.2123 0.2087 -1.4844 0.8336
## s.e. 0.1810 0.2215 0.0814 0.1777 0.2157
##
## sigma^2 estimated as 0.0009808: log likelihood = 394.48, aic = -776.96
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0009587829 0.03131757 0.02302112 NaN Inf 0.7762241
## ACF1
## Training set -0.003816505
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 194 0.001541972 -0.05983934 0.06292328
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6957 -1.2251 0.2061 -1.4985 0.8570
## s.e. 0.1501 0.1839 0.0789 0.1413 0.1709
##
## sigma^2 estimated as 0.0009815: log likelihood = 396.43, aic = -780.86
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001132705 0.03132885 0.02312122 NaN Inf 0.7741571
## ACF1
## Training set -0.001072994
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 195 0.01817969 -0.04322373 0.07958311
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 2.0455 -1.3758 0.2735 -1.9096 1.0000
## s.e. 0.0686 0.1282 0.0698 0.0320 0.0332
##
## sigma^2 estimated as 0.0009277: log likelihood = 401.64, aic = -791.28
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0003272964 0.0304582 0.02264742 NaN Inf 0.7569384
## ACF1
## Training set 0.004344951
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 196 -0.01213106 -0.07210927 0.04784715
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6942 -1.2193 0.1990 -1.5029 0.8624
## s.e. 0.1411 0.1719 0.0786 0.1294 0.1512
##
## sigma^2 estimated as 0.0009781: log likelihood = 400.86, aic = -789.72
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001161678 0.03127419 0.0231374 NaN Inf 0.7724381
## ACF1
## Training set -0.002403614
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 197 0.005842414 -0.05545387 0.0671387
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6848 -1.2123 0.196 -1.4988 0.8636
## s.e. 0.1437 0.1766 0.078 0.1334 0.1646
##
## sigma^2 estimated as 0.0009839: log likelihood = 402.33, aic = -792.66
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0009234295 0.03136665 0.02326776 NaN Inf 0.7711191
## ACF1
## Training set -0.00371644
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 198 -0.01554528 -0.07702279 0.04593223
##
## Forecast method: ARIMA(3,0,2) with zero mean
##
## Model Information:
##
## Call:
## arima(x = stock_train, order = c(3, 0, 2), include.mean = FALSE)
##
## Coefficients:
## ar1 ar2 ar3 ma1 ma2
## 1.6837 -1.2031 0.1941 -1.5003 0.8574
## s.e. 0.1425 0.1747 0.0779 0.1316 0.1607
##
## sigma^2 estimated as 0.0009809: log likelihood = 404.68, aic = -797.36
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001020018 0.03132008 0.02322684 NaN Inf 0.7679055
## ACF1
## Training set -0.005371666
##
## Forecasts:
## Point Forecast Lo 95 Hi 95
## 199 -0.00572295 -0.06710919 0.05566328
Độ lệch tiêu chuẩn của các hệ số ước lượng trong mức giới hạn chấp nhận được. Giá trị AIc tương đối thấp cho thấy model có tính chính xác cao. Chỉ số sai số toàn phương RMSE và bình quân sai số tuyệt đối MAE đều thấp là một tín hiệu đáng mừng khi dự báo không chệch nhiều so với thực tế.
Bước 5: Tính toán độ chính xác của model ARIMA
Bằng cách so sánh giá trị dự báo với giá trị thực tế của chuỗi lợi suất trên tập test data chúng ta sẽ biết được mức độ chính xác dự báo của ARIMA.
## Actual_series FORECAST Accuracy
## 2017-03-24 -0.006116227 -0.0135185346 1
## 2017-03-27 0.018237588 -0.0098306736 0
## 2017-03-28 0.010785006 0.0021259822 1
## 2017-03-29 -0.021687597 0.0048062974 0
## 2017-03-30 -0.009792000 -0.0006593134 1
## 2017-03-31 0.003683245 0.0032509024 1
## 2017-04-03 0.021819047 0.0175214619 1
## 2017-04-04 0.008358258 0.0217289920 1
## 2017-04-05 -0.020421130 -0.0003328250 1
## 2017-04-07 -0.013439420 -0.0095172438 1
## 2017-04-10 0.003683245 0.0128999818 1
## 2017-04-11 -0.019802627 0.0133924435 0
## 2017-04-12 -0.056570351 -0.0032078762 1
## 2017-04-13 -0.033627916 -0.0052145425 1
## 2017-04-14 0.036269926 0.0015419715 1
## 2017-04-17 -0.003965636 0.0181796864 0
## 2017-04-18 0.032576170 -0.0121310573 0
## 2017-04-19 -0.040554936 0.0058424142 0
## 2017-04-20 0.003997340 -0.0155452819 0
## 2017-04-21 0.015831465 -0.0057229503 0
## [1] 60
Như vậy model dự báo chính xác được 60% số trường hợp tăng, giảm của mã FLC. Và từ model ta có thể thấy tỷ suất lợi nhuận của 3 ngày hôm trước và ngày hôm qua có tác động mạnh lên tỷ suất lợi nhuận của mức giá hiện tại. Khoảng tin cậy 95% ôm trọn đường tỷ suất lợi nhuận là một khoảng biến động lợi nhuận rất chính xác.