def _get_norms(xz): x = swyft.get_x(xz) z = swyft.get_z(xz) x_mean = sum(x) / len(x) z_mean = sum(z) / len(z) x_var = sum([(x[i] - x_mean)**2 for i in range(len(x))]) / len(x) z_var = sum([(z[i] - z_mean)**2 for i in range(len(z))]) / len(z) return x_mean, x_var**0.5, z_mean, z_var**0.5
# Generate 2-dim posteriors network_2d = swyft.MLP_2d(x_dim, n_hidden, xz_init=xz) losses += swyft.train(network_2d, xz, n_steps=n_steps, lr=1e-3, n_particles=n_particles) losses += swyft.train(network_2d, xz, n_steps=n_steps, lr=1e-4, n_particles=n_particles) # Plot results 1-dim z_lnL = swyft.estimate_lnL(network, x0, swyft.get_z(xz)) plt.clf() plt.plot(z_lnL[0]['z'], np.exp(z_lnL[0]['lnL'])) plt.axvline(0.1) plt.axvline(0.9) plt.savefig("figs/testrun_09a.png") # plot 2-dim results z_lnL = swyft.estimate_lnL_2d(network_2d, x0, swyft.get_z(xz)) lnL = z_lnL['lnL'] z_ij = z_lnL['z'] plt.clf() plt.tricontour([zzz[0] for zzz in z_ij], [zzz[1] for zzz in z_ij], lnL * 2, levels=[-36, -25, -16, -9, -4, -1, 0]) t = np.linspace(0, 6.5, 1000)
# Generate test z0 and x0 z0 = np.array([0.10, 0.50]) x0 = model(z0, sigma=0.) x_dim = len(x0) z_dim = len(z0) # Initialize loss list losses = [] # And the first run xz1 = swyft.init_xz(model, n_sims=n_sims, n_dim=z_dim) network1 = Network(x_dim, z_dim, n_hidden, xz_init=xz1) losses += swyft.train(network1, xz1, n_steps=n_steps, lr=1e-3, n_particles=n_particles) losses += swyft.train(network1, xz1, n_steps=n_steps, lr=1e-4, n_particles=n_particles) # Plot results z_lnL = swyft.estimate_lnL(network1, x0, swyft.get_z(xz1)) plt.plot(z_lnL[0]['z'], np.exp(z_lnL[0]['lnL'])) plt.axvline(0.1) plt.axvline(0.9) plt.savefig("figs/testrun_05.png")
xz2, n_steps=n_steps, lr=1e-4, n_particles=n_particles) xz3 = swyft.update_xz(xz2, network2, x0, model, n_sims=n_sims, lnL_th=np.log(1e-5), append=False) network3 = swyft.MLP(x_dim, z_dim, n_hidden, xz_init=xz3) losses += swyft.train(network3, xz3, n_steps=n_steps, lr=1e-3, n_particles=n_particles) losses += swyft.train(network3, xz3, n_steps=n_steps, lr=1e-4, n_particles=n_particles) # Plot results z_lnL = swyft.estimate_lnL(network3, x0, swyft.get_z(xz3)) plt.plot(z_lnL[0]['z'], np.exp(z_lnL[0]['lnL'])) plt.axvline(0.1) plt.axvline(0.9) plt.savefig("figs/testrun_02.png")