aboutsummaryrefslogtreecommitdiffstats
path: root/src/make_plots.py
blob: 195157603ae69391a506ab8bda88a877bb47d9e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import matplotlib.pyplot as plt
import numpy as np
import cascade_creation
import convex_optimization
import algorithms
import rip_condition


def plot_rip_numberofnodes(max_proba, n_min, n_max, p_init, n_cascades, K_max):
    """
    Plots the RIP constant for varying number of nodes (n_max included)
    """
    x = np.arange(n_min, n_max+1)
    y = []

    for n_nodes in x:
        print(n_nodes)
        G = cascade_creation.InfluenceGraph(max_proba=.3)
        G.erdos_init(n=n_nodes, p=.1)  # TODO: handle different inits!
        cascades = cascade_creation.generate_cascades(G, p_init=p_init,
                                                      n_cascades=n_cascades)
        M, __ = cascade_creation.icc_matrixvector_for_node(cascades, None)
        M = cascade_creation.normalize_matrix(M)
        y.append(rip_condition.find_kth_rip_constants(M, 4)) #

    print(y)

    plt.clf()
    plt.plot(x, y)
    #plt.show()

    return x, y


def compare_greedy_and_lagrange_cs284r():
    """
    Compares the performance of the greedy algorithm on the
    lagrangian sparse recovery objective on the Facebook dataset
    for the CS284r project
    """
    G = cascade_creation.InfluenceGraph(max_proba = .8)
    G.import_from_file("../datasets/subset_facebook_SNAPnormalize.txt")
    A = cascade_creation.generate_cascades(G, p_init=.05, n_cascades=2000)

    #Greedy
    G_hat = algorithms.greedy_prediction(G, A)
    algorithms.correctness_measure(G, G_hat, print_values=True)

    #Lagrange Objective
    G_hat = algorithms.recovery_l1obj_l2constraint(G, A,
            passed_function=convex_optimization.sparse_recovery,
            floor_cstt=.1, lbda=10)
    algorithms.correctness_measure(G, G_hat, print_values=True)


def test():
    """
    unit test
    """
    if 0:
        plot_rip_numberofnodes(max_proba=.3, n_min=30, n_max=30,
                            p_init=.01, n_cascades=100, K_max=4)
    if 1:
        compare_greedy_and_lagrange_cs284r()

if __name__=="__main__":
    test()