jmschrei / pomegranate

Fast, flexible and easy to use probabilistic modelling in Python.
http://pomegranate.readthedocs.org/en/latest/
MIT License
3.38k stars 590 forks source link

[Question] Possible to freeze/constrain specific parameters? #1056

Open ebigelow opened 1 year ago

ebigelow commented 1 year ago

I'm trying to fit a MC or HMM with simple constraints on parameters - for example, keeping certain HMM state transitions frozen to 0 while fitting other state transitions, or enforcing that a 1-order Markov chain fits only 1 parameter instead of 2 (e.g. transition table with binary data is [[p, 1-p], [p, 1-p]] instead of [[p1, 1-p1], [p2, 1-p2]]).

Is this possible in pomegranate?

I've spent a while going through the documentation and messing with passing in custom distributions to MarkovChain. It seems like this could be possible with using an IndependentComponents distribution with separate distributions for each parameter, but then, my code for this doesn't work since MarkovChain expects the higher-order distributions to have a signature d.sample(n, X), which ConditionalCategorical has, but IndependentComponents.sample only takes 1 arg n. I haven't tried passing a custom transition distribution to the HMM class but I expect there are similar issues.

Other github issues that are similar, but don't seem to answer this:

jmschrei commented 1 year ago

Right now, if you set a HMM transition probability to 0 it will stay 0 forever. As for more general freezing of parameters, even though it would be something conceptually simple to add, it doesn't exist in a convenient form right now and I don't have the time :(. I am planning to do a burst of development in the October range, but right now I'm trying to get material ready for the academic job market.

Something you could do as a workaround is implement your own .fit function for HMMs that, after each round of training, goes in and resets the transition probabilities you'd like frozen to what they should've been. It shouldn't be too complicated.

I'm not sure what you're trying to do with MarkovChain but IndependentComponents means that each distribution represents a different feature in the data without covariance, whereas ConditionalCategorical depends on the previously generated observations (hence, the X requirement).

ebigelow commented 1 year ago

Right now, if you set a HMM transition probability to 0 it will stay 0 forever.

Got it, that's good to know. Fair enough about time constraints and job hunting, thanks for all your hard work on this project outside of that!

Earlier, I considered making the HMM change you suggested, but instead ended up exploring probabilistic programming languages like Pyro, since I'd also like to fit models with simple constraints other than freezing params, e.g. constraint that two parameters have the same value. Pyro seems a bit overkill for my use case, since I'd just like to fit HMMs/MCs with some simple parameter constraints - no fancy inference, simple model implementation, no hyper-priors, etc.

Re: Markov chains - I'm trying to implement simple chains for binary data, representing (a) 1/0 bias, or (b) repetition vs. alternation bias, where (b) is a simple model of the gambler's fallacy.

State diagrams from Nickerson, 2002 "The production and perception of randomness": Screenshot 2023-08-11 at 5 02 16 PM