Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incremental model update with PyMC3

Is it possible to incrementally update a model in pyMC3. I can currently find no information on this. All documentation is always working with a priori known data.

But in my understanding, a Bayesian model also means being able to update a belief. Is this possible in pyMC3? Where can I find info in this?

Thank you :)

like image 468
Christian Avatar asked Nov 29 '16 16:11

Christian


People also ask

What is incremental model in software development?

Incremental Model Incremental Model is a process of software development where requirements divided into multiple standalone modules of the software development cycle. In this model, each module goes through the requirements, design, implementation and testing phases. Every subsequent release of the module adds function to the previous release.

How to fit a model with the observed data in PyMC3?

In PyMC3, we can do so by the following lines of code. We then fit our model with the observed data. This can be done by the following lines of code. Internally, PyMC3 uses the Metropolis-Hastings algorithm to approximate the posterior distribution. The trace function determines the number of samples withdrawn from the posterior distribution.

How to develop incremental version of a project?

Each increments versions are developed following the analysis, design, code and test phase and also each incremental version is usually developed by following the iterative waterfall model. These versions can be developed using other models as well. Let us look at each stage in each incremental phase development.

What is the incremental model of SDLC?

Design & Development: In this phase of the Incremental model of SDLC, the design of the system functionality and the development method are finished with success. When software develops new practicality, the incremental model uses style and development phase. 3.


1 Answers

Following @ChrisFonnesbeck's advice, I wrote a small tutorial notebook about incremental prior updating. It can be found here:

https://github.com/pymc-devs/pymc3/blob/master/docs/source/notebooks/updating_priors.ipynb

Basically, you need to wrap your posterior samples in a custom Continuous class that computes the KDE from them. The following code does just that:

def from_posterior(param, samples):

    class FromPosterior(Continuous):

        def __init__(self, *args, **kwargs):
            self.logp = logp
            super(FromPosterior, self).__init__(*args, **kwargs)

    smin, smax = np.min(samples), np.max(samples)
    x = np.linspace(smin, smax, 100)
    y = stats.gaussian_kde(samples)(x)
    y0 = np.min(y) / 10 # what was never sampled should have a small probability but not 0

    @as_op(itypes=[tt.dscalar], otypes=[tt.dscalar])
    def logp(value):
        # Interpolates from observed values
        return np.array(np.log(np.interp(value, x, y, left=y0, right=y0)))

    return FromPosterior(param, testval=np.median(samples))

Then you define the prior of your model parameter (say alpha) by calling the from_posterior function with the parameter name and the trace samples from the posterior of the previous iteration:

alpha = from_posterior('alpha', trace['alpha'])
like image 198
David Brochart Avatar answered Oct 09 '22 21:10

David Brochart