コード例 #1
0
def step_size_sigma(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.cumsigma(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)
コード例 #2
0
def cumulative_correlation(title, x, y, time, γ, file):
    nsample = len(time)
    cov = stats.cum_covaraince(x, y)
    sigmax = stats.cumsigma(x)
    sigmay = stats.cumsigma(y)
    γt = numpy.zeros(len(cov))

    for i in range(1, len(cov)):
        γt[i] = cov[i] / (sigmax[i] * sigmay[i])

    figure, axis = pyplot.subplots(figsize=(10, 7))
    axis.set_xlabel("Time")
    axis.set_ylabel(r"$γ$")
    axis.set_title(title)
    axis.set_ylim([-1.1, 1.1])
    axis.set_xlim([10.0, nsample])
    axis.semilogx(time,
                  numpy.full((len(time)), γ),
                  label="Target γ",
                  color="#000000")
    axis.semilogx(time, γt)
    config.save_post_asset(figure, "hamiltonian_monte_carlo", file)
コード例 #3
0
def cumulative_standard_deviation(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.cumsigma(samples))
    config.save_post_asset(figure, "hamiltonian_monte_carlo", file)
コード例 #4
0
def sigma_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.0, 0.5])
    axis.set_yticks([0.1, 0.2, 0.3, 0.4])
    csigma = stats.cumsigma(samples)
    σs = numpy.full(len(samples), σ)
    axis.semilogx(time, σs, label="Target σ")
    axis.semilogx(time, csigma, label="Sampled σ")
    axis.legend(bbox_to_anchor=([0.95, 0.9]))
    config.save_post_asset(figure, "rejection_sampling", plot_name)
コード例 #5
0
ファイル: uniform_proposal.py プロジェクト: glyfish/MCMC
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")
axis.semilogx(time, stats.cumsigma(samples), label=f"Samples")
axis.legend()

# %%

title = f"Arcsine Distribution, Uniform Proposal, Autocorrelation"
auto_core_range = range(20000, 50000)
autocorr_samples = all_samples[0][auto_core_range]
nplot = 100

figure, axis = pyplot.subplots(figsize=(12, 9))
axis.set_title(title)
axis.set_xlabel("Time Lag")
axis.set_xlim([0, nplot])
ac = stats.autocorrelate(samples)
axis.plot(range(nplot), numpy.real(ac[:nplot]))
コード例 #6
0
# %%

σ = 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])
axis.set_ylim([-0.05, 1.05])
axis.semilogx(time, numpy.full(nsample, σ), label="Target σ", color="#000000")
for i in range(nplot):
    axis.semilogx(time, stats.cumsigma(all_samples[i]), label=r"$X_0$="+f"{format(x0[i], '2.1f')}")
axis.legend(bbox_to_anchor=(0.95, 0.95))
config.save_post_asset(figure, "metropolis_hastings_sampling", "normal_proposal_burnin-sigma-convergence")

# %%

sample_idx = [0, 5, 9, 11]
title = title = r"Weibull Target, Normal Proposal, Autocorrelation: " + f"stepsize={format(stepsize, '2.2f')}"
auto_core_range = range(20000, 50000)
nlag = 100
nplot = len(all_samples)

figure, axis = pyplot.subplots(figsize=(10, 7))
axis.set_title(title)
axis.set_xlabel("Time Lag")
axis.set_xlim([0.0, nlag])
コード例 #7
0
σ = 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])
axis.set_ylim([-0.01, 0.35])
axis.yaxis.set_ticks([0.0, 0.1, 0.2, 0.3])
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)
    sigma = stats.cumsigma(all_samples[0][thinned_range])
    axis.semilogx(range(len(sigma)), sigma, label=f"η={format(i+1, '2.0f')}")
axis.legend(bbox_to_anchor=(0.95, 0.55))
config.save_post_asset(figure, "metropolis_hastings_sampling", "normal_proposal_thinning-sigma-convergence")

# %%

thin = [1, 2, 5]
plot_interval = [20000, 20500]
text_pos = [[20050, 0.025], [10050, 0.025], [4050, 0.025]]
title = r"Thinned Weibull Target, Normal Proposal, Time Series: " + f"stepsize={format(stepsize, '2.2f')}, " + r"$X_0$="+f"{format(x0, '2.1f')}"

figure, axis = pyplot.subplots(nrows=3, ncols=1, figsize=(10, 9))
axis[0].set_title(title)
axis[-1].set_xlabel("Time")
コード例 #8
0
# %%

σ = 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])
axis.set_yticks([0.5, 1.0, 1.5, 2.0, 2.5])
axis.semilogx(x, numpy.full(nsamples, σ), label="Target σ")
axis.semilogx(x, stats.cumsigma(df_samples), label="Sampled σ")
axis.legend(bbox_to_anchor=(1.0, 0.85))
config.save_post_asset(figure, "inverse_cdf_sampling", "discrete_sampled_sigma_convergence")

# %%
# Inverse CDF sampling for exponential

nsamples = 100000
cdf_inv = lambda v: numpy.log(1.0 / (1.0 - v))
pdf = lambda v: numpy.exp(-v)

samples = [cdf_inv(u) for u in numpy.random.rand(nsamples)]
x = numpy.linspace(0.0, 6.0, 500)
dx = 6.0/499.0

# %%