def test_newton_4(self): # convergence relies heavily on x0 n, s = (40, 35) # n, s = (5, 35) A = mg.random_weighted_matrix_generator_dense(n, sup_ext=100, sym=False, seed=s, intweights=True) A[0, :] = 0 bA = np.array([[1 if aa != 0 else 0 for aa in a] for a in A]) k_out = np.sum(bA, axis=1) k_in = np.sum(bA, axis=0) s_out = np.sum(A, axis=1) s_in = np.sum(A, axis=0) x0 = 0.1 * np.ones(4 * n) # x0 = np.concatenate((-1*np.ones(2*n), np.ones(2*n))) args = (k_out, k_in, s_out, s_in) x0[np.concatenate(args) == 0] = 1e3 fun = lambda x: -mof.loglikelihood_prime_decm_exp(x, args) fun_jac = lambda x: -mof.loglikelihood_hessian_decm_exp(x, args) step_fun = lambda x: -mof.loglikelihood_decm_exp(x, args) lin_fun = lambda x: mof.linsearch_fun_DECM_exp(x, (step_fun, )) hes_reg = sof.matrix_regulariser_function sol = sof.solver( x0, fun=fun, step_fun=step_fun, fun_jac=fun_jac, linsearch_fun=lin_fun, tol=1e-6, eps=1e-5, max_steps=100, method="newton", verbose=False, regularise=True, full_return=False, linsearch=True, hessian_regulariser=hes_reg, ) sol = np.exp(-sol) ek = mof.expected_decm(sol) k = np.concatenate((k_out, k_in, s_out, s_in)) err = np.max(np.abs(ek - k)) # debug # print(ek) # print(k) # print('\ntest 4: error = {}'.format(err)) # print('method: {}, matrix {}x{} with zeros'.format('newton', n,n)) # test result self.assertTrue(err < 1e-1)
def test_quasinewton_1(self): n, s = (4, 25) A = mg.random_weighted_matrix_generator_dense(n, sup_ext=10, sym=False, seed=s, intweights=True) A[0, :] = 0 bA = np.array([[1 if aa != 0 else 0 for aa in a] for a in A]) k_out = np.sum(bA, axis=1) k_in = np.sum(bA, axis=0) s_out = np.sum(A, axis=1) s_in = np.sum(A, axis=0) x0 = 0.9 * np.ones(n * 4) args = (k_out, k_in, s_out, s_in) fun = lambda x: -mof.loglikelihood_prime_decm_exp(x, args) fun_jac = lambda x: -mof.loglikelihood_hessian_diag_decm_exp(x, args) step_fun = lambda x: -mof.loglikelihood_decm_exp(x, args) lin_fun = lambda x: mof.linsearch_fun_DECM_exp(x, ( mof.loglikelihood_decm_exp, args)) hes_reg = sof.matrix_regulariser_function sol = sof.solver( x0, fun=fun, step_fun=step_fun, fun_jac=fun_jac, linsearch_fun=lin_fun, tol=1e-6, eps=1e-10, max_steps=300, method="quasinewton", verbose=False, regularise=True, full_return=False, linsearch=True, hessian_regulariser=hes_reg, ) sol = np.exp(-sol) ek = mof.expected_decm(sol) k = np.concatenate((k_out, k_in, s_out, s_in)) err = np.max(np.abs(ek - k)) # debug # print(ek) # print(k) # print('\ntest 0: error = {}'.format(err)) # print('method = {}, matrix {}x{}'.format('quasinewton', n, n)) # test result self.assertTrue(err < 1e-1)
def test_iterative_3(self): n, s = (40, 35) # n, s = (5, 35) A = mg.random_weighted_matrix_generator_dense(n, sup_ext=100, sym=False, seed=s, intweights=True) A[0, :] = 0 bA = np.array([[1 if aa != 0 else 0 for aa in a] for a in A]) k_out = np.sum(bA, axis=1) k_in = np.sum(bA, axis=0) s_out = np.sum(A, axis=1) s_in = np.sum(A, axis=0) x0 = 0.1 * np.ones(n * 4) args = (k_out, k_in, s_out, s_in) x0[np.concatenate(args) == 0] = 1e3 fun = lambda x: mof.iterative_decm_exp(x, args) step_fun = lambda x: -mof.loglikelihood_decm_exp(x, args) lin_fun = lambda x: mof.linsearch_fun_DECM_exp(x, (step_fun, )) hes_reg = sof.matrix_regulariser_function sol = sof.solver( x0, fun=fun, step_fun=step_fun, linsearch_fun=lin_fun, tol=1e-6, eps=1e-10, max_steps=7000, method="fixed-point", verbose=False, regularise=True, full_return=False, linsearch=True, hessian_regulariser=hes_reg, ) sol = np.exp(-sol) ek = mof.expected_decm(sol) k = np.concatenate((k_out, k_in, s_out, s_in)) err = np.max(np.abs(ek - k)) # debug # print(ek) # print(k) # print('\ntest 6: error = {}'.format(err)) # print('method: {}, matrix {}x{} '.format('iterative', n,n)) # test result self.assertTrue(err < 1)
def test_iterative_1(self): n, seed = (4, 22) a = mg.random_binary_matrix_generator_dense(n, sym=False, seed=seed) a[0, :] = 0 k_out = np.sum(a, 1) k_in = np.sum(a, 0) g = sample.DirectedGraph(a) g.degree_reduction() g.initial_guess = "random" g.full_return = False g._set_initial_guess("dcm") g._set_args("dcm") x0 = g.x0 x0[x0 == 0] = 100 args = g.args fun = lambda x: iterative_dcm_exp(x, args) step_fun = lambda x: -loglikelihood_dcm_exp(x, args) lin_fun = lambda x: linsearch_fun_DCM_exp(x, (step_fun, )) hes_reg = sof.matrix_regulariser_function f = fun(x0) norm = np.linalg.norm(f) theta_sol = sof.solver( x0, fun=fun, step_fun=step_fun, linsearch_fun=lin_fun, tol=1e-6, eps=1e-10, max_steps=700, method="fixed-point", verbose=False, regularise=True, full_return=False, linsearch=False, hessian_regulariser=hes_reg, ) g._set_solved_problem_dcm(theta_sol) theta_sol_full = np.concatenate((g.x, g.y)) sol = np.exp(-theta_sol_full) sol[np.isnan(sol)] = 0 ek = np.concatenate(( expected_out_degree_dcm(sol), expected_in_degree_dcm(sol), )) k = np.concatenate((k_out, k_in)) err = np.max(abs(ek - k)) # debug # print(ek) # print(k) # print('\ntest 0: error = {}'.format(err)) # test result self.assertTrue(err < 1e-2)
def test_quasinewton_2(self): n, seed = (40, 26) a = mg.random_binary_matrix_generator_dense(n, sym=False, seed=seed) k_out = np.sum(a, 1) k_in = np.sum(a, 0) """ nz_index_out = np.array([0,1,2]) nz_index_in = np.array([0,1,2,3]) c = np.array([2,1,1]) """ g = sample.DirectedGraph(a) g.degree_reduction() g.initial_guess = "random" g.full_return = False g._set_initial_guess("dcm") g._set_args("dcm") x0 = g.x0 x0[x0 == 0] = np.infty args = g.args fun = lambda x: -loglikelihood_prime_dcm_exp(x, args) step_fun = lambda x: -loglikelihood_dcm_exp(x, args) fun_jac = lambda x: -loglikelihood_hessian_diag_dcm_exp(x, args) lin_fun = lambda x: linsearch_fun_DCM_exp(x, (step_fun, )) hes_reg = sof.matrix_regulariser_function f = fun(x0) norm = np.linalg.norm(f) theta_sol = sof.solver( x0, fun=fun, step_fun=step_fun, fun_jac=fun_jac, linsearch_fun=lin_fun, tol=1e-6, eps=1e-1, max_steps=100, method="quasinewton", verbose=False, regularise=True, full_return=False, linsearch=True, hessian_regulariser=hes_reg, ) g._set_solved_problem_dcm(theta_sol) theta_sol_full = np.concatenate((g.x, g.y)) sol = np.exp(-theta_sol_full) sol[np.isnan(sol)] = 0 ek = np.concatenate(( expected_out_degree_dcm(sol), expected_in_degree_dcm(sol), )) k = np.concatenate((k_out, k_in)) err = np.max(abs(ek - k)) # debug # print(ek) # print(k) # print('\ntest 6: error = {}'.format(err)) # test result self.assertTrue(err < 1e-2) # debug # print(ek) # print(k) # print('\ntest 7: error = {}'.format(err)) # test result self.assertTrue(err < 1e-2)