Example #1
0
    def test_gowl_vs_glasso_duality_gap_3(self):
        """
        Duality Gap goes negative in this case. Should that happen?
        """
        np.random.seed(680)
        p = 10
        blocks = [
            Block(dim=p,
                  idx=0,
                  block_min_size=2,
                  block_max_size=6,
                  block_value=0.9),
            Block(dim=p,
                  idx=1,
                  block_min_size=2,
                  block_max_size=6,
                  block_value=-0.9),
            Block(dim=p,
                  idx=3,
                  block_min_size=2,
                  block_max_size=6,
                  block_value=-0.5),
        ]
        theta_star, blocks, theta_blocks = generate_theta_star_gowl(p=p,
                                                                    alpha=0.5,
                                                                    noise=0.1,
                                                                    blocks=blocks)
        lam1 = 0.001  # controls sparsity
        lam2 = 0.01  # encourages equality of coefficients
        rho = oscar_weights(lam1, lam2, (p ** 2 - p) / 2)

        theta_star = theta_star[0]
        sigma = np.linalg.inv(theta_star)
        n = 100
        X = np.random.multivariate_normal(np.zeros(p), sigma, n)
        X = standardize(X)
        S = np.cov(X.T)

        theta_0 = np.linalg.inv(S)
        model = GOWLModel(X, S, theta_0, lam1, lam2, 'backtracking', max_iters=100000)
        model.fit()
        theta_gowl = model.theta_hat

        gl = GraphicalLasso(max_iter=200)
        gl.fit(S)
        theta_glasso = gl.get_precision()

        print('Non zero entries in precision matrix {}'.format(np.count_nonzero(theta_gowl)))
        plot_multiple_theta_matrices_2d([theta_blocks, theta_star, theta_glasso, theta_gowl],
                                        [f"Blocks: {len(blocks)}", 'True Theta', 'GLASSO', 'GOWL'])

        _fit_evaluations(theta_star, theta_glasso, 3, 'GLASSO')
        _fit_evaluations(theta_star, theta_gowl, 3, 'GOWL')

        y_hat_gowl = spectral_clustering(theta=theta_gowl, K=4)
        y_hat_glasso = spectral_clustering(theta=theta_glasso, K=4)
        y_true = spectral_clustering(theta=theta_blocks, K=4).flatten()
        _cluster_evaluations(y_true, y_hat_gowl, 'GOWL')
        _cluster_evaluations(y_true, y_hat_glasso, 'GLASSO')
Example #2
0
 def __init__(self, x, lam1, lam2, max_iters=10, epsilon=1e-05):
     super(CCGOWLModel, self).__init__('CCGOWL', x, None)
     self.nsfunc = OWL()
     self.sfunc = MSE()
     self.n, self.p = x.shape
     self._lambdas = oscar_weights(lam1, lam2, self.p - 1)
     self.max_iters = max_iters
     self.epsilon = epsilon
     self.beta_0 = np.zeros(x.shape[1] - 1)
     self.beta = np.zeros(x.shape[1] - 1)
     self.theta_hat = np.zeros((self.p, self.p))
Example #3
0
    def __init__(self,
                 x,
                 sample_cov,
                 lam1,
                 lam2,
                 ss_type,
                 dual_gap=True,
                 max_iters=10,
                 epsilon=1e-05):
        super(GOWLModel, self).__init__('GOWL', x, None)
        _, p = sample_cov.shape
        self.S = sample_cov
        self.nsfunc = GOWL()
        self.sfunc = LOGDET()
        self.theta0 = np.linalg.inv(sample_cov)
        self._lambdas = oscar_weights(lam1, lam2, (p**2 - p) / 2)
        self.dual_gap = dual_gap
        self.max_iters = max_iters
        self.epsilon = epsilon
        self.theta_hat = np.zeros(self.S.shape)

        assert ss_type in ['constant', 'backtracking'], \
            "Choose a valid option: [constant, backtracking]"
        self.ss_type = ss_type