Example #1
0
def test_spectral_clustering_on_generative_model(scalar):
    params = {
        'alpha': 0.05,
        'beta': 0.08,
        'mu_diag': 0.00075 * scalar,
        'mu_off_diag': 0.00035 if sim_type == 'b' else 0.00035 * scalar,
        'scale': False,
        'number_of_nodes': 256
    }

    event_dict, true_class_assignments = utils.simulate_community_hawkes(
        params)
    num_nodes = len(true_class_assignments)
    # Spectral clustering on aggregated adjacency matrix
    agg_adj = utils.event_dict_to_aggregated_adjacency(num_nodes, event_dict)
    agg_adj_pred = spectral_cluster(agg_adj, num_classes=n_classes)
    agg_adj_sc_rand = adjusted_rand_score(true_class_assignments, agg_adj_pred)

    if not also_use_unweighted_adjacency:
        return agg_adj_sc_rand

    # Spectral clustering on aggregated adjacency matrix
    adj = utils.event_dict_to_adjacency(num_nodes, event_dict)
    adj_pred = spectral_cluster(adj, num_classes=n_classes)
    adj_sc_rand = adjusted_rand_score(true_class_assignments, adj_pred)

    return agg_adj_sc_rand, adj_sc_rand, np.sum(adj) / (num_nodes**2)
def test_spectral_clustering_on_generative_model(n_nodes):
    if agg_adj_should_fail:
        params = {
            'number_of_nodes': n_nodes,
            'alpha': 7.0,
            'beta': 8.0,
            'mu_off_diag': 0.001,
            'mu_diag': 0.002,
            'scale': False,
            'end_time': 400,
            'class_probabilities': class_prob,
            'n_cores': chip_n_cores
        }
    else:
        params = {
            'number_of_nodes': n_nodes,
            'alpha': 0.001,
            'beta': 0.008,
            'mu_off_diag': 0.001,
            'mu_diag': 0.001,
            # 'mu_diag': 0.002,
            'alpha_diag': 0.006,
            'scale': False,
            'end_time': 400,
            'class_probabilities': class_prob,
            'n_cores': chip_n_cores
        }

    # event_dict, true_class_assignments = utils.simulate_community_hawkes(
    #     params, network_name="10-block-10k-nodes-higher-mu-diff")

    event_dict, true_class_assignments = utils.simulate_community_hawkes(
        params)

    # Spectral clustering on adjacency matrix
    adj = utils.event_dict_to_adjacency(n_nodes, event_dict)
    adj_sc_pred = spectral_cluster(adj, num_classes=n_classes, verbose=False)
    adj_sc_rand = adjusted_rand_score(true_class_assignments, adj_sc_pred)

    # Spectral clustering on aggregated adjacency matrix
    agg_adj = utils.event_dict_to_aggregated_adjacency(n_nodes, event_dict)
    agg_adj_pred = spectral_cluster(agg_adj,
                                    num_classes=n_classes,
                                    verbose=False)
    agg_adj_sc_rand = adjusted_rand_score(true_class_assignments, agg_adj_pred)

    return adj_sc_rand, agg_adj_sc_rand
Example #3
0
def test_spectral_clustering_on_generative_model(n, t, k):
    params = {'number_of_nodes': n,
              'end_time': t,
              'class_probabilities': np.ones(k) / k,
              'alpha': 0.06,
              'beta': 0.08,
              'mu_diag': 0.085,
              'mu_off_diag': 0.065,
              'scale': False,
              'n_cores': 1}

    event_dict, true_class_assignments = utils.simulate_community_hawkes(params)

    # Spectral clustering on aggregated adjacency matrix
    agg_adj = utils.event_dict_to_aggregated_adjacency(len(true_class_assignments), event_dict)
    agg_adj_pred = spectral_cluster(agg_adj, num_classes=k)
    agg_adj_sc_rand = adjusted_rand_score(true_class_assignments, agg_adj_pred)

    return agg_adj_sc_rand
