TuringLang / AbstractMCMC.jl

Abstract types and interfaces for Markov chain Monte Carlo methods
https://turinglang.org/AbstractMCMC.jl
MIT License
78 stars 18 forks source link

Support log density functions as models #113

Closed devmotion closed 1 year ago

devmotion commented 1 year ago

This PR adds implementations of sample for generic log density functions. It is assumed that models in the sample calls that are not subtypes of AbstractModel are log density functions. They are supposed to support the LogDensityProblems interface (this is checked) and wrapped in a LogDensityModel automatically, which is then used instead of the log density in the sample call.

The motivation is that in downstream PRs to me it seemed a bit inconvenient that users have to wrap their log density functions manually. By adding aupport for automatic wrapping to AbatractMCMC instead of downstream packages, all downstream packages can benefit from it without additional code in downstream packages (they only have to support LogDensityModel).

A potential problem could be method ambiguity issues. However, since AbstractSampler is owned by AbstractMCMC this would only be partly AbstractMCMC's fault and I assume it won't happen in practice (let's wait for the downstream tests).

codecov[bot] commented 1 year ago

Codecov Report

Base: 95.51% // Head: 97.33% // Increases project coverage by +1.81% :tada:

Coverage data is based on head (f36b5d0) compared to base (2d31f09). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #113 +/- ## ========================================== + Coverage 95.51% 97.33% +1.81% ========================================== Files 8 8 Lines 290 300 +10 ========================================== + Hits 277 292 +15 + Misses 13 8 -5 ``` | [Impacted Files](https://codecov.io/gh/TuringLang/AbstractMCMC.jl/pull/113?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TuringLang) | Coverage Δ | | |---|---|---| | [src/logdensityproblems.jl](https://codecov.io/gh/TuringLang/AbstractMCMC.jl/pull/113?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TuringLang#diff-c3JjL2xvZ2RlbnNpdHlwcm9ibGVtcy5qbA==) | `100.00% <100.00%> (+100.00%)` | :arrow_up: | | [src/sample.jl](https://codecov.io/gh/TuringLang/AbstractMCMC.jl/pull/113?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TuringLang#diff-c3JjL3NhbXBsZS5qbA==) | `96.64% <100.00%> (-0.04%)` | :arrow_down: | | [src/stepper.jl](https://codecov.io/gh/TuringLang/AbstractMCMC.jl/pull/113?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TuringLang#diff-c3JjL3N0ZXBwZXIuamw=) | `100.00% <100.00%> (ø)` | | | [src/transducer.jl](https://codecov.io/gh/TuringLang/AbstractMCMC.jl/pull/113?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TuringLang#diff-c3JjL3RyYW5zZHVjZXIuamw=) | `100.00% <100.00%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TuringLang). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TuringLang)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

devmotion commented 1 year ago

I wonder if we could/should add some tests in AbstractMCMC, in addition to the existing downstream tests.

torfjelde commented 1 year ago

Though I agree tests would be ideal, if you're busy atm I'm happy to merge as is given that downstream tests pass.

torfjelde commented 1 year ago

Oh actually one question @devmotion : do we need LogDensityModel after this PR?

devmotion commented 1 year ago

I would say, yes, it's still useful since it allows downstream packages to clearly state the AbstractModel-type(s) that they want to support with their sampler(s). Additionally, a dedicated model type for log densities makes it easier to define conversion from/to other model types.

devmotion commented 1 year ago

I'll see if I find time to add some tests tomorrow. Otherwise I'll merge on Monday, if there are no objections.

devmotion commented 1 year ago

I added tests but let's merge https://github.com/TuringLang/AbstractMCMC.jl/pull/114 first (I noticed CompatHelper was disabled due to inactivity).