maxmouchet / HMMBase.jl

Hidden Markov Models for Julia.
MIT License
94 stars 12 forks source link

2 state and 3 observation HMM #12

Closed GuilhermePFM closed 4 years ago

GuilhermePFM commented 4 years ago

Hello,

Thank you for this very nice package. I am just learning HMM and I would like to know if is it possible to estimate a HMM with 2 hidden states for 3 observations like this:

hmm = HMM([0.9 0.1; 0.1 0.9], [MvNormal(ones(3)), MvNormal(ones(3))])
nstates, obs_dim = size(hmm)
y = rand(hmm, 1000) 

I tried it but it gives an error of dimension mismatch:

ERROR: DimensionMismatch("tried to assign 3-element array to 1×2 destination")

Am I doing something wrong here?

GuilhermePFM commented 4 years ago

I think it just needs to change this function:

function rand(rng::AbstractRNG, hmm::AbstractHMM{Multivariate}, z::AbstractVector{<:Integer})
    y = Matrix{Float64}(undef, length(z), size(hmm, 1))
    for t in eachindex(
        y[t,:] = rand(rng, hmm.B[z[t]])
    end
    y
end

to y = Matrix{Float64}(undef, length(z), size(hmm, 2))

I tested here and it seems to be working. If that is ok with you I can open a PR with the fix and also add a new test for that.

maxmouchet commented 4 years ago

Hi Guilherme,

You're right, thanks for reporting this bug! Feel free to make a PR if you have the time :)

You can add a test in unit.jl: https://github.com/maxmouchet/HMMBase.jl/blob/master/test/unit.jl#L48-L76
Maybe you can add a third hmm (hmm3) which has a different number of states and observations, with a comment that links to this issue.

GuilhermePFM commented 4 years ago

Ok! Will do this right now, thank you