Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not fit MA(q) #323

Closed
shayandavoodii opened this issue Oct 13, 2023 · 10 comments
Closed

Can not fit MA(q) #323

shayandavoodii opened this issue Oct 13, 2023 · 10 comments

Comments

@shayandavoodii
Copy link

shayandavoodii commented Oct 13, 2023

Hi, I want to fit an $MA(q)$ using this package. I saw your documentation and tried to adjust it to get $MA(q)$ only. Here is what I tried and what I got:

julia> using StateSpaceModels

julia> ts = rand(100);

julia> model = SARIMA(ts, order=(0,0,2))
SARIMA(0, 0, 2)x(0, 0, 0, 0) with zero mean

julia> StateSpaceModel.fit!(model)
ERROR: type DataType has no field fit!
Stacktrace:
 [1] getproperty(x::Type, f::Symbol)
   @ Base .\Base.jl:32
 [2] top-level scope
   @ REPL[33]:1

How can I fit an $MA$ model on a vector?

@raphaelsaavedra
Copy link
Member

Hi @shayandavoodii, that's actually an easy one – you have a typo on your fit! call. Note that you're calling StateSpaceModel.fit!(model), not StateSpaceModels.fit!(model). So you're trying to access a fit field in the type StateSpaceModel, which doesn't exist. The error message gives you a clue :)

@shayandavoodii
Copy link
Author

Sorry, I was having dinner. Yes, you're right. I forgot to put the answer on my own. Thank you so much.
Considering the $MA(q)$ formulation:
$$X_t = \mu + \sum_{i=1}^{q} \theta_i \varepsilon_{t-i}$$
How can I get the coefficients? There are many attributes.

julia> using StateSpaceModels

julia> ts = rand(100);

julia> model = SARIMA(ts, order=(0,0,2))
SARIMA(0, 0, 2)x(0, 0, 0, 0) with zero mean

julia> res = StateSpaceModels.fit!(model)
SARIMA(0, 0, 2)x(0, 0, 0, 0) with zero mean

Does the following code give me the coefficients?

julia> res.results.coef_table.coef
3-element Vector{Float64}:
 0.5778745695380242
 0.36309170005361113
 0.17571266972630697

If yes, what's the order of them? I mean, does the first one represent $\theta_1$ for example?

@raphaelsaavedra
Copy link
Member

The coefficient table also has a names field to help you identify which is which (the indices will be the same across names and coefs):

julia> model.results.coef_table.names
3-element Vector{String}:
 "ma_L1"
 "ma_L2"
 "sigma2_η"

julia> model.results.coef_table.coef
3-element Vector{Float64}:
 0.7612171821159301
 0.43638168954943535
 0.16763243467500588

@shayandavoodii
Copy link
Author

shayandavoodii commented Oct 13, 2023

Thank you for your kind response! So, this means it is not necessary to call fit! in order to get the coefficients. The only thing is that I'm not familiar with a notation of MA that uses $L$. I don't know how to match that with the notation I provided earlier. The only things that I'm looking for are those $\theta$ s.

@shayandavoodii
Copy link
Author

I guess "ma_L1" and "ma_L2" refer to the MA (Moving Average) coefficients associated with the lagged error terms. Then, "ma_L1" represents the coefficient for the first lag of the error term, and "ma_L2" represents the coefficient for the second lag of the error term. Furthermore, "sigma2_η" represents the variance ($\sigma^2$) of the white noise error term ( $\epsilon_t$ ).

@raphaelsaavedra
Copy link
Member

You do need to call fit!, otherwise the parameters won't have been estimated yet:

julia> model = SARIMA(ts, order=(0,0,2))
SARIMA(0, 0, 2)x(0, 0, 0, 0) with zero mean    

julia> model.results.coef_table
StateSpaceModels.CoefficientTable{Float64}(String[], Float64[], Float64[], Float64[], Float64[])

julia> StateSpaceModels.fit!(model)
SARIMA(0, 0, 2)x(0, 0, 0, 0) with zero mean    

julia> model.results.coef_table
StateSpaceModels.CoefficientTable{Float64}(["ma_L1", "ma_L2", "sigma2_η"], [0.7612171821159301, 0.43638168954943535, 0.16763243467500588], [0.09444579747538931, 0.11219079770386164, 0.028962339753974605], [8.05983116733477, 3.889638887329303, 5.787945176356171], [0.0, 0.0, 0.0])

I haven't looked at this particular code in a long time, but I believe the L1 and L2 refer to the MA lagged parameters. So ma_L1 would be the first MA lagged parameter, and ma_L2 would be the second MA lagged parameter.

@shayandavoodii
Copy link
Author

Thank you so much. It means the world to me ❤

@raphaelsaavedra
Copy link
Member

Happy to help!

@guilhermebodin
Copy link
Member

As a comment, you can also call the function print_results

julia>  ts = rand(100);

julia> model = SARIMA(ts, order=(0,0,2))
SARIMA(0, 0, 2)x(0, 0, 0, 0) with zero mean

julia> res = StateSpaceModels.fit!(model)
SARIMA(0, 0, 2)x(0, 0, 0, 0) with zero mean

julia> print_results(model)
                             Results
===============================================================
Model:                        SARIMA(0, 0, 2)x(0, 0, 0, 0) with zero mean
Number of observations:       100
Number of unknown parameters: 3
Log-likelihood:               -58.5700
AIC:                          123.1401
AICc:                         123.3901
BIC:                          130.9556
---------------------------------------------------------------
Parameter      Estimate      Std.Error      z stat      p-value
ma_L1            0.5884         0.1198      4.9112       0.0000
ma_L2            0.3002         0.0987      3.0416       0.0000
sigma2_η         0.1880         0.0307      6.1302       0.0000

@shayandavoodii
Copy link
Author

Thank you so much! It's a great summary!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants