def test_loglikelihood_hessian_dcm(self): # n,s =(3, 1) n, s = (30, 1) a = mg.random_binary_matrix_generator_custom_density(n=n, p=0.15, sym=False, seed=s) g = sample.DirectedGraph(a) g.degree_reduction() g.initial_guess = "uniform" g.regularise = "eigenvalues" g._initialize_problem("dcm", "newton") k_out = g.args[0] k_in = g.args[1] nz_index_out = g.args[2] nz_index_in = g.args[3] n_rd = len(k_out) theta = np.random.rand(2 * n_rd) f_sample = np.zeros((n_rd * 2, n_rd * 2)) for i in range(n_rd * 2): f = lambda x: sample.loglikelihood_prime_dcm(x, g.args)[i] f_sample[i, :] = approx_fprime(theta, f, epsilon=1e-6) f_new = sample.loglikelihood_hessian_dcm(theta, g.args) """ for i in range(2*n_rd): for j in range(2*n_rd): if np.allclose(f_new[i,j], f_sample[i,j], atol=1e-1) == False: print(i,j) print(f_new[i,j]) print(f_sample[i,j]) print(f_sample[i,j]/f_new[i,j]) """ # debug # print(theta, x0) # print(g.args) # print(n_rd/n) # print(f_sample) # print(f_new) # test result self.assertTrue(np.allclose(f_sample, f_new, atol=1))
def test_loglikelihood_hessian_dcm_new_emi_simmetry(self): n, s = (50, 1) # n,s =(2, 1) a = mg.random_binary_matrix_generator_custom_density(n=n, p=0.15, sym=False, seed=s) g = sample.DirectedGraph(a) g.degree_reduction() g.initial_guess = "uniform" g.regularise = "identity" g._initialize_problem("dcm", "newton") k_out = g.args[0] k_in = g.args[1] nz_index_out = g.args[2] nz_index_in = g.args[3] n_rd = len(k_out) # print(n_rd/n) theta = np.random.rand(2 * n_rd) f_new = loglikelihood_hessian_dcm_new(theta, g.args) for i in range(2 * n_rd): for j in range(2 * n_rd): if f_new[i, j] - f_new[j, i] != 0: print(i, j) # debug # print(a) # print(theta, x0) # print(g.args) # print(f_sample) # print(f_new) # test result self.assertTrue( np.allclose(f_new - f_new.T, np.zeros((2 * n_rd, 2 * n_rd))))
def test_loglikelihood_hessian_dcm_new_emi(self): n, s = (50, 1) a = mg.random_binary_matrix_generator_custom_density(n=n, p=0.15, sym=False, seed=s) g = sample.DirectedGraph(a) g.degree_reduction() g.initial_guess = "uniform" g.regularise = "identity" g._initialize_problem("dcm", "newton") k_out = g.args[0] k_in = g.args[1] nz_index_out = g.args[2] nz_index_in = g.args[3] n_rd = len(k_out) theta = np.random.rand(2 * n_rd) f_sample = np.zeros((n_rd * 2, n_rd * 2)) for i in range(n_rd * 2): f = lambda x: loglikelihood_prime_dcm_new(x, g.args)[i] f_sample[i, :] = approx_fprime(theta, f, epsilon=1e-6) f_new = loglikelihood_hessian_dcm_new(theta, g.args) # debug # print(a) # print(theta, x0) # print(g.args) # print(f_sample) # print(f_new) # test result self.assertTrue(np.allclose(f_sample, f_new))