4.2

4.2 | 16 ratings Rate this file 320 downloads (last 30 days) File Size: 477.85 KB File ID: #15789

MS_Regress - A Package for Markov Regime Switching Models in Matlab

by Marcelo Perlin

 

01 Aug 2007 (Updated 31 Jan 2010)

Code covered by the BSD License  

Functions to Estimate, Simulate and Forecast Markov Regime Switching Models in Matlab

Download Now | Watch this File

File Information
Description

This submission provides functions (and examples scripts) for estimation, simulation and forecasting of a general Markov Regime Switching Regression.
 
Features of the package:
- Support for univariate and multivariate models.
- Support of any number of states and any number of explanatory variables.
- Estimation, by maximum likelihood, of any type of switching setup for the model. This means that you can choose which coefficients in the model, including distribution parameters, are switching states over time.
- A wrapper function for the estimation of regime switching autoregressive models, including multivariate case (MS-VAR) is included in the package.
- The values of standard error for the estimated coefficients can be calculated with 4 different methods.
- Includes a C version of hamilton’s filter that may be used for speeding up the estimation function (see pdf for details).
- Possibility of three distinct distribution assumptions for residual vector (Normal, t or GED).
- Support for reduced/constrained estimation (see pdf document for details).
- Loads of example scripts.
 
Limitations of the Package (so far):
- The EM algorithm is not implemented (all models are estimated by direct maximization of log likelihood function).
- It doesn’t support state space models with markov switching effects.
- It cannot estimate a model with time varying transition probabilities (TVPT).
- It doesn’t support models with garch type of filters for conditional volatility.
 
Here a few references in Markov Switching models:
   
ALEXANDER, C. (2008) ‘Market Risk Analysis: Practical Financial Econometrics’ Wiley.
BROOKS, C. (2002) ‘Introduction to Econometrics’ Cambridge University Press.
HAMILTON, J., D. (2005) Regime Switching Models. Palgrave Dictionary of Economics, (available at http://dss.ucsd.edu/~jhamilto/palgrav1.pdf )
HAMILTON, J., D. (1994) ‘Time Series Analysis’ Princeton University Press.
KIM, C., J., NELSON, C., R. (1999) State Space Model with Regime Switching: Classical and Gibbs-Sampling Approaches with Applications. The MIT press.
   
Make sure you read the file About_the_MS_Regression_Models.pdf within the downloaded zip file. There you can find details regarding the structure of the package, along with instructions of how to use it.
   
I also wrote a R/S+ version of the package (fMarkovSwitching). It is available within the Rmetrics project: http://r-forge.r-project.org/projects/rmetrics/.
   
Fell free to send any comments/suggestions to my email.

Required Products Optimization Toolbox
Statistics Toolbox
MATLAB release MATLAB 7.2 (R2006a)
Zip File Content  
Other Files
license.txt,
MS_Regress_FEX/About_the_MS_Regression_Models.pdf,
MS_Regress_FEX/Example_MS_Regress_Fit.m,
MS_Regress_FEX/Example_MS_Regress_Fit_GED_Dist.m,
MS_Regress_FEX/Example_MS_Regress_Fit_MSVAR.m,
MS_Regress_FEX/Example_MS_Regress_Fit_MultiVar.m,
MS_Regress_FEX/Example_MS_Regress_Fit_t_Dist.m,
MS_Regress_FEX/Example_MS_Regress_Fit_using_constCoeff.m,
MS_Regress_FEX/Example_MS_Regress_Fit_with_MEX.m,
MS_Regress_FEX/Example_MS_Regress_Simul_2_States_Multivar.m,
MS_Regress_FEX/Example_MS_Regress_Simul_3_States.m,
MS_Regress_FEX/Example_MS_Regress_Simul_and_Fit_2_States.m,
MS_Regress_FEX/Example_MS_Regress_Simul_and_Fit_3_States.m,
MS_Regress_FEX/Example_MS_Regress_Simul_Fit_2_States_Multivar.m,
MS_Regress_FEX/m_Files/build_constCoeff.m,
MS_Regress_FEX/m_Files/check_constCoeff.m,
MS_Regress_FEX/m_Files/checkInputs.m,
MS_Regress_FEX/m_Files/checkSize_constCoeff.m,
MS_Regress_FEX/m_Files/confuneq_MS_Regress.m,
MS_Regress_FEX/m_Files/doOutScreen.m,
MS_Regress_FEX/m_Files/doOutScreen_MSVAR.m,
MS_Regress_FEX/m_Files/doPlots.m,
MS_Regress_FEX/m_Files/getvarMatrix_MS_Regress.m,
MS_Regress_FEX/m_Files/mex_MS_Filter.cpp,
MS_Regress_FEX/m_Files/mex_MS_Filter.mexw32,
MS_Regress_FEX/m_Files/MS_Regress_Fit.m,
MS_Regress_FEX/m_Files/MS_Regress_For.m,
MS_Regress_FEX/m_Files/MS_Regress_Lik.m,
MS_Regress_FEX/m_Files/MS_Regress_Sim.m,
MS_Regress_FEX/m_Files/MS_VAR_Fit.m,
MS_Regress_FEX/m_Files/mvnpdf_MSPERLIN.m,
MS_Regress_FEX/m_Files/myMVNPDF.m,
MS_Regress_FEX/m_Files/nr3matlab.h,
MS_Regress_FEX/m_Files/param2spec.m,
MS_Regress_FEX/m_Files/preCalc_MSModel.m,
MS_Regress_FEX/m_Files/spec2param.m,
MS_Regress_FEX/matFiles/Example_FEX.mat,
MS_Regress_FEX/matFiles/GNP_Hamilton.mat,
MS_Regress_FEX/Script_Hamilton_Comparison.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (38)
07 Aug 2007 PAUL SHIWAMYA  
31 Mar 2008 Han A

Thank you. Very useful to start my own model.

12 Apr 2008 rao xhui

thank you! it is useful to me!

10 Jun 2008 tt fan

thanks!

15 Jun 2008 LONG FEI

Thanks!!!

21 Jun 2008 Anton Shishkin

This is really great. Thank you very much. Have you tried models with endogenous switching?

23 Jun 2008 Marcelo Perlin

Hi Anton,

I'm the author.
Thanks for your comments, I really apreciate it.

Regarding your question, no I haven't tried endogenous switching.

Cheers.
Marcelo.

06 Aug 2008 leo song

great!thanks your

22 Sep 2008 knani ramzi  
11 Dec 2008 Jonathan

Hi, what kind of parameter evaluation approach you are using in this package? is it the EM algorithm?

12 Dec 2008 Marcelo Perlin

Hi Jonathan,
The parameters are estimated by standard maximum likelihood procedure so, currently, the EM algorithm is not implemented.
I'll keep it in mind for the future.

Regards.
Marcelo.

12 Dec 2008 Jonathan

thanks for the quick response. Generally speaking, the code is already very useful, good work.

02 Feb 2009 Bongju

your program is useful. How can I draw the picture which compare realized y and predicted y according to data sequence?

14 Mar 2009 Elena Dumitrescu

Hi, I'm new in using matlab, but I would need to use a Markov switching model with time varying probabilities, evolving as logistic functions of the independent variables (as in Diebold and Weinbach 1994) . Can you, please, tell me how can I use your program in order to do that?
Thank you!

16 Mar 2009 Marcelo Perlin

Hi Elena,

Currently the package can't handle TVTP models as the transition matrix is assumed constant.

You'll have to modify the original code for this setup.

Regards.
Marcelo.

04 Apr 2009 Salman  
01 Aug 2009 gajd

Hi, Marcelo,

I've tried the R-package. It shows a mistake. Can we discull the mistake here or is there a special R-forum for that?

Regards, Gajd

03 Aug 2009 Marcelo Perlin

Hi gajd, fell free to contact me in my email.
Cheers.

06 Aug 2009 MehdiHK Hosseinkouchack

Nice file, thanks. Although, there are cases for which the algorithm does not work. You need to impose more structure on transition prob matrix Also if there are 2 states then there are only 2 prob's entering the search space not 4. Having more states increases the dimensions very fast.

07 Aug 2009 MehdiHK Hosseinkouchack

Well, I decided to recode things from the beginning. I think that the p-values are wrong which is because of the problem I mentioned before with the way you enter the probabilities into the search space. This is not just a dimension problem. When you calculate a numerical gradient, for the probability param's for example (for k=2), you change p11 while p12 is constant! In this case even if p11+p12 was 1, it will not be one when feeding it to the likelihood function!!
Another point is that you need to make sure that sum of the probabilities for state j (j=1,2,...,k) is 1. This is not guaranteed in your program.

07 Aug 2009 Marcelo Perlin

Dear MehdiHK, regarding your first comment, the way I coded the optimization problem was jsut a matter of simplicity in the algorithm. I'm fully aware that the search space could be decreased by one elemente by conducting it as a inequality optimization problem, as opposed to an equality one.

For your second comments, the p values of the coefficients are fine and so is the way I coded the transition matrix.
If you look closelly in MS_Regress_Fit, you'll see that I'm using constrained estimation so that the each column of Coeff.p is summing to 1, according to my choice of numerical convergence.

If you have any further question, feel free to drop me an email.
Marcelo.

07 Aug 2009 MehdiHK Hosseinkouchack

Dear Marcelo,

A) I am pretty sure that the probabilities do not sum up to the correct value. As a check: for a k=2 case, try to check if sum of all the elements in Coeff.p always is equal to 1 in all the cases!

