Skip to content

Base Model class for pymc3.models #1524

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

Closed
ferrine opened this issue Nov 12, 2016 · 7 comments
Closed

Base Model class for pymc3.models #1524

ferrine opened this issue Nov 12, 2016 · 7 comments

Comments

@ferrine
Copy link
Member

ferrine commented Nov 12, 2016

What do we need for base api? Good base class will help to maintain all models in the future. I think that lasagne like base class will be a good solution.

@ferrine ferrine mentioned this issue Nov 12, 2016
@ericmjl
Copy link
Member

ericmjl commented Nov 12, 2016

Here's my train of thought when I was designing the BEST object.

I thought first about how the data should be structured in a Pandas dataframe in order to make the code most concise, and then designed it around that. From my limited experience, statisticians like it best when data are provided structured and cleaned, with one row representing one "observation", control or treatment alike. I think one thing that would be a necessity would be to pass in the data into the base Model class.

I'm not sure what other keyword arguments would be necessary though. In the case of BEST, I needed to know what was the baseline "control", in order to compute other parameters, such as "Z-factors" or "effect sizes" (not yet implemented in my PR, wanted to start simple first). In the structured table that statisticians love, that's not going to be immediately evident without passing that in as well.

I think the base class should definitely provide a .fit() function; it'll be scikit-learn-like, and run the best "default" inference without needing a user to specify this. I can imagine it implementing the ADVI-init + NUTS sampling that @twiecki put in #1523.

Not so sure about the .plot_posterior() function being implemented in the base Model class; perhaps it should be declared but not implemented?

Here's a quick sketch of what it might look like:

class Model(object):
    def __init__(self, data):
        super(Model, self).__init__()
        self.data = data

    ...

    def fit(self):
        pm.sample_init(self.model)

    def plot_posterior(self):
        pass

What do you have in mind for functionality that the base class would need?

@ferrine
Copy link
Member Author

ferrine commented Nov 12, 2016

I think the essence is here. In constructor we give some input and description and construct the model itself. We should consider using _add_var method for that. And we are supposed to be in the pm.Model context manager

from ..model import modelcontext

class UserModel(object):
    def __init__(self, name):
        # name should be used as prefix for
        # all variables specified within a model
        self.vars = OrderedDict()
        self.name = name

    @property
    def model(self):
        # Just shortcut
        return modelcontext(None)

    def _add_var(self, name, dist, data=None):
        # Signature from pymc3.Model
        var = self.model.Var('{}_{}'.format(self.name, name), dist=dist, data=data)
        self.vars[name] = var
        return var

@ferrine ferrine mentioned this issue Nov 12, 2016
@twiecki
Copy link
Member

twiecki commented Nov 14, 2016

I like the direction this is taking. I worked on something similar before but mainly for caching model creation (if you want to run a model multiple times this is a serious overhead). Not sure we want to include it here but maybe also not rule out that it could one day be part of the API: https://gist.github.com/twiecki/972496588bf69fbfbd98ed6b01a3c8a8

@ferrine
Copy link
Member Author

ferrine commented Nov 14, 2016

I see such models as a building block for a big custom bayesian model, like layer in lasagne. That's the direction I see.

@twiecki
Copy link
Member

twiecki commented Nov 14, 2016

That's interesting, hadn't thought about that. Can you give an example of what a model would look like with this API?

@ferrine
Copy link
Member Author

ferrine commented Nov 14, 2016

Soon I'll write glm in that way using this api

@twiecki
Copy link
Member

twiecki commented Nov 28, 2016

Closed by #1525

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

No branches or pull requests

3 participants