Ejemplo n.º 1
0
    all_accepted.append(accepted)

all_samples = numpy.array(all_samples)
all_accepted = numpy.array(all_accepted)
acceptance = 100.0*all_accepted/nsample

# %%

title = r"Weibull Target, Normal Proposal: $X_0$="+f"{format(x0, '2.1f')}"
mh.acceptance(title, stepsize, acceptance, [0.0005, 20.0], [4, 10, 15], "metropolis_hastings_sampling", "normal_proposal_acceptance")

# %%

sample_idx = 4
title = f"Weibull Target, Normal Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.2f')}, "+r"$X_0$="+f"{format(x0, '2.1f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "normal_proposal_sampled_pdf-99", xrange=numpy.arange(0.1, 1.7, 0.01), )

# %%

sample_idx = 10
title = f"Weibull Target, Normal Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.2f')}, "+r"$X_0$="+f"{format(x0, '2.1f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "normal_proposal_sampled_pdf-82", ylimit=[0.0, 2.2])

# %%

sample_idx = 13
title = f"Weibull Target, Normal Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.2f')}, "+r"$X_0$="+f"{format(x0, '2.1f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "normal_proposal_sampled_pdf-44", ylimit=[0.0, 2.2])

# %%
Ejemplo n.º 2
0
    all_accepted.append(accepted)

all_samples = numpy.array(all_samples)
all_accepted = numpy.array(all_accepted)
acceptance = 100.0*all_accepted/nsample

# %%

title = f"Arcsine Distribution, Normal Proposal"
mh.acceptance(title, stepsize, acceptance, [0.005, 20.0])

# %%

sample_idx = 0
title = f"Arcsine Distribution, Normal Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], xrange=numpy.arange(0.001, 0.999, .001), ylimit=[0.0, 8.0])

# %%

sample_idx = 4
title = f"Arcsine Distribution, Normal Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], xrange=numpy.arange(0.001, 0.999, .001), ylimit=[0.0, 8.0])


# %%

sample_idx = 8
title = f"Arcsine Distribution, Normal Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], xrange=numpy.arange(0.001, 0.999, .001), ylimit=[0.0, 8.0])

# %%
Ejemplo n.º 3
0
acceptance = [100.0*a/nsample for a in all_accepted]
title = f"Weibull Target, Normal Proposal, k={k}, λ={λ}"
figure, axis = pyplot.subplots(figsize=(10, 7))
axis.set_xlabel(r"$X_0$")
axis.set_ylabel("Acceptance %")
axis.set_title(title)
axis.set_ylim([0.0, 100])
axis.plot(x0, acceptance, zorder=5, marker='o', markersize=15.0, linestyle="None", markeredgewidth=1.0)
config.save_post_asset(figure, "metropolis_hastings_sampling", "normal_proposal_burnin_acceptance")

# %%

sample_idx = 0
all_samples[sample_idx]
title = f"Weibull Target, Normal Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize, '2.2f')}, " + r"$X_0$=" + f"{format(x0[sample_idx], '2.1f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "normal_proposal_sampled_pdf_burnin-x-001")

# %%

sample_idx = 1
title = f"Weibull Target, Normal Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize, '2.2f')}, " + r"$X_0$=" + f"{x0[sample_idx]}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "normal_proposal_sampled_pdf_burnin-x-1")

# %%

sample_idx = 2
title = f"Weibull Target, Normal Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize, '2.2f')}, " + r"$X_0$=" + f"{x0[sample_idx]}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "normal_proposal_sampled_pdf_burnin-x-2")

# %%
Ejemplo n.º 4
0
bv.contour_plot(μ1, μ2, σ1, σ2, γ, [0.01, 0.05, 0.1, 0.15, 0.2], "hamiltonian_monte_carlo", "metropolis-hastings-bivariate-normal-target-pdf")

# %%

q, accepted = mh.component_metropolis_hastings(target_pdf, mh.normal_proposal, mh.normal_generator, stepsize, nsample=nsample, x0=x0)
accepted = accepted / 2.0

