stan-dev / posteriordb

Database with posteriors of interest for Bayesian inference
176 stars 36 forks source link

Add model implementations API for python #128

Closed eerolinna closed 4 years ago

eerolinna commented 4 years ago

This depends on #127 and should not be merged before it

This PR is mostly to make the discussion in #125 more concrete. If we reach the conclusion that this functionality should not be added then that's how it will be. However at least currently I think that this would be a good addition.

Add method implementations to Model, which takes a framework name and returns a model implementation object for that framework.

Currently model implementation objects only have a load method. Calling it loads the underlying model of the framework being used.

Here's an example how this might be used. (running these needs PR #129)

from posteriordb import PosteriorDatabase
my_pdb = PosteriorDatabase("../posterior_database/") # replace path if needed
model = my_pdb.model("eight_schools_centered")
data = my_pdb.data("eight_schools")

We can load a Stan model and obtain posterior draws

stan_imp = model.implementation("stan")
stan_model = stan_imp.load()
stan_draws = stan_model.sampling(data=data.values())

We can do the same with a PyMC3 model

import pymc3
pymc_imp = model.implementation("pymc3")
get_pymc_model = pymc_imp.load()
pymc_model = get_pymc_model(data.values())
pymc_draws = pymc3.sampling.sample(model=pymc_model)

Further down the line we might want to also add other methods to the model implementation objects. Obtaining prior or predictive draws might be one of these potential new methods.

eerolinna commented 4 years ago

The travis build timed out. For some reason the R CI checks seem to work really slowly right now, the python checks passed.

MansMeg commented 4 years ago

Sure. Ill take a look at this asap. Im now focused on the ICML deadline so I have some other stuff higher up in my todolist.

eerolinna commented 4 years ago

That's fine, thanks for letting me know

eerolinna commented 4 years ago

Any comments on this now? The ICML deadline has passed, right?

MansMeg commented 4 years ago

Yes. Got some other admin to do. This is now high on my todo.