HMMBase.jl icon indicating copy to clipboard operation
HMMBase.jl copied to clipboard

Adding support for time-varying HMMs

Open kaandocal opened this issue 3 years ago • 15 comments

Some applications require HMMs where the transition matrices and/or the output distributions vary with time. Since these are simple modifications I created an experimental fork with support for these - the code is pretty much identical except that the dimensions of hmm.A and hmm.B are increased by one and that samples from these HMMs have fixed lengths. I was wondering if there would be any interest for supporting these in HMMBase, or if a separate package would be a better place for this.

Please excuse the low code quality, I was planning on improving it and add some tests later when I have more time. This is preliminary!

kaandocal avatar Apr 23 '21 12:04 kaandocal

Hi, I am just curious do you have a reference for this type of HMM? So if you observe a chain of size T, you assume that the transition matrix/emission distribution has T different values? So you feed like N realization of this T sized chain? Or just one (which to me sounds like not enough to learn the transition matrix/emission distribution)

dmetivie avatar Apr 25 '21 18:04 dmetivie

Apologies for the delay in answering!

I am not aware of any specific reference for time-inhomogeneous HMMs, but they are basically Markov models where the transition matrix is different (but fixed) at each time point t=1, 2, 3, ... This usually means that the observed chains have a fixed length T, and that there are T-1 transition matrices to learn. And indeed, you would need to observe multiple realisations of the chain which is what I am currently doing, it took me a while to realise that this is currently not supported with HMMBase.

It might be worth putting time-inhomogeneous HMMs into a separate add-on package if there's any external use for it...

kaandocal avatar May 12 '21 11:05 kaandocal

Thanks for the answer. I don't know if a separate add-on is needed or not. Inhomogeneous chains (and periodic ones) are definitely interesting for me!, so thanks for your work.

  • It seems that it is needed to add in timevaryinghmm.jl or did I miss something?
rand(hmm::TimeVaryingHMM; kwargs...) = rand(GLOBAL_RNG, hmm; kwargs...)

rand(hmm::TimeVaryingHMM, z::AbstractVector{<:Integer}) = rand(GLOBAL_RNG, hmm, z)

for the rand to work properly.

  • However, I am confused with all the export processes and adding methods to functions: with your two modified files inside my .julia package, I get errors when I want to use a standard hmm. It basically says that there are no methods fit_mle for HMM Did I integrated your code in a bad way or something. Basically what is the proper way to test you code?

dmetivie avatar Jun 01 '21 14:06 dmetivie

Sorry for that! The pull request is super buggy and I continued working on it locally. I will upload a better version soon.

kaandocal avatar Jun 01 '21 14:06 kaandocal

Thanks! You probably are working on that, but I wonder, how do you plan to manage multiple observations of the same chain of length T. Would you add the multiple observations one after another vcat(y1,y2,...) (and make sure that T loops at T+1 to 1 in the other function)? Or in a multidimensional array hcat(y1,y2,...), where you will have to make sure that the dimensions are correct with Multivariate ?

Do you plan to deal with unfinished sequence where the chain stops before reaching T ?

dmetivie avatar Jun 02 '21 08:06 dmetivie

The more correct way would be to provide a multidimensional array, ie. using hcat. fit_mle! for time-inhomogeneous HMMs takes a TxN matrix where T is the length of the HMM and N is the number of observations. To provide support for unfinished sequences one could just replace this by an array of (potentially unfinished) observations, these would only train the first few transition matrices. I guess that this scenario is a bit more common for homogeneous HMMs, however...

kaandocal avatar Jun 02 '21 08:06 kaandocal

I would be interested if this package can support time-varying HMMs. May I ask if there have been any progress on this PR?

biona001 avatar Jan 14 '22 01:01 biona001

I do not have the time anymore to review complex PRs to HMMBase, but if anyone feels like taking over maintenance for this repository, or promoting a fork with new features, let me know.

maxmouchet avatar Jan 17 '22 08:01 maxmouchet

I have a specialised fork for this that I am planning to upload this or next week, it might be of use

kaandocal avatar Jan 18 '22 13:01 kaandocal

I have a very simple version over here, it mainly adds another index for time in the HMM structures. It's not well documented but most of the code is a straightforward extension of the original, so please let me know if you have any questions.

kaandocal avatar Jan 25 '22 11:01 kaandocal

I do not have the time anymore to review complex PRs to HMMBase, but if anyone feels like taking over maintenance for this repository, or promoting a fork with new features, let me know.

Hi. If it's okay with you, I'd like to continue maintaining this package. I'd also like to complete the pull request I submitted earlier, and consider incorporating it into MLJ.jl

sosuts avatar Feb 09 '22 17:02 sosuts

Incorporating this into MLJ.jl sounds like a very good idea - HMMs are a staple in many ML applications...

kaandocal avatar Feb 09 '22 18:02 kaandocal

Hello, I am definitelly interested in this PR. @kaandocal if you could provide a notebook with usage of the time-varing it would be great :) I started implementing it also (But only on the viterbi algorithm) I also implemented a beam search extension of the viterbi algorithm that return the n best paths. I think that @SosUts proposed to continue maintaining the package. It could be a nice idea that this package stays maintained, don't you think @maxmouchet Best

organic-chemistry avatar Jun 29 '22 13:06 organic-chemistry

I did that PeriodicHiddenMarkovModels.jl based on HMMBase.jl package. Here, the transition matrices A(t) and emission distribution B(t) are periodic over t=1:T. One observes Y over several periods n=1:N. This can be used to infer A(t) and B(t). Not sure if this is exactly what you have in mind, tough.

dmetivie avatar Jun 29 '22 21:06 dmetivie

Ok thanks I will check it out. But indeed in my case it's not periodic. But I don't think it will be a problem.

organic-chemistry avatar Jul 04 '22 07:07 organic-chemistry