B) About the search space, for a case of k=2, not that ONE parameters are more than enough, but that TWO are more than enough b/c P=[p11 p12; p21 p22] is identified if from each ROW exactly ONE element is know so 2 element is enough not 3. (you mentioned that 4 can be decreased by 1!!!)

C) ROWS should sum up to ONE not columns.

I closely looked into all of your codes. They are neat, but you have some mistakes that should be fixed.

Thanks a lot
Mehdi

08 Aug 2009 Marcelo Perlin

MehdiHK,

For A), from what you wrote it seems that the the transition matrix in the reference you used has a different notation than mine. In short, my transposed transition matrix will be equal to yours. Therefore, for my code, you should be looking at columns for the full process in each state (and not rows).

For B), I was not clear. I meant the decrease of one element in each state. So we agree.

Regards.

09 Aug 2009 MehdiHK Hosseinkouchack

Dear Marcelo,

This still does not solve the problem. I had a check on the probabilities: for k=2 I was checking if sum of the elements of the transition matrix adds up to 2, and it was not! well transposed or not transposed, rows or columns, does not matter now ...

For comment B decreasing search space based on my comment is agreed then.

Best of Wishes
MehdiHK

19 Sep 2009 MehdiHK Hosseinkouchack

Dear Marcelo,

I would like thank myself from your side for finding a little bug in your code. It is, however, excellent! I appreciate your efforts.

Best of Wishes
MehdiHK

29 Nov 2009 andrea182

great job!

I need help ...

I would like to create a function that given a vector of price variation I restore the prediction of the next price change ...

How could I do?

I think it's a very simple thing to do but this thing is making me crazy ...

thanks for any help ...

29 Nov 2009 Marcelo Perlin

Hi Andrea,

I used to have a function to calculate forecasts from a model but, with the new updates, the funcionality was lost. I shall write a compatible one once I have the free time.

In the meanwhile, have a look in the references writen in the pdf file. Its all there.

Regards.
Marcelo.

14 Jan 2010 Aaron

