def test_independent(self):
        # Use IID data

        x = [pymc.geweke(np.random.normal(size=1000), intervals=5)[0][1] for _ in range(10000)]

        assert_approx_equal(np.var(x), 1, 1)

        # If the model has converged, 95% the scores should lie
        # within 2 standard deviations of zero, under standard normal model
        intervals = 40
        x = np.transpose(pymc.geweke(np.random.normal(size=10000), intervals=intervals))[1]
        assert(sum(np.abs(x) < 2) >= int(0.9 * intervals))
Exemple #2
0
    def test_independent(self):
        # Use IID data

        x = [pymc.geweke(np.random.normal(size=1000),intervals=5, maxlag=5)[0][1] 
            for _ in range(1000)]


        assert_approx_equal(np.var(x), 1, 1)

        # If the model has converged, 95% the scores should lie
        # within 2 standard deviations of zero, under standard normal model
        intervals = 40
        x = np.transpose(pymc.geweke(
                np.random.normal(size=10000),intervals=intervals))[1]
        assert(sum(np.abs(x) < 2) >= int(0.9 * intervals))
Exemple #3
0
def convergence_diagnose_birt(m, a):
    # birt_model = pymc.MCMC(...)

    pymc.raftery_lewis(m.a, q=0.025, r=0.01)
    scores = pymc.geweke(m.a, intervals=20)
    pymc.Matplot.geweke_plot(scores)
    pymc.gelman_rubin(m)
def analyzeConvergence(dbFilename, db, pn, burnin):
	print 'ANALYZING {0}'.format(pn)

	vals = np.array(getParameter(db, pn, burnin=burnin))
	
	convDict = OrderedDict()
	gw = pymc.geweke(vals)
	gwDict = OrderedDict()
	gwDict['scores'] = gw

	frac2SD = len([x for x in gw if x[1] > -2 and x[1] < 2]) / float(len(gw))
	gwDict['frac_2sd'] = frac2SD
	convDict['geweke'] = gwDict

	rl = pymc.raftery_lewis(vals, 0.025, r=0.01)
	rlDict = OrderedDict()
	rlDict['iter_req_acc'] = rl[0]
	rlDict['thin_first_ord'] = rl[1]
	rlDict['burnin'] = rl[2]
	rlDict['iter_total'] = rl[3]
	rlDict['thin_ind'] = rl[4]

	convDict['raftery_lewis'] = rlDict

	print ''

	return convDict
Exemple #5
0
def plot(temp_path, traces):
    for i in range(len(traces)):
        trace_name = 'trace_' + str(i)
        pymc.Matplot.plot(traces[i], name=trace_name, path=temp_path, verbose=0)
        summary(traces[i], trace_name)
        try:
            scores = pymc.geweke(traces[i])
            pymc.Matplot.geweke_plot(scores, name='gweke_' + str(i), path=temp_path)
        except:
            print 'Failed to create Geweke plot.'
Exemple #6
0
def geweke_test_problem(model):

    for name, node_desc in model.iter_stochastics():
        node = node_desc['node']
        output = pm.geweke(node)
        values = np.array(output)[:, 1]
        if np.any(np.abs(values) > 2):
            print
            print "Geweke problem was found in: %s" % name
            return True
    return False
Exemple #7
0
def geweke_test_problem(model):

    for name, node_desc in model.iter_stochastics():
        node = node_desc['node']
        output = pm.geweke(node)
        values = np.array(output)[:,1]
        if np.any(np.abs(values) > 2):
            print
            print "Geweke problem was found in: %s" % name
            return True
    return False
def geweke_statistics(model_mcmc, features):
    geweke_scores = []
    # Geweke Test
    for feature in features:
        print feature
        scores = pymc.geweke(model_mcmc.trace('b_'+feature)[:])
        geweke_scores.append(scores)
    return geweke_scores

