Exemple #1
0
def _shrink(adj, X, lamb, **kwargs):
    # Initialize CMI cache matrix
    n, d = X.shape
    cmis = np.zeros([d, d])
    cmis[np.eye(d, dtype=bool)] = np.inf
    for i in range(d):
        x = X[:, i]
        for j in adj[i].nonzero()[0]:
            other_adj_i = adj[i] & (np.arange(d) != j)
            y = X[:, j]
            z = X[:, other_adj_i]
            cmis[i, j] = conditional_mutual_information(x, y, z, **kwargs)

    while np.count_nonzero(adj) > 0:
        scores = cmis + cmis.T
        imin, jmin = np.unravel_index(np.argmin(scores), scores.shape)
        if scores[imin, jmin] > lamb:
            return adj
        adj[imin, jmin] = adj[jmin, imin] = 0
        cmis[imin, jmin] = cmis[jmin, imin] = np.inf

        # Re-compute CMIs
        for i in range(d):
            x = X[:, i]
            for j in adj[i].nonzero()[0]:
                other_adj_i = adj[i] & (np.arange(d) != j)
                y = X[:, j]
                z = X[:, other_adj_i]
                cmis[i, j] = conditional_mutual_information(x, y, z, **kwargs)

    return adj
Exemple #2
0
def _grow(adj, X, lamb, **kwargs):
    # Initialize CMI cache matrix
    n, d = X.shape
    cmis = np.zeros([d, d])
    cmis[np.eye(d, dtype=bool)] = -np.inf
    for i in range(d):
        x = X[:, i]
        for j in range(i):
            y = X[:, j]
            cmis[i, j] = cmis[j, i] = mutual_information(x, y, **kwargs)

    while np.count_nonzero(adj) < d**2 - d:
        scores = cmis + cmis.T
        imax, jmax = np.unravel_index(np.argmax(scores), scores.shape)
        if scores[imax, jmax] <= lamb:
            return adj
        adj[imax, jmax] = adj[jmax, imax] = 1
        cmis[imax, jmax] = cmis[jmax, imax] = -np.inf

        # Re-compute CMIs
        for i in (imax, jmax):
            x = X[:, i]
            z = X[:, adj[i]]
            non_adj = ~adj[i] & (np.arange(d) != i)
            for j in non_adj.nonzero()[0]:
                y = X[:, j]
                cmis[i, j] = conditional_mutual_information(x, y, z, **kwargs)

    return adj
Exemple #3
0
def _shrink(selected, X, y, lamb, k, **kwargs):
    while True:
        min_cmi = np.inf
        for i in selected:
            x = X[:, i]
            z = X[:, list(set(selected) - {i})]
            cmi = conditional_mutual_information(x, y, z, **kwargs)
            if min_cmi > cmi:
                min_cmi = cmi
                min_idx = i
        if min_cmi >= lamb or len(selected) == k:
            return selected
        selected.remove(min_idx)
Exemple #4
0
def _shrink(adj, i, X, lamb, **kwargs):
    n, d = X.shape
    x = X[:, i]
    for j in adj[i].nonzero()[0]:
        other_adj = adj[i] & (np.arange(d) != j)
        y = X[:, j]
        z = X[:, other_adj]
        cmi = conditional_mutual_information(x, y, z, **kwargs)
        if cmi <= lamb:
            adj[i, j] = adj[j, i] = 0
        if np.count_nonzero(adj[i]) == 0:
            break
    return adj
Exemple #5
0
def _grow(selected, X, y, lamb, k, **kwargs):
    n, d = X.shape
    while True:
        max_cmi = -np.inf
        not_selected = set(range(d)) - set(selected)
        z = X[:, selected]
        for i in not_selected:
            x = X[:, i]
            cmi = conditional_mutual_information(x, y, z, **kwargs)
            if max_cmi < cmi:
                max_cmi = cmi
                max_idx = i
        if max_cmi <= lamb or len(selected) == k:
            return selected
        selected.append(max_idx)
Exemple #6
0
def _shrink(adj, i, X, lamb, **kwargs):
    n, d = X.shape
    x = X[:, i]
    updated = True
    while updated:
        updated = False
        for j in adj[i].nonzero()[0]:
            other_adj = adj[i] & (np.arange(d) != j)
            y = X[:, j]
            z = X[:, other_adj]
            cmi = conditional_mutual_information(x, y, z, **kwargs)
            if cmi <= lamb:
                adj[i, j] = adj[j, i] = 0
                updated = True
                break
    return adj
Exemple #7
0
def _grow(adj, i, X, lamb, **kwargs):
    n, d = X.shape
    x = X[:, i]
    updated = True
    while updated:
        updated = False
        non_adj = ~adj[i] & (np.arange(d) != i)
        for j in non_adj.nonzero()[0]:
            y = X[:, j]
            z = X[:, adj[i]]
            cmi = conditional_mutual_information(x, y, z, **kwargs)
            if cmi > lamb:
                adj[i, j] = adj[j, i] = 1
                updated = True
                break
    return adj
Exemple #8
0
def _grow(adj, i, X, lamb, **kwargs):
    n, d = X.shape
    x = X[:, i]
    updated = True
    while updated:
        updated = False
        vmax = -np.inf
        z = X[:, adj[i]]
        non_adj = ~adj[i] & (np.arange(d) != i)
        for j in non_adj.nonzero()[0]:
            y = X[:, j]
            cmi = conditional_mutual_information(x, y, z, **kwargs)
            if vmax < cmi:
                vmax = cmi
                imax, jmax = i, j
        if vmax >= lamb:
            adj[imax, jmax] = adj[jmax, imax] = 1
            updated = True
    return adj