Guest User

Untitled

a guest
Nov 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2.  
  3. N = 1000000
  4. S = N - 1
  5. I = 1
  6. beta = 0.6
  7.  
  8. sus = [] # infected compartment
  9. inf = [] # susceptible compartment
  10. prob = [] # probability of infection at time t
  11.  
  12. def infection(S, I, N):
  13. t = 0
  14. while (t < 100):
  15. S = S - beta * ((S * I / N))
  16. I = I + beta * ((S * I) / N)
  17. p = beta * (I / N)
  18.  
  19. sus.append(S)
  20. inf.append(I)
  21. prob.append(p)
  22. t = t + 1
  23.  
  24. infection(S, I, N)
  25. figure = plt.figure()
  26. figure.canvas.set_window_title('SI model')
  27.  
  28. figure.add_subplot(211)
  29. inf_line, =plt.plot(inf, label='I(t)')
  30.  
  31. sus_line, = plt.plot(sus, label='S(t)')
  32. plt.legend(handles=[inf_line, sus_line])
  33.  
  34. plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) # use scientific notation
  35.  
  36. ax = figure.add_subplot(212)
  37. prob_line = plt.plot(prob, label='p(t)')
  38. plt.legend(handles=prob_line)
  39.  
  40. type(ax) # matplotlib.axes._subplots.AxesSubplot
  41.  
  42. # manipulate
  43. vals = ax.get_yticks()
  44. ax.set_yticklabels(['{:3.2f}%'.format(x*100) for x in vals])
  45.  
  46. plt.xlabel('T')
  47. plt.ylabel('p')
  48.  
  49. plt.show()
  50.  
  51. [![import matplotlib.pylab as plt
  52.  
  53. N = 1000000
  54. S = N - 1
  55. I = 1
  56. beta = 0.3
  57. gamma = 0.1
  58.  
  59. sus = []
  60. inf = []
  61.  
  62. def infection(S, I, N):
  63. for t in range (0, 1000):
  64. S = S - (beta*S*I/N) + gamma * I
  65. I = I + (beta*S*I/N) - gamma * I
  66.  
  67. sus.append(S)
  68. inf.append(I)
  69.  
  70.  
  71. infection(S, I, N)
  72.  
  73. figure = plt.figure()
  74. figure.canvas.set_window_title('SIS model')
  75.  
  76. inf_line, =plt.plot(inf, label='I(t)')
  77.  
  78. sus_line, = plt.plot(sus, label='S(t)')
  79. plt.legend(handles=[inf_line, sus_line])
  80.  
  81. plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
  82.  
  83. plt.xlabel('T')
  84. plt.ylabel('N')
  85.  
  86. plt.show()
  87.  
  88. import matplotlib.pylab as plt
  89.  
  90. N = 1000000
  91. S = N - 1
  92. I = 1
  93. R = 0
  94. beta = 0.5
  95. mu = 0.1
  96.  
  97. sus = []
  98. inf = []
  99. rec = []
  100.  
  101. def infection(S, I, R, N):
  102. for t in range (1, 100):
  103. S = S -(beta * S * I)/N
  104. I = I + ((beta * S * I)/N) - R
  105. R = mu * I
  106.  
  107. sus.append(S)
  108. inf.append(I)
  109. rec.append(R)
  110.  
  111. infection(S, I, R, N)
  112.  
  113. figure = plt.figure()
  114. figure.canvas.set_window_title('SIR model')
  115.  
  116. inf_line, =plt.plot(inf, label='I(t)')
  117.  
  118. sus_line, = plt.plot(sus, label='S(t)')
  119.  
  120. rec_line, = plt.plot(rec, label='R(t)')
  121. plt.legend(handles=[inf_line, sus_line, rec_line])
  122.  
  123. plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
  124.  
  125. plt.xlabel('T')
  126. plt.ylabel('N')
  127.  
  128.  
  129. plt.show()
Add Comment
Please, Sign In to add comment