Esempio n. 1
0
def precision_similarity(precision, psi=None):
    from regain.norm import l1_od_norm
    from scipy.spatial.distance import squareform
    from itertools import combinations

    distances = squareform([l1_od_norm(t1 - t2) for t1, t2 in combinations(precision, 2)])
    print(distances)
    distances /= np.max(distances)
    return 1 - distances
Esempio n. 2
0
def objective(X, theta, alpha):
    n, d = X.shape
    objective = 0
    if not np.all(theta == theta.T):
        return np.float("inf")
    for r in range(d):
        selector = [i for i in range(d) if i != r]
        objective += objective_single_variable(X, theta[r, selector], n, r,
                                               selector, 0)
    return objective + alpha * l1_od_norm(theta)
def objective(n_samples, S, K, Z_0, Z_M, alpha, kernel, psi):
    """Objective function for time-varying graphical lasso."""
    obj = loss(S, K, n_samples=n_samples)
    if isinstance(alpha, np.ndarray):
        obj += sum(l1_od_norm(a * z) for a, z in zip(alpha, Z_0))
    else:
        obj += alpha * sum(map(l1_od_norm, Z_0))

    for m in range(1, Z_0.shape[0]):
        # all possible markovians jumps
        Z_L, Z_R = Z_M[m]
        obj += np.sum(np.array(list(map(psi, Z_R - Z_L))) * np.diag(kernel, m))

    return obj
Esempio n. 4
0
def objective(n_samples, S, K, Z_0, Z_1, Z_2, alpha, beta, psi):
    """Objective function for time-varying graphical lasso."""
    obj = loss(S, K, n_samples=n_samples)

    if isinstance(alpha, np.ndarray):
        obj += sum(l1_od_norm(a * z) for a, z in zip(alpha, Z_0))
    else:
        obj += alpha * sum(map(l1_od_norm, Z_0))

    if isinstance(beta, np.ndarray):
        obj += sum(b[0][0] * m for b, m in zip(beta, map(psi, Z_2 - Z_1)))
    else:
        obj += beta * sum(map(psi, Z_2 - Z_1))

    return obj
Esempio n. 5
0
def objective(X, theta, alpha):
    n, _ = X.shape
    objective = loss(X, theta)
    return objective + alpha * l1_od_norm(theta)
Esempio n. 6
0
def objective(S, R, K, L, alpha, tau):
    """Objective function for latent graphical lasso."""
    obj = 0.5 * squared_norm(S - R)
    obj += alpha * l1_od_norm(K)
    obj += tau * np.linalg.norm(L, ord="nuc")
    return obj
Esempio n. 7
0
def make_cluster_representative(
    n_dim=10,
    degree=2,
    n_clusters=3,
    T=15,
    n_samples=100,
    repetitions=False,
    cluster_series=None,
    shuffle=False,
):
    """Based on the cluster representative, generate similar graphs."""
    import networkx as nx

    cluster_reps = []
    adjacencies = []
    if cluster_series is not None:
        n_clusters = np.unique(cluster_series).size

    for i in range(n_clusters):
        representative = nx.random_regular_graph(d=degree, n=n_dim)
        A = nx.adjacency_matrix(representative).todense().astype(float)
        A[np.where(A != 0)] = np.random.rand(np.where(A != 0)[0].size) * 0.45
        np.fill_diagonal(A, 1)
        adjacencies.append(A)
        cluster_reps.append(representative)

    pos = np.arange(0, T, T // (n_clusters + 1))
    pos = list(pos) + [T - 1]

    if cluster_series is None:
        cluster_series = np.tile(range(n_clusters),
                                 (len(pos) // n_clusters) + 1)[:len(pos)]
        if shuffle:
            np.random.shuffle(cluster_series)
        # print(pos)
        # print(cluster_series)
        # pos = np.arange(0, T, T // (clusters + 1))
        # pos = list(pos) + [T - 1]
    else:
        assert len(cluster_series) == len(pos)
    #     a = np.where(cluster_series[:-1] != cluster_series[1:])[0] + 1
    #     T = len(cluster_series) # overwrites T
    #     pos = np.concatenate(([0], a, [T-1]))

    thetas = []
    for i in range(len(pos) - 1):
        # last one is always a representative
        how_many = int(pos[i + 1]) - int(pos[i]) - 1
        new_list = [adjacencies[cluster_series[i]]]
        target = adjacencies[cluster_series[i + 1]]

        for i in range(how_many):
            new = new_list[-1].copy()
            diffs = (new != 0).astype(int) - (target != 0).astype(int)
            diff = np.where(diffs != 0)
            if diff == ():
                break
            if i == 0:
                edges_per_change = int(
                    (np.nonzero(diffs)[0].shape[0] / 2) // (how_many + 1))
                if edges_per_change == 0:
                    edges_per_change += 1
            ixs = np.arange(diff[0].shape[0])
            np.random.shuffle(ixs)

            xs = diff[0][ixs[:edges_per_change]]
            ys = diff[1][ixs[:edges_per_change]]
            for j in range(xs.shape[0]):
                if diffs[xs[j], ys[j]] == -1:
                    new[xs[j], ys[j]] = np.random.rand(1) * 0.2
                    new[ys[j], xs[j]] = new[xs[j], ys[j]]
                else:
                    new[xs[j], ys[j]] = 0
                    new[ys[j], xs[j]] = 0
            new_list.append(new)

        thetas += new_list
    thetas.append(target)
    covs = [linalg.pinvh(t) for t in thetas]
    X = np.vstack([
        np.random.multivariate_normal(np.zeros(n_dim), c, size=n_samples)
        for c in covs
    ])
    y = np.repeat(np.arange(len(covs)), n_samples)

    distances = squareform(
        [l1_od_norm(t1 - t2) for t1, t2 in combinations(thetas, 2)])
    distances /= np.max(distances)
    labels_pred = AgglomerativeClustering(
        n_clusters=n_clusters, affinity="precomputed",
        linkage="complete").fit_predict(distances)

    id_cluster = np.repeat(labels_pred, n_samples)
    data = Bunch(
        X=X,
        y=y,
        id_cluster=id_cluster,
        precs=np.array(thetas),
        thetas=np.array(thetas),
        sparse_precs=np.array(thetas),
        cluster_reps=cluster_reps,
        cluster_series=cluster_series,
    )
    return data
Esempio n. 8
0
def objective(emp_cov, x, z, alpha):
    return -logl(emp_cov, x) + l1_od_norm(alpha * z)