Awesome code. I am really trying to do something absurdly simple here, which might be the problem. I want to generate a univariate two-state Markov regime switching process, where the series is only related to a regime-dependent mean (e.g. the only independent variable is a column of ones). Can MS_Regress_Sim.m be used for this purpose>

15 Jan 2010 Marcelo Perlin

Hi Aron,

Thanks for your comments.

Yes, the code can handle your specification. For that, just set the indepMean=1 and indepStd=0. This will do the trick.

Regards.

22 Jan 2010 Aaron

Hi Marcelo,

Thanks...This is exactly as I thought...The problem is that there are no non-switching independent parameters in this case. Setting nS_param to the null vector produces an error

23 Jan 2010 Marcelo Perlin

Aaron, yes, I got the same error. It seems to a be a simple problem whitin the code. I'll fix it and include in the new update.

Thanks for the letting me know about this.

Regards,
M. P.

06 Feb 2010 Saad

Hi,

I am currently working on the MS VAR and I was wondering if anyone of you who have been using the MS_regress_fit got a standard error of zero and a p value of zero (just below the estimation of the coeffcients). The estimations of the coefficients of the MSVAR seem intuitive to me but I am surprised to have a standard error and a pvalue of zero? Any thoughts about this issue? Thanks in advance

10 Feb 2010 Alexey Malashonok

Hi Marcelo,

I've tried to run a couple of examples from your package and got following error:
Error using ==> fmincon
FMINCON cannot continue because...
Variable 'indep_nS' is used as a command/function.

There is probably a mistake in the code with vector of n-S independent parameters in the likelihood function. Unfortunately, I could not find where the problem is.
Could you help please.

PS: Thanks a lot for this package.

10 Feb 2010 Marcelo Perlin

Hi Alexey,

I've seen this before and it is a version issue (I wrote the code in matlab 2008).

For fixing it for you version, you'll have to search for the lines where the error was found and replace with equivalent, version compatible, lines.

Cheers.
Marcelo.

25 Feb 2010 Ansgar Walther

Hi Marcelo,

I've been trying to extend your code to include time-varying transition probabilities. I think I've got it to work, which mainly involved a few changes to the coefficient structure and the Hamilton filter in the MS_Regress_Lik file. The only odd thing is that fmincon converges to local maxima that don't make sense, so I've used fminsearch instead.

It would be great if you could have a look at it to see whether what I've done makes any sense. Drop me an email (aw452@cam.ac.uk) if you're up for it.

Cheers,

Ansgar Walther

26 Feb 2010 Marcelo Perlin

Hi Ansgar,
My replies by email.

M. P.

09 Mar 2010 Jorge

Excelent! I would like to combine TVP Models with Regime Switching as Kim y Nelson, have you any model like this?

10 Mar 2010 Marcelo Perlin

Hi Jorge,

Sorry but no, I don't have the code for regime switching time varying parameter model.

Regards.

Please login to add a comment or rating.
Updates
30 Aug 2007

* Fixed Description and organized the m files in folders

19 Sep 2007

* Added simulation and forecasting functions and scripts

08 Oct 2007

* Changed output format of transition Matrix

05 Nov 2007

Fixed small bug with MS_AR_FOR.m (I changed the probabilities name at Spec struct and forgot to also change it at MS_AR_FOR.m)

21 Nov 2007

* Added choice of distribution for estimation (normal or t) with example scripts. * Added a pdf document (details at summary). * added calculation of conditional mean and conditional std at output

26 Nov 2007

* Fixed gramatical errors at description

16 Jan 2008

* Fixed a Bug at MS_Regress_For (thanks Mr. Panagiotis Papanastasiou-Ballis). * Added a spreadsheet with different outputs from different matlab versions

16 Jan 2008

Change in description of the submission and inclusion of data (and script) for Professor's Hardy MS spreadsheet (more detais at description)

21 Jan 2008

