alan-turing-institute / TimeSeriesClassification.jl

Machine Learning with Time Series in Julia
MIT License
27 stars 5 forks source link

Wrapping ARCHModels? #13

Open azev77 opened 3 years ago

azev77 commented 3 years ago

How hard would it be to wrap ARCHModels.jl? It can fit ARMA{p,q} models (where p, q are the tuning parameters)

julia> using ARCHModels;
julia> fit_arma(df, p, q) = fit(ARCH{0}, df, meanspec=ARMA{p,q});
julia> fit_arma(BG96, 2, 3)
TGARCH{0,0,0} model with Gaussian errors, T=1974.
Mean equation parameters:
────────────────────────────────────────────────
       Estimate  Std.Error     z value  Pr(>|z|)
────────────────────────────────────────────────
c   -0.00983746  0.0354041  -0.277862     0.7811
φ₁   0.551574    0.589292    0.935995     0.3493
φ₂  -0.144346    1.92247    -0.0750838    0.9401
θ₁  -0.542057    0.591114   -0.91701      0.3591
θ₂   0.113263    1.92454     0.0588521    0.9531
θ₃   0.0501891   0.0282235   1.77828      0.0754
────────────────────────────────────────────────
Volatility parameters:
─────────────────────────────────────────
   Estimate  Std.Error  z value  Pr(>|z|)
─────────────────────────────────────────
ω  0.220462  0.0117617  18.7441    <1e-77
─────────────────────────────────────────
julia>

It can comes w/ a self-tuning function to easily & quickly select the optimal ARMA{p,q}:

julia> auto_arma(df, bic) = selectmodel(ARCH{0}, df, meanspec=ARMA, criterion=bic, minlags=1, maxlags=3);
julia> auto_arma(BG96, bic)       # ARMA(1,1)
TGARCH{0,0,0} model with Gaussian errors, T=1974.
Mean equation parameters:
─────────────────────────────────────────────
      Estimate  Std.Error   z value  Pr(>|z|)
─────────────────────────────────────────────
c   -0.0266446  0.0174716  -1.52502    0.1273
φ₁  -0.621838   0.160741   -3.86857    0.0001
θ₁   0.643588   0.154303    4.17095    <1e-4
─────────────────────────────────────────────
Volatility parameters:
─────────────────────────────────────────
   Estimate  Std.Error  z value  Pr(>|z|)
─────────────────────────────────────────
ω  0.220848  0.0118061  18.7063    <1e-77
─────────────────────────────────────────
julia> auto_arma(DOW29[:,1], bic) # ARMA(2,2)
TGARCH{0,0,0} model with Gaussian errors, T=2785.
Mean equation parameters:
───────────────────────────────────────────────
      Estimate  Std.Error     z value  Pr(>|z|)
───────────────────────────────────────────────
c   -0.166748   0.0681206   -2.44783     0.0144
φ₁   0.0178542  0.0401341    0.444864    0.6564
φ₂  -0.932372   0.0803993  -11.5968      <1e-30
θ₁  -0.0191798  0.0463979   -0.413375    0.6793
θ₂   0.903732   0.0963863    9.37614     <1e-20
───────────────────────────────────────────────
Volatility parameters:
─────────────────────────────────────────
   Estimate  Std.Error  z value  Pr(>|z|)
─────────────────────────────────────────
ω   3.65185   0.220716  16.5455    <1e-60
─────────────────────────────────────────
julia> auto_arma(DOW29[:,3], bic) # ARMA(2,1)
TGARCH{0,0,0} model with Gaussian errors, T=2785.
Mean equation parameters:
────────────────────────────────────────────────
       Estimate  Std.Error     z value  Pr(>|z|)
────────────────────────────────────────────────
c    0.00192406  0.0345892   0.0556262    0.9556
φ₁  -0.371152    0.2418     -1.53496      0.1248
φ₂  -0.145134    0.0625429  -2.32055      0.0203
θ₁   0.231451    0.235409    0.983186     0.3255
────────────────────────────────────────────────
Volatility parameters:
─────────────────────────────────────────
   Estimate  Std.Error  z value  Pr(>|z|)
─────────────────────────────────────────
ω   2.20732   0.164313  13.4336    <1e-40
─────────────────────────────────────────
julia> auto_arma(DOW29[:,9], bic) # ARMA(1,2)
TGARCH{0,0,0} model with Gaussian errors, T=2785.
Mean equation parameters:
──────────────────────────────────────────────
      Estimate  Std.Error    z value  Pr(>|z|)
──────────────────────────────────────────────
c   -0.0184696  0.0215819  -0.855789    0.3921
φ₁   0.109868   0.306715    0.358211    0.7202
θ₁  -0.110765   0.308797   -0.358699    0.7198
θ₂  -0.107618   0.0347561  -3.09639     0.0020
──────────────────────────────────────────────
Volatility parameters:
─────────────────────────────────────────
   Estimate  Std.Error  z value  Pr(>|z|)
─────────────────────────────────────────
ω   1.79002   0.109611  16.3307    <1e-59
─────────────────────────────────────────

It takes any vector as input (it assumes vector is in chronological order)...

auto_arma(randn(5000), bic) 

using MLJ;
X, y = @load_boston;
auto_arma(y, bic) #Nonsense bc y has no time structure in this dataset

Can you please help me wrap ARCHModels.jl?

aa25desh commented 3 years ago

I just had quick overlook through ARCHModels.jl not sure how it works.

If ARCHModels follows steps like classification algorithm (fit, predict, update) we can wrap it with MLJ.

If ARCHModels takes continuous stream as input to predict or has online algorithms we have to make new Interface to handle that. (I guess)

Again, I am not expert, @ablaom might have some ideas.

Can you please help me wrap ARCHModels.jl?

Preparing for exams now, but happy to help in PR. I will be back again in Nov last week.