Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Adding support for time-varying HMMs #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kaandocal
Copy link

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!

@dmetivie
Copy link

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)

@kaandocal
Copy link
Author

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...

@dmetivie
Copy link

dmetivie commented Jun 1, 2021

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?

@kaandocal
Copy link
Author

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

@dmetivie
Copy link

dmetivie commented Jun 2, 2021

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 ?

@kaandocal
Copy link
Author

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...

@biona001
Copy link
Contributor

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

@maxmouchet
Copy link
Owner

maxmouchet commented Jan 17, 2022

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.

@kaandocal
Copy link
Author

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

@kaandocal
Copy link
Author

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.

@sosuts
Copy link

sosuts commented Feb 9, 2022

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

@kaandocal
Copy link
Author

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

@organic-chemistry
Copy link

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

@dmetivie
Copy link

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.

@organic-chemistry
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants