Exemplo n.º 1
0
def show_setup():
    integrator = create_integrator()
    actual_IC = PerturbedRiemannIC(Settings.Simulation.IC.ground_truth)
    unperturbed_IC = PerturbedRiemannIC([0] * 3)

    x_vals = Settings.Simulation.get_xvals()

    unperturbed_u_start = [unperturbed_IC(x) for x in x_vals]
    perturbed_u_start = [actual_IC(x) for x in x_vals]
    unperturbed_u_end = integrator(unperturbed_IC)
    perturbed_u_end = integrator(actual_IC)

    measurer = create_measurer()
    measurement_lims = zip(measurer.left_limits, measurer.right_limits)

    plt.plot(x_vals, unperturbed_u_end, 'k--', label="0,1 Riemann problem")
    plt.plot(x_vals, unperturbed_u_start, 'k--')
    plt.plot(x_vals, perturbed_u_end, 'b')
    plt.plot(x_vals, perturbed_u_start, 'b', label="ground truth for MCMC")
    for left_idx, right_idx in measurement_lims:
        plt.axvspan(x_vals[left_idx],
                    x_vals[right_idx],
                    facecolor='g',
                    alpha=0.5)

    plt.legend()
    plt.title("Setup, at T = 0 and T = 1")
    plt.xlabel("x")
    plt.ylabel("w")

    store_figure("burgers_setup")
Exemplo n.º 2
0
def show_chain_evolution(samples):
    def shock_location(d1, d2, s):
        return BurgersEquation.riemann_shock_pos(1 + d1, d2, s,
                                                 Settings.Simulation.T_end)

    x_vals = Settings.Simulation.get_xvals()
    measurer = create_measurer()
    measurement_lims = zip(measurer.left_limits, measurer.right_limits)
    for l_idx, r_idx in measurement_lims:
        plt.axhspan(x_vals[l_idx], x_vals[r_idx], facecolor='r', alpha=0.3)

    for a in Settings.Simulation.IC.ground_truth:
        plt.axhline(a, color='k')

    for i in range(3):
        plt.plot(samples[i, :], label=Settings.Simulation.IC.names[i])

    shock_locs = [
        shock_location(*samples[:, i]) for i in range(len(samples[0, :]))
    ]
    plt.plot(shock_locs, label="Shock location")
    plt.ylim(-2, 2)
    plt.title("Chain evolution")
    plt.legend()
    store_figure(Settings.filename() + "_chain")
Exemplo n.º 3
0
def main():
    sampler = create_mcmc_sampler()

    samples_full = load_or_compute(
        Settings.filename(), sampler.run,
        (Settings.Sampling.u_0, Settings.Sampling.N, 0, 1))

    samples_full = samples_full.T
    # Add pertubations to means
    for i in range(len(samples_full[0, :])):
        samples_full[:, i] += Settings.Prior.mean

    # do burn_in and sample_interval after the fact
    samples = samples_full[:, Settings.Sampling.burn_in:]
    samples = samples[:, ::Settings.Sampling.sample_interval]

    # plot densities
    fig, plts = plt.subplots(1, 3, figsize=(20, 10))

    priors = [
        GaussianDistribution(mu, Settings.Prior.std_dev)
        for mu in Settings.Prior.mean
    ]

    intervals = [(-2, 2)] * 3

    plot_info = zip(priors, intervals, Settings.Simulation.IC.ground_truth,
                    Settings.Simulation.IC.names, plts)

    for i, (prior, interval, true_val, name, ax) in enumerate(plot_info):
        ax.hist(samples[i, :], density=True)
        x_range = np.linspace(*interval, num=300)
        ax.plot(x_range, [prior(x) for x in x_range])
        ax.axvline(true_val, c='r')
        ax.set_title(f"Prior and posterior for {name}")
        ax.set(xlabel=name, ylabel="Probability")

    fig.suptitle("Posteriors and priors")
    store_figure(Settings.filename() + "_densities")

    # autocorrelation
    ac = autocorrelation(samples_full, int(Settings.Sampling.N / 10), 10)
    for i in range(3):
        plt.plot(ac[i, :], label=Settings.Simulation.IC.names[i])
    plt.title("Autocorrelation")
    plt.xlabel("Lag")
    plt.legend()
    store_figure(Settings.filename() + "_ac")

    show_chain_evolution(samples_full)
    show_setup()
Exemplo n.º 4
0
def main():
    grid_cells = [32, 64, 128, 256]
    data = create_data(grid_cells)

    # support of the data
    data_ndims = data[0].shape[0]
    assert data_ndims == 3, ""
    intervals = np.empty((data_ndims, 2))
    for i in range(data_ndims):
        intervals[i, :] = [
            min(chain[i, :].min() for chain in data),
            max(chain[i, :].max() for chain in data)
        ]

    # bin data
    n_bins = 20

    # data_binned = [np.histogram(chain[0, :], bins=n_bins, range=intervals[0])[0]
    #                for chain in data]
    # data_binned = [binned_chain / np.sum(binned_chain) for binned_chain in data_binned]

    # ground_truth, _ = np.histogram([Settings.Simulation.IC.delta_1], bins=n_bins, range=intervals[0])
    data_binned = [
        np.histogramdd(chain.T, bins=n_bins, range=intervals)[0]
        for chain in data
    ]
    data_binned = [
        binned_chain / np.sum(binned_chain) for binned_chain in data_binned
    ]

    ground_truth, _ = np.histogramdd(
        Settings.Simulation.IC.ground_truth.reshape(1, 3),
        bins=n_bins,
        range=intervals)
    errors = [
        wasserstein_distance(density, ground_truth, intervals)
        for density in data_binned
    ]

    plt.plot(grid_cells, errors)
    plt.title("$W_1$ for different grid spacing")
    plt.xlabel("Grid cells in the underlying simulation")
    plt.ylabel("$W_1$")
    store_figure(Settings.filename() + "_wasserstein_convergence_grid")

    for chain in data:
        for i in range(len(chain[0, :])):
            chain[:, i] -= Settings.Prior.mean
        print(f"Showing chain with length {len(chain[0, :])}")
        show_chain(chain, Settings.Sampling.burn_in,
                   Settings.Sampling.sample_interval)
Exemplo n.º 5
0
def show_chain_evolution_and_step(samples):
    fig, (parameters, shock,
          steps) = plt.subplots(3,
                                1,
                                figsize=(15, 10),
                                gridspec_kw={'height_ratios': [3, 3, 1]})

    # ground truth lines
    for a in Settings.Simulation.IC.ground_truth:
        parameters.axhline(a, linestyle='dashed', color='k')

    # actual chain values
    for i in range(3):
        parameters.plot(samples[i, :], label=Settings.Simulation.IC.names[i])
    parameters.set_ylim(-.8, 1.6)
    parameters.set_xlim(0, Settings.Sampling.N)
    parameters.legend()
    parameters.set_title("Parameters")

    # measurement indicators
    x_vals = Settings.Simulation.get_xvals()
    measurer = create_measurer()
    measurement_lims = zip(measurer.left_limits, measurer.right_limits)
    for l_idx, r_idx in measurement_lims:
        shock.axhspan(x_vals[l_idx], x_vals[r_idx], facecolor='r', alpha=0.3)

    # shock locations
    shock_locs = [
        BurgersEquation.riemann_shock_pos(samples[0, i] + 1, samples[1, i],
                                          samples[2, i],
                                          Settings.Simulation.T_end)
        for i in range(len(samples[0, :]))
    ]
    shock.plot(shock_locs, color='r')
    shock.set_ylim(Settings.Simulation.domain)
    shock.set_xlim(0, Settings.Sampling.N)
    shock.set_title("Shock locations and measurement intervals")

    # beta evolution
    step_vals = np.array(
        [Settings.Sampling.step(i) for i in range(len(samples[0, :]))])
    steps.plot(step_vals, color='k')
    steps.set_ylim(0, Settings.Sampling.step.d_s)
    steps.set_xlim(0, Settings.Sampling.N)
    steps.set_title("Step size")

    fig.suptitle("Chain evolution")
    store_figure(Settings.filename() + "_chain_beta")
