-
Notifications
You must be signed in to change notification settings - Fork 9
Add StatsBase.predict to the interface #81
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
Changes from 2 commits
f51166d
f4d7823
7862931
01b6acf
4a5eb0c
b72a963
ff52fb3
076a7a5
58a7931
66bf79e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||
using AbstractMCMC | ||||||||||||||||||||||||||||||||||||
using DensityInterface | ||||||||||||||||||||||||||||||||||||
using Random | ||||||||||||||||||||||||||||||||||||
using StatsBase | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||
|
@@ -80,3 +81,29 @@ end | |||||||||||||||||||||||||||||||||||
function Base.rand(model::AbstractProbabilisticProgram) | ||||||||||||||||||||||||||||||||||||
return rand(Random.default_rng(), NamedTuple, model) | ||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||
predict( | ||||||||||||||||||||||||||||||||||||
[rng::AbstractRNG=Random.default_rng(),] | ||||||||||||||||||||||||||||||||||||
[T=NamedTuple,] | ||||||||||||||||||||||||||||||||||||
model::AbstractProbabilisticProgram, | ||||||||||||||||||||||||||||||||||||
params, | ||||||||||||||||||||||||||||||||||||
) -> T | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
Draw a sample from the joint distribution specified by `model` conditioned on the values in | ||||||||||||||||||||||||||||||||||||
`params`. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
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)) | ||||||||||||||||||||||||||||||||||||
sunxd3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||
function StatsBase.predict(T::Type, model::AbstractProbabilisticProgram, params) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @torfjelde @yebai @penelopeysm do you think type I think it's probably okay for the function to be a bit under-speced now, so DynamicPPL and JuliaBUGS and others can decide what type to return. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure we need a concrete implementation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think when Seth added this, it was modeled after the AbstractPPL.jl/src/abstractprobprog.jl Lines 108 to 124 in b72a963
I am for just having a simple Moreover, should we slim down the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want DynamicPPL function to have a type argument There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with this, my comments only reflects that we don't have T argument right now if I recall correctly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohhh, I see! For a general interface, though, if we don't specify What do you think of something like this: # default_return_type(model) specifies the default type returned by
# rand([rng, ]model) and predict([rng, ]model, params)
function default_return_type end
# Then we can have rand like this
function Base.rand(
rng::Random.AbstractRNG = Random.default_rng(),
::Type{T} = default_return_type(model),
model::AbstractProbabilisticProgram)
)
AbstractPPL._rand(rng, T, model) # User has to implement this
end
# And predict like this
function StatsBase.predict(
rng::Random.AbstractRNG = Random.default_rng(),
::Type{T} = default_return_type(model),
model::AbstractProbabilisticProgram),
params
)
AbstractPPL._predict(rng, T, model, params) # User has to implement this
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically, I don't feel super comfortable only being able to return NamedTuple. I think the user should be allowed to choose what return type they want (in our case sometimes we might want varinfo). Enforcing a specific return type at this level might be too limiting. I also know I'm possibly overcomplicating things, sorry 😄 Also, I don't know how this would interact with different params types as well. Because the output type would surely depend on whether we pass in one set of params (e.g. a NamedTuple) or multiple sets of params (e.g. a chain). 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's a good idea to have an interface function Whether we should have an explicit argument to specify the returned type is a separate issue, and I agree with the above discussions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@sunxd3 feel free to slip down the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @penelopeysm really sorry for missing the previous reply, your comments makes perfect sense to me. I think it's a good idea to let user decide what type to return. My view is: it might also not be a bad idea to let PPL packages that implements I am just a bit uneasy to have a default return type that we don't always support ( |
||||||||||||||||||||||||||||||||||||
return StatsBase.predict(Random.default_rng(), T, model, params) | ||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||
function StatsBase.predict(model::AbstractProbabilisticProgram, params) | ||||||||||||||||||||||||||||||||||||
return StatsBase.predict(NamedTuple, model, params) | ||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||
function StatsBase.predict(rng::AbstractRNG, params) | ||||||||||||||||||||||||||||||||||||
return StatsBase.predict(rng, NamedTuple, model, params) | ||||||||||||||||||||||||||||||||||||
end |
Uh oh!
There was an error while loading. Please reload this page.