VAR

VAR#

import os
os.chdir("../../")

import pandas as pd
import numpy as np
from statsmodels.tsa.vector_ar.vecm import VECM
from statsmodels.tsa.vector_ar.vecm import select_order
from scripts.python.tsa.mtsmodel import *
from scripts.python.tsa.ts_eval import *

import warnings
warnings.filterwarnings("ignore")
for country in ["palau", "samoa", "tonga", "solomon_islands", "vanuatu"]:
    var = VARPipeline(country=country, data=None,
                      var_name=["total", "seats_arrivals_intl"],
                      exog=["covid", "stringency_index", "covid"])
    var.read_and_merge()
    var.transform()
    var.varma_search()
    var.fit()
    var.get_fittedvalues()
    var.evaluate_models()
    
    pred_df = var.data[["date", "total", "pred_total"]]
    
    saved_pred_path = var.country_data_folder + "/model/var_" + str(country) + ".csv"
    pred_df.to_csv(saved_pred_path, encoding="utf-8")
Best P, Q, Trend Combination: (3, 1, 't')
                                  Statespace Model Results                                  
============================================================================================
Dep. Variable:     ['total', 'seats_arrivals_intl']   No. Observations:                   45
Model:                                  VARMAX(3,1)   Log Likelihood                -124.461
                                        + intercept   AIC                            302.923
Date:                              Thu, 08 Jun 2023   BIC                            351.703
Time:                                      20:22:04   HQIC                           321.108
Sample:                                           0                                         
                                               - 45                                         
Covariance Type:                                opg                                         
===================================================================================
Ljung-Box (L1) (Q):             0.30, 0.06   Jarque-Bera (JB):        233.13, 35.28
Prob(Q):                        0.58, 0.81   Prob(JB):                   0.00, 0.00
Heteroskedasticity (H):         0.06, 0.12   Skew:                      2.77, -0.34
Prob(H) (two-sided):            0.00, 0.00   Kurtosis:                  12.68, 7.28
                                  Results for equation total                                 
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
drift                         0.1532      0.152      1.008      0.313      -0.145       0.451
L1.total                      0.6935      2.006      0.346      0.730      -3.238       4.625
L1.seats_arrivals_intl        0.0215      2.741      0.008      0.994      -5.352       5.394
L2.total                     -0.2521      2.208     -0.114      0.909      -4.580       4.076
L2.seats_arrivals_intl       -0.2135      1.964     -0.109      0.913      -4.064       3.637
L3.total                     -0.0565      1.070     -0.053      0.958      -2.154       2.041
L3.seats_arrivals_intl       -0.1320      0.925     -0.143      0.887      -1.945       1.681
L1.e(total)                   0.0196      2.055      0.010      0.992      -4.008       4.047
L1.e(seats_arrivals_intl)    -0.8520      2.658     -0.321      0.749      -6.062       4.358
beta.covid                   -4.5075      4.081     -1.105      0.269     -12.506       3.491
beta.stringency_index         0.0191      0.059      0.322      0.747      -0.097       0.136
beta.covid                   -4.5075      4.081     -1.105      0.269     -12.506       3.491
                           Results for equation seats_arrivals_intl                          
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
drift                         0.2071      0.299      0.692      0.489      -0.380       0.794
L1.total                      0.3064      0.731      0.419      0.675      -1.126       1.738
L1.seats_arrivals_intl       -0.3916      1.027     -0.381      0.703      -2.405       1.622
L2.total                      0.0982      1.310      0.075      0.940      -2.469       2.665
L2.seats_arrivals_intl       -0.1935      1.288     -0.150      0.881      -2.717       2.330
L3.total                     -0.2270      0.373     -0.609      0.543      -0.957       0.503
L3.seats_arrivals_intl        0.0267      0.361      0.074      0.941      -0.681       0.735
L1.e(total)                  -0.0826      0.683     -0.121      0.904      -1.421       1.256
L1.e(seats_arrivals_intl)     0.1944      1.317      0.148      0.883      -2.386       2.775
beta.covid                   -4.9146      8.130     -0.605      0.546     -20.849      11.020
beta.stringency_index        -0.0049      0.057     -0.085      0.932      -0.117       0.107
beta.covid                   -4.9146      8.130     -0.605      0.546     -20.849      11.020
                                       Error covariance matrix                                        
======================================================================================================
                                         coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------------------------------
sqrt.var.total                         1.3587      0.529      2.568      0.010       0.322       2.396
sqrt.cov.total.seats_arrivals_intl     1.1035      0.298      3.704      0.000       0.520       1.687
sqrt.var.seats_arrivals_intl           0.6617      0.332      1.991      0.047       0.010       1.313
======================================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
[2] Covariance matrix is singular or near-singular, with condition number 1.24e+17. Standard errors may be unstable.
MSE RMSE MAE SMAPE
naive 1.298621e+06 1139.570483 677.477273 53.868245
mean 1.219030e+07 3491.460526 3125.890370 130.888109
VAR (scaled) 1.127006e+06 1061.605449 554.935555 38.243616
Best P, Q, Trend Combination: (1, 1, 'c')
                                  Statespace Model Results                                  
============================================================================================
Dep. Variable:     ['total', 'seats_arrivals_intl']   No. Observations:                   45
Model:                                  VARMAX(1,1)   Log Likelihood                -150.021
                                        + intercept   AIC                            338.041
Date:                              Thu, 08 Jun 2023   BIC                            372.368
Time:                                      20:23:31   HQIC                           350.838
Sample:                                           0                                         
                                               - 45                                         
Covariance Type:                                opg                                         
===================================================================================
Ljung-Box (L1) (Q):             0.26, 0.21   Jarque-Bera (JB):        303.19, 83.91
Prob(Q):                        0.61, 0.65   Prob(JB):                   0.00, 0.00
Heteroskedasticity (H):         0.60, 1.22   Skew:                      3.16, -0.89
Prob(H) (two-sided):            0.33, 0.70   Kurtosis:                  14.03, 9.45
                                  Results for equation total                                 
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
intercept                     0.7106      2.797      0.254      0.799      -4.771       6.192
L1.total                      0.4323      0.911      0.475      0.635      -1.353       2.218
L1.seats_arrivals_intl        0.0696      0.881      0.079      0.937      -1.658       1.797
L1.e(total)                   0.4995      0.887      0.563      0.573      -1.239       2.238
L1.e(seats_arrivals_intl)    -0.9557      1.397     -0.684      0.494      -3.694       1.783
beta.covid                   -0.8085      4.216     -0.192      0.848      -9.071       7.454
beta.stringency_index        -0.0890      0.120     -0.741      0.459      -0.324       0.146
beta.covid                   -0.8085      4.216     -0.192      0.848      -9.071       7.454
                           Results for equation seats_arrivals_intl                          
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
intercept                     3.0195      1.317      2.293      0.022       0.439       5.600
L1.total                     -0.1394      0.746     -0.187      0.852      -1.602       1.323
L1.seats_arrivals_intl       -0.3394      0.709     -0.479      0.632      -1.729       1.050
L1.e(total)                   0.2423      0.605      0.400      0.689      -0.944       1.429
L1.e(seats_arrivals_intl)     0.2607      0.714      0.365      0.715      -1.138       1.659
beta.covid                   -1.5307      2.774     -0.552      0.581      -6.968       3.906
beta.stringency_index        -0.1142      0.079     -1.450      0.147      -0.268       0.040
beta.covid                   -1.5307      2.774     -0.552      0.581      -6.968       3.906
                                       Error covariance matrix                                        
======================================================================================================
                                         coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------------------------------
