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 bold upper Y Subscript n comma t Baseline equals left-parenthesis y Subscript n comma 1 t Baseline comma ellipsis comma y Subscript n comma p t Baseline right-parenthesis prime, where n equals 1 comma ellipsis comma upper N and t equals 1 comma ellipsis comma upper T Subscript n Baseline, denote a p-dimensional vector of random variables at the tth position in the nth section, where upper T Subscript n is the sample size of the nth section. The bold upper Y Subscript n comma t follows the Gaussian HMM; that is,

StartLayout 1st Row  bold upper Y Subscript n comma t Baseline vertical-bar bold upper S Subscript n comma t Baseline tilde upper N left-parenthesis mu Subscript bold upper S Sub Subscript n comma t Subscript Baseline comma normal upper Sigma Subscript bold upper S Sub Subscript n comma t Subscript Baseline right-parenthesis EndLayout

where mu Subscript bold upper S Sub Subscript n comma t and normal upper Sigma Subscript bold upper S Sub Subscript n comma t are mean and covariance parameters, respectively, whose values depend on the state variable bold upper S Subscript n comma t. bold upper S Subscript n comma t follows the first-order Markov chain; that is,

StartLayout 1st Row  p left-parenthesis bold upper S Subscript n comma t Baseline vertical-bar bold upper S Subscript n comma t minus 1 Baseline comma bold upper S Subscript n comma t minus 2 Baseline comma ellipsis comma bold upper S Subscript n comma 1 Baseline right-parenthesis equals p left-parenthesis bold upper S Subscript n comma t Baseline vertical-bar bold upper S Subscript n comma t minus 1 Baseline right-parenthesis EndLayout

where p left-parenthesis period vertical-bar period right-parenthesis denotes the conditional probability. bold upper S Subscript n comma t is independent of any bold upper S Subscript m comma s Baseline comma m equals 1 comma ellipsis comma upper N comma s equals 1 comma ellipsis comma upper T Subscript m Baseline comma and m not-equals n. The range of bold upper S Subscript n comma t is a finite set, StartSet 1 comma ellipsis comma upper K EndSet. The transition probability from state i to state j is expressed as

StartLayout 1st Row  a Subscript i j Baseline equals p left-parenthesis bold upper S Subscript n comma t Baseline equals j vertical-bar bold upper S Subscript n comma t minus 1 Baseline equals i right-parenthesis EndLayout

The upper K times upper K matrix bold upper A equals StartSet a Subscript i j Baseline EndSet is called the transition probability matrix (TPM). The initial state probability vector (ISPV), pi, of the first state bold upper S Subscript n comma 1 is

StartLayout 1st Row  pi equals StartSet pi Subscript i Baseline equals p left-parenthesis bold upper S Subscript n comma 1 Baseline equals i right-parenthesis comma i equals 1 comma ellipsis comma upper K EndSet EndLayout

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 StartSet pi comma bold upper A comma bold upper B EndSet, where bold upper B identical-to StartSet mu Subscript i Baseline comma normal upper Sigma Subscript i Baseline comma i equals 1 comma ellipsis comma upper K EndSet, 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:

StartLayout 1st Row  pi equals Start 6 By 1 Matrix 1st Row  one-sixth 2nd Row  one-sixth 3rd Row  one-sixth 4th Row  one-sixth 5th Row  one-sixth 6th Row  one-sixth EndMatrix comma bold upper A equals Start 6 By 6 Matrix 1st Row 1st Column 0.95 2nd Column 0.01 3rd Column 0.01 4th Column 0.01 5th Column 0.01 6th Column 0.01 2nd Row 1st Column 0.01 2nd Column 0.95 3rd Column 0.01 4th Column 0.01 5th Column 0.01 6th Column 0.01 3rd Row 1st Column 0.01 2nd Column 0.01 3rd Column 0.95 4th Column 0.01 5th Column 0.01 6th Column 0.01 4th Row 1st Column 0.01 2nd Column 0.01 3rd Column 0.01 4th Column 0.95 5th Column 0.01 6th Column 0.01 5th Row 1st Column 0.01 2nd Column 0.01 3rd Column 0.01 4th Column 0.01 5th Column 0.95 6th Column 0.01 6th Row 1st Column 0.01 2nd Column 0.01 3rd Column 0.01 4th Column 0.01 5th Column 0.01 6th Column 0.95 EndMatrix EndLayout
StartLayout 1st Row  bold upper B equals StartSet Start 6 By 1 Matrix 1st Row  mu 1 2nd Row  mu 2 3rd Row  mu 3 4th Row  mu 4 5th Row  mu 5 6th Row  mu 6 EndMatrix equals Start 6 By 1 Matrix 1st Row  0 2nd Row  0 3rd Row  0 4th Row  0 5th Row  0 6th Row  0 EndMatrix comma Start 6 By 1 Matrix 1st Row  normal upper Sigma 1 2nd Row  normal upper Sigma 2 3rd Row  normal upper Sigma 3 4th Row  normal upper Sigma 4 5th Row  normal upper Sigma 5 6th Row  normal upper Sigma 6 EndMatrix equals Start 6 By 1 Matrix 1st Row  0.0625 2nd Row  0.25 3rd Row  1 4th Row  4 5th Row  16 6th Row  64 EndMatrix EndSet EndLayout

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
StateEstimation
10.16814
20.16340
30.16812
40.16424
50.16594
60.17016


Figure 19: Estimates of the Transition Probability Matrix (TPM)

Estimated Transition Probability Matrix
State123456
10.949930.009630.010130.010340.009700.01028
20.009920.950710.010500.009060.009420.01037
30.010400.009730.949350.009500.010670.01035
40.010100.009760.009730.949940.010520.00995
50.010150.009390.010320.010170.950120.00985
60.010020.009630.010490.010090.009320.95045


Figure 20: Estimates of the Mean Parameters

Mu
Statey
10.00025
2-0.00200
3-0.00254
40.00695
5-0.02130
60.01899


Figure 21: Estimates of the Covariance Parameters

Sigma
StateVariabley
1y0.06257
2y0.24731
3y0.99708
4y3.97252
5y15.95629
6y64.01087


Last updated: July 09, 2026