Exemple #1
0
def fit_latent_simplex_v2(X, iter=20000, burn=10000, thin=10):
    vars = latent_simplex_v2(X)

    m = mc.MAP([vars['alpha'], vars['X_obs']])
    m.fit(method='fmin_powell', verbose=1)
    print vars['pi'].value

    m = mc.MAP([vars['sigma'], vars['X_obs']])
    m.fit(method='fmin_powell', verbose=1)
    print[['%.2f' % sigma_tj.value for sigma_tj in sigma_t]
          for sigma_t in vars['sigma']]

    m = mc.MCMC(vars)

    for alpha_t, beta_t, sigma_t in zip(m.alpha, m.beta, m.sigma):
        m.use_step_method(mc.AdaptiveMetropolis, alpha_t + [beta_t])
        #m.use_step_method(mc.AdaptiveMetropolis, sigma_t)

    m.sample(iter, burn, thin, verbose=1)
    pi = m.pi.trace()

    print 'mean: ', pl.floor(m.pi.stats()['mean'] * 100. + .5) / 100.
    print 'ui:\n', pl.floor(m.pi.stats()['95% HPD interval'] * 100. +
                            .5) / 100.

    return m, pi.view(pl.recarray)
Exemple #2
0
def find_re_initial_vals(vars, method, tol, verbose):
    if 'hierarchy' not in vars:
        return

    col_map = dict([[key, i] for i,key in enumerate(vars['U'].columns)])

    for reps in range(3):
        for p in nx.traversal.bfs_tree(vars['hierarchy'], 'all'):
            successors = vars['hierarchy'].successors(p)
            if successors:
                #print successors

                vars_to_fit = [vars.get('p_obs'), vars.get('pi_sim'), vars.get('smooth_gamma'), vars.get('parent_similarity'),
                               vars.get('mu_sim'), vars.get('mu_age_derivative_potential'), vars.get('covariate_constraint')]
                vars_to_fit += [vars.get('alpha_potentials')]

                re_vars = [vars['alpha'][col_map[n]] for n in list(successors) + [p] if n in vars['U']]
                vars_to_fit += re_vars
                if len(re_vars) > 0:
                    mc.MAP(vars_to_fit).fit(method=method, tol=tol, verbose=verbose)

                #print np.round_([re.value for re in re_vars if isinstance(re, mc.Node)], 2)
                #print_mare(vars)

    #print 'sigma_alpha'
    vars_to_fit = [vars.get('p_obs'), vars.get('pi_sim'), vars.get('smooth_gamma'), vars.get('parent_similarity'),
                   vars.get('mu_sim'), vars.get('mu_age_derivative_potential'), vars.get('covariate_constraint')]
    vars_to_fit += [vars.get('sigma_alpha')]
    mc.MAP(vars_to_fit).fit(method=method, tol=tol, verbose=verbose)
Exemple #3
0
    def train(self, steps=20000, burn=10000, thin=10):
        '''train the Markov Chain Monte Carlo to estimate the parameters (b, c) in 
        mean pt loss and (a,) in pt loss distribution.

        Args: 
            steps (int): total number of Metropolis-Hastings random walk steps in the parameter space
                        default: 200000 steps
            burn (int): number of steps that are not used for final statistics; the estimated number
                        of steps for the MCMC to converge, default: 100000 steps
            thin (int): skipping number of steps to avoid auto-correlation, for final analysis.
                        The auto-correlation comes from Markov Chain (the next step in parameter space
                        is proposed using normal distribution whose mean is the current step).

        Updated:
            self.mdl_: MCMC model, which can be accessed by function self.get_mcmc_model()
            self.a (1d np.array): the trace of 'a' in the parameter space,
            self.b (1d np.array): the trace of 'b' in the parameter space,
            self.c (1d np.array): the trace of 'c' in the parameter space,
        '''
        raa_map = pm.MAP(
            self.__model__(self.RAA_x, self.RAA_y, self.RAA_xerr,
                           self.RAA_yerr))
        raa_map.fit(iterlim=20000, tol=0.01)
        self.mdl_ = pm.MCMC(raa_map.variables)
        self.mdl_.sample(steps, burn, thin, verbose=0)
        self.a = self.mdl_.trace('a')[...]
        self.b = self.mdl_.trace('b')[...]
        self.c = self.mdl_.trace('c')[...]
Exemple #4
0
    def plot(self):
        mc_map = pm.MAP(self.mc)
        mc_map.fit(tol=.01)
        # iterlim = 250,
        print("BIC score: {}".format(mc_map.BIC))
        plot(self.mc)
        # set years and months
        years = mdates.YearLocator()  # every year
        months = mdates.MonthLocator()  # every month
        years_fmt = mdates.DateFormatter('%Y')
        fig, ax = plt.subplots()
        # plot the data
        ax.plot(months_list,
                confirmed_cases,
                'o',
                mec='black',
                color='black',
                label='confirmed cases')
        ax.plot(months_list,
                mortalitysim.stats()['mean'],
                color='red',
                linewidth=1,
                label='MIH (mean)')
        y_min = mortalitysim.stats()['quantiles'][2.5]
        y_max = mortalitysim.stats()['quantiles'][97.5]
        ax.fill_between(months_list,
                        y_min,
                        y_max,
                        color='r',
                        alpha=0.3,
                        label='BPL (95% CI)')
        # format the ticks
        ax.xaxis.set_major_locator(years)
        ax.xaxis.set_major_formatter(years_fmt)
        ax.xaxis.set_minor_locator(months)
        # set the axis limit
        datemin = min(months_list) - 1
        datemax = max(months_list) + 1
        ax.set_xlim(datemin, datemax)

        # format the coords message box
        def price(x):
            return '$%1.2f' % x

        ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
        ax.format_ydata = price
        ax.grid(True)
        # rotates and right aligns the x labels, and moves the bottom of the
        # axes up to make room for them
        fig.autofmt_xdate()
        # some extra plot formating
        ax.legend(loc='best')
        plt.style.use('ggplot')
        plt.rc('font', size=12)
        plt.rc('lines', linewidth=2)
        plt.title("Plague model fit to laboratory confirmed cases")
        plt.xlabel('Time in months')
        plt.ylabel('Number of infecteds')
        plt.legend()
        plt.savefig(self.dir.split("\\")[-1] + '_fit.png')
Exemple #5
0
    def __init__(self, sim, params, database=None, overwrite=False):
        self.sim = sim
        self.params = params
        self.model_dict = self.make_model_dict(self.sim, dict(self.params))
        self.MAP = pymc.MAP(self.model_dict)
        self.dbname = database
        if self.dbname is None:
            self.MCMC = pymc.MCMC(self.MAP.variables)
        else:
            base, ext = os.path.splitext(self.dbname)
            self.dbtype = ext[1:]

            if os.path.exists(self.dbname) and overwrite:
                print("Overwriting %s" % self.dbname)
                os.remove(self.dbname)

            self.MCMC = pymc.MCMC(self.MAP.variables,
                                  db=self.dbtype,
                                  dbname=self.dbname)

            if self.MCMC.db.chains > 0:
                self.set_mu()

        self.MCMCparams = {}

        self.number_of_bins = 200
 def MAP_run(outname=None):
     '''Find Maximum a posteriori distribution'''
     tic = time.time()
     M = pm.MAP(Bayes_model, prior_eps)
     print('Fitting....')
     M.fit()
     # Return statistics
     print('Estimate complete. Time elapsed: {}'.format(time.time() - tic))
     print('Free stochastic variables: {}'.format(M.len))
     print('Joint log-probability of model: {}'.format(M.logp))
     print('Max joint log-probability of model: {}'.format(M.logp_at_max))
     print('Maximum log-likelihood: {}'.format(M.lnL))
     print("Akaike's Information Criterion {}".format(M.AIC), flush=True)
     print('---------------Variable estimates---------------')
     for var in Bayes_model.stochastics:
         print('{} = {}'.format(var, var.value))
     # Save result to file
     if outname is None:
         outname = 'Max_aPosteriori_Estimate.txt'
     with open(outname, 'w') as fobj:
         fobj.write('Time elapsed: {}\n'.format(time.time() - tic))
         fobj.write('Free stochastic variables: {}\n'.format(M.len))
         fobj.write('Joint log-probability of model: {}\n'.format(M.logp))
         fobj.write('Max joint log-probability of model: {}\n'.format(
             M.logp_at_max))
         fobj.write('Maximum log-likelihood: {}\n'.format(M.lnL))
         fobj.write("Akaike's Information Criterion {}\n".format(M.AIC))
         fobj.write('---------------Variable estimates---------------\n')
         for var in Bayes_model.stochastics:
             fobj.write('{} = {}\n'.format(var, var.value))
     print('Result saved to {}.'.format(outname))
     return M
Exemple #7
0
    def map(self, runs=2, warn_crit=5, method='fmin_powell', **kwargs):
        """
        Find MAP and set optimized values to nodes.

        :Arguments:
            runs : int
                How many runs to make with different starting values
            warn_crit: float
                How far must the two best fitting values be apart in order to print a warning message

        :Returns:
            pymc.MAP object of model.

        :Note:
            Forwards additional keyword arguments to pymc.MAP().

        """

        from operator import attrgetter

        # I.S: when using MAP with Hierarchical model the subjects nodes should be
        # integrated out before the computation of the MAP (see Pinheiro JC, Bates DM., 1995, 2000).
        # since we are not integrating we get a point estimation for each
        # subject which is not what we want.
        if self.is_group_model:
            raise NotImplementedError(
                """Sorry, This method is not yet implemented for group models.
            you might consider using the approximate_map method""")

        maps = []

        for i in range(runs):
            # (re)create nodes to get new initival values.
            #nodes are not created for the first iteration if they already exist
            self.mc = pm.MAP(self.nodes_db.node)
            if i != 0:
                self.draw_from_prior()

            self.mc.fit(method, **kwargs)
            print self.mc.logp
            maps.append(self.mc)

        self.mc = None

        # We want to use values of the best fitting model
        sorted_maps = sorted(maps, key=attrgetter('logp'))
        max_map = sorted_maps[-1]

        # If maximum logp values are not in the same range, there
        # could be a problem with the model.
        if runs >= 2:
            abs_err = np.abs(sorted_maps[-1].logp - sorted_maps[-2].logp)
            if abs_err > warn_crit:
                print "Warning! Two best fitting MAP estimates are %f apart. Consider using more runs to avoid local minima." % abs_err

        # Set values of nodes
        for max_node in max_map.stochastics:
            self.nodes_db.node.ix[max_node.__name__].set_value(max_node.value)

        return max_map
Exemple #8
0
    def fit(self, how='mcmc', iter=10000, burn=5000, thin=5):
        """ Fit the model
        :Parameters:
          - `how` : str, 'mcmc' or 'map'
          'mcmc' is slower but provides uncertainty estimates
          - iter, burn, thin : int
          mcmc options

        :Notes:
        This must come after a call to .setup_model.
        """
        from . import fit

        if 'rate_type' in self.model_settings:
            rate_type=self.model_settings['rate_type']

            if how=='mcmc':
                self.map, self.mcmc = fit.asr(
                    self, rate_type,
                    iter=iter, burn=burn, thin=thin)
            elif how=='map':
                self.map = mc.MAP(self.vars[rate_type])
                fit.find_asr_initial_vals(
                    self.vars[rate_type], 'fmin_powell', tol=1e-3, verbose=0)
                self.map.fit(method='fmin_powell')
        elif 'consistent' in self.model_settings:
            if how=='mcmc':
                self.map, self.mcmc = fit.consistent(
                    self,
                    iter=iter, burn=burn, thin=thin)
            elif how=='map':
                raise NotImplementedError('not yet implemented')
        else:
            raise NotImplementedError('Need to call .setup_model before calling fit.')
Exemple #9
0
def map_fit(pymc_model):
    """
    Find the maximum a posteriori (MAP) fit.

    Parameters
    ----------
    pymc_model : pymc model
       The pymc model to sample.

    Returns
    -------
    map : pymc.MAP
       The MAP fit.

    """
    map = pymc.MAP(pymc_model)
    ncycles = 50

    # DEBUG
    ncycles = 5

    for cycle in range(ncycles):
        if (cycle + 1) % 5 == 0:
            print('MAP fitting cycle %d/%d' % (cycle + 1, ncycles))
        map.fit()

    return map
Exemple #10
0
    def map_estimate(self, iterations=2000):
        """
        Compute the Maximum A Posteriori estimates for the initialised model
        """

        self.map = mc.MAP(self.model)
        self.map.fit(iterlim=iterations, tol=1e-3)
def mcmcSimulations(temperature, failures):
    '''Perform the MCMC-simulations'''

    # Define the prior distributions for alpha and beta
    # 'value' sets the start parameter for the simulation
    # The second parameter for the normal distributions is the "precision",
    # i.e. the inverse of the standard deviation
    np.random.seed(1234)
    beta = pm.Normal("beta", 0, 0.001, value=0)
    alpha = pm.Normal("alpha", 0, 0.001, value=0)

    # Define the model-function for the temperature
    @pm.deterministic
    def p(t=temperature, alpha=alpha, beta=beta):
        return 1.0 / (1. + np.exp(beta * t + alpha))

    # connect the probabilities in `p` with our observations through a
    # Bernoulli random variable.
    observed = pm.Bernoulli("bernoulli_obs", p, value=failures, observed=True)

    # Combine the values to a model
    model = pm.Model([observed, beta, alpha])

    # Perform the simulations
    map_ = pm.MAP(model)
    map_.fit()
    mcmc = pm.MCMC(model)
    mcmc.sample(120000, 100000, 2)

    # --- Show the resulting posterior distributions ---
    alpha_samples = mcmc.trace('alpha')[:, None]  # best to make them 1d
    beta_samples = mcmc.trace('beta')[:, None]

    return (alpha_samples, beta_samples)
def Main():

    # Plot Raw Data
    plot_raw(challenger_data)

    # Calculate observed values
    observed = pm.Bernoulli("bernoulli_obs", p, value=D, observed=True)

    # Create and sample from model
    model = pm.Model([observed, beta, alpha])

    # Once again, this code will be explored next chapter
    map_ = pm.MAP(model)
    map_.fit()
    mcmc = pm.MCMC(model)
    mcmc.sample(120000, 100000, 2)

    alpha_samples = mcmc.trace('alpha')[:, None]  # best to make them 1d
    beta_samples = mcmc.trace('beta')[:, None]

    # Do some plotting
    plot_posterior(alpha_samples, beta_samples)

    t = np.linspace(temperature.min() - 5, temperature.max() + 5, 50)[:, None]
    p_t = logistic(t.T, beta_samples, alpha_samples)

    mean_prob_t = p_t.mean(axis=0)

    # vectorized bottom and top 2.5% quantiles for credible interval
    qs = mquantiles(p_t, [0.025, 0.975], axis=0)

    plot_against_data(t, mean_prob_t, qs)

    plot_t_31(alpha_samples, beta_samples)
def record_parameters_and_probabilities(df, model):

    # set up empty slots for our upcoming values
    # but only we havent already
    try:
        df.alpha_param[0]
    except AttributeError:
        df['alpha_param'] = np.nan
        df['beta_param'] = np.nan
        df['cdf'] = np.nan
        df['one_minus_cdf'] = np.nan
        df['one_minus_cdf_BH'] = np.nan
        df['MAP_succeeded'] = False

    # set up a mask for this distance_bin
    bin_mask = df.distance_bin == model.bin_id_tag

    # perform Max A Posteriori
    model_runner = mc.MAP(model)

    # Parameter names for access through the model
    a_name = "{bin_id}_alpha".format(bin_id=model.bin_id_tag)
    b_name = "{bin_id}_beta".format(bin_id=model.bin_id_tag)

    try:
        # if we can take the short cut, great!
        model_runner.fit()
        model.MCMC_run = False
    except RuntimeError as exc:
        if "Posterior probability optimization converged to value with zero probability." in exc.message:
            # Otherwise lets run the MCMC experiment
            model_runner = mc.MCMC(model)
            model.MCMC_run = model_runner

            # re-initialize the alpha and beta starting values randomly
            # to avoid non-allowed values that seem to slip in from the MAP.fit()
            current_alpha, current_beta = model.get_node(
                a_name), model.get_node(b_name)
            current_alpha.random()
            current_beta.random()

            # do the learning
            model_runner.sample(iter=40000, burn=20000, thin=1)

    # get and store parameters
    alpha, beta = model.get_node(a_name), model.get_node(b_name)

    df[bin_mask].alpha_param = alpha.value.item()
    df[bin_mask].beta_param = beta.value.item()

    # get and store probabilities
    # Record the probabilities of obtaining each R2 value (or smaller) in the bin afer scaling
    df.loc[bin_mask,
           'cdf'] = scipy.stats.beta.cdf(df[bin_mask].R2_scaled_for_B,
                                         alpha.value.item(), beta.value.item())
    df.loc[bin_mask, 'one_minus_cdf'] = 1 - df[bin_mask]['cdf']
    df.loc[bin_mask,
           'one_minus_cdf_BH'] = smm.multipletests(df[bin_mask].one_minus_cdf,
                                                   method='fdr_bh')[1]