sqrt.var.total                         2.0024      0.816      2.453      0.014       0.403       3.602
sqrt.cov.total.seats_arrivals_intl     1.2602      0.397      3.177      0.001       0.483       2.038
sqrt.var.seats_arrivals_intl           0.8412      0.346      2.435      0.015       0.164       1.518
======================================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
[2] Covariance matrix is singular or near-singular, with condition number 2.04e+17. Standard errors may be unstable.
MSE RMSE MAE SMAPE
naive 7.970104e+06 2823.137282 1518.795455 49.120902
mean 4.747392e+07 6890.131975 6027.062716 155.537953
VAR (scaled) 1.029089e+07 3207.941282 1757.557409 141.614714
Best P, Q, Trend Combination: (2, 2, 't')
                                  Statespace Model Results                                  
============================================================================================
Dep. Variable:     ['total', 'seats_arrivals_intl']   No. Observations:                   36
Model:                                  VARMAX(2,2)   Log Likelihood                -114.995
                                        + intercept   AIC                            283.990
Date:                              Thu, 08 Jun 2023   BIC                            326.745
Time:                                      20:24:49   HQIC                           298.913
Sample:                                           0                                         
                                               - 36                                         
Covariance Type:                                opg                                         
===================================================================================
Ljung-Box (L1) (Q):             0.00, 0.55   Jarque-Bera (JB):         296.03, 0.50
Prob(Q):                        0.97, 0.46   Prob(JB):                   0.00, 0.78
Heteroskedasticity (H):         0.23, 1.42   Skew:                       2.89, 0.18
Prob(H) (two-sided):            0.02, 0.55   Kurtosis:                  15.80, 2.54
                                  Results for equation total                                 
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
drift                         0.0814      0.471      0.173      0.863      -0.842       1.005
L1.total                      0.3433      1.638      0.210      0.834      -2.868       3.555
L1.seats_arrivals_intl        0.0070      2.680      0.003      0.998      -5.246       5.260
L2.total                     -0.0883      2.361     -0.037      0.970      -4.717       4.540
L2.seats_arrivals_intl       -0.0139      2.239     -0.006      0.995      -4.402       4.375
L1.e(total)                  -0.0268      8.060     -0.003      0.997     -15.823      15.770
L1.e(seats_arrivals_intl)     0.1534      7.545      0.020      0.984     -14.634      14.940
L2.e(total)                  -0.2397      6.816     -0.035      0.972     -13.599      13.120
L2.e(seats_arrivals_intl)     0.3266      6.178      0.053      0.958     -11.782      12.435
beta.covid                   -1.9506     12.650     -0.154      0.877     -26.745      22.844
beta.stringency_index        -0.0681      0.214     -0.318      0.750      -0.487       0.351
beta.covid                   -1.9506     12.650     -0.154      0.877     -26.745      22.844
                           Results for equation seats_arrivals_intl                          
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
drift                         0.1251      0.377      0.332      0.740      -0.613       0.863
L1.total                      0.3466      4.524      0.077      0.939      -8.520       9.214
L1.seats_arrivals_intl        0.3757      2.121      0.177      0.859      -3.782       4.533
L2.total                     -0.4958      3.786     -0.131      0.896      -7.916       6.925
L2.seats_arrivals_intl       -0.0737      3.150     -0.023      0.981      -6.247       6.100
L1.e(total)                  -0.0065     13.351     -0.000      1.000     -26.174      26.161
L1.e(seats_arrivals_intl)    -0.4660     12.451     -0.037      0.970     -24.870      23.938
L2.e(total)                   0.5867      9.535      0.062      0.951     -18.101      19.274
L2.e(seats_arrivals_intl)    -0.1605      8.794     -0.018      0.985     -17.397      17.076
beta.covid                   -2.3151     11.317     -0.205      0.838     -24.496      19.866
beta.stringency_index        -0.0381      0.209     -0.183      0.855      -0.447       0.371
beta.covid                   -2.3151     11.317     -0.205      0.838     -24.496      19.866
                                       Error covariance matrix                                        
======================================================================================================
                                         coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------------------------------
