Exemple #1
0
def initialize(N0, L, Nt, pflag):
    """Generate network, and initial conditions
    If pflag is true, construct and return transport matrix
    """
    qmax, qnet, enet = net.generate(N0, L, Nt)
    N = (N0 + Nt)
    #generate initial conditions. all nodes have S,E,C=(1,0,0)
    #except the infected node which has S,E,C=(0.1,0.05,0.05)
    init = np.zeros(3 * N)
    for i in range(N):
        init[i] = 1
    #the highest degree node is infected
    infnode = qnet.argmax(axis=0) + 1
    init[infnode - 1] = 0.1
    init[N + infnode - 1] = 0.05
    init[2 * N + infnode - 1] = 0.05
    if (pflag == True):
        #compute the transport matrix from the degree vector
        #and the adjacency matrix
        A = net.adjacency_matrix(N, enet)
        P = np.zeros([N, N])
        for j in range(N):
            for i in range(N):
                P[i, j] = qnet[i] * A[i, j]
            P[:, j] = P[:, j] / sum(P[:, j])
        return init, infnode, P
    return init, infnode
Exemple #2
0
def degree(N0, L, Nt, display=False):
    """Compute and analyze degree distribution based on degree list, q,
    corresponding to a recursive network (with model parameters specified
    as input.
    """
    #Call our generate function
    net.generate(N0, L, Nt)
    #find the size of the array we will need
    o = max(net.qnet)
    k, p, d, u = np.zeros(o), np.zeros(o), np.zeros(o), np.zeros(o)
    #Loop to increase k for every connection appearing in qnet
    for l in net.qnet:
        k[l - 1] = k[l - 1] + 1
    al = sum(k)
    #Assign the probability
    for i in range(o):
        u[i - 1] = k[i - 1] / al
    t = 0
    #Loop to assign all the non-zero numbers to d  and delete all the zero
    #elements from p
    for j in range(o):
        if u[j] != 0:
            p[t] = u[j]
            d[t] = j
            t = t + 1
    d = d[0:t]
    p = p[0:t]
    #If statement to plt or not plot the graph
    if display == True:
        #Try to find appropriate fit
        p2 = p[0:int(len(p) / 2)]
        d2 = d[0:int(len(p) / 2)]
        m = np.polyfit(d2, np.log(p2), 1)
        width = 1 / 1.5
        #Plot the figure
        plt.figure()
        plt.bar(d, p, width, color="blue")
        plt.plot(d2, np.exp(d2 * m[0] + m[1]), color="red")
        #Legend of the plot
        plt.legend(['D to P'])
        plt.xlabel('D')
        plt.ylabel('P')
        plt.title('Evgeniia Gleizer. Created by degree.')
        plt.show()
    print d
    print p
    return
Exemple #3
0
def degree(N0, L, Nt, display=False):
    """Compute and analyze degree distribution based on degree list, q,
    corresponding to a recursive network (with model parameters specified
    as input). The functional form of the the plot follows a negative
    y=a+1/x trend.
    """
    #   call generate and extract qmax
    qmax = net.generate(N0, L, Nt)

    #   create d, the array of degrees upto qmax
    d = np.array(range(qmax))

    #   run over d and remove values that have degree 0
    #   at the end, put d in order
    for n in d:
        if (n not in net.qnet):
            d = d[d != n]
    d = np.sort(d)

    #   initialise P as zeros with the dimension of d
    P = np.zeros(np.size(d))

    #   count repititions of node degrees
    for j in net.qnet:
        P[d == j] = P[d == j] + 1

#   normalise P
    P = P / np.sum(P)

    #   create plot
    if display:

        trunc = int(np.floor(np.size(P) / 2))
        Ptrunc = P[:trunc]
        dtrunc = d[:trunc]

        #       polyfit a polynomial of deg=1 approximating a negative exp function
        mu, rem = np.polyfit(dtrunc, np.log(Ptrunc), 1)

        #       plot bar chart and overlay the approximation
        plt.figure()
        plt.grid()
        plt.plot(d, np.exp(mu * d + rem), 'r')
        plt.bar(d, P, label='P(d)')
        plt.title('CHRIS MCLEOD, via degree(5,2,4000)')
        plt.xlabel('d')
        plt.ylabel('P')
        plt.legend(loc='best')
        plt.show()

    return d, P
Exemple #4
0
def initialize(N0,L,Nt,pflag):
    """Generate network, and initial conditions
    If pflag is true, construct and return transport matrix
    """
    #Call the generate function 
    qmax,qnet,enet=net.generate(N0,L,Nt)
    #assigning the length of qnet
    l=len(qnet)
    q=0
    #Loop for finding maximum valur of qnet
    for i in range(l):
        if (qnet[i]>q):
            q=qnet[i]
            n=i
    #Setting the initial conditions       
    y0=np.zeros(3*l)
    y0[0:l]=1
    y0[n-1]=0.1
    y0[l+(n-1)]=0.05
    y0[2*l+(n-1)]=0.05
    
    #Checking the display flag condition
    if pflag==True:
        #Calling the adjacency matrix fir anet
        anet=net.adjacency_matrix(N0+Nt,enet)
        #Setting the size of the transport matrix
        P=np.zeros((len(anet),len(anet)))
        #Loop over it's length
        for j in range(len(anet)):
            summ=0
            #Loop for the denominatoe
            for m in range(len(anet)):
                #Computing the denominator
                summ=summ+(qnet[m]*anet[m,j])
            #Loop over the width of P
            for i in range(len(anet)):
                #Assigning P
                P[i,j]=float(qnet[i]*anet[i,j])/summ
        return y0,n, P
    else:
        return y0,n
Exemple #5
0
def degree(N0, L, Nt, display=False):
    """Compute and analyze degree distribution based on degree list, q,
    corresponding to a recursive network (with model parameters specified
    as input.
    The functional form seen in the saved figure is the exponential of 
    the polyfit of degree 3 to the log of the first Np/2 values of P that
    are non zero.
    """
    qmax = net.generate(N0, L, Nt)
    q = net.qnet.tolist()
    P = np.zeros(qmax)
    d = np.array(range(1, qmax + 1))
    #compute degree distribution
    for i in d:
        P[i - 1] = float(q.count(i)) / float(np.size(q))
    Np = np.size(P)
    #if Nt>1000 compute a functional form for P(d)
    if (Nt > 1000):
        P1 = (P[:Np / 2 + 1])
        P1 = P1[np.nonzero(P1)]
        lP1 = np.log(P1)
        a1, a2, a3, a4 = np.polyfit(d[np.nonzero(P1)], lP1, 3)
        f = np.exp(a1 * np.power(d, 3) + a2 * np.power(d, 2) +
                   a3 * np.power(d, 1) + a4 * np.power(d, 0))
    #if display is True then plot the distribution and fit if Nt>1000
    if (display == True):
        width = 1 / 1.5
        plt.figure()
        plt.bar(d, P, width, align='center')
        if (Nt > 1000):
            plt.plot(d, f, 'r', label='Fit to P(d)')
        plt.xticks(np.arange(min(d), max(d) + 1, 10))
        plt.title('Mathilde Duverger degree')
        plt.xlabel('d')
        plt.ylabel('P(d)')
        plt.legend(loc='best')
        plt.show()
    return d, P


#if __name__ == "__main__":