aboutsummaryrefslogtreecommitdiffstats
path: root/simulation/main.py
blob: 81133c7840e8178883998551d1aa787646f66143 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import mleNode as mn

import numpy as np
from numpy.linalg import norm
import numpy.random as nr
from scipy.optimize import minimize
import matplotlib.pyplot as plt
import seaborn
from random import random, randint
from six.moves import range

seaborn.set_style("white")


def create_random_graph(n_nodes, p=.5):
    graph = .5 * np.random.binomial(2, p=.5, size=(n_nodes, n_nodes))
    for k in range(len(graph)):
        graph[k, k] = 0
    return np.log(1. / (1 - p * graph))


def create_star(n_nodes, p=.5):
    graph = np.zeros((n_nodes, n_nodes))
    graph[0] = np.ones((n_nodes,))
    graph[0, 0] = 0
    for index, row in enumerate(graph[1:-1]):
        row[index + 1] = 1
    graph[-1, 1] = 1
    return np.log(1. / (1 - p * graph))


def simulate_cascade(x, graph):
    """
    Simulate an IC cascade given a graph and initial state.

    For each time step we yield:
        - susc: the nodes susceptible at the beginning of this time step
        - x: the subset of susc who became infected
    """
    yield x, np.zeros(graph.shape[0], dtype=bool)
    susc = np.ones(graph.shape[0], dtype=bool)
    while np.any(x):
        susc = susc ^ x  # nodes infected at previous step are now inactive
        if not np.any(susc):
            break
        x = 1 - np.exp(-np.dot(graph.T, x))
        y = nr.random(x.shape[0])
        x = (x >= y) & susc
        yield x, susc


def uniform_source(graph, *args, **kwargs):
    x0 = np.zeros(graph.shape[0], dtype=bool)
    x0[nr.randint(0, graph.shape[0])] = True
    return x0


def var_source(graph, t, node_p=None, *args, **kwargs):
    if node_p is None:
        node_p = np.ones(len(graph)) / len(graph)
    x0 = np.zeros(graph.shape[0], dtype=bool)
    x0[nr.choice(a=len(graph), p=node_p)] = True
    return x0


def simulate_cascades(n, graph, source=uniform_source):
    for t in range(n):
        x0 = source(graph, t)
        yield simulate_cascade(x0, graph)


def build_cascade_list(cascades, collapse=False):
    x, s = [], []
    for cascade in cascades:
        xlist, slist = zip(*cascade)
        x.append(np.vstack(xlist))
        s.append(np.vstack(slist))
    if not collapse:
        return x, s
    else:
        return np.vstack(x), np.vstack(s)


if __name__ == "__main__":
    #g = np.array([[0, 0, 1], [0, 0, 0.5], [0, 0, 0]])
    #p = 0.5
    #g = np.log(1. / (1 - p * g))
    g = create_random_graph(n_nodes=3)
    print(g)
    sizes = [10**3]
    for si in sizes:
        cascades = simulate_cascades(si, g)
        cascade, y_obs = mn.build_matrix(cascades, 2)
        print(mn.infer(cascade, y_obs))
        #conf = mn.bootstrap(cascade, y_obs, n_iter=100)
        #estimand = np.linalg.norm(np.delete(conf - g[0], 0, axis=1), axis=1)
        #plt.hist(estimand, bins=40)
        #plt.show()
        #error.append(mn.confidence_interval(*np.histogram(estimand, bins=50)))
    #plt.plot(sizes, error)
    #plt.show()