sqrt.var.total                         1.6572      0.897      1.847      0.065      -0.102       3.416
sqrt.cov.total.seats_arrivals_intl     1.6108      2.921      0.551      0.581      -4.115       7.337
sqrt.var.seats_arrivals_intl           0.9357      8.905      0.105      0.916     -16.518      18.389
======================================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
[2] Covariance matrix is singular or near-singular, with condition number 6.73e+17. Standard errors may be unstable.
MSE RMSE MAE SMAPE
naive 1.045461e+06 1022.477747 597.257143 69.556373
mean 7.898678e+06 2810.458659 2511.586420 147.552078
VAR (scaled) 5.478471e+05 740.166954 354.330235 61.940427
Best P, Q, Trend Combination: (1, 1, 'ct')
                                  Statespace Model Results                                  
============================================================================================
Dep. Variable:     ['total', 'seats_arrivals_intl']   No. Observations:                   24
Model:                                  VARMAX(1,1)   Log Likelihood                 -61.690
                                        + intercept   AIC                            165.380
Date:                              Thu, 08 Jun 2023   BIC                            190.119
Time:                                      20:25:46   HQIC                           171.943
Sample:                                           0                                         
                                               - 24                                         
Covariance Type:                                opg                                         
===================================================================================
Ljung-Box (L1) (Q):             1.75, 0.35   Jarque-Bera (JB):          0.37, 91.71
Prob(Q):                        0.19, 0.55   Prob(JB):                   0.83, 0.00
Heteroskedasticity (H):         0.83, 3.04   Skew:                       0.28, 2.41
Prob(H) (two-sided):            0.80, 0.14   Kurtosis:                  2.75, 11.28
                                  Results for equation total                                 
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
intercept                     0.9646      7.078      0.136      0.892     -12.908      14.837
intercept                     0.7162      8.942      0.080      0.936     -16.810      18.243
L1.total                      0.0850      1.861      0.046      0.964      -3.563       3.733
L1.seats_arrivals_intl       -0.1824      1.777     -0.103      0.918      -3.665       3.300
L1.e(total)                   0.1519      2.814      0.054      0.957      -5.364       5.668
L1.e(seats_arrivals_intl)     1.0805      2.247      0.481      0.631      -3.323       5.484
beta.covid                   -1.4308      2.244     -0.638      0.524      -5.828       2.967
beta.stringency_index        -0.1115      0.193     -0.579      0.562      -0.489       0.266
beta.covid                   -1.4308      2.244     -0.638      0.524      -5.828       2.967
                           Results for equation seats_arrivals_intl                          
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
drift                         0.1455      0.969      0.150      0.881      -1.754       2.045
drift                         0.3453      0.964      0.358      0.720      -1.545       2.235
L1.total                      0.4916      0.740      0.664      0.506      -0.959       1.942
L1.seats_arrivals_intl       -0.6662      0.520     -1.281      0.200      -1.686       0.353
L1.e(total)                  -0.3073      0.941     -0.327      0.744      -2.152       1.537
L1.e(seats_arrivals_intl)     0.8599      1.688      0.509      0.611      -2.449       4.169
beta.covid                   -1.3231      1.910     -0.693      0.488      -5.067       2.420
beta.stringency_index        -0.1592      0.201     -0.791      0.429      -0.554       0.235
beta.covid                   -1.3231      1.910     -0.693      0.488      -5.067       2.420
                                       Error covariance matrix                                        
======================================================================================================
                                         coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------------------------------
sqrt.var.total                         0.5525      0.456      1.212      0.225      -0.341       1.446
sqrt.cov.total.seats_arrivals_intl    -0.3034      3.635     -0.083      0.933      -7.427       6.820
sqrt.var.seats_arrivals_intl           1.3951      0.885      1.576      0.115      -0.339       3.130
======================================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
[2] Covariance matrix is singular or near-singular, with condition number 1.55e+17. Standard errors may be unstable.
MSE RMSE MAE SMAPE
naive 1.721267e+05 414.881597 268.478261 46.835106
mean 1.290167e+06 1135.855000 1065.215278 100.969388
VAR (scaled) 6.502167e+04 254.993470 154.338247 17.694166
Best P, Q, Trend Combination: (2, 2, 'c')
                                  Statespace Model Results                                  
