コード例 #1
0
    def test_CReAMa_original_Dianati_random_dense_20_dir(self):

        network = mg.random_weighted_matrix_generator_dense(
            n=20, sup_ext=10, sym=False, seed=None
        )
        network_bin = (network > 0).astype(int)

        g = sample.DirectedGraph(adjacency=network)

        g.solve_tool(
            model="CReAMa",
            method="fixed-point",
            initial_guess = "random",
            adjacency=network_bin,
            max_steps=1000,
            verbose=False,
        )

        g.solution_error()

        # test result

        self.assertTrue(g.relative_error_strength < 1e-1)
        self.assertTrue(g.relative_error_strength < 1e-2)
        self.assertTrue((g._weighted_realisation() >= 0).all())
コード例 #2
0
    def test_CReAMa_cm_quasinewton_random_dense_20(self):

        network = mg.random_weighted_matrix_generator_dense(n=20,
                                                            sup_ext=10,
                                                            sym=True,
                                                            seed=None)
        network_bin = (network > 0).astype(int)

        g = sample_und.UndirectedGraph(adjacency=network)

        g.solve_tool(
            model="CReAMa",
            method="newton",
            initial_guess="random",
            adjacency="cm-new",
            max_steps=1000,
            verbose=False,
        )

        g.solution_error()

        # test result

        self.assertTrue(g.relative_error_strength < 1e-1)
        self.assertTrue(g.relative_error_strength < 1e-2)
    def test_fixedpoint_dcm_10(self):
        # test Matrix 1
        n, seed = (40, 35)
        A = mg.random_weighted_matrix_generator_dense(n,
                                                      sym=False,
                                                      seed=seed,
                                                      sup_ext=100,
                                                      intweights=True)
        A[0, :] = 0

        g = sample.DirectedGraph(A)

        g._solve_problem(
            model="decm",
            method="fixed-point",
            max_steps=20000,
            verbose=False,
            initial_guess="uniform",
            linsearch=True,
        )

        g.solution_error()
        # debug
        # print("\n test 4, zeros, dimension n = {}, error = {}".format(n, g.error))

        # test result
        self.assertTrue(g.error < 1)
コード例 #4
0
    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: -sample.loglikelihood_prime_decm_new(x, args)
        fun_jac = lambda x: -sample.loglikelihood_hessian_decm_new(x, args)
        step_fun = lambda x: -sample.loglikelihood_decm_new(x, args)
        lin_fun = lambda x: sample.linsearch_fun_DECM_new(x, (step_fun, ))
        hes_reg = sample.hessian_regulariser_function

        sol = sample.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 = sample.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)
コード例 #5
0
    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: -sample.loglikelihood_prime_decm_new(x, args)
        fun_jac = lambda x: -sample.loglikelihood_hessian_diag_decm_new(
            x, args)
        step_fun = lambda x: -sample.loglikelihood_decm_new(x, args)
        lin_fun = lambda x: sample.linsearch_fun_DECM_new(x, (step_fun, ))
        hes_reg = sample.hessian_regulariser_function

        sol = sample.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 = sample.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)
