Basic Usage

using Distributions
using HMMBase
using PyPlot
using Seaborn

Model Specification

B can contains any probability distribution from the Distributions package

a = [0.6, 0.4]
A = [0.9 0.1; 0.1 0.9]
B = [MvNormal([0.0, 5.0], ones(2) * 1), MvNormal([0.0, 5.0], ones(2) * 3)]
hmm = HMM(a, A, B)
size(hmm) # (number of states, observations dimension)
(2, 2)

Sampling

z, y = rand(hmm, 500, seq = true)
([1, 2, 2, 2, 1, 1, 1, 1, 1, 1  …  2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [-0.493490750379989 5.31524987805863; 0.6422657268664405 5.830559001317976; … ; 4.013288778027721 1.6921191920569503; 0.4884112876031232 2.361287879046037])

Let's plot the observations and the hidden state sequence:

_, axes = subplots(nrows = 2, figsize = (9, 3))
axes[1].plot(y)
axes[2].plot(z, linestyle = :steps)

We can also drop the time dimension and plot the data in the plane:

_, axes = subplots(ncols = 2, figsize = (9, 3))
axes[1].scatter(y[:, 1], y[:, 2], s = 3.0)
axes[2].scatter(y[:, 1], y[:, 2], s = 3.0, c = z, cmap = "tab10")
axes[1].set_title("Observations")
axes[2].set_title("Observations and hidden states")

Inference

α, logtot = forward(hmm, y)
β, logtot = backward(hmm, y)

γ = posteriors(hmm, y) # or
γ = posteriors(α, β)

size(α), size(β), size(γ)
((500, 2), (500, 2), (500, 2))
plot([α[:, 1] β[:, 1] γ[:, 1]])
legend(["Forward", "Backward", "Posteriors"], loc = "upper right")
_, axes = subplots(ncols = 3, figsize = (9, 3))
for (ax, probs, title) in zip(axes, [α, β, γ], ["Forward", "Backward", "Posteriors"])
    ax.scatter(y[:, 1], y[:, 2], s = 3.0, c = probs[:, 1], cmap = "Reds")
    ax.set_title(title)
end
z_map = [z.I[2] for z in argmax(γ, dims = 2)][:]
z_viterbi = viterbi(hmm, y)

plot([z z_map z_viterbi])
legend(["True sequence", "MAP", "Viterbi"], loc = "upper right")
_, axes = subplots(ncols = 2, figsize = (9, 3))
for (ax, seq, title) in zip(axes, [z_map, z_viterbi], ["MAP", "Viterbi"])
    ax.scatter(y[:, 1], y[:, 2], s = 3.0, c = seq, cmap = "Reds_r")
    ax.set_title(title)
end

Parameters Estimation

hmm = HMM(randtransmat(2), [MvNormal(rand(2), ones(2)), MvNormal(rand(2), ones(2))])
hmm, hist = fit_mle(hmm, y, display = :iter, init = :kmeans)
hmm
HMM{Multivariate,Float64}([0.9999999999778088, 2.2191275774011083e-11], [0.9113885698572843 0.08861143014271576; 0.093121941756994 0.906878058243006], Distribution{Multivariate,S} where S<:ValueSupport[DiagNormal(
dim: 2
μ: [-0.0039025970134157436, 5.07548829484438]
Σ: [0.9564920337388856 0.0; 0.0 0.8669369297511905]
)
, DiagNormal(
dim: 2
μ: [0.325022929516672, 4.5825628487696894]
Σ: [8.380672324361253 0.0; 0.0 8.541357225102102]
)
])
plot(hist.logtots)

This page was generated using Literate.jl.