HMM Procedure
Gaussian Hidden Markov Model for Cross-Sectional Time Series Data
Cross-sectional time series data consist of observations over both time and many subjects (such as individuals, objects, firms, or geographical areas). Let , where
and
, denote a p-dimensional vector of random variables at the tth position in the nth section, where
is the sample size of the nth section. The
follows the Gaussian HMM; that is,
where and
are mean and covariance parameters, respectively, whose values depend on the state variable
.
follows the first-order Markov chain; that is,
where denotes the conditional probability.
is independent of any
and
. The range of
is a finite set,
. The transition probability from state i to state j is expressed as
The matrix
is called the transition probability matrix (TPM). The initial state probability vector (ISPV),
, of the first state
is
The Gaussian HMM for cross-sectional time series data, which is the same as the Gaussian HMM for time series data, can be described as , where
, which consists of parameters that define the state-dependent distribution of observable variables.
Consider a univariate six-state Gaussian HMM that has the following parameter values:
The following statements simulate the 10,000-section time series from the previous model to provide test data for the HMM procedure:
%let nSections = 10000;
%let T = 100;
%let seed = 1234;
%let nStates = 6;
%let mu1 = 0;
%let sigma1 = 0.0625;
%let mu2 = 0;
%let sigma2 = 0.25;
%let mu3 = 0;
%let sigma3 = 1;
%let mu4 = 0;
%let sigma4 = 4;
%let mu5 = 0;
%let sigma5 = 16;
%let mu6 = 0;
%let sigma6 = 64;
%let selfTransProb = 0.95;
%macro createCstsTable(tableName);
data &tableName.;
do sec = 1 to &nSections.;
state = ceil(uniform(&seed.)*&nStates.);
do t = 1 to &T.;
%do i = 1 %to &nStates.;
if(state=&i.) then do;
y = &&mu&i.. + sqrt(&&sigma&i..)*normal(&seed.);
output;
end;
%end;
u = uniform(&seed.);
if(u>&selfTransProb.) then do;
u = (u-&selfTransProb.)/(1-&selfTransProb.)*(&nStates.-1);
state=state + ceil(u);
if(state>&nStates.) then state = state - &nStates.;
end;
end;
end;
run;
%mend;
%createCstsTable(cstsDGP);
data mylib.cstsDGP;
set cstsDGP;
run;
The following statements estimate the Gaussian HMM:
proc hmm data=mylib.cstsDGP
outstat=mylib.cstsStat;
id section=sec time=t;
model y / method=ml type=gaussian nstate=6;
optimize ALGORITHM=activeset printlevel=3 printIterFreq=1;
estimate out=mylib.cstsEst;
evaluate out=mylib.cstsEval;
decode out=mylib.cstsDecode;
filter out=mylib.cstsFilter;
smooth out=mylib.cstsSmooth;
forecast out=mylib.cstsForcast;
score outmodel=mylib.cstsModel;
run;
The estimates of the ISPV, TPM, mean parameters, and covariance parameters are shown in Figure 18, Figure 19, Figure 20, and Figure 21, respectively. They are all very close to the true parameters that are used in the data generating process.
Figure 18: Estimates of the Initial State Probability Vector (ISPV)
| Initial State Probabilities | |
|---|---|
| State | Estimation |
| 1 | 0.16814 |
| 2 | 0.16340 |
| 3 | 0.16812 |
| 4 | 0.16424 |
| 5 | 0.16594 |
| 6 | 0.17016 |
Figure 19: Estimates of the Transition Probability Matrix (TPM)
| Estimated Transition Probability Matrix | ||||||
|---|---|---|---|---|---|---|
| State | 1 | 2 | 3 | 4 | 5 | 6 |
| 1 | 0.94993 | 0.00963 | 0.01013 | 0.01034 | 0.00970 | 0.01028 |
| 2 | 0.00992 | 0.95071 | 0.01050 | 0.00906 | 0.00942 | 0.01037 |
| 3 | 0.01040 | 0.00973 | 0.94935 | 0.00950 | 0.01067 | 0.01035 |
| 4 | 0.01010 | 0.00976 | 0.00973 | 0.94994 | 0.01052 | 0.00995 |
| 5 | 0.01015 | 0.00939 | 0.01032 | 0.01017 | 0.95012 | 0.00985 |
| 6 | 0.01002 | 0.00963 | 0.01049 | 0.01009 | 0.00932 | 0.95045 |
Figure 20: Estimates of the Mean Parameters
| Mu | |
|---|---|
| State | y |
| 1 | 0.00025 |
| 2 | -0.00200 |
| 3 | -0.00254 |
| 4 | 0.00695 |
| 5 | -0.02130 |
| 6 | 0.01899 |
Figure 21: Estimates of the Covariance Parameters
| Sigma | ||
|---|---|---|
| State | Variable | y |
| 1 | y | 0.06257 |
| 2 | y | 0.24731 |
| 3 | y | 0.99708 |
| 4 | y | 3.97252 |
| 5 | y | 15.95629 |
| 6 | y | 64.01087 |