Exemple #14
0
def fit_linear():
    vars = linear()

    mc.MAP(vars).fit(method='fmin_powell')

    m = mc.MCMC(vars)
    m.sample(iter=10000, burn=5000, thin=5)
    return m
Exemple #15
0
def get_logp(S, model):
    """compute log(p) given a pyMC model"""
    M = pymc.MAP(model)
    traces = np.array([S.trace(s)[:] for s in S.stochastics])
    logp = np.zeros(traces.shape[1])
    for i in range(len(logp)):
        logp[i] = -M.func(traces[:, i])
    return logp
Exemple #16
0
    def ICs(self):
        self.MAP = pymc.MAP(self.M)
        self.MAP.fit()

        self.BIC = self.MAP.BIC
        self.AIC = self.MAP.AIC
        self.logp = self.MAP.logp
        self.logp_at_max = self.MAP.logp_at_max
        return self.AIC, self.BIC
def test_binom():
    """ Fit a binomial model to a single row of data, confirm the mean
    and std dev are as expected"""

    pi = mc.Uniform('pi', lower=0, upper=1)
    obs = dismod_mr.model.likelihood.binom('prevalence', pi, [.5], [10])

    mc.MAP([pi, obs]).fit()
    assert np.allclose(pi.value, .5, rtol=.01)
Exemple #18
0
def compute_logp(S, model):
    #From astroml, computes the log(p)s for a MCMC model:
    M = pymc.MAP(model)
    traces = np.array([S.trace(s)[:] for s in S.stochastics])
    logp = np.zeros(traces.shape[1])
    #Compute the log(p) at every coordinate of the traces:
    for i in range(len(logp)):
        logp[i] = -M.func(traces[:, i])
    return logp
Exemple #19
0
def pubBModeResidual(data = None):

    if data is None:
        data = {}

    if 'bmodes' not in data:
        data['bmodes'] = cPickle.load(open('bmodes.pkl', 'rb'))


    bmodes = data['bmodes']
    
    xdelta = np.arange(-1.5, 1.5, 0.001)
    
    if 'vsigma' not in data:

        gsigma = np.std(bmodes)

        vmodel = sd.VoigtModel(None, None, bmodes, np.zeros_like(bmodes))
        map = pymc.MAP(vmodel)
        map.fit()

        vsigma = map.sigma.value
        vgamma = map.gamma.value

        
        data['vsigma'] = vsigma
        data['vgamma'] = vgamma
        data['gsigma'] = gsigma

    else:

        vsigma = data['vsigma']
        vgamma = data['vgamma']
        gsigma = data['gsigma']


    
    fig = pylab.figure()
    ax = fig.add_axes([0.12, 0.12, 0.95 - 0.12, 0.95 - 0.12])
    hist, edges, patches = ax.hist(bmodes, bins = 151, range=(-1.5, 1.5), log=True, normed=True, label='B-Modes from 27 Cluster Fields', histtype='step', color='k')
    ax.plot(xdelta, vtools.voigtProfile(xdelta, vsigma, vgamma), 'r-', label='Voigt Model', linewidth=1.5)
    ax.plot(xdelta, gaussprofile(xdelta, gsigma), 'b-.', label='Gaussian Model', linewidth=1.5)

    ax.set_ylim(5e-5, 1e1)
    

    ax.set_ylabel(r'$P(g_X)$')
    ax.set_xlabel(r'Shear B-mode $g_X$')

    ax.legend(loc='lower center', numpoints=1)


    fig.savefig('publication/data_bmode_scatter.eps')


    return fig, data
