def test_check_convergence(): N = 500 nu = 0.1 M_test = 200 N_test = int(nu * N) mu = np.array([0.0, 0.1, 2 * np.pi, 0.1 * np.pi], dtype=np.float32) lb_a12 = 0.0 ub_a12 = 10.0 lb_a21 = -10.0 ub_a21 = 0.0 a11 = Parameter("a11", 1, 0.0) a12 = Parameter("a12", 1, lb_a12, ub_a12) a21 = Parameter("a21", 1, lb_a21, ub_a21) a22 = Parameter("a22", 1, ub=0.0) params = [a11, a12, a21, a22] M = Model("lds_2D", params) M.set_eps(linear2D_freq) q_theta, opt_data, epi_path, failed = M.epi( mu, num_iters=1000, K=10, N=N, stop_early=True, save_movie_data=False, random_seed=1, ) assert not failed assert (opt_data["converged"] == True).sum() > 0 epi_df = M.get_epi_df() epi_df_row = epi_df[epi_df["iteration"] == epi_df["iteration"].max()].iloc[0] init = epi_df_row["init"] init_params = {"mu": init["mu"], "Sigma": init["Sigma"]} nf = M._df_row_to_nf(epi_df_row) aug_lag_hps = M._df_row_to_al_hps(epi_df_row) best_k, converged, best_H = M.get_convergence_epoch( init_params, nf, mu, aug_lag_hps, alpha=0.05, nu=0.1, ) assert converged return None
def test_epi(): mu = np.array([0.0, 0.1, 2 * np.pi, 0.1 * np.pi]) lb_a12 = 0.0 ub_a12 = 10.0 lb_a21 = -10.0 ub_a21 = 0.0 a11 = Parameter("a11", 1, 0.0) a12 = Parameter("a12", 1, lb_a12, ub_a12) a21 = Parameter("a21", 1, lb_a21, ub_a21) a22 = Parameter("a22", 1, ub=0.0) params = [a11, a12, a21, a22] M = Model("lds_2D", params) M.set_eps(linear2D_freq) q_theta, opt_data, epi_path, failed = M.epi( mu, num_iters=100, K=1, save_movie_data=True, log_rate=10, ) z = q_theta(50) g = q_theta.plot_dist(z) M.epi_opt_movie(epi_path) params = [a11, a12, a21, a22] # should load from prev epi M = Model("lds_2D", params) M.set_eps(linear2D_freq) q_theta, opt_data, epi_path, failed = M.epi( mu, num_iters=100, K=1, save_movie_data=True ) print("epi_path", epi_path) epi_df = M.get_epi_df() epi_df_row = epi_df[epi_df["iteration"] == 100].iloc[0] q_theta = M.get_epi_dist(epi_df_row) opt_data_filename = os.path.join(epi_path, "opt_data.csv") M.set_eps(linear2D_freq) q_theta, opt_data, epi_path, failed = M.epi( mu, num_iters=100, K=1, save_movie_data=True, log_rate=10, ) opt_data_cols = ["k", "iteration", "H", "cost", "converged"] + [ "R%d" % i for i in range(1, M.m + 1) ] for x, y in zip(opt_data.columns, opt_data_cols): assert x == y assert q_theta is not None z = q_theta(1000) log_q_z = q_theta.log_prob(z) assert np.sum(z[:, 0] < 0.0) == 0 assert np.sum(z[:, 1] < lb_a12) == 0 assert np.sum(z[:, 1] > ub_a12) == 0 assert np.sum(z[:, 2] < lb_a21) == 0 assert np.sum(z[:, 2] > ub_a21) == 0 assert np.sum(z[:, 3] > 0.0) == 0 assert np.sum(1 - np.isfinite(z)) == 0 # Intentionally swap order in list to insure proper handling. params = [a22, a21, a12, a11] M = Model("lds", params) M.set_eps(linear2D_freq) q_theta, opt_data, epi_path, _ = M.epi( mu, K=2, num_iters=100, stop_early=True, verbose=True, save_movie_data=True, log_rate=10, ) M.epi_opt_movie(epi_path) z = q_theta(1000) log_q_z = q_theta.log_prob(z) assert np.sum(z[:, 0] < 0.0) == 0 assert np.sum(z[:, 1] < lb_a12) == 0 assert np.sum(z[:, 1] > ub_a12) == 0 assert np.sum(z[:, 2] < lb_a21) == 0 assert np.sum(z[:, 2] > ub_a21) == 0 assert np.sum(z[:, 3] > 0.0) == 0 assert np.sum(1 - np.isfinite(z)) == 0 print("DOING ABC NOW") # Need finite support for ABC a11 = Parameter("a11", 1, -10.0, 10.0) a12 = Parameter("a12", 1, -10.0, 10.0) a21 = Parameter("a21", 1, -10.0, 10.0) a22 = Parameter("a22", 1, -10.0, 10.0) params = [a11, a12, a21, a22] M = Model("lds_2D", params) M.set_eps(linear2D_freq) init_type = "abc" init_params = {"num_keep": 50, "mean": mu[:2], "std": np.sqrt(mu[2:])} q_theta, opt_data, epi_path, failed = M.epi( mu, num_iters=100, K=1, init_type=init_type, init_params=init_params, save_movie_data=True, log_rate=10, ) params = [a11, a12, a21, a22] M = Model("lds2", params) M.set_eps(linear2D_freq) # This should cause opt to fail with nan since c0=1e20 is too high. q_theta, opt_data, epi_path, _ = M.epi( mu, K=3, num_iters=1000, c0=1e20, stop_early=True, verbose=True, save_movie_data=False, log_rate=10, ) with raises(IOError): M.epi_opt_movie(epi_path) for x, y in zip(opt_data.columns, opt_data_cols): assert x == y with raises(ValueError): def bad_f(a11, a12, a21, a22): return tf.expand_dims(a11 + a12 + a21 + a22, 0) M.set_eps(bad_f) params = [a11, a12, a21, a22] M = Model("lds2", params) init_params = {"mu": 2 * np.zeros((4,)), "Sigma": np.eye(4)} nf = NormalizingFlow("autoregressive", 4, 1, 2, 10) al_hps = AugLagHPs() epi_path, exists = M.get_epi_path(init_params, nf, mu, al_hps, eps_name="foo") assert not exists return None