def calc_mean_and_error_of_count_estiamte(n_nodes):
    params = {
        'number_of_nodes': n_nodes,
        'class_probabilities': class_probs,
        'end_time': end_time,
        'mu_diag': mu_diag,
        'mu_off_diag': mu_off_diag,
        'alpha': alpha_off_diag,
        'alpha_diag': alpha_diag,
        'beta': beta_off_diag,
        'beta_diag': beta_diag,
        'scale': False
    }

    event_dict, true_node_membership = utils.simulate_community_hawkes(params)

    invalid_cluster = True

    while invalid_cluster:
        # Spectral clustering on aggregated adjacency matrix
        agg_adj = utils.event_dict_to_aggregated_adjacency(n_nodes, event_dict)
        node_membership = spectral_cluster(agg_adj,
                                           num_classes=n_classes,
                                           verbose=False)
        unique_vals, cnts = np.unique(node_membership, return_counts=True)
        invalid_cluster = len(unique_vals) != n_classes
        if len(unique_vals) != n_classes:
            print(unique_vals, cnts)

    sc_rand = adjusted_rand_score(true_node_membership, node_membership)
    sc_rand = np.zeros(
        (n_classes, n_classes
         )) + sc_rand  # match the shape of other params to retrieve easily

    # param estimation with estimated communities
    bp_mu, bp_alpha, bp_beta, bp_alpha_beta_ratio = model_utils.estimate_bp_hawkes_params(
        event_dict, node_membership, end_time, n_classes)
    # param estimation with known communities. k_ is for known_
    k_bp_mu, k_bp_alpha, k_bp_beta, k_bp_alpha_beta_ratio = model_utils.estimate_bp_hawkes_params(
        event_dict, true_node_membership, end_time, n_classes)
    return bp_mu, bp_alpha_beta_ratio, bp_alpha, bp_beta, sc_rand, k_bp_mu, k_bp_alpha_beta_ratio, k_bp_alpha, k_bp_beta
def calc_mean_and_error_of_count_estiamte(n_nodes):
    params = {
        'number_of_nodes': n_nodes,
        'class_probabilities': class_probs,
        'end_time': end_time,
        'alpha': alpha,
        'beta': beta,
        'mu_diag': mu_diag,
        'scale': False
    }

    event_dict, node_membership = utils.simulate_community_hawkes(params)

    if estimate_alpha_beta:
        bp_mu, bp_alpha, bp_beta, bp_alpha_beta_ratio = model_utils.estimate_bp_hawkes_params(
            event_dict, node_membership, end_time, len(class_probs))
        return bp_mu, bp_alpha_beta_ratio, bp_alpha, bp_beta

    agg_adj = utils.event_dict_to_aggregated_adjacency(n_nodes, event_dict)
    bp_mu, bp_alpha_beta_ratio = estimate_hawkes_from_counts(
        agg_adj, node_membership, end_time, 1e-10 / end_time)

    return bp_mu, bp_alpha_beta_ratio
    n_classes = 4
    n_nodes = 1024
    duration = 50

    params = {
        'number_of_nodes': n_nodes,
        'alpha': 0.6,
        'beta': 0.8,
        'mu_off_diag': 0.8,
        'mu_diag': 1.6,
        'end_time': duration,
        'class_probabilities': np.ones(n_classes) / n_classes,
        'n_cores': -1
    }

    event_dict, true_class_assignments = utils.simulate_community_hawkes(
        params, network_name="local_seach_test_networks", load_if_exists=False)

    agg_adj = utils.event_dict_to_aggregated_adjacency(n_nodes, event_dict)
    spectral_node_membership = spectral_cluster(agg_adj, num_classes=n_classes)
    sc_rand = adjusted_rand_score(true_class_assignments,
                                  spectral_node_membership)
    print(f"SC Rand index: {sc_rand:.3f}")

    print("Parallel")
    tic = time.time()
    local_search_node_membership = chip_local_search(event_dict,
                                                     n_classes,
                                                     spectral_node_membership,
                                                     duration,
                                                     max_iter=10,
                                                     n_cores=34,