Exemple #20
0
def fe(data):
    """ Fixed Effect model::
    
        Y_r,c,t = beta * X_r,c,t + e_r,c,t
        e_r,c,t ~ N(0, sigma^2)
    """
    # covariates
    K1 = count_covariates(data, 'x')
    X = pl.array([data['x%d' % i] for i in range(K1)])

    K2 = count_covariates(data, 'w')
    W = pl.array([data['w%d' % i] for i in range(K1)])

    # priors
    beta = mc.Uninformative('beta', value=pl.zeros(K1))
    gamma = mc.Uninformative('gamma', value=pl.zeros(K2))
    sigma_e = mc.Uniform('sigma_e', lower=0, upper=1000, value=1)

    # predictions
    @mc.deterministic
    def mu(X=X, beta=beta):
        return pl.dot(beta, X)

    param_predicted = mu

    @mc.deterministic
    def sigma_explained(W=W, gamma=gamma):
        """ sigma_explained_i,r,c,t,a = gamma * W_i,r,c,t,a"""
        return pl.dot(gamma, W)

    @mc.deterministic
    def predicted(mu=mu, sigma_explained=sigma_explained, sigma_e=sigma_e):
        return mc.rnormal(mu, 1 / (sigma_explained**2. + sigma_e**2.))

    # likelihood
    i_obs = pl.find(1 - pl.isnan(data.y))

    @mc.observed
    def obs(value=data.y,
            i_obs=i_obs,
            mu=mu,
            sigma_explained=sigma_explained,
            sigma_e=sigma_e):
        return mc.normal_like(value[i_obs], mu[i_obs],
                              1. / (sigma_explained[i_obs]**2. + sigma_e**-2.))

    # set up MCMC step methods
    mod_mc = mc.MCMC(vars())
    mod_mc.use_step_method(mc.AdaptiveMetropolis, mod_mc.beta)

    # find good initial conditions with MAP approx
    print 'attempting to maximize likelihood'
    var_list = [mod_mc.beta, mod_mc.obs, mod_mc.sigma_e]
    mc.MAP(var_list).fit(method='fmin_powell', verbose=1)

    return mod_mc
Exemple #21
0
def compute_MCMC_results(niter=20000, burn=2000):
    S = pymc.MCMC(model)
    S.sample(iter=niter, burn=burn)
    traces = [S.trace(s)[:] for s in ['b0', 'A', 'omega', 'beta']]

    M = pymc.MAP(model)
    M.fit()
    fit_vals = (M.b0.value, M.beta.value, M.A.value, M.omega.value)

    return traces, fit_vals
Exemple #22
0
 def map_fit(stoch_names):
     print '\nfitting', ' '.join(stoch_names)
     map = mc.MAP([dm.vars[key] for key in stoch_names] + [dm.vars['observed_counts'], dm.vars['rate_potential'], dm.vars['priors']])
     try:
         map.fit(method='fmin_powell', verbose=verbose)
     except KeyboardInterrupt:
         debug('User halted optimization routine before optimal value found')
     for key in stoch_names:
         print key, dm.vars[key].value.round(2)
     sys.stdout.flush()
def compute_MCMC_results(niter=25000, burn=4000):
    S = pymc.MCMC(model)
    S.sample(iter=niter, burn=burn)
    traces = [S.trace(s)[:] for s in ['b0', 'A', 'T', 'alpha']]

    M = pymc.MAP(model)
    M.fit()
    fit_vals = (M.b0.value, M.A.value, M.alpha.value, M.T.value)

    return traces, fit_vals
Exemple #24
0
    def reset(self, params):
        # this doesn't work
        self.sim = deepcopy(self.original_sim)

        self.model_dict = self.make_model_dict(self.sim, params)
        self.params = params
        self.MAP = pymc.MAP(self.model_dict)
        self.MCMC = pymc.MCMC(self.MAP.variables)

        return self.sim
