m-dadej / MarSwitching.jl

MarSwitching.jl: Julia package for Markov switching dynamic models :chart_with_upwards_trend:
https://m-dadej.github.io/MarSwitching.jl/
MIT License
37 stars 3 forks source link

Issue while predict #12

Closed kjrathore closed 3 weeks ago

kjrathore commented 1 month ago

Could you please help with this? Even using your example as is, I can't predict, let me know if I am doing this wrong. image

kjrathore commented 1 month ago

Can I only forecast for next step?

m-dadej commented 1 month ago

Hey @kjrathore,

Thanks for opening this issue and sorry for late reply. I was pretty busy with my thesis lately.

Predicting with Markov switching models is a bit tricky. Mostly because in order to predict, we need to know not only the design matrix/covariates in the time t+1 but also which state we are going to be in (and as a result which state regression equation to use).

The reason you have an error is that what predict function does is to first infer the state probabilites from the provided data for time $t$, in order to calculate the predicted state probabilities for $t+1$. Namely:

\hat{y}_t = \sum_{i=1}^{k} \hat{\xi}_{i,t}X_{t}'\hat{\beta}_{i}

And for one step ahead, the state probabilities have to be predicted themselves (this is with instantaneous = false argument):

\hat{y}_{t+1} = \sum_{i=1}^{k} (P\hat{\xi}_{i,t})X_{t+1}'\hat{\beta}_{i}

That is why, the model requires at least two observations as the model implicitly uses data from two time periods. A bit more on that you may find in the longer paper at SSRN or check out the test for prediction in the unit tests (the one called "predict function" to be precise).

I might make some example in the docs with forecasting for other to make it clear. Also, a possible extension is to provide arguments in the function for the user to choose the state. However, this might be contrary to the latent interpretation of the states. If you need this functionality as of now, you can access the model parameters with model.\beta and model.\mu and use some linear algebra as with standard regression.

Hope it clarify your issue. Please feel free to ask if something is not yet clear.