MarSwitching.jl - Markov Switching regression in Julia

MarSwitching.jl is a package that allows to estimate the Markov switching dynamic regression (also called regime switching regression).

It’s in general registry and the repo can be found here: GitHub - m-dadej/MarSwitching.jl: MarSwitching.jl: Julia package for Markov Switching dynamic models

Briefly, this class of models allows for the parameters to change over time, depending on the unobservable state/regime (e.g. economic recession). The regime itself follows a Markovian process. Estimation of the model also allows to infer the probability of being in particular state at time t.

Current functionality:

  • Model with a combination of switching/non-switching exogenous variables, variance or intercept.
  • Time-varying transition probabilities (a la Filardo 1994)
  • filtered and smoothed probabilities of each state
  • instantaneous and one step ahead prediction
  • Simulation of data both from estimated model and from provided parameters

You can read more about the package and see an example in the repository.

Appreciate any comments, feedback and functionality requests.



New release of MarSwitching.jl v0.2.1 !

Changes since the initial release for general registry:

  • Expectations-maximization algorithm for initial values (improved finding of global maxima)
  • documentation with Documenter.jl
  • Examples
  • Performance comparison with Python/Cython, R, MATLAB
  • Random search model selection with grid_search_msm() function
  • Ergodic probabilites with ergodic_probs() function

Additionally, I uploaded working paper at SSRN, which I encourage to cite when using the package in your research!


Someone pointed this out. Here is performance comparison of the most recent version (from dev docs), which improve the package results. It is the same as in the working paper.

1 Like

Looks cool! I’m currently working on a revamp of HiddenMarkovModels.jl which will allow time-varying transition and emission probabilities. It means I can probably simulate a Markov switching regression, so once I do we could compare notes :slight_smile:

1 Like

Nice, looking forward to your implementation!

It landed on the main branch of the repo, and I put a Markov switching regression example in the tutorials.


Looks great! Both implementations converge to approximately the same parameters. Did not know one can also estimate these models with controlled HMM.

1 Like

I try to analyze data of subject behavior in time. For each subject I have observation of state and some factors:

data = DataFrame(subject = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4], 
period = [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4], 
state = ["A", "B", "C", "D","A", "C", "C", "C","A", "B", "A", "D","B", "B", "C", "A"],
factor = ["F1", "F1", "F2", "F1", "F1", "F2", "F1", "F1", "F1", "F2", "F1", "F1", "F2", "F2", "F2", "F1"])

Can I use MarSwitching.jl to get transition matrix? Can I use MarSwitching.jl to evaluate the influence of the “factor” on transition probabilities? (How if possible?)

What would be the regression equation like? Just to be clear, you may think of Markov switching model as regression where parameters change between states according to Markov process.

If you are interested in estimating only transition matrix and possibly effect of exogenous factors on transition probabilites it might be better to use HiddenMarkovModels.jl

It seems the Markov model isn’t even hidden here? If the factors are discrete and the states are observed, you can just count estimate the transition matrices factor-by-factor, counting the state, next_state pairs?

I’m looking for something like this: Multi-state models for the analysis of time-to-event data in Julia. Now I’m using msm package for R Project. Regression equation, I think, should looks like

P_{ab} \sim P_{ab}^{(0)} exp(\beta_{ab}^T X)

But it will be great to find solution in Julia.

So, I suppose, first I should made inference - is this model time homogeneous Markov model or it is non-homogeneous Markov processes.