The file submission referenced in this post is no longer available on File Exchange.
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!
Let us know what you think here or leave a comment for Marcelo.
Get the MATLAB code
Published with MATLAB® 7.11
Comments are closed.
7 CommentsOldest to Newest
plot(log(cumprod(1 + returns)))
makes it obvious where the bull and bear markets stop and start. From this chart you can pick the turning points almost to the day, unlike the state plot above.
While your graphical approach does work, Nick, it really would only be applicable to cases like the simplified example that we provide above.
Marcelo’s PDF documentation provides plenty of more realistic examples where such an approach would not work, examples featuring multiple time series, exogeneous (independent) regression variables, and more. You should check it out!
I already have checked it out. I was so impressed by your example of how easy the package was to use that I actually used it. I’m hooked. My point still stands, though. For this particular example the package did not work as well as a simple plot. So my concerns are: for a more “realistic” example how can I get a feeling for how well it is working?
My gut feeling is that’s a (literally) billion dollar question: anyone who can reliably answer it would probably be able to make a killing on the markets. The fact that nobody has implies that it’s an open question.
I’m not as well-versed on the state of the art for these models at the experts are, so I’m not sure if some sort of a hypothesis test or misspecification test framework is in place for regime-switching models like it is for other time-series approaches like VARMAX or GARCH. The best that I can see in Marcelo’s approach is a report on standard errors in the parameter estimates, which can certainly be used to disqualify certain models, even if it doesn’t help us to qualify them.
A preliminary search of the literature shows that researchers seem to be still looking into this, with some hypothesis tests that compare no-regime-switching (null) to regime-switching (alternative) or which use a loglikelihood ratio test (LRATIOTEST in Econometrics Toolbox) to evaluate competing regime-switching models. Beyond that, I can’t say.
The code runs well on my computer. My question is which output gives the probability that we now are heading into regime 1 or 2? Thanks.
The package is said to be deleted from MATLAB Central. Does anyone have it?
Alex, have you contacted the author directly?