Added calculation of standard errors by White (1984) and Newey and West (1987) and also changed a lot of the inputs structure (check the new example files).

05 Mar 2008

Fixed some grammatical errors at pdf documentation and also fixed small bug at calculation of smoothed (and not filtered) probabilities

21 Apr 2008

Fixed some name clashes with the other ms package. Changed a couple of other things, nothing special.

29 Apr 2008

Implemented mex version of hamilton's filter. The gain in speed is quite impressive (5x-8x times the original version). For instructions of how to use it, please check pdf documentation.

09 Jun 2008

Fixed but at standard error calculation. Thanks Axel Gro

22 Jul 2008

Added a few for features and fixed major bug at calculation of standard errors. All merits to Steve Guo for finding and fixing it. Thanks for the code you sended, they were of great help.

25 Jul 2008

Resend the files (last update didnt went through)

05 Aug 2008

Fixed the code for cases where a very small transition probability led the partial derivatives to zero. This means no more NaN values at the standard errors.

07 Aug 2008

fixed small mistake at the display of results

29 Sep 2008

Added the p values of coefficients in the output

09 Nov 2008

Changed a few things in pdf file, along with output to screen format of transition matrix.

15 Dec 2008

Small change at description (link to R version of package)

25 Feb 2009

* Added GED Distribution in MS_Regress_fit.m. * Added the flexibility of choosing wheter distribution parameters will switch state or not.

25 Feb 2009

* added GED distribution to MS_regress_fit, * Added the possibility of choosing if distribution parameters will switch or not.

23 Mar 2009

Added the possibility of contrained estimation (see pdf for details)

07 Apr 2009

Changed Description of Package.

16 Apr 2009

Fixed bug on MS_Regress_Fit

03 May 2009

Increased the robustness of the estimation function regarding different scales of variables. Changed a few things in manual and FEX description.

28 May 2009

fix in reference link

04 Jun 2009

Added a section in the Manual (and scripts) for estimating Hamilton's model

11 Jun 2009

Fixed small typo in MS_Regress_Fot and added a few things in manual.

13 Jun 2009

Fixed another typo due to update in notation

21 Jun 2009

A couple of improvements in Manual.

27 Oct 2009

Added support for multivariate models and autoregressive specifications, including MS VAR. See pdf for details.

31 Jan 2010

Fixed bug for the cases where simulation model only had switching variables. Included a missing script for comparison against hamilton's model.

01 Feb 2010

Fixed cases where there was an error for a simulation model with no non switching parameters (see comments by Aaron). Added a couple of more things in input checking.

Tag Activity for this File
Tag Applied By Date/Time
finance Marcelo Perlin 22 Oct 2008 09:21:33
modeling Marcelo Perlin 22 Oct 2008 09:21:33
analysis Marcelo Perlin 22 Oct 2008 09:21:33
markov switching Marcelo Perlin 22 Oct 2008 09:21:33
econometrics Marcelo Perlin 22 Oct 2008 09:21:33
regression Marcelo Perlin 22 Oct 2008 09:21:33
statistics Marcelo Perlin 09 Nov 2008 19:50:04
optimization Marcelo Perlin 09 Nov 2008 19:50:04
optimization Cristina McIntire 10 Nov 2008 10:29:48
statistics Cristina McIntire 10 Nov 2008 10:29:51
analysis Cristina McIntire 10 Nov 2008 10:30:38
modeling Cristina McIntire 10 Nov 2008 10:30:41
regression Rodolphe Sitter 07 Dec 2008 10:16:54
econometrics Kihyun Park 23 Jun 2009 12:12:39
finance jhgespinosa@gmail.com GOMEZ ESPINOSA 02 Jul 2009 10:22:37
markov switching gajd 07 Aug 2009 06:26:53
markov switching Elvis Casco 25 Aug 2009 13:42:38
statistics Paul Brown 30 Nov 2009 09:01:34
econometrics John Valica 22 Feb 2010 14:51:25
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com