Exemplo n.º 6
0
def show_chain_evolution(samples):
    # measurement indicators
    x_vals = Settings.Simulation.get_xvals()
    measurer = create_measurer()
    measurement_lims = zip(measurer.left_limits, measurer.right_limits)
    for l_idx, r_idx in measurement_lims:
        plt.axhspan(x_vals[l_idx], x_vals[r_idx], facecolor='r', alpha=0.3)

    # ground truth lines
    for a in Settings.Simulation.IC.ground_truth:
        plt.axhline(a, color='k')

    # actual chain values
    for i in range(3):
        plt.plot(samples[i, :], label=Settings.Simulation.IC.names[i])

    # shock locations
    shock_locs = [
        BurgersEquation.riemann_shock_pos(samples[0, i] + 1, samples[1, i],
                                          samples[2, i],
                                          Settings.Simulation.T_end)
        for i in range(len(samples[0, :]))
    ]
    plt.plot(shock_locs, label="Shock location")

    # computed characteristics
    # l_b = len_burn_in(samples)
    # if l_b == len(samples[0, :]):
    #     tau = "burn-in not finished"
    # else:
    #     tau = uncorrelated_sample_spacing(samples[:, l_b:])
    # plt.text(0, 1.6, f"burn-in: {l_b}\ndecorrelation-length: {tau}")

    # characteristics
    plt.text(0, 1.6, (
        f"burn-in: {Settings.Sampling.burn_in}\n"
        f"sampling-interval: {Settings.Sampling.sample_interval}\n"
        f"usable samples: {(Settings.Sampling.N - Settings.Sampling.burn_in)/Settings.Sampling.sample_interval}"
    ))

    plt.ylim(-1, 1.8)
    plt.title("Chain evolution")
    plt.legend()
    store_figure(Settings.filename() + "_chain")
Exemplo n.º 7
0
def main():
    chain_length = 100000
    data = create_data(chain_length)

    # support of the data
    data_ndims = data[0].shape[0]
    assert data_ndims == 3, ""
    intervals = np.empty((data_ndims, 2))
    for i in range(data_ndims):
        intervals[i, :] = [
            min(chain[i, :].min() for chain in data),
            max(chain[i, :].max() for chain in data)
        ]

    # bin data
    n_bins = 20

    # data_binned = [np.histogram(chain[0, :], bins=n_bins, range=intervals[0])[0]
    #                for chain in data]
    # data_binned = [binned_chain / np.sum(binned_chain) for binned_chain in data_binned]

    # ground_truth, _ = np.histogram([Settings.Simulation.IC.delta_1], bins=n_bins, range=intervals[0])
    data_binned = [
        np.histogramdd(chain.T, bins=n_bins, range=intervals)[0]
        for chain in data
    ]
    data_binned = [
        binned_chain / np.sum(binned_chain) for binned_chain in data_binned
    ]

    ground_truth, _ = np.histogramdd(
        Settings.Simulation.IC.ground_truth.reshape(1, 3),
        bins=n_bins,
        range=intervals)
    # -----------------------
    # data_binned = [np.histogramdd(chain.T, bins=n_bins, range=intervals, density=False)[0]
    #                for chain in data]

    # data_binned = [binned_chain / np.sum(binned_chain) for binned_chain in data_binned]

    # ground_truth, _ = np.histogramdd(Settings.Simulation.IC.ground_truth.reshape(1,3),
    #                                  bins=n_bins, range=intervals, density=False)

    # _, (bd1, bd2, bs0) = np.histogramdd(data[0].T, bins=n_bins, range=intervals, density=False)

    # # print(np.sum(H, axis=(1,2)))
    # for i, H in enumerate(data_binned):
    #     plt.plot(bd1[:-1], np.sum(H, axis=(1,2)), label=i)

    # plt.plot(bd1[:-1], np.sum(ground_truth, axis=(1,2)), label="gt")

    # plt.legend()
    # plt.show()

    # return
    # # plt.plot(bd1[:-1], np.sum(ground_truth, axis=(1,2)))
    # # plt.show()

    # H, _ = np.histogram(data[-1][0, :], bins=n_bins, range=intervals[0], density=True)

    # plt.plot(_[:-1], H)
    # plt.show()

    # return

    # print(np.sum(data[-1], axis=(0,1)))
    # plt.plot(np.sum(data[-1], axis=(0,1)))
    # plt.show()

    # return

    # # generate ground truth
    # ground_truth, _ = np.histogramdd(Settings.Simulation.IC.ground_truth.reshape(1,3),
    #                                  bins=n_bins, range=intervals, density=True)

    # errors = [wasserstein_distance(density, ground_truth, intervals[:1].reshape(1,2)) for density in data_binned]
    errors = [
        wasserstein_distance(density, ground_truth, intervals)
        for density in data_binned
    ]

    plt.plot([chain_length / (2**(i + 1)) for i in range(len(data))], errors)
    plt.title("$W_1$ for different chain lengths")
    plt.xlabel("Length of the chain")
    plt.ylabel("$W_1$")
    store_figure(Settings.filename() + "_wasserstein_convergence_chain")