# %%

title = f"MH Bivariate Normal" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%"
hmc.pdf_samples_contour(bv.pdf(μ1, μ2, σ1, σ2, γ), q[:,0], q[:,1], q1range, q2range, [0.01, 0.05, 0.1, 0.15, 0.2], [r"$q_1$", r"$q_2$"], title, f"{file_prefix}-samples-contour")

# %%

title = f"MH Bivariate Normal "  + r"$q_1$" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%"
gplot.pdf_samples(title, bv.marginal(0.0, σ1), q[:,0], "hamiltonian_monte_carlo",  f"{file_prefix}-samples-1")

# %%

title = f"MH Bivariate Normal "  + r"$q_2$" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%"
gplot.pdf_samples(title, bv.marginal(0.0, σ2), q[:,1], "hamiltonian_monte_carlo",  f"{file_prefix}-samples-2")

# %%

title = f"MH Bivariate Normal "  + r"$q_1$" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%"
vals = q[:,0]
time = range(9000, 9500)
hmc.time_series(title, vals[time], time, [min(vals), max(vals)], f"{file_prefix}-timeseries-1")

# %%
Ejemplo n.º 5
0
H, p, q, accept = HMC(q0, mass, potential_energy(σ), kinetic_energy(mass), dUdq(σ), dKdp(mass), momentum_verlet_hmc, nsample, nsteps, ε)

# %%

title = f"HMC Normal: σ={σ}, nsample={nsample}, accepted={accept}"
hmc.pdf_samples_contour(pdf, p, q, xrange, yrange, [0.01, 0.025, 0.05, 0.1, 0.15, 0.2], ["q", "p"], title, "hmc-normal-phase-space-histogram-1")

# %%

hmc.distribution_samples(p, q, xrange, yrange,  ["q", "p"], title, "hmc-normal-phase-space-histogram-2")

# %%

title = f"HMC Normal Target: Δt={ε}, nsteps={nsteps}, nsample={nsample}, accepted={accept}"
gplot.pdf_samples(title, target_pdf(σ), q, "hamiltonian_monte_carlo", "hmc-normal-sampled-pdf-1")

# %%

title = f"HMC Normal Target: Δt={ε}, nsteps={nsteps}, nsample={nsample}, accepted={accept}"
time = range(0, len(q))
hmc.time_series(title, q, time, [min(q), max(q)], "hmc-normal-position-timeseries-1")

# %%

title = f"HMC Normal Target: Δt={ε}, nsteps={nsteps}, nsample={nsample}, accepted={accept}"
time = range(9000, 9500)
hmc.time_series(title, q[time], time, [min(q), max(q)], "hmc-normal-position-timeseries-2")

# %%
Ejemplo n.º 6
0
    thinned_range = range(burn_in, nsample, thin[i])
    plot_start = int(plot_interval[0]/thin[i])
    plot_range = range(plot_start, plot_start + plot_interval[1] - plot_interval[0])
    thinned_samples = all_samples[0][thinned_range]
    axis[i].set_xlim(plot_start, plot_start + plot_interval[1] - plot_interval[0])
    axis[i].set_ylim([-0.2, 1.7])
    axis[i].plot(plot_range, thinned_samples[plot_range], lw=2)
    axis[i].text(text_pos[i][0], text_pos[i][1], f"η={format(thin[i], '2.0f')}", fontsize=15, bbox=bbox)
config.save_post_asset(figure, "metropolis_hastings_sampling", "normal_proposal_thinning-time-series")

# %%

thin = 1
thinned_range = range(burn_in, nsample, thin)
title = f"Thinned Weibull Target, Normal Proposal: η={format(thin, '2.0f')}, stepsize={format(stepsize, '2.2f')}, "+r"$X_0$="+f"{format(x0, '2.1f')}"
gplot.pdf_samples(title, target_pdf, all_samples[0][thinned_range], "metropolis_hastings_sampling", f"normal_proposal_sampled_pdf_thinning-{thin}", ylimit=[0.0, 2.2])

