{"id":2650,"date":"2011-02-25T13:56:46","date_gmt":"2011-02-25T13:56:46","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/02\/25\/markov-regime-switching-models-in-matlab\/"},"modified":"2016-11-11T05:30:01","modified_gmt":"2016-11-11T10:30:01","slug":"markov-regime-switching-models-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/02\/25\/markov-regime-switching-models-in-matlab\/","title":{"rendered":"Markov Regime Switching Models in MATLAB"},"content":{"rendered":"<div class=\"alert alert-info\"> <span class=\"alert_icon icon-alert-info-reverse\"><\/span><p class=\"alert_heading\"><strong>Note<\/strong><\/p><p>The file submission referenced in this post is no longer available on File Exchange.<\/p><\/div>\r\n<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31739\">Michael<\/a>'s pick this week is MS_Regress by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/21596\">Marcelo Perlin<\/a>.\r\n   <\/p>\r\n   <p><i>First a quick introduction: Hi!  I'm Michael, one of Jiro and Brett's colleagues here at MathWorks.  I've been asked to provide\r\n         a pick this week that combines my personal interests (statistics, optimization, and other numerical methods) with my professional\r\n         interests (where I focus on our users in the financial community).  This week's pick combines all of these in a single package.<\/i><\/p>\r\n   <p>Financial analysts are often concerned with detecting when the markets \"change\": the typical behavior of a market over a periods\r\n      of months or even years can switch to a very different behavior in an instant. Investors would like to detect these changes\r\n      as they happen so that may adjust their strategies accordingly, but doing so can be difficult.\r\n   <\/p>\r\n   <p>Let's consider a simplified example.  A <i>bull market<\/i> can be defined as a period of time where the markets are, on average, increasing in value with relatively low volatility.\r\n       A <i>bear market<\/i> corresponds to a downward turn in the market with relatively higher volatility.  We can approximate this behavior with a\r\n      random number generator: it will generate the <i>daily returns<\/i> (or change in price) of some stock or index during both bull and bear markets, each lasting 100 days:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bull1 = normrnd( 0.10, 0.15, 100, 1);\r\nbear  = normrnd(-0.01, 0.20, 100, 1);\r\nbull2 = normrnd( 0.10, 0.15, 100, 1);\r\n\r\nreturns = [bull1; bear; bull2];<\/pre><p>Note that the bull periods have a positive average (corresponding to growth), while the bear has a negative average.  Note\r\n      also that the bear is more volatile (it has a larger variance) than the bull markets.\r\n   <\/p>\r\n   <p>Because we created this data, we know exactly how it is behaving. Consider, though, the point of view of an investor who is\r\n      simply observing these returns as they happen:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(returns)\r\nxlabel(<span style=\"color: #A020F0\">'Day number'<\/span>)\r\nylabel(<span style=\"color: #A020F0\">'Daily change in price'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/michael\/potw_ms_regress\/potw_ms_regress_01.png\"> <p>Because of the volatility in the data, it can be difficult to detect when (or even if) the bear market occurs: the above plot\r\n      can look very much like a single random process rather than three adjacent bull\/bear\/bull periods.\r\n   <\/p>\r\n   <p>Marcelo's <tt>MS_Regress<\/tt> package is designed to shed light on these types of problems. It treats the above returns series as a <i>regime switching<\/i> 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\r\n      to get some value from Marcelo's code.  Instead, you only need a few lines of code:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">indep = ones(size(returns)); <span style=\"color: #228B22\">% A dummy explanatory variable<\/span>\r\nk = 2; <span style=\"color: #228B22\">% How many regimes we expect: bull and bear<\/span>\r\nS = [1 1]; <span style=\"color: #228B22\">% Both the mean and the volatility differ in bulls and bears<\/span>\r\n\r\nSpecOut = MS_Regress_Fit(returns, indep, k, S);\r\n<span style=\"color: #228B22\">% (Some on-screen outputs are omitted here.)<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/michael\/potw_ms_regress\/potw_ms_regress_02.png\"> <p>These plots, which were generated by Marcelo&#8217;s file, show us several things.  First, the top graph is basically a copy of\r\n      my own plot from above: it confirms that it&#8217;s difficult to eyeball where the regime switch occurs.  The middle graph\r\n      points toward increased volatility (an increased standard deviation) between about the 100th and the 200th day. Most importantly,\r\n      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).\r\n       The <tt>SpecOut<\/tt> variable is rich with information on the estimated parameters describing the bull and bear markets as well as the Markov\r\n      process that governs the transition between them.\r\n   <\/p>\r\n   <p>I like many things about this submission.  Marcelo has provided a PDF \"User's Guide\" with his files as well as plenty of example\r\n      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\r\n      more about the underlying mathematics if we wish.  Finally, his package seems to scale well from the simple case described\r\n      above to more complicated (and realistic) cases, where we track multiple time series at the same time, introduce explanatory\r\n      variables, and more.  I'm looking forward to exploring his other MATLAB Central submissions soon!\r\n   <\/p>\r\n   <p><b>Comment<\/b><\/p>\r\n   <p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2650#respond\">here<\/a> or leave a comment for Marcelo.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_1083140d92d048fb9017bbd5ba559985() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='1083140d92d048fb9017bbd5ba559985 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1083140d92d048fb9017bbd5ba559985';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Michael Weidman';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_1083140d92d048fb9017bbd5ba559985()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.11<br><\/p>\r\n<\/div>\r\n<!--\r\n1083140d92d048fb9017bbd5ba559985 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31739\r\n% Michael>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/15789-msregress-a-package-for-markov-regime-switching-models-in-matlab\r\n% MS_Regress> by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/21596 Marcelo Perlin>.\r\n%\r\n% _First a quick introduction: Hi!  I'm Michael, one of Jiro and Brett's\r\n% colleagues here at MathWorks.  I've been asked to provide a pick this\r\n% week that combines my personal interests (statistics, optimization, and\r\n% other numerical methods) with my professional interests (where I focus on\r\n% our users in the financial community).  This week's pick combines all of\r\n% these in a single package._\r\n%\r\n% Financial analysts are often concerned with detecting when the markets\r\n% \"change\": the typical behavior of a market over a periods of months or\r\n% even years can switch to a very different behavior in an instant.\r\n% Investors would like to detect these changes as they happen so that may\r\n% adjust their strategies accordingly, but doing so can be difficult.\r\n%\r\n% Let's consider a simplified example.  A _bull market_ can be defined as a\r\n% period of time where the markets are, on average, increasing in value\r\n% with relatively low volatility.  A _bear market_ corresponds to a\r\n% downward turn in the market with relatively higher volatility.  We can\r\n% approximate this behavior with a random number generator: it will\r\n% generate the _daily returns_ (or change in price) of some stock or index\r\n% during both bull and bear markets, each lasting 100 days:\r\nbull1 = normrnd( 0.10, 0.15, 100, 1);\r\nbear  = normrnd(-0.01, 0.20, 100, 1);\r\nbull2 = normrnd( 0.10, 0.15, 100, 1);\r\n\r\nreturns = [bull1; bear; bull2];\r\n%%\r\n% Note that the bull periods have a positive average (corresponding to\r\n% growth), while the bear has a negative average.  Note also that the bear\r\n% is more volatile (it has a larger variance) than the bull markets.\r\n%\r\n% Because we created this data, we know exactly how it is behaving.\r\n% Consider, though, the point of view of an investor who is simply\r\n% observing these returns as they happen:\r\nplot(returns)\r\nxlabel('Day number')\r\nylabel('Daily change in price')\r\n%%\r\n% Because of the volatility in the data, it can be difficult to detect when\r\n% (or even if) the bear market occurs: the above plot can look very much\r\n% like a single random process rather than three adjacent bull\/bear\/bull\r\n% periods.\r\n%\r\n% Marcelo's |MS_Regress| package is designed to shed light on these types\r\n% of problems. It treats the above returns series as a _regime switching_\r\n% model governed by a Markov process to switch between states.  What I like\r\n% is that you don't even need to know what that means to get some value\r\n% from Marcelo's code.  Instead, you only need a few lines of code:\r\n\r\nindep = ones(size(returns)); % A dummy explanatory variable\r\nk = 2; % How many regimes we expect: bull and bear\r\nS = [1 1]; % Both the mean and the volatility differ in bulls and bears\r\n\r\nSpecOut = MS_Regress_Fit(returns, indep, k, S);\r\n% (Some on-screen outputs are omitted here.)\r\n\r\n%%\r\n% These plots, which were generated by Marcelo\u00e2\u20ac\u2122s file, show us several\r\n% things.  First, the top graph is basically a copy of my own plot from\r\n% above: it confirms that it\u00e2\u20ac\u2122s difficult to eyeball where the regime switch\r\n% occurs.  The middle graph points toward increased volatility (an\r\n% increased standard deviation) between about the 100th and the 200th day.\r\n% Most importantly, the bottom plot clearly shows that the markets switch\r\n% from a bull to a bear (and back) at about the 100th and 200th days\r\n% (respectively).  The |SpecOut| variable is rich with information on the\r\n% estimated parameters describing the bull and bear markets as well as the\r\n% Markov process that governs the transition between them.\r\n%\r\n% I like many things about this submission.  Marcelo has provided a PDF\r\n% \"User's Guide\" with his files as well as plenty of example files, so it\r\n% is easy to pick up and learn how to use the package.  He also provides\r\n% plenty of references so that we can learn more about the underlying\r\n% mathematics if we wish.  Finally, his package seems to scale well from\r\n% the simple case described above to more complicated (and realistic)\r\n% cases, where we track multiple time series at the same time, introduce\r\n% explanatory variables, and more.  I'm looking forward to exploring his\r\n% other MATLAB Central submissions soon!\r\n%\r\n% *Comment*\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2650#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/15789-msregress-a-package-for-markov-regime-switching-models-in-matlab#comments\r\n% comment> for Marcelo.\r\n##### SOURCE END ##### 1083140d92d048fb9017bbd5ba559985\r\n-->","protected":false},"excerpt":{"rendered":"<p> NoteThe file submission referenced in this post is no longer available on File Exchange.\r\n\r\n   Michael's pick this week is MS_Regress by Marcelo Perlin.\r\n   \r\n   First a quick introduction: Hi!  I'm... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/02\/25\/markov-regime-switching-models-in-matlab\/\">read more >><\/a><\/p>","protected":false},"author":36,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2650"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2650"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2650\/revisions"}],"predecessor-version":[{"id":8033,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2650\/revisions\/8033"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}