Exemple #25
0
def sample_emcee(model=None, nwalkers=500, samples=1000, burn=500, thin=10):
    import pymc.progressbar as pbar

    # This is the likelihood function for emcee
    def lnprob(vals):
        try:
            for val, var in zip(vals, model.stochastics):
                var.value = val
            return model.logp
        except mc.ZeroProbability:
            return -1 * inf

    # emcee parameters
    ndim = len(model.stochastics)

    # Find MAP
    mc.MAP(model).fit()
    start = empty(ndim)
    for i, var in enumerate(model.stochastics):
        start[i] = var.value

    # sample starting points for walkers around the MAP
    p0 = random.randn(ndim * nwalkers).reshape((nwalkers, ndim)) + start

    # instantiate sampler passing in the pymc likelihood function
    sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob)

    bar = pbar.progress_bar(burn + samples)
    i = 0

    # burn-in
    for pos, prob, state in sampler.sample(p0, iterations=burn):
        i += 1
        bar.update(i)
    sampler.reset()

    # sample
    try:
        for p, lnprob, lnlike in sampler.sample(pos,
                                                iterations=samples,
                                                thin=thin):
            i += 1
            bar.update(i)
#    except KeyboardInterrupt:
#       pass
    finally:
        print("\nMean acceptance fraction during sampling: {}".format(
            mean(sampler.acceptance_fraction)))
        mcmc = mc.MCMC(model)  # MCMC instance for model
        mcmc.sample(1, progress_bar=False)  # This call is to set up the chains

        for i, var in enumerate(model.stochastics):
            var.trace._trace[0] = sampler.flatchain[:, i]

        return mcmc
Exemple #26
0
    def reset(self, params):
        raise ValueError('not implemented yet')
        # this doesn't work
        self.sim = deepcopy(self.original_sim)

        self.model_dict = self.make_model_dict(self.sim, params)
        self.params = params
        self.MAP = pymc.MAP(self.model_dict)
        self.MCMC = pymc.MCMC(self.MAP.variables)

        return self.sim
Exemple #27
0
 def _get_samples(self):
     '''set the Markov chain Monte Carlo with specified
     No. of iterations, burn-in length and thinning'''
     iterations = self.mcmc_iterations
     burn_in = self.mcmc_burn_in
     thinning = self.mcmc_thinning
     map_ = pymc.MAP(self.model)
     map_.fit()
     sampling_engine = pymc.MCMC(self.model)
     sampling_engine.sample(iter=iterations, burn=burn_in, thin=thinning)
     return sampling_engine
Exemple #28
0
def find_spline_initial_vals(vars, method, tol, verbose):
    ## generate initial value by fitting knots sequentially
    vars_to_fit = [vars.get('p_obs'), vars.get('pi_sim'), vars.get('smooth_gamma'), vars.get('parent_similarity'),
                   vars.get('mu_sim'), vars.get('mu_age_derivative_potential'), vars.get('covariate_constraint')]

    for i, n in enumerate(vars['gamma']):
        if verbose:
            print('fitting first %d knots of %d' % (i+1, len(vars['gamma'])))
        vars_to_fit.append(n)
        mc.MAP(vars_to_fit).fit(method=method, tol=tol, verbose=verbose)
        if verbose:
            print_mare(vars)
def test_beta_binom_2():
    """ Fit a fast beta binomial model to a single row of data, confirm the mean
    and std dev are as expected"""

    pi = mc.Uniform('pi', lower=0, upper=1, size=2)
    obs = dismod_mr.model.likelihood.beta_binom_2('prevalence', pi,
                                                  np.array([1, 1]),
                                                  np.array([.5, .5]),
                                                  np.array([10, 10]))

    mc.MAP([pi, obs]).fit()
    assert np.allclose(pi.value, .5, rtol=.01)
Exemple #30
0
def find_dispersion_initial_vals(vars, method, tol, verbose):
    vars_to_fit = [
        vars.get('p_obs'),
        vars.get('pi_sim'),
        vars.get('smooth_gamma'),
        vars.get('parent_similarity'),
        vars.get('mu_sim'),
        vars.get('mu_age_derivative_potential'),
        vars.get('covariate_constraint')
    ]
    vars_to_fit += [vars.get('eta'), vars.get('zeta')]
    mc.MAP(vars_to_fit).fit(method=method, tol=tol, verbose=verbose)