# %%

thin = 3
thinned_range = range(burn_in, nsample, thin)
title = f"Thinned Weibull Target, Normal Proposal: η={format(thin, '2.0f')}, stepsize={format(stepsize, '2.2f')}, "+r"$X_0$="+f"{format(x0, '2.1f')}"
gplot.pdf_samples(title, target_pdf, all_samples[0][thinned_range], "metropolis_hastings_sampling", f"normal_proposal_sampled_pdf_thinning-{thin}", ylimit=[0.0, 2.2])

# %%

thin = 6
thinned_range = range(burn_in, nsample, thin)
title = f"Thinned Weibull Target, Normal Proposal: η={format(thin, '2.0f')}, stepsize={format(stepsize, '2.2f')}, "+r"$X_0$="+f"{format(x0, '2.1f')}"
gplot.pdf_samples(title, target_pdf, all_samples[0][thinned_range], "metropolis_hastings_sampling", f"normal_proposal_sampled_pdf_thinning-{thin}", ylimit=[0.0, 2.2])
Ejemplo n.º 7
0
    all_accepted.append(accepted)

all_samples = numpy.array(all_samples)
all_accepted = numpy.array(all_accepted)
acceptance = 100.0*all_accepted/nsample

# %%

title = f"Weibull Distribution, Gamma Proposal: k={k}, λ={λ}"
mh.acceptance(title, stepsize, acceptance, [0.000008, 20.0], [4, 13, 22], "metropolis_hastings_sampling", "gamma_proposal_acceptance")

# %%

sample_idx = 5
title = f"Weibull Distribution, Gamma Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.5f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "gamma_proposal_sampled_pdf-98")

# %%

sample_idx = 13
title = f"Weibull Distribution, Gamma Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "gamma_proposal_sampled_pdf-80")

# %%

sample_idx = 17
title = f"Weibull Distribution, Gamma Proposal: Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], "metropolis_hastings_sampling", "gamma_proposal_sampled_pdf-48")

# %%
Ejemplo n.º 8
0
    all_accepted.append(accepted)

all_samples = numpy.array(all_samples)
all_accepted = numpy.array(all_accepted)
acceptance = 100.0*all_accepted/nsample

# %%

title = f"Bimodal Normal Distribution, Normal Proposal"
gplot.acceptance(title, stepsize, acceptance, [0.0005, 200.0])

# %%

sample_idx = 8
title = f"Bimodal Normal Distribution, Normal Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], xrange=numpy.arange(-7.0, 7.0, 0.05))

# %%

sample_idx = 13
title = f"Bimodal Normal Distribution, Normal Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx])

# %%

sample_idx = 15
title = f"Bimodal Normal Distribution, Normal Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx])

# %%
Ejemplo n.º 9
0
    all_accepted.append(accepted)

all_samples = numpy.array(all_samples)
all_accepted = numpy.array(all_accepted)
acceptance = 100.0*all_accepted/nsample

# %%

title = f"Weibull Distribution, Normal Independence Proposal, k={k}, λ={λ}"
gplot.acceptance(title, stepsize, acceptance, [0.005, 20.0])

# %%

sample_idx = 8
title = f"Weibull Distribution, Normal Independence Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], xrange=numpy.arange(0.1, 1.7, 0.05))

# %%

sample_idx = 11
title = f"Weibull Distribution, Normal Independence Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], xrange=numpy.arange(0.1, 1.7, 0.05))


# %%

sample_idx = 14
title = f"Weibull Distribution, Normal Independence Proposal, Accepted {format(acceptance[sample_idx], '2.0f')}%, stepsize={format(stepsize[sample_idx], '2.3f')}"
gplot.pdf_samples(title, target_pdf, all_samples[sample_idx], xrange=numpy.arange(0.1, 1.7, 0.05))

# %%