def main(n_particles=100, T=250): ts, xs, ys = load_generated_traj() apf = make_apf_obj() res = apf.run( n_particles, ys, ) xs_apf = res['xts'] aidx_apf = res['aidxs'] long_res = to_long_format( xs_apf, aidx_apf, T=T, ) fig, ax = visualize_genealogy(**long_res, window=5) odir = osp.join(osp.dirname(osp.dirname(osp.abspath(__name__))), 'res') if not osp.exists(odir): mkdir(odir) fig.savefig(osp.join(odir, 'ex2-gen-exe.png'))
def main(n_particles = 100, T = 250): odir = osp.join(osp.dirname(osp.dirname(osp.abspath(__name__))),'res') if not osp.exists(odir): mkdir(odir) ts, xs, ys = load_generated_traj() apf = make_apf_obj() res_wess = apf.run(n_particles, ys, resampler = 'systematic', adaptive_resampling = True) xs_apf_wess,_,aidxs_apf_wess,ness_wess = res_wess.values() fig_ness,ax_ness = visualize_ness_comp(ness_wess,n_particles) fig_ness.savefig(osp.join(odir,'ex2-ness-comp.png')) n_uni_wess = get_unique_primary_ancestors(aidxs_apf_wess) long_res = to_long_format(xs_apf_wess, aidxs_apf_wess, T = T, ) fig_gen, ax_gen = visualize_genealogy(**long_res, window = 5, xs_true = xs,) fig_gen.savefig(osp.join(odir,'ex2-gen-exg.png'))
def main(n_particles=100, T=250): odir = osp.join(osp.dirname(osp.dirname(osp.abspath(__name__))), 'res') if not osp.exists(odir): mkdir(odir) ts, xs, ys = load_generated_traj() apf = make_apf_obj() res_sys = apf.run(n_particles, ys, resampler='systematic') xs_apf_sys = res_sys['xts'] aidxs_apf_sys = res_sys['aidxs'] res_mult = apf.run(n_particles, ys, resampler='multinomial') xs_apf_mult = res_mult['xts'] aidxs_apf_mult = res_mult['aidxs'] n_uni_sys = get_unique_primary_ancestors(aidxs_apf_sys) n_uni_mult = get_unique_primary_ancestors(aidxs_apf_mult) fig_uni, ax_uni = visualize_diveristy(n_uni_sys, n_uni_mult, labels=['systematic', 'multinomial']) fig_uni.savefig(osp.join(odir, 'ex2-sys-vs-mult.png')) long_res = to_long_format( xs_apf_sys, aidxs_apf_sys, T=T, ) fig_gen, ax_gen = visualize_genealogy(**long_res, window=5) fig_gen.savefig(osp.join(odir, 'ex2-gen-exf.png'))
def main(): ts, xs, ys = load_generated_traj() KF = make_kf_obj() xs_kf, ps_kf = KF.estimate_traj(ys, T=ts[-1] + 1) _, Nsm, diff_bpf_mu, diff_bpf_var = load_bpf_results() apf = make_apf_obj() mu_results = {} var_results = {} table_results = np.zeros((Nsm.shape[0], 2)) for k, n_particles in enumerate(Nsm): res = apf.run(n_particles, ys) xs_apf = res['xts'] aidxs_apf = res['aidxs'] mu = np.sum(xs_apf * res['weights'], axis=1) var = np.sum(res['weights'] * (xs_apf - mu.reshape(-1, 1))**2, axis=1) #mu = xs_apf.mean(axis = 1) #var = xs_apf.var(axis = 1) mu_diff = np.abs(mu - xs_kf) var_diff = np.abs(var - ps_kf) table_results[k, 0] = np.mean(mu_diff) table_results[k, 1] = np.mean(var_diff) mu_results.update({'mu_' + str(n_particles): mu_diff}) var_results.update({'var_' + str(n_particles): var_diff}) odir = osp.join(osp.dirname(osp.dirname(osp.abspath(__name__))), 'res') if not osp.exists(odir): mkdir(odir) comp_fig, comp_ax = visualize_difference_comp( bpf_mu=diff_bpf_mu.mean(axis=0), bpf_var=diff_bpf_var.mean(axis=0), apf_mu=table_results[:, 0], apf_var=table_results[:, 1], ns=Nsm) comp_fig.savefig(osp.join(odir, 'ex2-comp-bpf-apf.png')) table_results = pd.DataFrame(table_results, index=Nsm, columns=['Mean', 'Variance']) mu_results = pd.DataFrame(mu_results, index=ts) var_results = pd.DataFrame(var_results, index=ts) mu_results.to_csv(osp.join(odir, 'ex2-mu-difference-apf.tsv'), sep='\t', header=True, index=True) var_results.to_csv(osp.join(odir, 'ex2-var-difference-apf.tsv'), sep='\t', header=True, index=True) with open(osp.join(odir, 'ex2-stat-mean-table-apf.tex'), 'w+') as fopen: ltx = table_results.to_latex(index=True, bold_rows=True, column_format='c|c|c') ltx = ltx.replace('\\\\', '\\\\ \n \\hline') fopen.writelines(ltx)
def main(): ts, xs, ys = load_generated_traj() xt_xtm1 = lambda xtm1: st.norm(0.8 * xtm1, np.sqrt(0.5)) yt_xt = lambda xt: st.norm(2 * xt, np.sqrt(0.1)) x0 = st.norm(0, np.sqrt(2)) KF = make_kf_obj() xs_kf, ps_kf = KF.estimate_traj(ys, T=ts[-1] + 1) bspf = BSPF( xt_xtm1_dist=xt_xtm1, yt_xt_dist=yt_xt, x0_dist=x0, ) ns = np.array([10, 50, 100, 2000, 5000]).astype(int) mu_results = {} var_results = {} table_results = np.zeros((ns.shape[0], 2)) width = ns.shape[0] * 3 + 0.4 height = 2 * 3 + 0.4 fig, ax = plt.subplots(2, ns.shape[0], sharey='row', figsize=(width, height)) for aa in ax.flatten(): aa.yaxis.set_tick_params(labelleft=True) for k, n in enumerate(ns): res = bspf.run(ys, n) mu = np.sum(res['xs'] * res['weights'], axis=0) var = np.sum(res['weights'] * (res['xs'] - mu)**2, axis=0) mu_diff = np.abs(mu - xs_kf) var_diff = np.abs(var - ps_kf) table_results[k, 0] = np.mean(mu_diff) table_results[k, 1] = np.mean(var_diff) mu_results.update({'mu_' + str(n): mu_diff}) var_results.update({'var_' + str(n): var_diff}) ax[0, k].plot(ts[1::], mu_diff[1::], color='black') ax[1, k].plot(ts[1::], var_diff[1::], color='black') ax[0, k].set_ylabel(r'$|\mu_{KF}-\mu_{PF}|$') ax[1, k].set_ylabel(r'$|\mathrm{Var}_{KF}-\mathrm{Var}_{PF}|$') ax[0, k].set_xlabel('Time') ax[1, k].set_xlabel('Time') ax[0, k].set_title(f"{n} Particles") for pos in ['top', 'right']: for r in range(ax.shape[0]): ax[r, k].spines[pos].set_visible(False) odir = osp.join(osp.dirname(osp.dirname(osp.abspath(__name__))), 'res') if not osp.exists(odir): mkdir(odir) fig.savefig(osp.join(odir, 'ex2-kalman-vs-pf.png')) mu_results = pd.DataFrame(mu_results, index=ts) var_results = pd.DataFrame(var_results, index=ts) table_results = pd.DataFrame(table_results, index=ns, columns=['Mean', 'Variance']) mu_results.to_csv(osp.join(odir, 'ex2-mu-difference-bpf.tsv'), sep='\t', header=True, index=True) var_results.to_csv(osp.join(odir, 'ex2-var-difference-bpf.tsv'), sep='\t', header=True, index=True) with open(osp.join(odir, 'ex2-stat-mean-table.tex'), 'w+') as fopen: ltx = table_results.to_latex(index=True, bold_rows=True, column_format='c|c|c') ltx = ltx.replace('\\\\', '\\\\ \n \\hline') fopen.writelines(ltx)