Markov Switching Models | Switching Models in Econometrics
From the series: Switching Models in Econometrics
This is the first video in a two-part series that shows how to model time series data in the presence of regime shifts in MATLAB. In this video, William Mueller uses Markov switching models from the Econometrics Toolbox™ to model unemployment data across different economic regimes.
Highlights:
- Import and visualize unemployment data across regimes
- Set up ARIMA or vector auto-regressive sub models to describe the data in each economic state
- Create a discrete-time Markov switching mechanism using the Econometrics Toolbox to transition between different economic states
- Visualize the transition probabilities as well as the switching mechanism for the regime shifts using Markov chain graph plotting functionality
- Assemble a switching model using built-in functionality in the Econometrics Toolbox to switch between the sub models in different macroeconomic regimes
- Estimate model parameters, simulate the estimated model over multiple paths and time periods, and forecast the estimated model from the unemployment data
Published: 20 Jan 2022
Hello, my name is Bill Mueller. I'm a developer for Econometrics Toolbox at MathWorks here to talk about switching models in Econometrics. This is the first of a two-part series on switching models. This first part will deal with Markov switching models.
So to begin, what is a switching model and why would you want to build one? Switching models are typically used to describe data that comes from an economic environment that seems to be shifting among different modes of operation, which we call states or regimes. The data in each of the states has its own distinct statistical characteristics.
To determine a good statistical description of the data in each of the states, you might do one of three things. First of all, if you have experience with the data or economic theory that describes the data, you may be able to determine parameters like levels and volatilities and autoregressive structures beforehand and build them into the model from to start.
The second thing you might do is a thorough statistical analysis of the data in each of the states to determine things like means and variances et cetera to build into the model. Thirdly, if you have less familiarity with the data or an incomplete understanding of how it behaves, you might want to fit a model to the data with an estimation algorithm and use that to determine the best parameters in your model.
Here, in this presentation, I'm not going to discuss economic theory or do anything like a thorough statistical analysis of any of the data. But I will show an estimation algorithm that allows you to determine parameters directly from the data as part of a process of building a model and then using the model to do things like forecasting and simulations.
So to build a switching model, the first thing you're going to do is build a submodel for each of the individual states with a good statistical description of the data in that state. Then, you're going to assemble all of the submodels together into a vector of submodels to describe the entirety of the data. And finally, you're going to add a switching mechanism that tells the model how to transition from one state to the next. In this case, the switching mechanism will be a Markov chain.
A classic example of where you might want to use a switching model is in describing business cycles, as in this unemployment rate data that you see in the figure. Here, even without doing any kind of statistical analysis, just visually you can see very different behavior in the white bands, which represent expansion periods in the economy where the unemployment rate is going down in a fairly steady way with a certain characteristic volatility. And then, the data in the gray bands, which represent recessions where the unemployment rate ticks up very sharply with a very different kind of statistical description.
So here we see data with two very different regimes. Let's look at this data in MATLAB where we can talk about the steps of putting together a switching model to describe it. This is a live script which is available on the web together with the PowerPoint slides for this presentation. And the first thing we're going to do is load some unemployment data. This is one of the many data sets that ships with the Econometrics Toolbox that you can use to calibrate and test your Econometric models.
And when we load this unemployment data into MATLAB and take a look at it at it in the command window, we see that the data has been loaded here. It's a 12 by 45 array. We can open it up in the data browser here and see that the data is formatted as having 12 rows-- these represent the different months of the year-- and then the 45 different columns representing the different years.
So the first thing we're going to want to do is just convert this data into a single series that reads down the columns one after another. And that's what the first bit of code here does. It just creates a column vector out of that data. The second thing we're going to want to do is convert the dates to describe the data into something called the datetime. If you look back in the command window here, the dates-- if I open them up in the data browser-- are as described from 1954 up until 1998.
If you want more recent data or other data to work with, all of this data comes from the Federal Reserve who maintain a database called FRED at the St Louis Fed, and you can download any of this data from there.
So the second bit of code here, we're just taking those serial dates and converting them into what's called a datetime vector by extracting the day, month, and year from the data and then using this datetime command to create a datetime vector. Datetimes are the preferred container for times and dates in MATLAB for doing any kind of arithmetic with the dates or when you're creating plots like we will here and you want to have a nice display of the dates on one of the axes.
So if we plot this data-- finally using our plotting command-- we'll just plot the dates versus the unemployment vector here. And finally, I will issue this recessionplot command, which is a nice little utility that comes with the Econometrics Toolbox. What it does is on any time series plot-- if you execute this recessionplot command-- it'll overlay these gray recession bands which represent historical recessions as certified by the National Bureau of Economic Research.
So here, we have our unemployment data with these two very clear and distinct states during the expansion periods and the recession periods with the upticks here. And we're going to want to try and build a two-state switching model to try and describe this data.
So the first step is to create the submodels that describe the data in each individual state, and there are two ways you can do this. It depends entirely on whether you're dealing with univariate data-- as we are here with a single series-- or multivariate data where you have several different series interacting with each other that you want to describe simultaneously.
The two classes of models that we use in Econometrics Toolbox for describing these kind of theories are either ARIMA models to describe univariate data. We'll describe some fairly simple models talking about the constants and simple autoregressive structures and certain variances, but these can be more complicated. They can involve things like differences, moving average terms, regression components, et cetera.
Likewise, if you're working with multivariate data, the vector autoregressive models constructed by the VARM command allow you to create these multivariate models, which you can plug into your switching model and have multivariate switching models. So again, let's return to our live script and just remind ourselves how we create these different types of sub models in MATLAB.
So for ARIMA submodels, what I'm going to do here is just describe a simple AR2 model where I'll give a constant 2 autoregressive coefficients describing the lags at lag 1 and lag 2 and then a variance for the random innovations in the model. Again, this could be more complicated. I'm not claiming that this is any kind of a good description of the data we're looking at, but just to demonstrate how you would take this statistical information wherever you acquired it from and construct it into an ARIMA model.
All you do is you call the ARIMA constructor. You give it the constants-- the autoregressive terms, the variants, et cetera-- in these parameter value arguments, and that creates a model here with all of the characteristics that you've used specify the model.
You can then take the model object here and input it into one of the various methods of the ARIMA class, such as the simulate method. If you wanted to simulate this model for a hundred times steps, you would call the simulate method this way. And you can see we get a simulation of the ARIMA submodel that we've specified here.
And because it has-- because it's a stochastic model with random innovations here, if we run this again, of course, we get a different simulation. Run it again, get a different simulation. All of these different paths of the model have the same statistical description that we've constructed here, but they behave differently due to the random innovations that are part of the model.
Likewise, if you wanted to build a multivariate model-- which we won't do here-- but just to show you how to do it. Same setup-- the coefficients here are now in this case for a bivariate model, you're going to have two constants. Your autoregressive terms are now going to be 2 by 2 arrays to include all the different combinations of lags in each of the two series. You're going to have a covariance matrix instead of a variance to describe the model.
And then, you construct it in a completely parallel way here using the VARM constructor and input these parameter value arguments to say that I want to construct this now VARM model here. And again, you can then apply these different methods from that particular class-- things like simulations and forecasting-- directly from the model and create simulations of that particular submodel.
If I go back to the MATLAB workspace here and navigate in my MATLAB installation to the toolbox econ econ directory, you'll see listed here all of the different classes that we have in Econometrics Toolbox. So, for example, the ARIMA class is here. And if we open that up, we see here's the ARIMA constructor that we used, the simulation method that we use, and then a number of other different methods you can use for working with that class.
Likewise, further down is the VARM class for working with multivariate models, and it has its own set of methods you can use for working with those. And then, what we'll be looking at as we build our switching models today is the DTMC class-- which is the discrete-time Markov chain class-- which has many methods for working with Markov chains. Whether you're building a switching model or not, this is the place to build Markov chains. And then the msVAR class here, which is the class for creating the Markov switching models themselves.
So after you've built submodels for each of the different states, the final thing you're going to need as a component of your switching model is the switch itself, which was going to be a Markov chain. You're going to want to think of these switching models as switching between these different latent states which are typically unobserved in some probabilistic fashion. And a Markov chain is the right description of these kinds of transitions.
We're going to think about Markov chains in one of two ways, either visually as a directed graph-- like you see here-- or algebraically in terms of something called a transition matrix, which, of course, is something that MATLAB likes, working with matrices and linear algebra. So let's just take a look at how we would put together a Markov chain as a switch for a switching model.
In this next section of the live script, you see here that we're going to input this 4 by 4 matrix representing the transitions in the Markov chain. And the way you read this is each row is read as a series of probabilities where these probabilities-- this first one here of about 50% is the probability that the Markov chain will transition from the first state back to the first state. So it's about-- has about a 50% stickiness of staying in that state from one step to the next. It has a 0 probability of transitioning from state 1 to state 2. So that's an impossible transition. It has about a 33% transition probability going from state 1 to state 3, and then about a 17% probability of going from state 1 to state 4, and then et cetera from state 2 to state 1, state 2 to state 2, state 2 to state 3, et cetera here.
You put together these probabilities of transition among the different states. And you then use the DTMC constructor as here to input your transition matrix and then again different parameter value pairs that help you describe your Markov chain. Here, we'll give names to each of the states just to demonstrate a particular parameter value pair.
Once you've constructed your Markov chain object here, you have access to all of the methods for the DTMC class, including this graphplot command. We'll input the Markov chain and turn on this ColorEdges parameter. If we do that, we see a visualization of the Markov chain that looks something like this, and you get a pretty good idea of the probabilities of moving from one state to another.
For example, regime A here-- the first regime-- you see there's a return probability in green that corresponds to about a 50% probability of returning to regime A, exactly as we designated in the transition matrix. And, likewise, there don't seem to be any transitions from regime A to regime B, indicating a zero transition probability in this case. So once you've built your submodels and described your switching mechanism with a Markov chain, you assemble these components together to create your Markov switching model.
So three steps to constructing a Markov switching model-- the first is to use either the ARIMA or VARM constructors to create individual submodels for each of the different states. Second, you create a Markov chain with the same number of states and describe the transition probabilities between states. And finally, you're going to assemble these all together with the msVAR constructor to describe the switching model as a whole. So let's take a look at that.
Here again, I'm not making any claims that this is a good description of our unemployment rate data. I'm just for purposes of demonstration creating a simple two-state autoregressive model for describing each of the different states in the unemployment rate data. We're going to estimate the model in a moment here and see what better values for these parameters might be. But just to show you the mechanics of how to construct this model, this is the way you would go about doing it.
You describe, for example, constants for each of the model's autoregressive structures for each of the models and variances here, where it will have say, a higher variance in the first model than the second model. You then create the individual submodels both with the ARIMA constructor here. We'll call them model 1 and model 2. You create your switch-- your Markov chain-- with the DTMC constructor here. We'll have a simple two-state transition matrix with the first state having an 80% stickiness to it where a return probability back to state 1 from state 1 at any step. And then we have from state 2 to state 2 a little bit less stickiness, though state 2 only has a 60% return probability. And then very simply, to construct your switching-- your complete switching model-- now, you call the msVAR constructor, give it your switch and your vector of submodels, and you create a Markov switching model, which is described here in the display.
So let's just take a quick look at this. We'll use one of the methods of the msVAR class-- the simulate method like we did for ARIMA and VARM-- and then I'll simulate a switching model. So we input the model object. We'll simulate it for 100 times steps. And this Y0 here is just some presample data. Because we have autoregressive terms, we might want to specify something other than the default presample values to initiate the simulation.
And if you call simulate method, there are different outputs. The first output is just the simulated series itself. The third output here is the state path that shows you what state the Markov chain is in at each step. And if we plot these separately, we get something like this.
So, here now, we have a simulation of our fully constructed switching model and also a display of the state path. And we see the chain going from state 1 to state 2, back to state 1, et cetera, with certain probabilities. We do see that state 1-- we've seen that these longer self times in state 1 where it just stays in that state for longer. That's consistent with the transition matrix that we created where we have an 80% probability of staying in state 1 and only a 60% probability of staying in state 2.
And once again, if we want to run this multiple times, we get different paths of our Markov switching model which you can use to build up a series of paths that you might use for say, Monte Carlo analysis of the distribution of the statistics in these models.
So that's all there is to it for building a Markov switching model. Once you've done that, as I've mentioned, there's a number of different methods that are available in the msVAR class for making use of these models and doing the typical kind of things you'd want to do like simulation and forecasting. But first, let me look at the estimation method, which is an optimization algorithm that allows you to fit a switching model to the data that you have.
So how would that work? What you're going to do-- if you want to estimate a model instead of fully specifying it beforehand based on whatever a priori knowledge. you might have of the data-- if you want to just estimate the parameters in the model, you create an msVAR switching model in the same way as we did above, except for the parameter values you input NaN-- not a number-- for the parameter values to indicate that you'd like the algorithm to estimate those particular parameters.
Now, if you know some of these parameters like the transition probabilities or you'd like to fix them, you can do a mix of NaNs and equality constraints, putting in specific values here. I'll show you how to do that in the second part of this presentation when we talk about threshold switching models. But here, let's just try and estimate all the parameters at once. That means, in this case, it looks like eight parameters we're going to try an estimate from our data.
Typically, you want to be careful here, that if you are trying to estimate too many parameters from a limited data set, the estimation algorithm is not going to perform as well. And you're going to get a larger standard errors for your parameter estimates et cetera. You want to keep your model descriptions as parsimonious as you can, as simple as you can.
You'll often find that you not only get better estimation performance with a smaller number of parameters, but you also get typically better forecast performance and simulation performance and so on from simpler models than very complicated models. If you ask for too many parameters to be estimated, you might just get bad results here.
But let's give it a try here. We've got eight different parameters. We specify the model with NaNs and then to start the algorithm, which is an expectation maximization algorithm, we need to give initial values for the different parameters. These don't have to be anything accurate in terms of what we think the final result is going to be, but just starting points so the algorithm can get started. So we're going to have, say, two different constants here in the two different models, just saying that maybe if you look at the constants and the AR1 terms-- the autoregressive terms-- here, you can calculate the means in the standard way.
So we're just saying let's specify a starting point where the mean is 4 and a starting point in the second state where the mean is 8. That's consistent with the unemployment data that we are trying to fit originally here. Notice that the rates are going between 4 and 8 in terms of means. Those are reasonable starting points in terms of the data.
So here's the model we want to specify with NaN values for the parameters we want to estimate. Here's an initial model. We'll just say 50% transition probabilities all the way around on the switch, and we create that as kind of an initial model. And then, the estimate method takes the model we want to estimate-- the initial model-- and finally, the data that we want to fit to the model and then various other parameter value pairs you can specify to fine tune the optimization routine.
I'll just turn on this iteration plot so we can see how the algorithm approaches the best fit. What it creates is this plot for each iteration. It shows the log-likelihood of the data for the model that's been fitted. And you can see this steady increase-- this monotonic increase-- which is typical of an expectation maximization algorithm. And it levels off until it stops changing within a certain default tolerance. And the algorithm terminates after about 50 iterations.
To see the results of the estimation, the best way is to use this summarize method. You say summarize the estimated model. The estimated model is, of course, what came out of our estimate command here. This has now-- there's now a fully specified switching model with all of the parameters estimated, based on our unemployment rate data. And we get this display here.
You can see things like the transition matrix. It has a very sticky first date here-- about a 98% chance of staying in state 1-- and somewhat smaller stickiness in the second state. So it would seem like the second state here would be the recession state, which typically don't-- recessions typically don't last as long as the expansion periods. That's what we saw in the data. And then, you get various fit statistics here, like information criteria for the final model that was fit at termination of the algorithm, and then a table here, giving you statistics on the different parameters that we estimated, the values and their standard errors.
So this is a very handy tool. Again, if you don't have complete knowledge of the parameters that you want to use for the statistical descriptions of the different states of your switching model, you can use this estimation algorithm to get good estimates-- often good estimates-- depending on how many parameters you're estimating and what the shape of your log-likelihood surface is like. You generally get pretty good results with this expectation maximization algorithm. It can serve at least as a starting point for doing further statistical analysis.
Then, as I said, you can use any of the other-- if you use the model we've estimated as your switching model, you can then go back and use any of the other methods for the msVAR class, like the simulation method. Here, we can do a simulation of not the model that we just constructed with some arbitrary parameters, but the actual model that we estimated from the data.
And if we do a simulation, we see a series that seems to have similar statistical characteristics to the original data. We see that it has a similar range here between 2% and 8%. There are these definite expansion periods with the unemployment rate going down with a certain volatility. And then, these very distinct upticks where we have a recession kick in at certain points with certain probability and then transition back to an expansion here. So this seems to be at least a reasonable fit of the unemployment data described as a switching model.
Finally, you might want to do things like use many simulations. Here, we're just going to call the simulate command with an additional parameter value pair saying we want to generate 50 different paths all at once. We'll start all the paths with a presample value of 4-- that's to initiate the autoregressive term-- and have all the paths start from the same place. And you get a display like this.
If you plot the different simulations, you can see again, they all seem to be in a similar range here, where we have the sort of density of different paths. But there are instances where the unemployment rate can get very high in some of these simulations. And this kind of creating multiple paths with the simulation method is so that you can do Monte Carlo analysis of distributions of the statistics that are connected with these paths in this particular model.
Finally, you might want to do forecasting and use the original data to forecast ahead into a certain forecast period. There are two ways you can create a forecast with these msVAR models. The first one if you call the forecast method with your fully specified model-- In this case, our estimated model-- with just a single output, you'll get the optimal point forecast, in other words, the forecast of the mean that minimizes the mean squared error of the forecast-- minimum mean squared error forecasts if you do this.
If you call the forecast method with two outputs and ask for this estimated variance as an output here, you get simulation-based. forecasts. So what the forecast method will do is a kind of Monte Carlo-- generate multiple paths and then average those to create the forecast mean. And we see plotted here in red is the minimum means great error forecast going ahead-- in this case 120 steps or 10 years. In the magenta here, is the average of the default number of simulations in the simulation forecast. I think a hundred different simulations are averaged to create that.
If you want to create something other than a point forecast, like an interval forecast, you would use the estimated variance here. That's output from the simulation forecast. I'll show you how to do that in the second part of the series.
So to conclude, let me just mention that again, this live script is available together with these PowerPoint slides in this presentation. And you can make use of it to remind yourself of the different steps for building these switching models.
If you want to get into this deeper and see how to really fine tune these models, and do analysis of how to create the submodels, and how to use the switching models to do economic analyzes, we've got lots and lots of examples in our documentation. So I recommend that you go to the MathWorks help econ section where you'll see lots of great examples about both Markov chains and Markov switching models.
So that concludes this first section of our presentation on switching models. The second part of this presentation will deal with threshold switching models.