# function to perform cross validation
    """
Exemple #9
0
def check_geweke(model, assert_=True):
    # Test for convergence using geweke method
    for param in model.group_params.values():
        geweke = np.array(pm.geweke(param))
        if assert_:
            assert (np.any(np.abs(geweke[:,1]) < 2)), 'Chain of %s not properly converged'%param
            return False
        else:
            if np.any(np.abs(geweke[:,1]) > 2):
                print("Chain of %s not properly converged" % param)
                return False

    return True
Exemple #10
0
def check_geweke(model, assert_=True):
    # Test for convergence using geweke method
    for name, param in model.iter_stochastics():
        geweke = np.array(pm.geweke(param['node']))
        if np.any(np.abs(geweke[:,1]) > 2):
            msg = "Chain of %s not properly converged" % param
            if assert_:
                raise AssertionError(msg)
            else:
                print msg
            return False

    return True
Exemple #11
0
def check_geweke(model, assert_=True):
    # Test for convergence using geweke method
    for name, param in model.iter_stochastics():
        geweke = np.array(pm.geweke(param['node']))
        if np.any(np.abs(geweke[:, 1]) > 2):
            msg = "Chain of %s not properly converged" % param
            if assert_:
                raise AssertionError(msg)
            else:
                print(msg)
            return False

    return True
    def test_simple(self):

        intervals = 20
        scores = pymc.geweke(S, intervals=intervals)
        a_scores = scores['a']
        assert_equal(len(a_scores), intervals)

        # Plot diagnostics (if plotting is available)
        try:
            from pymc.Matplot import geweke_plot as plot
            plot(scores, path=DIR, verbose=0)
        except ImportError:
            pass
Exemple #13
0
def check_geweke(model, assert_=True):
    # Test for convergence using geweke method
    for param in model.group_params.itervalues():
        geweke = np.array(pm.geweke(param))
        if assert_:
            assert (np.any(np.abs(geweke[:, 1]) < 2)
                    ), 'Chain of %s not properly converged' % param
            return False
        else:
            if np.any(np.abs(geweke[:, 1]) > 2):
                print "Chain of %s not properly converged" % param
                return False

    return True
    def test_simple(self):

        intervals = 20

        scores = pymc.geweke(S, intervals=intervals, maxlag=5)
        a_scores = scores['a']
        assert_equal(len(a_scores), intervals)

        # Plot diagnostics (if plotting is available)
        try:
            from pymc.Matplot import geweke_plot as plot
            plot(scores, path=DIR)
        except ImportError:
            pass
Exemple #15
0
    def test_simple(self):
        scores = pymc.geweke(S, intervals=20)
        a_scores = scores['a']
        assert_equal(len(a_scores), 20)

        # If the model has converged, 95% the scores should lie
        # within 2 standard deviations of zero, under standard normal model
        assert(sum(np.abs(np.array(a_scores)[:, 1]) > 1.96) < 2)

        # Plot diagnostics (if plotting is available)
        try:
            from pymc.Matplot import geweke_plot as plot
            plot(scores, path=DIR, verbose=0)
        except ImportError:
            pass
Exemple #16
0
    def test_simple(self):
        scores = pymc.geweke(S, intervals=20)
        a_scores = scores['a']
        assert_equal(len(a_scores), 20)

        # If the model has converged, 95% the scores should lie
        # within 2 standard deviations of zero, under standard normal model
        assert(sum(np.abs(np.array(a_scores)[:,1]) > 1.96) < 2)

        # Plot diagnostics (if plotting is available)
        try:
            from pymc.Matplot import geweke_plot as plot
            plot(scores,  path=DIR, verbose=0)
        except ImportError:
            pass
Exemple #17
0
    def plot_geweke(self, n_chains = None,  burn = 20., n_trace = 1000, axes_style = "ticks", savefig = True, **kwds):

        path = kwds.get("path", "Plots/")
        name = kwds.get("name", self.model_name+"_Geweke")
        format = kwds.get("format", ".png")

        Stochs = []
        if not hasattr(self, "_plot_traces"):
            self._select_trace()

        if len(self.LD.trace(self._plot_traces[0], chain = n_chains)[:])< n_trace:
            n_trace = len(self.LD.trace(self._plot_traces[0], chain = n_chains)[:])

        n_values = np.linspace(len(self.LD.trace(self._plot_traces[0], chain = n_chains)[:])*burn/100,
         len(self.LD.trace(self._plot_traces[0], chain = n_chains)[:])-1,n_trace, dtype = int)


        Stochs = self._plot_traces
        rows = int(len(Stochs)/3+1)
        fig, axs = plt.subplots(rows, 3, sharex=True, sharey=True, figsize = (13, 23))

        for i, Stoch in enumerate(Stochs):
            axa = axs[i/3,i- 3*(i/3)]
            try:
                geweke_val =  pm.geweke(self.LD.trace(Stoch, chain = n_chains)[n_values])
                self._plot_geweke_f(geweke_val, Stoch , ax = axa)
            except ValueError:
                print Stoch
                continue
            except LinAlgError:
                print "Alg",Stoch
                continue

            if savefig == True:
                if not os.path.exists(path):
                    os.makedirs(path)
                plt.savefig(path+name+"_"+Stoch+format, transparent = True)
Exemple #18
0
def geweke_problems(model, fname=None, **kwargs):
    """
    return a list of nodes who were detected as problemtic according to the geweke test
    Input:
        fname : string (deafult - None)
            Save result to file named fname
        kwargs : keywords argument passed to the geweke function
    """

    #search for geweke problems
    g = pm.geweke(model.mc)
    problems = []
    for node, output in g.iteritems():
        values = np.array(output)[:,1]
        if np.any(np.abs(values) > 2):
            problems.append(node)

    #write results to file if needed
    if fname is not None:
        with open(fname, 'w') as f:
            for node in problems:
                f.write(node)

    return problems
Exemple #19
0
def geweke_problems(model, fname=None, **kwargs):
    """
    return a list of nodes who were detected as problemtic according to the geweke test
    Input:
        fname : string (deafult - None)
            Save result to file named fname
        kwargs : keywords argument passed to the geweke function
    """

    #search for geweke problems
    g = pm.geweke(model.mc)
    problems = []
    for node, output in g.items():
        values = np.array(output)[:, 1]
        if np.any(np.abs(values) > 2):
            problems.append(node)

    #write results to file if needed
    if fname is not None:
        with open(fname, 'w') as f:
            for node in problems:
                f.write(node)

    return problems
Exemple #20
0
    # return (cur_x, funEvals['funevals']) if returnFunEvals else cur_x


if __name__ == '__main__':
    npr.seed(1)

    import pylab as pl
    import pymc

    D = 10
    fn = lambda x: -0.5 * np.sum(x**2)

    iters = 1000
    samps = np.zeros((iters, D))
    for ii in xrange(1, iters):
        samps[ii, :] = slice_sample(samps[ii - 1, :],
                                    fn,
                                    sigma=0.1,
                                    step_out=False,
                                    doubling_step=True,
                                    verbose=False)

    ll = -0.5 * np.sum(samps**2, axis=1)

    scores = pymc.geweke(ll)
    pymc.Matplot.geweke_plot(scores, 'test')

    pymc.raftery_lewis(ll, q=0.025, r=0.01)

    pymc.Matplot.autocorrelation(ll, 'test')
Exemple #21
0
def plot_traces(db=db,path='./diagnostics',format='png'):
    '''Plot the traces of the unknown variables to check for convergence.
    Also compute several convergence methods and print them out.'''
    lw = 1 #line width

    # Specify variables to include in each figure and subplot
    #   Each sublist is a figure. Each OrderedDict is a subplot with the
    #   key as the trace name and the val as the LaTeX string name.
    var_names = []
    var_names.append([OrderedDict([('f_a1', r'$f:a_1$'), ('f_a2', r'$f:a_2$')])])
    var_names[0].append(OrderedDict([('f_b1', r'$f:b_1$'), ('f_b2', r'$f:b_2$'),
                                     ('g_aw', r'$g:a_w$'), ('g_bw', r'$g:b_w$')]))
    var_names[0].append(OrderedDict([('sig_x', r'$\sigma_x$'), ('sig_y', r'$\sigma_y$'),
                                     ('sig_xl', r'local $\sigma_x$'),
                                     ('sig_yl', r'local $\sigma_y$')]))
    var_names[0].append(OrderedDict([('corr', r'$\rho$'), ('corr_l', r'local $\rho$'),
                                     ('lam', r'$\lambda$')]))
    var_names.append([OrderedDict([('xi', r'$\xi$'), ('em_obs_prob', r'emerg obs prob'),
                                   ('grid_obs_prob', r'grid obs prob')])])
    sent_names = []
    for name in db.trace_names[0]:
        if name[:13] == 'sent_obs_prob':
            id = name[-1]
            sent_names.append((name, id))
    var_names[1].append(OrderedDict(sent_names))

    plt.figure()
    plt.hold(True)

    f_clrs = [0.3, 0.7]
    g_clrs = [0.1, 0.9]
    sig_clrs = [0.01, 0.99, 0.25, 0.75]
    corr_lam_clrs = [0.01, 0.25, 0.5]
    probs_clrs = [0.01, 0.5, 0.99]
    clrs_list = [f_clrs, f_clrs+g_clrs, sig_clrs, corr_lam_clrs, probs_clrs]

    plt.title("Traces of unknown model parameters")
    for ii in range(len(var_names[0])):
        plt.subplot(len(var_names[0]), 1, ii+1)
        cnt = 0
        for name, label in var_names[0][ii].items():
            plt.plot(db.trace(name, chain=None)[:], label="trace of "+label,
                     c=cmap(clrs_list[ii][cnt]), lw=lw)
            cnt += 1
        leg = plt.legend(loc="upper left")
        leg.get_frame().set_alpha(0.7)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        plt.draw()
        plt.pause(0.0001)

    plt.figure()
    plt.hold(True)

    plt.subplot(211)
    plt.title("Traces of unknown Bayesian model parameters")
    # xi, em_obs_prob, grid_obs_prob
    cnt = 0
    for name, label in var_names[1][0].items():
        plt.plot(db.trace(name, chain=None)[:], label="trace of "+label,
                 c=cmap(probs_clrs[cnt]), lw=lw)
        cnt += 1
    leg = plt.legend(loc="upper right")
    leg.get_frame().set_alpha(0.7)

    # sent_obs_probs
    plt.subplot(212)
    cnt = 0
    for name, label in var_names[1][1].items():
        plt.plot(db.trace(name, chain=None)[:], label="trace of prob field "+label,
                 c=cmap(.10+cnt*.16), lw=lw)
        cnt += 1
    leg = plt.legend(loc="upper right")
    leg.get_frame().set_alpha(0.7)

    plt.draw()

    ##### Convergence tests #####

    # Geweke
    f, axarr = plt.subplots(len(var_names[0]), sharex=True)
    axarr[0].set_title('Geweke Plots')
    axarr[0].hold(True)
    for ii in range(len(var_names[0])):
        cnt = 0
        ymax = 0
        ymin = 0
        for name, label in var_names[0][ii].items():
            scores = pm.geweke(db.trace(name, chain=None)[:])
            x, y = np.transpose(scores)
            axarr[ii].scatter(x.tolist(), y.tolist(), label=label,
                        c=cmap(clrs_list[ii][cnt]))
            ymax = max(ymax, np.max(y))
            ymin = min(ymin, np.min(y))
            cnt += 1
        # Legend
        leg = axarr[ii].legend(loc="upper left",prop={'size':9})
        leg.get_frame().set_alpha(0.7)
        # Labels
        axarr[ii].set_ylabel('Z-score')
        # Plot lines at +/- 2 std from zero
        axarr[ii].plot((np.min(x), np.max(x)), (2, 2), '--')
        axarr[ii].plot((np.min(x), np.max(x)), (-2, -2), '--')
        # Plot bounds
        axarr[ii].set_ylim(min(-2.5, ymin), max(2.5, ymax))
        axarr[ii].set_xlim(0, np.max(x))
    axarr[-1].set_xlabel('First iteration')

    plt.hold(False)
    if not os.path.exists(path):
        os.mkdir(path)
    if not path.endswith('/'):
        path += '/'
    plt.savefig("{}.{}".format(path+'_Geweke',format),dpi=200)
    plt.draw()
Exemple #22
0
@pm.deterministic
def theta(a=alpha, b=beta, d=dose):
    """theta = inv_logit(a+b)"""
    return pm.invlogit(a+b*d)

"""deaths ~ binomial(n, p)"""
deaths = pm.Binomial('deaths', n=n, p=theta, value=x, observed=True)

my_model = [alpha, beta, theta, deaths]

# Instantiate and run sampler
S = pm.MCMC(my_model)
S.sample(10000, burn=5000)

# Calculate and plot Geweke scores
scores = pm.geweke(S, intervals=20)
pm.Matplot.geweke_plot(scores)

# Geweke plot for a single parameter
trace = S.trace('alpha')[:]
alpha_scores = pm.geweke(trace, intervals=20)
pm.Matplot.geweke_plot(alpha_scores, 'alpha')

# Calculate Raftery-Lewis diagnostics
pm.raftery_lewis(S, q=0.025, r=0.01)

"""
Sample output:

========================
Raftery-Lewis Diagnostic
                     {**feed_dict, x_start:samples[enum]}
                    )

        return samples, log_probs

    num_samples = 8192
    samples, log_probs = run_mcmc(test_means, num_samples)

    emp_means = np.mean(samples, axis=0)
    emp_cov = np.cov(samples.T)
    print(emp_means, '\n\n', test_cov, '\n\n', emp_cov, '\n\n',
          test_cov / emp_cov)

    import pylab as pl
    import pymc
    scores = pymc.geweke(log_probs)
    pymc.Matplot.geweke_plot(scores, 'test')
    pymc.raftery_lewis(log_probs, q=0.025, r=0.01)
    pymc.Matplot.autocorrelation(log_probs, 'test')
    pl.show()

    bp()

# ==============================================
#                            Developers' Section
# ==============================================
# ------------ Scrap work goes here ------------
'''
# 'Stepping in' with a mode sophisticated stopping criterion.
def step_in(self, local_fn, threshold, upper, lower, dtype=None):
	if (dtype is None): dtype = threshold.dtype
Exemple #24
0
        direction = direction / np.sqrt(np.sum(direction**2))
        new_x = direction_slice(direction, init_x)

    if scalar:
        return float(new_x[0])
    else:
        return new_x
                    
if __name__ == '__main__':
    npr.seed(1)

    import pylab as pl

    D  = 10
    fn = lambda x: -0.5*np.sum(x**2)

    iters = 1000
    samps = np.zeros((iters,D))
    for ii in xrange(1,iters):
        samps[ii,:] = slice_sample(samps[ii-1,:], fn, sigma=0.1, step_out=False, doubling_step=True, verbose=False)

    ll = -0.5*np.sum(samps**2, axis=1)

    scores = pymc.geweke(ll)
    pymc.Matplot.geweke_plot(scores, 'test')

    pymc.raftery_lewis(ll, q=0.025, r=0.01)

    pymc.Matplot.autocorrelation(ll, 'test')

Exemple #25
0
mcmc.cont(BAOh.trace('omega_M_0')[:],BAOh.trace('w')[:],color='blue',nsmooth=sm)
mcmc.cont(AllBAOh.trace('omega_M_0')[:],AllBAOh.trace('w')[:],color='red',nsmooth=sm)
xx=np.linspace(0,1,1000)
plot(xx,xx*0-1,'k:')








#### convergence checking

# Geweke: mean in segments compared with global mean
scores=pymc.geweke(BAOh,intervals=10)
pymc.Matplot.geweke_plot(scores)

# Raftery-Lewis
pymc.raftery_lewis(BAOh,q=0.68,r=0.01)

ft=scipy.fft(BAOh.trace('w')[:])
ps=abs(ft)**2
clf()
xscale('log')
yscale('log')
plot(ps)



        for t in things:
            print(db.trace(t).stats())

        print("means:")
        for t in things:
            print(t, "\t", db.trace(t).stats()['mean'])

        print("#samples: %d" % db.trace('mg_pv_K').stats()['n'])

        pymc.raftery_lewis(S, q=0.025, r=0.01)

        b = 1
        t = 1

        scores = pymc.geweke(S, intervals=20)

        pymc.Matplot.trace(db.trace('deviance',
                                    chain=None).gettrace(burn=1000,
                                                         thin=t,
                                                         chain=None),
                           'deviance',
                           rows=2,
                           columns=9,
                           num=1)

        pymc.Matplot.trace(db.trace('mg_pv_K',
                                    chain=None).gettrace(thin=t, chain=None),
                           'mg_pv_K',
                           rows=2,
                           columns=9,
Exemple #27
0
def plot(dirname,
         data,
         bkgd,
         resmat,
         trace,
         nuisancetrace,
         lower=[],
         upper=[]):
    import os
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    dirname = os.path.normpath(dirname) + os.sep

    plt.imshow(resmat, interpolation='none', origin='lower', alpha=0.5)
    plt.savefig(dirname + 'resmat.png')
    plt.close()

    ndim = len(data)
    # overlay data and background
    x = arange(0.5, ndim + 0.5)
    plt.plot(x, data, 'k', label='data', drawstyle='steps-mid')
    if len(bkgd) > 0:
        plt.plot(x,
                 array(bkgd).sum(axis=0),
                 'b',
                 label='background',
                 drawstyle='steps-mid')
    plt.ylim([0., max(data) * 1.3])
    plt.xlim([0., len(data)])
    plt.savefig(dirname + 'databckg.png')
    plt.close()

    # plot traces and autocorrelation
    #for ii,bckg in enumerate(bckgtrace):
    #mcplot(bckg,common_scale=False,suffix='_summary',path=dirname,format='eps')
    #plt.close()

    for name, nuisance in nuisancetrace.items():
        plothistandtrace(dirname + name, nuisance, -5., 5.)

    nbins = len(trace)
    for bin in xrange(nbins):
        ## need to be fixed
        ##mcplot(trace,common_scale=False,suffix='_summary',path=dirname,format='eps')
        ##plt.close()

        scores = pymc.geweke(trace[bin])
        # plot geweke test
        geweke_plot(scores,
                    'truth',
                    path=dirname,
                    format='png',
                    suffix='bin%d-geweke' % bin)
        plt.close()
        # raftery lewis test
        ##not very useful
        ##        pymc.raftery_lewis(scores, q=0.975, r=0.005)

        plothistandtrace(dirname + 'bin%d' % bin, trace[bin], lower[bin],
                         upper[bin])

        for name, nuisance in nuisancetrace.items():
            plt.plot(trace[bin], nuisance, ',')
            plt.savefig(dirname + '%s_bin%d.png' % (name, bin))
            plt.close()
Exemple #28
0
def plot_geweke_scores(mcmc_database, keys, working_dir):
    for key in keys:
        print('plotting geweke scores for %s...' % key)
        scores = pymc.geweke(mcmc_database.trace(key)[:])
        pymc.Matplot.geweke_plot(scores, name=key, path=working_dir)
    return None
            plt.close()

# plot autocorrelation
os.chdir(proj_dir + "/outputs/model results/simple random effects by state/mcmc plots/autocorrelation/")
for p in plot_me:
    mc.Matplot.autocorrelation(p, suffix="_autocorrelation")
    if len(p.shape) == 0:
        plt.close()
    else:
        for i in range(np.int(np.ceil(p.shape[0] / 4.0))):
            plt.close()

# plot geweke
os.chdir(proj_dir + "/outputs/model results/simple random effects by state/mcmc plots/geweke/")
for p in plot_me:
    scores = mc.geweke(p, intervals=20)
    mc.Matplot.geweke_plot(scores, p.__name__, suffix="_geweke")
    if len(p.shape) == 0:
        plt.close()
    else:
        for i in range(np.int(np.ceil(p.shape[0] / 4.0))):
            plt.close()


"""
# plot predictions
pp = PdfPages(proj_dir + 'outputs/model results/epi transition by state/predictions.pdf')
for g in g_list:
    d =     output[output.state == g]
    fig =   plt.figure()
    axis_num = 0
        for t in things:
            print db.trace(t).stats()

        print "means:"
        for t in things:
            print t,"\t",db.trace(t).stats()['mean']

        print "#samples: %d" % db.trace('mg_pv_K').stats()['n']

        pymc.raftery_lewis(S, q=0.025, r=0.01)

        b = 1
        t = 1

        scores = pymc.geweke(S, intervals=20)

        pymc.Matplot.trace(db.trace('deviance',chain=None).gettrace(burn=1000,thin=t,chain=None),'deviance',rows=2,columns=9,num=1)


        pymc.Matplot.trace(db.trace('mg_pv_K',chain=None).gettrace(thin=t,chain=None),'mg_pv_K',rows=2,columns=9,num=2)
        pymc.Matplot.histogram(np.array(db.trace('mg_pv_K',chain=None).gettrace(burn=b,chain=None)),'mg_pv_K',rows=2,columns=9,num=11)

        pymc.Matplot.trace(db.trace('mg_pv_K_prime',chain=None).gettrace(thin=t,chain=None),'mg_pv_K_prime',rows=2,columns=9,num=3)
        pymc.Matplot.histogram(np.array(db.trace('mg_pv_K_prime',chain=None).gettrace(burn=b,chain=None)),'mg_pv_K_prime',rows=2,columns=9,num=12)

        pymc.Matplot.trace(db.trace('mg_pv_G',chain=None).gettrace(thin=t,chain=None),'mg_pv_G',rows=2,columns=9,num=4)
        pymc.Matplot.histogram(np.array(db.trace('mg_pv_G',chain=None).gettrace(burn=b,chain=None)),'mg_pv_G',rows=2,columns=9,num=13)

        pymc.Matplot.trace(db.trace('mg_pv_G_prime',chain=None).gettrace(thin=t,chain=None),'mg_pv_G_prime',rows=2,columns=9,num=5)
        pymc.Matplot.histogram(np.array(db.trace('mg_pv_G_prime',chain=None).gettrace(burn=b,chain=None)),'mg_pv_G_prime',rows=2,columns=9,num=14)
Exemple #31
0
    # approximation (but not exactly the same):
    normal = pymc.NormApprox(make_poisson(5, 1., 20.))
    normal.fit()  # optimizes and saves results as attributes

    # Use MCMC for posterior sampling via MCMC; store samples in a Python
    # pickle file:
    mcmc = pymc.MCMC(make_poisson(5, 1., 20.), db='pickle')
    # Run 3 chains:
    for i in range(3):
        mcmc.sample(iter=10000, burn=5000, thin=1)
    print  # to handle missing newline from progress bar

    # Generate a dict of Geweke test z scores for each RV, here using early
    # segments 10% of the chain length, a final segment 50% of the length,
    # and producing scores for 10 early intervals.
    scores = pymc.geweke(mcmc, first=0.1, last=0.5, intervals=10)

    # The Matplot functions automatically produce new figures for each plot.
    pymc.Matplot.geweke_plot(scores['rate'], 'rate')
    pymc.Matplot.geweke_plot(scores['mu'], 'mu')

    print 'Rhat values:', pymc.gelman_rubin(mcmc)

    # Plot credible regions and R values:
    pymc.Matplot.summary_plot(mcmc)


def make_on_off(n_off, expo_off, n_on, expo_on, mean0):
    """
    Make a PyMC model for inferring a Poisson signal rate parameter, `s`, for
    'on-off' observations with uncertain background rate, `b`.
Exemple #32
0
        listTraces=[['Age1','Age2','Age2','Age4','Age6','Age7'],['ACAR','LV1AR','LV2AR','B211AR','B503AR','SAAR'],['err1','err2','err3','err4','err6','err7']]
        for k in range(6):
            for i in range(3):
                tracePlot=plt.subplot(gs[k,i])
                trace=mcmc.trace(listTraces[i][k])[extraburn::thin]
                tracePlot.plot(range(trace.size),trace,lw=1)
                tracePlot.set_title(listTraces[i][k])
        fig.savefig(('6SectionsData_tset_traceFig_ACSET.pdf'), format='pdf', dpi=300)  
        
#%%
variables=['velocity1','velocity2','velocity3','velocity6','velocity7','Age','RR','err1']
for i in variables:
    burn=0
    thin=1
    trace=mcmc.trace(i)[burn::thin]
    test=pm.geweke(trace, intervals=20)
    pm.Matplot.geweke_plot(test,'test',lw=1)
    
#%%
class HRAM(pm.Gibbs):
    def __init__(self, stochastic, proposal_sd=None, verbose=None):
        pm.Metropolis.__init__(self, stochastic, proposal_sd=proposal_sd,
                            verbose=verbose, tally=False)
        self.proposal_tau = self.proposal_sd**-2.
        self.n = 0
        self.N = 11
        self.value = pm.rnormal(self.stochastic.value, self.proposal_tau, size=tuple([self.N] + list(self.stochastic.value.shape)))
 
    def step(self):
        x0 = self.value[self.n]
        u = pm.rnormal(np.zeros(self.N), 1.)