📄 ftesthmm.pas
字号:
{@abstract(test for the tutorial on hidden Markov models )
@author(Nikolai Shokhirev <nikolai@shokhirev.com> <nikolai@u.arizona.edu>
http://www.shokhirev.com/nikolai.html
http://www.u.arizona.edu/~nikolai/
http://www.chem.arizona.edu/~shokhirn/nikolai.html )
@created(2006.02.02)
㎞ikolai V. Shokhirev, 2003-2006 }
{ 2006.02.24 - added PosteriorDecodingIdxs }
unit fTestHMM;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, uMatTypes, uDynArrays, StdCtrls, uHMM;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Memo1: TMemo;
Label4: TLabel;
Label5: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
N: TInt;
M: TInt;
K: TInt;
ss: string;
Tmax: TInt;
A: IFArr2D;
B: IFArr2D;
P0: IFArr1D;
Idxs, S: IIArr1D;
hmm : Thmm;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
begin
N := 2;
M := 3;
K := 3;
(* http://en.wikipedia.org/wiki/Viterbi_algorithm
states = ('Rainy', 'Sunny')
observations = ('walk', 'shop', 'clean')
start_probability = {'Rainy': 0.6, 'Sunny': 0.4}
transition_probability = {
'Rainy' : {'Rainy': 0.7, 'Sunny': 0.3},
'Sunny' : {'Rainy': 0.4, 'Sunny': 0.6},
}
emission_probability = {
'Rainy' : {'walk': 0.1, 'shop': 0.4, 'clean': 0.5},
'Sunny' : {'walk': 0.6, 'shop': 0.3, 'clean': 0.1},
}
*)
P0 := TFArr1D.Create(N);
P0[1] := 0.6;
P0[2] := 0.4;
A := TFArr2D.Create(N,N);
A[1,1] := 0.7; A[1,2] := 0.3;
A[2,1] := 0.4; A[2,2] := 0.6;
B := TFArr2D.Create(N,M);
B[1,1] := 0.1; B[1,2] := 0.4; B[1,3] := 0.5;
B[2,1] := 0.6; B[2,2] := 0.3; B[2,3] := 0.1;
// ['walk', 'shop', 'clean']
Idxs := TIArr1D.Create(K);
Idxs[1] := 1; Idxs[2] := 2; Idxs[3] := 3;
hmm := Thmm.Create(A, B, P0);
Label1.Caption := 'Forward: '+FloatToStr(hmm.GetProbabilityF(Idxs));
Label2.Caption := 'Backward: '+FloatToStr(hmm.GetProbabilityB(Idxs));
S := hmm.ViterbiStateIdxs(Idxs);
ss := 'Viterbi States: ';
for i := S.Lo1 to S.Hi1 do
ss := ss + IntToStr(S[i]) +',';
Label3.Caption := ss;
//Output: ['Sunny', 'Rainy', 'Rainy']
S := hmm.PosteriorDecodingIdxs(Idxs);
ss := 'Posterior States: ';
for i := S.Lo1 to S.Hi1 do
ss := ss + IntToStr(S[i]) +',';
Label5.Caption := ss;
hmm.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i : integer;
begin
N := 3;
M := 3;
K := 12;
(* http://en.wikipedia.org/wiki/Viterbi_algorithm
states = ('bull', 'bear','normal')
observations = ('up', 'down', 'unchanged')
start_probability = {'bull': 0, 'bear': 0, 'normal': 1}
transition_probability = {
'bull' : {'bull': 0.6, 'bear': 0.2, 'normal': 0.2},
'bear' : {'bull': 0.5, 'bear': 0.3, 'normal': 0.2},
'normal': {'bull': 0.4, 'bear': 0.1, 'normal': 0.5},
}
emission_probability = {
'bull' : {'up': 0.7, 'down': 0.1, 'unchanged': 0.2},
'bear' : {'up': 0.1, 'down': 0.6, 'unchanged': 0.3},
'normal': {'up': 0.3, 'down': 0.3, 'unchanged': 0.4},
}
*)
P0 := TFArr1D.Create(N);
P0[1] := 0.0;
P0[2] := 0.0;
P0[3] := 1.0;
A := TFArr2D.Create(N,N);
A[1,1] := 0.6; A[1,2] := 0.2; A[1,3] := 0.2;
A[2,1] := 0.5; A[2,2] := 0.3; A[2,3] := 0.2;
A[3,1] := 0.4; A[3,2] := 0.1; A[3,3] := 0.5;
B := TFArr2D.Create(N,M);
B[1,1] := 0.7; B[1,2] := 0.1; B[1,3] := 0.2;
B[2,1] := 0.1; B[2,2] := 0.6; B[2,3] := 0.3;
B[3,1] := 0.3; B[3,2] := 0.3; B[3,3] := 0.4;
// up, up, down, unchanged, unchanged, unchanged, up, up, down, down, down, down
// 1 1 2 3 3 3 1 1 2 2 2 2
Idxs := TIArr1D.Create(K);
Idxs[1] := 1; Idxs[2] := 1; Idxs[3] := 2; Idxs[4] := 3; Idxs[5] := 3;
Idxs[6] := 3; Idxs[7] := 1; Idxs[8] := 1; Idxs[9] := 2; Idxs[10] := 2;
Idxs[11] := 2; Idxs[12] := 2;
hmm := Thmm.Create(A, B, P0);
Label1.Caption := 'Forward: '+FloatToStr(hmm.GetProbabilityF(Idxs));
Label2.Caption := 'Backward: '+FloatToStr(hmm.GetProbabilityB(Idxs));
S := hmm.ViterbiStateIdxs(Idxs);
ss := 'Viterbi States: ';
for i := S.Lo1 to S.Hi1 do
ss := ss + IntToStr(S[i]) +',';
Label3.Caption := ss;
//Output: ['Sunny', 'Rainy', 'Rainy']
S := hmm.PosteriorDecodingIdxs(Idxs);
ss := 'Posterior States: ';
for i := S.Lo1 to S.Hi1 do
ss := ss + IntToStr(S[i]) +',';
Label5.Caption := ss;
hmm.Free;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -