Advertisement
Guest User

Untitled

a guest
Mar 30th, 2013
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. import numpy as np
  2. from scipy import integrate
  3. from matplotlib.pyplot import *
  4. import sys
  5.  
  6.  
  7. # Age-structured SIR couple ODEs
  8. def SIR(x, t) :#, beta, gamma, mu, M) :
  9.  
  10.    
  11.     # Ensure x has correct shape
  12.     # ( reshape to (_, 3) in size and
  13.     # reshape at return to (_,) in size
  14.     if len(np.shape(x)) != 2 :
  15.         x = x.reshape(len(x)/3, 3);
  16.  
  17.    
  18.  
  19.     # Preallocate array
  20.     X = np.zeros(x.shape);
  21.  
  22.  
  23.     # Temporary params
  24.     IonN = x[:, 1] / sum(x.T);
  25.  
  26.  
  27.  
  28.     # S_0
  29.     X[0,0] = mu*x[-1,0] - alpha*x[0,0] - x[0,0]*beta*np.dot(M[0, :], IonN);
  30.          # birth ##   # aging ####   # infection ######################
  31.  
  32.     # S_i, i \in {1, ..., n-1}
  33.     X[1:-1, 0] = alpha*(x[0:-2, 0]-x[1:-1, 0]) - x[1:-1, 0]*beta*np.dot(M[1:-1, :], IonN);
  34.              # aging ####################    # infection ############################
  35.  
  36.     # S_n
  37.     X[-1, 0] = -mu*x[-1,0] + alpha*x[-2, 0] - x[-1,0]*beta*np.dot(M[-1,:], IonN);
  38.             # death ##   # aging ######   # infection ######################
  39.  
  40.  
  41.  
  42.  
  43.     # I_0
  44.     X[0, 1] = -alpha*x[0,1] + x[0,0]*beta*np.dot(M[0, :], IonN) - gamma*x[0, 1];
  45.           #aging ######   # infection #####################   # recovery ##
  46.  
  47.     # I_i, i \in {1, ..., n-1}
  48.     X[1:-1, 1] = alpha*(x[0:-2,1]-x[1:-1,1]) + x[1:-1, 0]*beta*np.dot(M[1:-1,:], IonN) - gamma*x[1:-1, 1];
  49.              # aging ###################   # infection ###########################   # recovery #####
  50.  
  51.     # I_n
  52.     X[-1, 1] = alpha*x[-2, 1] - mu*x[-1, 1] + x[-1, 0]*beta*np.dot(M[-1, :], IonN) - gamma*x[-1, 1];
  53.            # aging ######   # death ###   # infection ########################   # recovery ###
  54.  
  55.  
  56.  
  57.  
  58.     # R_0
  59.     X[0, 2] = -alpha*x[0, 2] + gamma*x[0, 1];
  60.           # aging ######   # recovery ##
  61.  
  62.     # R_i, i \in {1, ..., n-1}
  63.     X[1:-1, 2] = alpha*(x[0:-2, 2]-x[1:-1, 2]) - gamma*x[1:-1, 1];
  64.              # aging #####################   # recovery #####
  65.  
  66.     # R_n
  67.     X[-1, 2] = alpha*x[-2, 2] - mu*x[-1, 2] - gamma*x[-1, 2];
  68.            # aging ######   # death ###   # recovery ###
  69.  
  70.  
  71.    
  72.     return X.reshape(ageclasses*3,);
  73.    
  74.  
  75.  
  76.  
  77.     #return x#.reshape(ageclasses*3,);
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. if __name__ == '__main__':
  88.  
  89.     # Params
  90.     # [Time] = weeks
  91.     popsize = 100000.;  # Total population size
  92.     mu = 1./(52.*60.);  # Death rate
  93.     gamma = 1./2.;      # Recovery rate
  94.     beta = 3.;
  95.     ageclasses = 61;    # Number of age classes
  96.     t = np.arange(0, 520, 1);
  97.     t = t.reshape(len(t),1);
  98.     M = np.ones([ageclasses, ageclasses]);
  99.  
  100.     # Define population vector N
  101.     N = np.ones([ageclasses]) * popsize / ageclasses;
  102.  
  103.     # Import contact matrix M
  104.     M = np.ones([ageclasses, ageclasses]);
  105.    
  106.     # Import initial population distribution
  107.     file = open("agedistribution.csv");
  108.     x0 = np.array([]);
  109.     for line in file :
  110.         x0 = np.append(x0, line);
  111.     x0 = np.concatenate((np.array([x0.astype(np.float)]).T, np.zeros([ageclasses, 2])), axis=1);
  112.     x0 = x0.reshape(len(x0)*3, );
  113.  
  114.  
  115.     x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
  116.  
  117. #   plot(t, x);
  118. #   show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement