File Exchange Pick of the Week

Our best user submissions

Markov Regime Switching Models in MATLAB

Note

The file submission referenced in this post is no longer available on File Exchange.

Michael's pick this week is MS_Regress by Marcelo Perlin.

First a quick introduction: Hi! I'm Michael, one of Jiro and Brett's colleagues here at MathWorks. I've been asked to provide a pick this week that combines my personal interests (statistics, optimization, and other numerical methods) with my professional interests (where I focus on our users in the financial community). This week's pick combines all of these in a single package.

Financial analysts are often concerned with detecting when the markets "change": the typical behavior of a market over a periods of months or even years can switch to a very different behavior in an instant. Investors would like to detect these changes as they happen so that may adjust their strategies accordingly, but doing so can be difficult.

Let's consider a simplified example. A bull market can be defined as a period of time where the markets are, on average, increasing in value with relatively low volatility. A bear market corresponds to a downward turn in the market with relatively higher volatility. We can approximate this behavior with a random number generator: it will generate the daily returns (or change in price) of some stock or index during both bull and bear markets, each lasting 100 days:

bull1 = normrnd( 0.10, 0.15, 100, 1);
bear  = normrnd(-0.01, 0.20, 100, 1);
bull2 = normrnd( 0.10, 0.15, 100, 1);

returns = [bull1; bear; bull2];

Note that the bull periods have a positive average (corresponding to growth), while the bear has a negative average. Note also that the bear is more volatile (it has a larger variance) than the bull markets.

Because we created this data, we know exactly how it is behaving. Consider, though, the point of view of an investor who is simply observing these returns as they happen:

plot(returns)
xlabel('Day number')
ylabel('Daily change in price')

Because of the volatility in the data, it can be difficult to detect when (or even if) the bear market occurs: the above plot can look very much like a single random process rather than three adjacent bull/bear/bull periods.

Marcelo's MS_Regress package is designed to shed light on these types of problems. It treats the above returns series as a regime switching model governed by a Markov process to switch between states. What I like is that you don't even need to know what that means to get some value from Marcelo's code. Instead, you only need a few lines of code:

indep = ones(size(returns)); % A dummy explanatory variable
k = 2; % How many regimes we expect: bull and bear
S = [1 1]; % Both the mean and the volatility differ in bulls and bears

SpecOut = MS_Regress_Fit(returns, indep, k, S);
% (Some on-screen outputs are omitted here.)

These plots, which were generated by Marcelo’s file, show us several things. First, the top graph is basically a copy of my own plot from above: it confirms that it’s difficult to eyeball where the regime switch occurs. The middle graph points toward increased volatility (an increased standard deviation) between about the 100th and the 200th day. Most importantly, the bottom plot clearly shows that the markets switch from a bull to a bear (and back) at about the 100th and 200th days (respectively). The SpecOut variable is rich with information on the estimated parameters describing the bull and bear markets as well as the Markov process that governs the transition between them.

I like many things about this submission. Marcelo has provided a PDF "User's Guide" with his files as well as plenty of example files, so it is easy to pick up and learn how to use the package. He also provides plenty of references so that we can learn more about the underlying mathematics if we wish. Finally, his package seems to scale well from the simple case described above to more complicated (and realistic) cases, where we track multiple time series at the same time, introduce explanatory variables, and more. I'm looking forward to exploring his other MATLAB Central submissions soon!

Comment

Let us know what you think here or leave a comment for Marcelo.




Published with MATLAB® 7.11

|

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.