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

Add StatsBase.predict to the interface #81

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Add StatsBase.predict to the interface #81

wants to merge 3 commits into from

Conversation

sethaxen
Copy link
Member

As suggested in TuringLang/DynamicPPL.jl#466 (comment), this PR adds StatsBase.predict to the API with a default implementation.

The sample will be returned as format specified by `T`.
"""
function StatsBase.predict(rand::AbstractRNG, T::Type, model::AbstractProbabilisticProgram, params)
return rand(rng, T, condition(model, params))
Copy link
Member Author

Choose a reason for hiding this comment

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

The docstring for condition would technically limit params to observations, for which this definitely does the wrong thing. Should condition be generalized?

Copy link
Member

Choose a reason for hiding this comment

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

The docstring for condition would technically limit params to observations

What do you mean by this?

Copy link
Member Author

Choose a reason for hiding this comment

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

The docstring for condition reads:

Condition the generative model model on some observed data

So as documented it is only meant for conditioning on observations, or equivalently, leaves in a DAG. But here predict wants to fix everything but the leaves in the DAG (or perhaps more generally, a subgraph that contains a leaf).

params,
) -> T

Draw a sample from the joint distribution specified by `model` conditioned on the values in
Copy link
Member Author

Choose a reason for hiding this comment

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

"condition" isn't quite right. If the model has a probabilistic graph x -> y -> z, and params contains y, this will sample x ~ p(x) and z ~ p(z | y), when conditioning would give x ~ p(x | y) and z ~ p(z | y)

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we actually want a do operator here instead of condition.

Copy link
Member

Choose a reason for hiding this comment

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

This is a good point.

I guess we actually want a do operator here instead of condition.

But doesn't do cut the graph too, and so it would also remove the x completely?

Copy link
Member Author

@sethaxen sethaxen Mar 25, 2023

Choose a reason for hiding this comment

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

But doesn't do cut the graph too, and so it would also remove the x completely?

As I understand it, do(y=something) only deletes the edges going into y and sets y to the specified value. The rest of the graph is unchanged; in particular, no nodes are deleted. So x would be kept.

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes that makes sense.

Okay so we basically want doto be like condition but without including the accumulation of it's (log) prob?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can use fix now. Also worth to introduce fix in abstractprobprog.jl.

@sethaxen
Copy link
Member Author

sethaxen commented Mar 8, 2023

Bump, maybe @devmotion or @torfjelde?

@sethaxen
Copy link
Member Author

Bump again.

Copy link
Member

@torfjelde torfjelde left a comment

Choose a reason for hiding this comment

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

Cheers for the bump; had missed this!

It's worth noting that DPPL is still not compatible wtih [email protected] so we might also want to add this to [email protected].

Furthermore, I'm slightly worried about the state of AbstractPPL atm; it's not clear if anyone has any ownership of the package atm, and IMO it's objectives are a bit all over the place.
I'd personally be happy to go against what was originally suggested in TuringLang/DynamicPPL.jl#466 (comment) and just putting this directly in DPPL.

Or we need to start giving AbstractPPL some love 😕

The sample will be returned as format specified by `T`.
"""
function StatsBase.predict(rand::AbstractRNG, T::Type, model::AbstractProbabilisticProgram, params)
return rand(rng, T, condition(model, params))
Copy link
Member

Choose a reason for hiding this comment

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

The docstring for condition would technically limit params to observations

What do you mean by this?

@yebai
Copy link
Member

yebai commented Mar 25, 2023

@sunxd3 can help backport this to v0.5 once merged.

It would be great to update DynamicPPL to support [email protected] thought.

@sunxd3
Copy link
Collaborator

sunxd3 commented Mar 25, 2023

I can try and help bring DynamicPPL up to AbstractPPL 0.6, what exactly break in 0.6 from 0.5 @yebai @torfjelde ?

@yebai
Copy link
Member

yebai commented Mar 26, 2023

@sunxd3 It is related to changing behavior of the colon syntax. You can follow this issue TuringLang/DynamicPPL.jl#440 and the issues it linked.

We can discuss this more in our next meeting.

Copy link

codecov bot commented Nov 8, 2023

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Comparison is base (b342b3d) 84.82% compared to head (7862931) 80.39%.
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #81      +/-   ##
==========================================
- Coverage   84.82%   80.39%   -4.44%     
==========================================
  Files           3        3              
  Lines         145      153       +8     
==========================================
  Hits          123      123              
- Misses         22       30       +8     
Files Coverage Δ
src/abstractprobprog.jl 40.00% <0.00%> (-45.72%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yebai
Copy link
Member

yebai commented Sep 19, 2024

@torfjelde @sunxd3 @penelopeysm, anything missing here? If not, can we push to merge this?

@sunxd3
Copy link
Collaborator

sunxd3 commented Sep 19, 2024

as far as I can tell, we can introduce fix to AbstractPPL, and use it for predict.

On a higher level, we can also add predict(model, vector_of_params_and_weights) and support some kind of importance sampling so when predict, we don't need to go over all the posterior samples.

(I need to finish TuringLang/DynamicPPL.jl#651)

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.

4 participants