============================================================================================
Dep. Variable:     ['total', 'seats_arrivals_intl']   No. Observations:                   43
Model:                                  VARMAX(2,2)   Log Likelihood                -156.966
                                        + intercept   AIC                            367.932
Date:                              Thu, 08 Jun 2023   BIC                            415.485
Time:                                      20:27:14   HQIC                           385.468
Sample:                                           0                                         
                                               - 43                                         
Covariance Type:                                opg                                         
===================================================================================
Ljung-Box (L1) (Q):             0.38, 1.22   Jarque-Bera (JB):         43.45, 85.76
Prob(Q):                        0.54, 0.27   Prob(JB):                   0.00, 0.00
Heteroskedasticity (H):         0.23, 0.85   Skew:                      1.25, -0.85
Prob(H) (two-sided):            0.01, 0.76   Kurtosis:                   7.24, 9.71
                                  Results for equation total                                 
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
intercept                     2.7879      1.539      1.811      0.070      -0.229       5.805
L1.total                      0.1296      0.453      0.286      0.775      -0.758       1.017
L1.seats_arrivals_intl       -0.0458      0.782     -0.059      0.953      -1.579       1.487
L2.total                     -0.0978      0.427     -0.229      0.819      -0.936       0.740
L2.seats_arrivals_intl       -0.0299      0.594     -0.050      0.960      -1.194       1.134
L1.e(total)                  -0.0571      0.491     -0.116      0.907      -1.020       0.906
L1.e(seats_arrivals_intl)     0.0051      0.687      0.007      0.994      -1.341       1.351
L2.e(total)                  -0.2549      0.350     -0.728      0.467      -0.941       0.431
L2.e(seats_arrivals_intl)    -0.6611      0.605     -1.093      0.274      -1.847       0.524
beta.covid                   -4.3852      1.703     -2.575      0.010      -7.723      -1.047
beta.stringency_index        -0.0641      0.029     -2.242      0.025      -0.120      -0.008
beta.covid                   -4.3852      1.703     -2.575      0.010      -7.723      -1.047
                           Results for equation seats_arrivals_intl                          
=============================================================================================
                                coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------------------
intercept                     2.2902      2.130      1.075      0.282      -1.884       6.464
L1.total                      0.4332      0.457      0.949      0.343      -0.462       1.328
L1.seats_arrivals_intl        0.4219      0.582      0.725      0.469      -0.719       1.563
L2.total                     -0.6276      0.383     -1.637      0.102      -1.379       0.124
L2.seats_arrivals_intl       -0.0903      0.357     -0.253      0.800      -0.790       0.609
L1.e(total)                  -0.3777      0.570     -0.663      0.507      -1.494       0.739
L1.e(seats_arrivals_intl)     0.2111      0.628      0.336      0.737      -1.019       1.441
L2.e(total)                   0.1953      0.699      0.279      0.780      -1.175       1.565
L2.e(seats_arrivals_intl)    -0.1561      0.455     -0.343      0.732      -1.048       0.736
beta.covid                   -2.1837      3.363     -0.649      0.516      -8.776       4.408
beta.stringency_index        -0.0263      0.082     -0.321      0.748      -0.187       0.134
beta.covid                   -2.1837      3.363     -0.649      0.516      -8.776       4.408
                                       Error covariance matrix                                        
======================================================================================================
                                         coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------------------------------
sqrt.var.total                         1.1858      0.351      3.376      0.001       0.497       1.874
sqrt.cov.total.seats_arrivals_intl     0.1719      0.856      0.201      0.841      -1.506       1.850
sqrt.var.seats_arrivals_intl           1.5067      0.289      5.218      0.000       0.941       2.073
======================================================================================================

Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
[2] Covariance matrix is singular or near-singular, with condition number  7e+18. Standard errors may be unstable.
MSE RMSE MAE SMAPE
naive 2.446383e+06 1564.091833 808.452381 45.153360
mean 2.273834e+07 4768.473803 4264.861006 156.566459
VAR (scaled) 1.600258e+06 1265.013157 554.204122 134.493067