Professional Documents
Culture Documents
USER MANUAL
Marcelo Perlin Reading University (UK), Henley Business School Contact: marceloperlin@gmail.com
1
Contents
INTRODUCTION .................................................................................................................. 3 ESTIMATING MARKOV SWITCHING MODELS ......................................................... 3
INTERFACE TO UNIVARIATE MODELLING ...........................................................................................................3 INTERFACE TO MULTIVARIATE MODELLING ......................................................................................................7 Estimating Regime Switching Vector Autoregressive Models (MS VAR) ..................................................7 THE OUTPUT FROM THE ESTIMATION FUNCTION ................................................................................................8 USING ADVANCED OPTIONS ...............................................................................................................................8 Using the Mex Version of Hamiltons Filter ............................................................................................... 10 Using advOpt.constCoeff (constraining coefficients) ................................................................................. 11
HAMILTON (1989)S MODEL .......................................................................................... 14 ADVICES FOR USING THE PACKAGE ......................................................................... 16 REPORTING A BUG/PROBLEM ..................................................................................... 17 CITING THE PACKAGE ................................................................................................... 17 REFERENCES...................................................................................................................... 17
Introduction
The purpose of this document is to show the general structure of this matlab package1 designed to handle markov switching specifications. The mathematical notation behind markov switching models and Hamiltons filter is not going to be exposed here. My advice to those who are just starting out is to first read through the papers and books at the references and then come back here to check the application of the algorithm and the computational structure (input/output) of the package.
yt = 1, St x1t + 2, St x2t + t
2 t N ( 0, S )
t
St
2 S
t
- represent the state at time t, that is, St = 1....K , where K is the number of states - Error variance at state St
t
i, S
- beta coefficient for explanatory variable i at state St where i goes from 1 to n - residual vector which follows a particular distribution (in this case Normal)
t
1
I also wrote a shorter/limited version of the package in R (S+) which I name fMarkovSwitching. It is available in the Rmetrics project (http://r-forge.r-project.org/projects/rmetrics/).
Now, changing the input argument to S=[1 1 0], the model is:
yt = 1, St x1t + 2, St x2t + t
t N ( 0, 2 )
Notes that that with S=[1 1 0], the variance term is no longer switching states. Now, with the switching argument as S=[0 1 1], the model is:
yt = 1 x1t + 2, St x2t + t
2 t N ( 0, S
t
With this change in the input argument S, only the second coefficient and the models variance are switching according to the transition probabilities, that is, the first coefficient ( 1 ) doesnt change states. Therefore, the logic is clear: the first elements of S control the switching dynamic of the mean equation, while the last term controls the switching dynamic of the residual vector, including distribution parameters. As an example, consider the following definition for a model with GED (generalized error distribution) errors and with S=[1 1 1 1]. This configuration yields:
yt = 1, St x1t + 2, St x2t + t
2 t ~ GED ( 0, S , KS
t t
In this setup the new parameter2 K, will also switch states. If we had set S=[1 1 1 0], the model would switch in all coefficients, except in the K parameter. As an example for the markov switching fitting function, the following options at MS_Regress_Fit():
dep=logRet(:,1); constVec=ones(length(dep),1); indep=[constVec logRet(:,2:3)]; k=2; S=[1 0 0 1]; advOpt.distrib='Normal'; advOpt.std_method=1;
2
% % % % % % %
Defining dependent variable Defining a constant vector Defining some explanatory variables Number of States Defining which variables switch The Distribution Defining method for std error
K is a parameter of the GED distribution and should not be confused with k (number of states in the model).
t ~ N ( 0, 12 )
State 2 (St = 2) yt = 1,2 x1t + 2 x2 t + 3 x3t + t
t N ( 0, 22 )
with:
p P = 11 p12
p21 p22
as the transition matrix, which controls the probability of a switch from state j (column j) to state i (row i). The sum of each column in P is equal to one, since they represent full probabilities of the process for each state. Note that these options to the fitting function are the same provided at the example script Example_MS_Regress_Fit.m. Another flexibility of the package is that, since I wrote it for dealing with a generic type of regression, you can set any kind of explanatory variable in the model as long as they are observed (you have it available for the whole time period studied). This includes autoregressive components, constants or just plain regression on other variables. This means that you can build your own econometric model without changing any part of the original code. If you run the script Example_MS_Regress_Fit.m with matlab version3 7.6.0.324 (R2008a), this is the output you should be getting if you have all the proper packages installed (optimization, statistics). Note that Ive also added some clarifications for the output (in red).
You should be aware that different versions of matlab will output different maximum likelihood estimates, but the divergence should be small.
***** Numerical Optimization Converged ***** Final log Likelihood: 2487.1985 Number of estimated parameters: 10 Type of Switching Model: Univariate Distribution Assumption -> Normal Method SE calculation -> 1 ***** Final Parameters for Equation #1 ***** ---> Non Switching Parameters <--(the parameters that dont switch (the 0 options at input S) (maximum log likelihood found) (number of estimated parameters in the model) (type of model: univariate or multivariate) (the distribution assumption. You can change it at advOpt.distrib) (method for SE calculation, see using advOpt topic)
Non Switching Parameter for Equation #1, Indep column 2 (value of non switching coefficient for indep column 2) Value: 0.4798 Std Error (p. value): 0.0269 (0.00) (standard error and p value for coefficient) Non Switching Parameter for Equation #1, Indep column 3 Value: 0.1564 (value of non switching coefficient for indep column 3) Std Error (p. value): 0.0333 (0.00) (standard error and p value for coefficient) ---> Switching Parameters (Distribution Parameters) <--the parameters that do switch (the 1 options at input S)
State 1 Model's Variance: Std Error (p. value): State 2 Model's Variance: Std Error (p. value):
(the models variance at state 1) (standard error and p value for corresponding sigma) (the models variance at state 2) (standard error and p value for corresponding sigma)
---> Switching Parameters (Regressors) <--Switching Parameters for Equation #1 - Indep column 1 State 1 Value: 0.0003 Std Error (p. value): 0.0007 (0.66) State 2 Value: 0.0006 Std Error (p. value): 0.0017 (0.75)
(value of the switching variable Indep column 1 at state 1) (associated standard error and p value) (value of the switching variable Indep column 1 at state 2) (associated standard error and p value) (the matrix that controls the probability of each regime switch, along with standard errors and pvalues)
0.99 (0.03,0.00) 0.02 (0.01,0.04) 0.01 (0.00,0.06) 0.98 (0.02,0.00) ---> Expected Duration of Regimes <--(the expected duration of each regime, calculated as 1/(1p_ii) )
Expected duration of Regime #1: 109.27 time periods Expected duration of Regime #2: 43.57 time periods
y1,t = 1,1, St x1t + 1,2, St x2t + 1,t y2,t = 2,1, St x3t + 2,t
2 1,t N ( 0, 1, S
2 2,t N ( 0, 2, S
) )
% % % % % % matrix with 2 columns independent matrix for first equation independent matrix for second equation Number of States Defining S for Equation 1 Defining S for Equation 2
St = 1, 2
This last system of 2 equations is translated in the packages notation as:
dep=y; indep{1}=[x1 x2]; indep{2}=x3; k=2; S{1}=[1 1 1]; S{2}=[1 1];
So, as you can see, the inputs have the same shape as for the univariate case, but they are iterated over the equations using cell arrays. All options to univariate modelling, including reduced estimation, are also available for the multivariate case. For further details see Example_MS_Regress_Fit_MultiVar.m.
These fields should contain all the information you need regarding your model.
4 5
These are calculated based only on filtered probabilities prior to time t. These are also calculated with information prior to time t.
(degrees of freedom). If advOpt.distrib=GED then the distribution for the error is the Generalized Error distribution with one extra parameter (K). advOpt.std_method-Defines the method to be used for the calculation of the standard errors. The options are: advOpt.std_method=1 --> Calculation of the covariance matrix is performed using the second partial derivatives of log likelihood function advOpt.std_method=2 --> Calculation of the covariance matrix is performed using the first partial derivatives of log likelihood, that is, the outer product matrix advOpt.std_method=3 --> Calculation of the covariance matrix is performed using whites method. The resulting covariance matrix is robust to heteroskedasticity of unknown form. advOpt.std_method=4 --> Calculation of the covariance matrix is performed using Newey and West method. The resulting covariance matrix is robust to heteroskedasticity and serial correlation. More details for the first three types of covariance matrix can be found at Hamilton (1994), chapter 5. For the last one it can be found in Newey and West (1987). advOpt.useMex - Defines whether to use mex version of Hamilton filter in the calculation of likelihood function. The advantage in using the mex version is the increase of speed in the fitting function. But, in order to use it, youll need a proper C++ compiler. Please check next topic for more details. advOpt.constCoeff - Defines the use of constrained/reduced estimation of the model. See following topic for instructions on how to use it. advOpt.diagCovMat - Defines the use of diagonal matrix for sigma (covariance matrix) in multivariate estimation only. advOpt.printOut Flag for printing out to screen the model in the end of estimation. advOpt.printIter Flag for printing out numerical iterations of maximum likelihood estimation. advOpt.doPlots Flag for plotting fitted conditional standard deviations and smoothed probabilities in the end of estimation. The next table show the possible values for each input in advOpt and also the default values (if no value is assigned).
Table 1 Default Values for input arguments in advOpt Input argument advOpt.distrib advOpt.std_method advOpt.useMex Possible Values Normal, t or GED 1,2,3 or 4 1 or 0 Default Value Normal 1 0 A structure with fields containing string e. This means that all coefficients are estimated from data (no restrictions on coefficients) 1 1 1 1
advOpt.constCoeff
1 or 0 1 or 0 1 or 0 1 or 0
10
2) The size of each of these fields7 are set according to the number of switching/non switching parameters and the number of states in the model (value of k). For all of them (except covMat), the cells iterates over the equations in the system, the columns iterate the states and the rows iterate the coefficients. For instance, for nS_Param{iEq}, the element (1,1) of this matrix is the fist8 non switching parameters of equation iEq, the element (2,1) is the second non switching parameter and so on. For S_Param{iEq}, the element (1,2) is the parameter of the first9 switching variable, at state 2, equation iEq. The sizes of covMat, df and K also have to respect the choices made at input S. For instance if the model is switching standard deviation, then covMat should have size {1,k}, otherwise it should be a {1,1} cell. 3) The rule for building the fields is, use the string e (as in estimate) if you want the respective parameter to be estimated from data or a numeric value if you want the respective parameter to take that value. For instance, using:
Again, all options would have to be provided by cell array iterations over all equations and fields (e.g. advOpt.constCoeff.S_Param{1}={ 0.5 , e};. 7 All the fields in constCoeff should be cell arrays (and not matrices). See example files for details. 8 The order is defined by input S. For example, if S=[1 0 1 1], then the first non switching coefficient is column 2. 9 The order is also defined by S. See previous footnote.
11
youre telling MS_Regress_Fit to set the coefficients of the first switching parameter of equation #1, in state 1, equal to 0.5, the second switching parameter, state 2, equal to 0.1 and estimate the rest of them. Examples should make the structure on the use of advOpt.constCoeff clear. Consider the estimation of the following model:
t N ( 0, 12 )
State 2 (St = 2) yt = 1 + 1,2 x1t + 0.x2t + t
2 t N ( 0, 2 )
with:
p P = 11 p12
1,1 = 0 2,2 = 0
p21 p22
In order to estimate this model, these are the options fed to the fitting function (Example_MS_Regress_Fit_using_constCoeff.m):
k=2; S=[0 1 1 1]; advOpt.distrib='Normal'; advOpt.std_method=1; % % % % Number of States Defining which will switch states The Distribution assumption Defining the method for calculation
12
% fix the switching parameter of indep column 2 to 0 at state 1, indep column 3 equal to 0 at state 2 and estimate the rest advOpt.constCoeff.S_Param{1}={ 0 ,'e' ; 'e', 0 } % estimate both standard deviations/variances advOpt.constCoeff.covMat{1}(1,1)={'e'}; % cells iterate over states advOpt.constCoeff.covMat{2}(1,1)={'e'}; % estimate all transition probabilities advOpt.constCoeff.p={'e','e' ;... 'e','e' };
t N ( 0, 0.0004 )
State 2 (St = 2) yt = 1 + 1,2 x1t 0.8 x2t + t
2 t N ( 0, 2 )
0.95 P= 0.05
p21 p22
% estimate the non switching parameter (cells iterates over equations, in this case only 1) advOpt.constCoeff.nS_Param{1}={'e'}; % fix the switching parameter of indep column 2, state 1 to 0.5, the switching parameter of indep column 3, state 2 to -0.8 and estimate the rest advOpt.constCoeff.S_Param{1}={0.5, 'e' ; 'e',-0.8}; % set variance at state 1 as 0.0004 (standard deviation equal to sqrt(0.0004)=0.02) and estimate variance for the state 2 % for covMat the cells iterate over states advOpt.constCoeff.covMat{1}(1,1)={0.02^2)}; advOpt.constCoeff.covMat{2}(1,1)={'e'}; % set p11=0.95, p12=0.05 and estimate the rest advOpt.constCoeff.p={0.95,'e' ; 0.05,'e'};
The use of constCoeff should be clear by now. For any further enquiry, feel free to contact me.
t N ( 0, 2 )
As you can see from previous equation, the independent variables are conditional on the states, an unobserved process. That means that the regressors,
(y
t i
unobserved prior to estimation. My package was not built to deal with this type of setup but a
14
2 step estimation process is identifiable. Consider the following notation for Hamiltons Model:
zt = yt uSt zt = i zt + t
i =1
4
t N ( 0, 2 )
Now, rearranging first equation we get:
yt = uSt + zt
where zt are the residuals from this reduced model. This setup is possible to estimate using my package. The two steps for the approximation are: 1. Estimate:
yt = uSt + 1,t
1,t N ( 0, 2 )
St = 1, 2 2. Retrieve the 1, t vector and regress it on its 4 lags:
2,t N ( 0, 22 )
2 Note that the parameter 2 , from last equation, will approximate 2 of Hamiltons model. Next, I show the estimated parameters from Hamilton (1989) and the parameters from my approximation10:
Two-step MS_Regress_Fit 1.101 -0.480 0.906 0.682 0.988 0.126 0.104 -0.133 -0.104
phi 1 0.010 phi 2 -0.060 phi 3 -0.250 phi 4 -0.210 * from Hamilton (1994), page 698
10
From last table, one can see that the parameters are fairly comparable for the markov regime switching part of the model. For the autoregressive part, they are not as comparable as for the first part but, since most of the ' s are not statistically significant, the approximation performance is still good. When looking at the regimes probabilities (Hamilton(1994), page 697), its also clear that they are very similar. The code for this estimation is available within the packages zip file (Script_Hamilton_Comparison.m).
16
Reporting a Bug
Im very happy to hear about any sort of bug in the program. If you have found one please report it to my email containing: Name of functions and lines of alleged bug (if applicable). Reason why you think it is a bug. Zip file with codes youre running (including data and scripts). Matlabs error message (if applicable). And Ill happily look into it.
References
ALEXANDER, C. (2008) Market Risk Analysis: Practical Financial Econometrics Wiley. BROOKS, C. (2002) Introduction to Econometrics Cambridge University Press. HAMILTON, J., D. (1989) A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle Econometrica, ed. 57. HAMILTON, J., D. (1994) Time Series Analysis Princeton University Press. HAMILTON, J., D. (2005) Regime Switching Models Palgrave Dictionary of Economic. KIM, C., J., NELSON, C., R. (1999) State Space Model with Regime Switching: Classical and Gibbs-Sampling Approaches with Applications The MIT press. NEWEY, B., WEST, D (1987) A Simple, Positive semidefinite, Heteroskedasticity and Autocorrelation Consistent Covariance Matrix Econometrica, ed. 55, p. 347-370. WHITE, H. (1984) Asymptotic Theory for Econometricians New York: Academic Press.
17