Exemplo n.º 8
0
def main():
    stepsize = PWLinear(0.1, 0.001, Settings.Sampling.burn_in)
    steps = [stepsize]
    burn_ins = [Settings.Sampling.burn_in]
    sample_intervals = [Settings.Sampling.sample_interval]

    for step, burn_in, sample_interval in zip(steps, burn_ins,
                                              sample_intervals):
        Settings.Sampling.step = step
        Settings.Sampling.burn_in = burn_in
        Settings.Sampling.sample_interval = sample_interval

        sampler = create_mcmc_sampler()

        samples_full = load_or_compute(
            Settings.filename(), sampler.run,
            (Settings.Sampling.u_0, Settings.Sampling.N, 0, 1))

        samples_full = samples_full.T
        # Add pertubations to means
        for i in range(len(samples_full[0, :])):
            samples_full[:, i] += Settings.Prior.mean

        # samples = clean_samples(samples_full)
        samples = samples_full[:, Settings.Sampling.burn_in:]
        samples = samples[:, ::Settings.Sampling.sample_interval]

        # plot densities
        fig, plts = plt.subplots(1, 3, figsize=(20, 10))

        priors = [
            GaussianDistribution(mu, Settings.Prior.std_dev)
            for mu in Settings.Prior.mean
        ]

        intervals = [(-2, 2)] * 3

        plot_info = zip(priors, intervals, Settings.Simulation.IC.ground_truth,
                        Settings.Simulation.IC.names, plts)

        for i, (prior, interval, true_val, name, ax) in enumerate(plot_info):
            ax.hist(samples[i, :], density=True)
            x_range = np.linspace(*interval, num=300)
            ax.plot(x_range, [prior(x) for x in x_range])
            ax.axvline(true_val, c='r')
            ax.set_title(f"Prior and posterior for {name}")
            ax.set(xlabel=name, ylabel="Probability")

        fig.suptitle("Posteriors and priors")
        store_figure(Settings.filename() + "_densities")

        # autocorrelation
        # samples_burned = samples_full[:, len_burn_in(samples_full):]
        ac = autocorrelation(samples_full[:, Settings.Sampling.burn_in:], 100)
        for i in range(3):
            plt.plot(ac[i, :], label=Settings.Simulation.IC.names[i])

        plt.axhline(0, color='k', alpha=0.5)

        plt.title("Autocorrelation")
        plt.xlabel("Lag")
        plt.legend()
        store_figure(Settings.filename() + "_ac")

        show_chain_evolution_and_step(samples_full)