pymc-devs / pymc2

THIS IS THE **OLD** PYMC PROJECT (VERSION 2). PLEASE USE PYMC INSTEAD:
http://pymc-devs.github.com/pymc/
Other
879 stars 229 forks source link

Adaptive Metropolis and Degenerate Likelihoods #166

Closed julienmalard closed 6 years ago

julienmalard commented 6 years ago

When trying to fit a model with an external function, the external function will not rerun at each iteration if the variable list (l_var_paráms) includes a degenerate likelihood or a uniform likelihood with upper==lower.

import matplotlib.pyplot as dib
import numpy as np
import pymc

i = 0

class ModBayes(object):
    def __init__(símismo, función, dic_argums, d_obs, lista_d_paráms, aprioris, lista_líms, id_calib,
                 función_llenar_coefs):

        símismo.id = id_calib

        l_var_paráms = [pymc.Normal('n', mu=10, tau=1), pymc.Uniform('u', upper=1, lower=1)]

        # Without the following 4 lines, the model will not run correctly.
        for x in l_var_paráms.copy():
            if isinstance(x, pymc.Uniform):
                if x.parents['upper'] == x.parents['lower']:
                    l_var_paráms.remove(x)

        def fun(**kwargs):
            global i
            print(i)
            i += 1

            res = función(**kwargs)['Normal']
            return res

        @pymc.deterministic(trace=True)
        def simul(_=l_var_paráms):
            return fun(**dic_argums)

        l_var_obs = []

        for tipo, obs in d_obs.items():
            if tipo == 'Normal':
                tau = 1 / simul['sigma'] ** 2
                var_obs = pymc.Normal('obs', mu=simul['mu'], tau=tau, value=obs, observed=True)
                l_var_obs.extend([var_obs, tau])

        símismo.MCMC = pymc.MCMC({simul, *l_var_paráms, *l_var_obs})

    def calib(símismo, rep, quema, extraer):

        símismo.MCMC.use_step_method(pymc.AdaptiveMetropolis, símismo.MCMC.stochastics)

        símismo.MCMC.sample(iter=500, burn=0, thin=1, verbose=1)
fonnesbeck commented 6 years ago

PyMC2 is no longer being actively developed, though you are welcome to submit a pull request. I would strongly recommend looking at PyMC3, as the Hamiltonian MC samplers (particularly NUTS) are far more effective than adaptive Metropolis.

julienmalard commented 6 years ago

Thank you! I am mainly dealing with a relatively slow external model (1-2 seconds to evaluate, including reading output; https://github.com/julienmalard/Tikon/). Am I correct in understanding that NUTS would not be a possible MC sampler for this case? (And if not, would you have any recommendations?) I have been seriously considering porting to PyMC3, but wanted to understand better what the performance improvement might be before dedicating myself to it.

Many thanks, Julien Malard