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

Adopt DensityInterface.jl #41

Merged
merged 6 commits into from
Nov 28, 2021
Merged

Adopt DensityInterface.jl #41

merged 6 commits into from
Nov 28, 2021

Conversation

phipsgabler
Copy link
Member

This is in the hope of finally having a shared logdensity function across "probabilistic packages". Distributions.jl is already pretty much on board, Soss.jl hopefully will join later as soon as questions about how to integrate this with properly done measure theory (implicit/explicit base measures) have been resolved.

The consensus was to let the name be logdensityof (and densityof) to avoid name clashes. The interface is intended for both types that "have a density" and "are a density".

The other part of DensityInterface.jl is a trait, hasdensity, which for AbstractProbabilisticModel I have defaulted to true. I think likelihood-free inference is out of scope for the kind of models we target -- is that a reasonable assumption?

See JuliaStats/Distributions.jl#1416, JuliaMath/DensityInterface.jl#1 and JuliaMath/DensityInterface.jl#3.

@codecov
Copy link

codecov bot commented Nov 8, 2021

Codecov Report

Merging #41 (a999d88) into main (a7902a3) will decrease coverage by 0.71%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #41      +/-   ##
==========================================
- Coverage   77.77%   77.06%   -0.72%     
==========================================
  Files           1        2       +1     
  Lines         108      109       +1     
==========================================
  Hits           84       84              
- Misses         24       25       +1     
Impacted Files Coverage Δ
src/abstractprobprog.jl 0.00% <0.00%> (ø)
src/varname.jl 77.77% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a7902a3...a999d88. Read the comment docs.

@coveralls
Copy link

coveralls commented Nov 8, 2021

Pull Request Test Coverage Report for Build 1440131306

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 1 (0.0%) changed or added relevant line in 1 file are covered.
  • 17 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.7%) to 77.064%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/abstractprobprog.jl 0 1 0.0%
Files with Coverage Reduction New Missed Lines %
src/varname.jl 17 77.78%
Totals Coverage Status
Change from base Build 1429508741: -0.7%
Covered Lines: 84
Relevant Lines: 109

💛 - Coveralls

@yebai
Copy link
Member

yebai commented Nov 8, 2021

I think likelihood-free inference is out of scope for the kind of models we target -- is that a reasonable assumption?

That should be fine for now. We don't have any support for likelihood-free inference yet. But even for likelihood-free inference, one often uses an approximate likelihood/loss function. So we can still use the density interface.

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also update the version number 🙂

interface.md Outdated Show resolved Hide resolved
interface.md Outdated Show resolved Hide resolved
src/AbstractPPL.jl Outdated Show resolved Hide resolved
src/abstractprobprog.jl Outdated Show resolved Hide resolved
interface.md Outdated Show resolved Hide resolved
interface.md Outdated Show resolved Hide resolved
src/AbstractPPL.jl Show resolved Hide resolved
@mschauer
Copy link

mschauer commented Nov 9, 2021

I think this supports logdensityof as function on Turing models. Are there plans to support Turing statements

 X ~ B

for objects supporting logdensityof?

@devmotion
Copy link
Member

Yes, this is one of the main reasons for a unified interface. However, it will require some changes in DynamicPPL since we don't use logpdf currently but loglikelihood due to its implicit IID assumptions and support of multiple observations.

@mschauer
Copy link

mschauer commented Nov 9, 2021

That's why I am asking, in the current form of JuliaStats/Distributions.jl#1416 using IIDDensity in Turing will mean that the logdensityof interface is only supported for B<:Distribution because IIDDensity lives in Distributions.jl

@devmotion
Copy link
Member

Yes, that's something we have to address in the future in some way 🙂 Currently, Distributions is baked in in DynamicPPL and Turing very deeply, so it will take some time and effort to generalize it regardless of IIDDensity.

@phipsgabler
Copy link
Member Author

TBH, I had no idea we used loglikelihood. Is that the use case? Is it used only for observations/conditioned variables?

julia> @model function foo(x)
           m ~ Normal()
           x ~ Normal(m)
       end
foo (generic function with 2 methods)

julia> foo(1.2)()
1.2

julia> foo([1.2, 0.2])()
2-element Vector{Float64}:
 1.2
 0.2

Is this really a good thing? It definitely makes the type checker in my brain complain.

@devmotion
Copy link
Member

devmotion commented Nov 9, 2021

It is a good idea since before we had to maintain all kinds of custom dispatches for different distributions (but of course would miss any others) and always allocated arrays for the individual log densities which then were summed immediately. It is much more sane to let it implement whoever defines a Distribution type.

Edit:

I think it is also useful in your example since otherwise, if the distribution on the RHS depends on the number of samples, it seems you would have to artificially wrap the single observation in a vector or to introduce a type instability (Normal() for a single sample, filldist(Normal(), n) for multiple samples).

Generally, type-wise loglikelihood is fine since for it can be determined from the type if it is a single sample or multiple samples (and we don't have to define it but it is part of the implementation in eg Distributions). Actually, in some way it is even cleaner than logpdf: it will always return a real number whereas logpdf can return a real number (for a single sample) or a collection/array/... of real numbers (for multiple samples). And for univariate distributions the case of multiple sample is deprecated which means one has to manually broadcast if one wants to avoid deprecation warnings.

@devmotion
Copy link
Member

But I would suggest not polluting this PR with the seemingly unrelated discussion 🙂

@mschauer
Copy link

mschauer commented Nov 9, 2021

"Polluting" perhaps don't use that loaded language.

@devmotion
Copy link
Member

I'm sorry, I was in a rush and assume it was a bit strong. I only wanted to say that it seemed to distract from this PR and the discussion felt unrelated to this PR 🙂

@mschauer
Copy link

mschauer commented Nov 9, 2021

No problem, I just wanted to signal that the statement touched a nerve with me on the first take.

Project.toml Show resolved Hide resolved
@phipsgabler phipsgabler merged commit 5e8f1a3 into main Nov 28, 2021
@phipsgabler phipsgabler deleted the phg/densityinterface branch November 28, 2021 11:03
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 this pull request may close these issues.

5 participants