The HMM Procedure

Example 18.4 Analysis of English Characters by Using a Finite HMM

In natural language processing (NLP), practitioners want to learn the structure of sentences and predict the sequences of words. HMMs have been widely applied for characterizing the statistical features of natural languages; for more information, see Jurafsky and Martin (2019). This example runs the model from Cave and Neuwirth (1980) to illustrate how to use the finite HMM to analyze the English language. The data set used in this example was extracted and converted from the classic novel Vanity Fair. To process the raw text, first remove all punctuation and numbers and convert all letters to lowercase.

The following statements create the table for the first 5,000 symbols. The table has two columns: column t is the position of the symbol, and column character is the value at time t.

data text;
   input t character $8. ;
datalines;
1   c
2   h
3   a
4   p
5   t
6   e
7   r
8

   ... more lines ...   

4992    c
4993    k
4994
4995    o
4996    n
4997
4998    h
4999    e
5000    r
;

When you get data set text, you can translate the column value into integers. The following statements relabel the character variables as the unique integers that the finite HMM procedure uses:

data tbltemp;
   set text;
   _temp_count_ = 1;
run;

proc sql;
   create table f0 as
     SELECT tbl.character, /* any other columns to be grouped */
            sum(tbl._temp_count_) as count
     FROM tbltemp tbl
     group by tbl.character
   ;
quit;

data f0Id;
   set f0;
   y = _n_;
   keep character y;
run;

proc print data=f0Id; run;

proc sort data=text out=textSort;
   by character;
run;

PROC SQL;
   CREATE TABLE textLeft AS
   SELECT A.*, B.y
   FROM textSort A LEFT JOIN f0Id B
   ON A.character = B.character
   ORDER BY character;
QUIT;

proc sort data=textLeft out=textLeft;
   by t;
run;

data text2;
   set textLeft;
run;

data mycas.text;;
   set text2;
run;

This converts the novel into a sequence of 26 distinct letters and the word space, for a total of 27 symbols. Then, translate the characters to numbers, a to 2, b to 3, …, z to 27, and word space to 1.

The conversion matching table is shown in Output 18.4.1.

Output 18.4.1: Matching Table

Obscharactery
1 1
2a2
3b3
4c4
5d5
6e6
7f7
8g8
9h9
10i10
11j11
12k12
13l13
14m14
15n15
16o16
17p17
18q18
19r19
20s20
21t21
22u22
23v23
24w24
25x25
26y26
27z27


The following statements print the table after value conversion. The table, which is shown in Output 18.4.2, contains three columns: column t is the position of the symbol, column character is the original symbol, and column y is the translated value of the symbol at position t:

proc print data=text2(obs=10) noobs;
run;

Output 18.4.2: Converted Data of Vanity Fair

tcharactery
1c4
2h9
3a2
4p17
5t21
6e6
7r19
8 1
9i10
10c4


Cave and Neuwirth’s model is assumed to have an underlying Markov chain with two hidden states. For each of these hidden states, assume that the 27 symbols appear according to a fixed probability distribution. The following statements estimate a finite HMM in which NSTATE=2 and NCATEGORY=27. There might be a lot of local maximums for the finite HMM; hence, you specify the multistart mode by specifying MULTISTART=1 in the OPTIMIZE statement.

data mycas.text;
   set text;
run;
proc hmm data=mycas.text ;
   id time=t;
   model y / type=finite nstate=2 nCategory=27;
   optimize algorithm=interiorpoint multistart=1;
run;

The number of observations and model information are shown in Output 18.4.3.

Output 18.4.3: Number of Observations and Model Information

Analysis of English Text

Observation Information
Number of Observations5000
Number of Missing Observations0

Model Information
Type of ModelFinite HMM
StationaryYes
Number of States2
Number of Dependent Variables1


The parameter estimates are shown in Output 18.4.4, and the fit statistics are shown in Output 18.4.5.

Output 18.4.4: Parameter Estimates

Parameter Estimates
ParameterEstimateStandard
Error
t ValuePr > |t|
TPM1_10.7047590.02411429.23<.0001
TPM1_20.2952410.02411412.24<.0001
TPM2_10.1669370.0252416.61<.0001
TPM2_20.8330630.02524133.00<.0001
CPM1_10.1607010.01296612.39<.0001
CPM1_20.0898680.0106028.48<.0001
CPM1_30.0303180.0064514.70<.0001
CPM1_43.2313166E-90.0000027080.000.9990
CPM1_50.0532450.0131514.05<.0001
CPM1_60.1459470.01384610.54<.0001
CPM1_79.8853784E-80.0000947720.000.9992
CPM1_81.0702962E-80.0000086080.000.9990
CPM1_90.0035580.0073720.480.6294
CPM1_100.0791020.0097188.14<.0001
CPM1_110.0104070.0032353.220.0013
CPM1_122.9038092E-90.0000024350.000.9990
CPM1_130.0846970.0110747.65<.0001
CPM1_140.0750360.0100277.48<.0001
CPM1_150.0072640.0169130.430.6676
CPM1_169.4048854E-90.0000084000.000.9991
CPM1_170.0166440.0070552.360.0183
CPM1_181.5509094E-90.0000012670.000.9990
CPM1_190.0558100.0111794.99<.0001
CPM1_200.1294210.0147028.80<.0001
CPM1_219.8906516E-90.0000092330.000.9991
CPM1_220.0004440.0040540.110.9127
CPM1_230.0204820.0039715.16<.0001
CPM1_240.0049520.0045361.090.2749
CPM1_250.0007220.0019350.370.7090
CPM1_260.0291660.0063214.61<.0001
CPM1_270.0022140.0011301.960.0502
CPM2_10.1633610.00848419.25<.0001
CPM2_20.0484280.0069536.97<.0001
CPM2_30.0025780.0027600.930.3503
CPM2_40.0382020.0040869.35<.0001
CPM2_50.0221740.0083442.660.0079
CPM2_60.0824640.0085179.68<.0001
CPM2_70.0281820.0033778.34<.0001
CPM2_80.0216060.0028777.51<.0001
CPM2_90.0740780.00667111.10<.0001
CPM2_100.0548310.0055589.87<.0001
CPM2_110.0010020.0012320.810.4159
CPM2_120.0106460.0019245.53<.0001
CPM2_134.4047865E-90.0000037470.000.9991
CPM2_140.0026470.0044080.600.5483
CPM2_150.0863860.0088979.71<.0001
CPM2_160.0920610.00750412.27<.0001
CPM2_170.0197060.0043324.55<.0001
CPM2_180.0034440.0010573.260.0011
CPM2_190.0542290.0068407.93<.0001
CPM2_200.0316910.0077564.09<.0001
CPM2_210.0998890.00797712.52<.0001
CPM2_220.0304350.0040607.50<.0001
CPM2_239.164156E-90.0000086150.000.9992
CPM2_240.0219360.0036246.05<.0001
CPM2_250.0020970.0013241.580.1133
CPM2_260.0079260.0031752.500.0126
CPM2_276.007417E-132.2325664E-80.001.0000


Output 18.4.5: Fit Statistics

Fit Statistics
Log Likelihood-14289.90987
AIC28691.819747
AICC28693.11127
BIC29056.782566
HQC28819.733474


The most interesting part of this result is the result of the category probability matrix (CPM). In state 1, the probabilities of containing 3, 4, 5, …, 26, which correspond to the consonants, are significant. In state 2, the probabilities of containing 1, 2, 6, 10, 16, 22, which correspond to the vowels a, e, i, o, and u and the space, are significant. Without your having made any assumptions about the two hidden states, the CPM shows that one hidden state contains the vowels and the other hidden state contains the consonants. Interestingly, the space is treated more like a vowel in the context of English. The PROC HMM result shows that the distinction between vowels and consonants is a statistically significant feature inherent in the language. And thanks to HMMs, this could easily be deduced by someone who has no background knowledge of the language.

Last updated: December 05, 2022