def cumulative_mean(title, samples, time, μ, stepsize, post, plot, legend_pos=None): nplot = len(samples) nsample = len(time) figure, axis = pyplot.subplots(figsize=(10, 7)) axis.set_xlabel("Time") axis.set_ylabel(r"$μ$") axis.set_title(title) axis.set_xlim([10.0, nsample]) axis.semilogx(time, numpy.full((len(time)), μ), label="Target μ", color="#000000") for i in range(nplot): axis.semilogx(time, stats.cummean(samples[i]), label=f"stepsize={format(stepsize[i], '2.2f')}") if legend_pos is None: axis.legend() else: axis.legend(bbox_to_anchor=legend_pos) config.save_post_asset(figure, post, plot)
def cumulative_mean(title, samples, time, μ, ylim, file): nsample = len(time) figure, axis = pyplot.subplots(figsize=(10, 7)) axis.set_xlabel("Time") axis.set_ylabel(r"$μ$") axis.set_title(title) axis.set_ylim(ylim) axis.set_xlim([10.0, nsample]) axis.semilogx(time, numpy.full((len(time)), μ), label="Target μ", color="#000000") axis.semilogx(time, stats.cummean(samples)) config.save_post_asset(figure, "hamiltonian_monte_carlo", file)
def mean_convergence(title, samples, μ, plot_name): nsamples = len(samples) time = range(nsamples) figure, axis = pyplot.subplots(figsize=(10, 6)) axis.set_xlabel("Sample Number") axis.set_ylabel("μ") axis.set_title(title) axis.set_xlim([1.0, nsamples]) axis.set_ylim( [0.5, 1.5]) cmean = stats.cummean(samples) μs = numpy.full(len(samples), μ) axis.semilogx(time, μs, label="Target μ") axis.semilogx(time, cmean, label="Sampled μ") axis.legend(bbox_to_anchor=([0.9, 0.9])) config.save_post_asset(figure, "rejection_sampling", plot_name)
# %% μ = 0.5 sample_idx = [0] title = f"Arcsine Distribution, Uniform Proposal, sample μ" time = range(nsample) mean_samples = all_samples[0][time] figure, axis = pyplot.subplots(figsize=(12, 6)) axis.set_xlabel("Time") axis.set_ylabel(r"$μ$") axis.set_title(title) axis.set_xlim([10.0, nsample]) axis.semilogx(time, numpy.full((len(time)), μ), label="Target μ", color="#000000") axis.semilogx(time, stats.cummean(samples), label=f"Samples") axis.legend() # %% σ = numpy.sqrt(0.125) title = f"Arcsine Distribution, Uniform Proposal, sample σ convergence" time = range(nsample) sigma_samples = all_samples[0] figure, axis = pyplot.subplots(figsize=(12, 6)) axis.set_xlabel("Time") axis.set_ylabel(r"$σ$") axis.set_title(title) axis.set_xlim([1.0, nsample]) axis.semilogx(time, numpy.full((len(time)), σ), label="Target σ", color="#000000")
μ = stats.weibull_mean(k, λ) title = r"Weibull Target, Normal Proposal, $μ_E$ Convergence: " + f"stepsize={format(stepsize, '2.2f')}" time = range(nsample) nplot = len(all_samples) figure, axis = pyplot.subplots(figsize=(10, 7)) axis.set_xlabel("Time") axis.set_ylabel(r"$μ$") axis.set_title(title) axis.set_xlim([1.0, nsample]) axis.set_ylim([-0.2, 3.7]) axis.yaxis.set_ticks([0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5]) axis.semilogx(time, numpy.full(nsample, μ), label="Target μ", color="#000000") for i in range(nplot): axis.semilogx(time, stats.cummean(all_samples[i]), label=r"$X_0$="+f"{format(x0[i], '2.1f')}") axis.legend(bbox_to_anchor=(0.9, 0.95)) config.save_post_asset(figure, "metropolis_hastings_sampling", "normal_proposal_burnin-mean-convergence") # %% σ = stats.weibull_sigma(k, λ) title = r"Weibull Target, Normal Proposal, $σ_E$ Convergence: " + f"stepsize={format(stepsize, '2.2f')}" time = range(nsample) nplot = len(all_samples) figure, axis = pyplot.subplots(figsize=(10, 7)) axis.set_xlabel("Time") axis.set_ylabel(r"$σ$") axis.set_title(title) axis.set_xlim([1.0, nsample])
μ = stats.weibull_mean(k, λ) title = r"Thinned Weibull Target, Normal Proposal, $μ_E$ Convergence: " + f"stepsize={format(stepsize, '2.2f')}, " + r"$X_0$="+f"{format(x0, '2.1f')}" nplot = len(all_samples) figure, axis = pyplot.subplots(figsize=(10, 7)) axis.set_xlabel("Time") axis.set_ylabel(r"$μ$") axis.set_title(title) axis.set_xlim([1.0, nsample]) axis.set_ylim([0.4, 1.2]) axis.yaxis.set_ticks([0.5, 0.75, 1.0]) axis.semilogx(range(0, nsample - burn_in), numpy.full(nsample - burn_in, μ), label="Target μ", color="#000000") for i in range(nplot): thinned_range = range(burn_in, nsample, i+1) mean = stats.cummean(all_samples[0][thinned_range]) axis.semilogx(range(len(mean)), mean, label=f"η={format(i+1, '2.0f')}") axis.legend(bbox_to_anchor=(0.95, 0.5)) config.save_post_asset(figure, "metropolis_hastings_sampling", "normal_proposal_thinning-mean-convergence") # %% σ = stats.weibull_sigma(k, λ) title = r"Thinned Weibull Target, Normal Proposal, $σ_E$ Convergence: " + f"stepsize={format(stepsize, '2.2f')}, " + r"$X_0$="+f"{format(x0, '2.1f')}" nplot = len(all_samples) figure, axis = pyplot.subplots(figsize=(10, 7)) axis.set_xlabel("Time") axis.set_ylabel(r"$μ$") axis.set_title(title) axis.set_xlim([1.0, nsample])
# %% μ = discrete_mean(df) title = f"Sampled Discrete Distribution μ Convergence" x = range(nsamples) figure, axis = pyplot.subplots(figsize=(10, 6)) axis.set_xlabel("Sample Number") axis.set_ylabel("μ") axis.set_title(title) axis.set_xlim([1.0, nsamples]) axis.set_ylim([0.0, 6.0]) axis.set_yticks([1.0, 2.0, 3.0, 4.0, 5.0]) axis.semilogx(x, numpy.full(nsamples, μ), label="Target μ") axis.semilogx(x, stats.cummean(df_samples), label="Sampled μ") axis.legend(bbox_to_anchor=(1.0, 0.85)) config.save_post_asset(figure, "inverse_cdf_sampling", "discrete_sampled_mean_convergence") # %% σ = discrete_sigma(df) title = f"Sampled Discrete Distribution σ Convergence" x = range(nsamples) figure, axis = pyplot.subplots(figsize=(10, 6)) axis.set_xlabel("Sample Number") axis.set_ylabel("σ") axis.set_title(title) axis.set_xlim([1.0, nsamples]) axis.set_ylim([0.0, 3.0])