コード例 #6
0
    def test_iterative_2(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: sample.iterative_decm_new(x, args)
        step_fun = lambda x: -sample.loglikelihood_decm_new(x, args)
        lin_fun = lambda x: sample.linsearch_fun_DECM_new(x, (step_fun, ))
        hes_reg = sample.hessian_regulariser_function

        sol = sample.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 = sample.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 5: error = {}'.format(err))
        # print('method: {}, matrix {}x{} '.format('iterative', n,n))

        # test result
        self.assertTrue(err < 1)
コード例 #7
0
    def test_ecm(self):
        n, seed = (4, 22)
        A = mg.random_weighted_matrix_generator_dense(n,
                                                      sym=False,
                                                      seed=seed,
                                                      sup_ext=100,
                                                      intweights=True)

        x0 = np.random.rand(n)
        g = sample_u.UndirectedGraph(A)
        g.initial_guess = x0
        g._set_initial_guess_CReAMa()
        self.assertTrue(g.x0.all() == x0.all())
コード例 #8
0
    def test_CREAMA_uniform(self):
        n, seed = (4, 22)
        A = mg.random_weighted_matrix_generator_dense(n,
                                                      sym=False,
                                                      seed=seed,
                                                      sup_ext=100,
                                                      intweights=True)

        g = sample_u.UndirectedGraph(A)
        g.initial_guess = 'strengths_minor'
        g._set_initial_guess('CReAMa')

        x = (g.strength_sequence > 0).astype(float) / (g.strength_sequence + 1)
        self.assertTrue(g.x0.all() == x.all())
コード例 #9
0
    def test_decm_new_uniform(self):
        n, seed = (4, 22)
        A = mg.random_weighted_matrix_generator_dense(n,
                                                      sym=False,
                                                      seed=seed,
                                                      sup_ext=100,
                                                      intweights=True)

        g = sample.DirectedGraph(A)
        g.initial_guess = 'uniform'
        g._set_initial_guess('decm_new')
        self.assertTrue(
            np.concatenate((g.x, g.y, g.out_strength,
                            g.in_strength)).all() == np.ones(4 * n).all())
コード例 #10
0
    def test_decm(self):
        n, seed = (4, 22)
        A = mg.random_weighted_matrix_generator_dense(n,
                                                      sym=False,
                                                      seed=seed,
                                                      sup_ext=100,
                                                      intweights=True)

        x0 = np.random.rand(4 * n)
        g = sample.DirectedGraph(A)
        g.initial_guess = x0
        g._set_initial_guess('decm')
        g._set_solved_problem_decm(x0)
        self.assertTrue(np.concatenate((g.x, g.y)).all() == x0.all())
コード例 #11
0
    def test_loglikelihood_hessian_diag_dcm_new_zeros(self):

        # convergence relies heavily on x0
        n, s = (10, 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
        A[:, 5] = 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)

        g = sample.DirectedGraph(A)
        g.initial_guess = "uniform"
        g.regularise = "identity"
        g._initialize_problem("decm", "newton")
        # theta = np.random.rand(6)
        theta = 0.5 * np.ones(n * 4)
        theta[np.concatenate((k_out, k_in, s_out, s_in)) == 0] = 1e4

        x0 = np.exp(-theta)

        f_sample = np.zeros(n * 4)
        for i in range(n * 4):
            f = lambda x: loglikelihood_prime_decm_new(x, g.args)[i]
            f_sample[i] = approx_fprime(theta, f, epsilon=1e-6)[i]

        f_new = loglikelihood_hessian_diag_decm_new(theta, g.args)

        # debug
        # print(a)
        # print(theta, x0)
        # print(g.args)
        # print('approx',f_sample)
        # print('my',f_new)
        # print('gradient', loglikelihood_prime_decm_new(theta, g.args))
        # print('diff',f_sample - f_new)
        # print('max',np.max(np.abs(f_sample - f_new)))

        # test result
        self.assertTrue(np.allclose(f_sample, f_new))
コード例 #12
0
    def test_CREAMA(self):
        n, seed = (4, 22)
        A = mg.random_weighted_matrix_generator_dense(n,
                                                      sym=False,
                                                      seed=seed,
                                                      sup_ext=100,
                                                      intweights=True)

        x0 = np.random.rand(n)
        g = sample_u.UndirectedGraph(A)
        g.initial_guess = x0
        g._set_initial_guess_CReAMa()
        g.full_return = False
        g._set_solved_problem_CReAMa(g.x0)
        self.assertTrue(g.beta.all() == x0.all())
コード例 #13
0
    def test_CREAMA_uniform(self):
        n, seed = (4, 22)
        A = mg.random_weighted_matrix_generator_dense(n,
                                                      sym=False,
                                                      seed=seed,
                                                      sup_ext=100,
                                                      intweights=True)

        g = sample.DirectedGraph(A)
        g.initial_guess = 'strengths_minor'
        g._set_initial_guess_CReAMa()
        x = np.concatenate(
            (sample.out_strength(A) / (sample.out_strength(A) + 1),
             sample.in_strength(A) / (sample.in_strength(A) + 1)))
        self.assertTrue(g.x0.all() == x.all())
コード例 #14
0
    def test_loglikelihood(self):
        # problem initialisation
        A = mg.random_weighted_matrix_generator_dense(n=10, sym=False)
        prova = sample.DirectedGraph(A)
        prova.initial_guess = "random"
        prova._initialize_problem("decm", "quasinewton")
        sol = np.concatenate((prova.x, prova.y, prova.b_out, prova.b_in))

        # casadi functions initialization
        x, f = casadi_loglikelihood_decm(A)
        casadi_fun = Function("f", [x], [f])
        f_og = sample.loglikelihood_decm(sol, prova.args)
        f_casadi = casadi_fun(sol)

        err_loglikelihood = abs(f_og - f_casadi)
        # print('loglikelihood og-casadi = {}'.format(err_loglikelihood))
        self.assertTrue(err_loglikelihood < 1e-10)
コード例 #15
0
    def test_loglikelihood_prime(self):
        # problem initialisation
        A = mg.random_weighted_matrix_generator_dense(n=10, sym=False)
        prova = sample.DirectedGraph(A)
        prova.initial_guess = "random"
        prova._initialize_problem("decm", "quasinewton")
        sol = np.concatenate((prova.x, prova.y, prova.b_out, prova.b_in))

        # casadi functions initialization
        x, f = casadi_loglikelihood_decm(A)
        casadi_fun = Function("f", [x], [f])

        fj = jacobian(f, x)
        casadi_fun_gradient = Function("j", [x], [fj])
        fj_og = sample.loglikelihood_prime_decm(sol, prova.args)
        fj_casadi = casadi_fun_gradient(sol)

        err_ll_prime = max(abs(fj_og - fj_casadi.__array__()[0]))

        # print('loglikelihood prime og-casadi = {}'.format(err_ll_prime ))

        self.assertTrue(err_ll_prime < 1e-10)
コード例 #16
0
    def test_ECM_Dianati_random_dense_20_undir(self):

        network = mg.random_weighted_matrix_generator_dense(n=20,
                                                            sup_ext=10,
                                                            sym=True,
                                                            seed=None,
                                                            intweights=True)
        network_bin = (network > 0).astype(int)

        g = sample_und.UndirectedGraph(adjacency=network)

        g.solve_tool(
            model="ecm",
            method="quasinewton",
            max_steps=1000,
            verbose=False,
            initial_guess="random",
        )

        g.solution_error()

        # test result
        self.assertTrue(g.error < 1e-1)
コード例 #17
0
    def test_loglikelihood_second(self):
        # problem initialisation
        A = mg.random_weighted_matrix_generator_dense(n=2, sym=False)
        prova = sample.DirectedGraph(A)
        prova.initial_guess = "random"
        prova._initialize_problem("decm", "quasinewton")
        sol = np.concatenate((prova.x, prova.y, prova.b_out, prova.b_in))

        # casadi functions initialization
        x, f = casadi_loglikelihood_decm(A)
        casadi_fun = Function("f", [x], [f])

        fj = jacobian(f, x)
        casadi_fun_gradient = Function("j", [x], [fj])

        fh = jacobian(fj, x)
        casadi_fun_hessian = Function("h", [x], [fh])
        fh_og = sample.loglikelihood_hessian_decm(sol, prova.args)
        fh_casadi = casadi_fun_hessian(sol)

        err_ll_second = np.max(np.abs(fh_og - fh_casadi.__array__()))
        # print(err_ll_second)

        self.assertTrue(err_ll_second < 1e-10)