Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference for mixture distributions #1307

Closed
kaandocal opened this issue Apr 20, 2021 · 5 comments · Fixed by #1308
Closed

Type inference for mixture distributions #1307

kaandocal opened this issue Apr 20, 2021 · 5 comments · Fixed by #1308

Comments

@kaandocal
Copy link

I am currently using Distributions.jl with mixture distributions (example: mixture of Gaussians) via MixtureModel. I have noticed that the compiler seems unable to perform type inference for basic functions such as mean and logpdf even when the MixtureModel struct is fully instantiated. Checking the source code these functions are implemented on the UnivariateMixture. I am currently specialising the functions I need for the specialised types I am working with but I was wondering if there isn't a better way of doing that....

@devmotion
Copy link
Member

If you only work with mixtures of univariate Gaussians, you might want to use UnivariateGMM which is optimized for this use case.

Generally, if there are type inference problems with MixtureModel that are caused by the implementation in Distributions, it would be great to fix them. Do you have a concrete example?

@kaandocal
Copy link
Author

Thanks for the recommendation! Unfortunately right now I'm using different mixture models including Poisson, NB etc.

Any example would be

using Distributions

mixture = MixtureModel([ Poisson(2.0), Poisson(10.0) ], Categorical(2))

@code_warntype mean(mixture)
@code_warntype pdf(mixture, 5)

Here several of the intermediate variables (including within _mixpdf1) don't have inferred types which leads to a significant performance decrease at the moment. I'd be happy to experiment with solutions but I don't want to break anything and Distributions is a very heavily used package...

@devmotion
Copy link
Member

I made a PR with some fixes: #1308

@kaandocal
Copy link
Author

Upon further inspection the problem seems to be that MixtureModel does not fully instantiate Categorical in its prior field. The compiler is then unable to infer the return value of probs(d); after annotating it all the type problems I've encountered in mean and logpdf go away.

@kaandocal
Copy link
Author

This seems to be exactly the fix that's needed! Thanks a lot for submitting this.

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

Successfully merging a pull request may close this issue.

2 participants