-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo_normal.jl
53 lines (40 loc) · 1.02 KB
/
demo_normal.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Demo of univariate normal HMM using Viterbi algorithm and Baum-Welch.
include("models.jl")
module demo_normal
using PyPlot
using Distributions
using NormalHMM
# settings
m = 2
n = 1000
tolerance = 1e-3
# parameters
log_pi = log([.4,.6])
log_T = log([.9 .1
.2 .8])
phi = [Normal(-1,1),Normal(1,1)]
# simulate data
x,z0 = NormalHMM.generate(n,log_pi,log_T,phi)
# compute optimal z
z = NormalHMM.viterbi(x,log_pi,log_T,phi)
# estimate parameters
log_pi_est,log_T_est,phi_est,log_m_est = NormalHMM.estimate(x,m,tolerance)
# display results
println("\nViterbi percent correct:")
println(mean(z.==z0))
println("\nTrue transition matrix:")
println(exp(log_T))
println("\nEstimated transition matrix:")
println(exp(log_T_est))
println("\nTrue emission distributions:")
println(phi)
println("\nEstimated emission distributions:")
println(phi_est)
figure(1); clf(); hold(true)
plot(1:n,x,"k.")
plot(1:n,[-1,1][z0],"b-")
figure(2); clf(); hold(true)
plot(1:n,[-1,1][z0],"b-")
plot(1:n,[-1,1][z],"r-")
ylim(-2,2)
end # module