LAMPSPUC / StateSpaceModels.jl

StateSpaceModels.jl is a Julia package for time-series analysis using state-space models.
https://lampspuc.github.io/StateSpaceModels.jl/latest/
MIT License
272 stars 25 forks source link

max_q must be strictly positive? (auto arima) #315

Closed dazhwu closed 1 year ago

dazhwu commented 1 year ago

What if I just want to pick up the optimal p given that both d and q are zero?

In R, I could set max.q =0.

guilhermebodin commented 1 year ago

We can rewrite those asserts

    @assert max_p > 0
    @assert max_q > 0
    @assert max_d > 0
    @assert max_P > 0
    @assert max_D > 0
    @assert max_Q > 0
    @assert max_order > 0

There is no reason for them to be strictly positive

dazhwu commented 1 year ago

I changed the following line in the auto_arima function:

@assert max_q >= 0

Changing ">" to ">=" allows me to call auto_arima with max_q=0 (my intention is to pick up the best AR model):

mdl_ar=StateSpaceModels.auto_arima(mdl_ivx["ols_residuals"], max_p=5, d=0, max_q=0, information_criteria="bic")
StateSpaceModels.fit!(mdl_ar)
print(mdl_ar.results)

However, the model chosen by auto_arima is p=2; d=0; q=2:

StateSpaceModels.Results{Float64}("SARIMA(2, 0, 2)x(0, 0, 0, 0) with zero mean    ", StateSpaceModels.CoefficientTable{Float64}(["ar_L1", "ar_L2", "ma_L1", "ma_L2", "sigma2_η"], [0.5812890069326717, 0.3225272626177295, -0.0740566496419704, -0.3756228739571143, 7.081303066016176e-5], [NaN, 
NaN, NaN, NaN, NaN], [NaN, NaN, NaN, NaN, NaN], [NaN, NaN, NaN, NaN, NaN]), 580.6591619420767, -1151.3183238841534, -1150.9590424470277, -1135.5518659116644, 173, 5) *  Terminal will be reused by tasks, press any key to close it. 

Obviously, (2, 0, 2) doesn't satisfy the requirement of max_q=0. (2, 0, 2) was generated by the following block.

if seasonal == 0
        push!(candidate_models, SARIMA(y; order = (2, d, 2), include_mean = include_mean, suppress_warns = true))
        push!(candidate_models, SARIMA(y; order = (0, d, 0), include_mean = include_mean, suppress_warns = true))
        push!(candidate_models, SARIMA(y; order = (1, d, 0), include_mean = include_mean, suppress_warns = true))
        push!(candidate_models, SARIMA(y; order = (0, d, 1), include_mean = include_mean, suppress_warns = true))

I guess the two instances of “-1:1" in code below need to be changed.

function add_new_p_q_models!(candidate_models::Vector{SARIMA}, 
                             max_p::Int, max_q::Int, max_order::Int,
                             visited_models::Vector{NamedTuple})
    best_model = candidate_models[1]
    for p in -1:1, q in -1:1
        new_p = best_model.order.p + p
        new_q = best_model.order.q + q
        # invalid new orders
guilhermebodin commented 1 year ago

Hi @dazhwu thanks for the tests, I will merge this PR tomorrow and release a new version with the fixes. If you want to test it right now you can add the specific branch of the fix add StateSpaceModels#gb/fix-auto-arima-orders.