Example #1
0
    def test_decm_new(self):
        """test with 3 classes of cardinality 1
        and no zero degrees
        """

        n, s = (10, 25)

        A = mg.random_weighted_matrix_generator_dense(
            n, sup_ext=10, sym=False, seed=s, intweights=True
        )

        g = sample.DirectedGraph(A)

        g.solve_tool(
            model="decm_exp",
            method="quasinewton",
            initial_guess="uniform",
            max_steps=200,
            verbose=False,
        )
        l = -mof.loglikelihood_decm_exp(g.solution_array, g.args)

        # g._solution_error()
        # debug
        # print(g.r_dseq_out)
        # print(g.r_dseq_in)
        # print(g.rnz_dseq_out)
        # print(g.rnz_dseq_in)
        # print('\ntest 0: error = {}'.format(g.error))

        # test result
        self.assertTrue(l == g.model_loglikelihood())
Example #2
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: -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)
Example #3
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: -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)
Example #4
0
    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)