-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIRsim.m
127 lines (90 loc) · 3.48 KB
/
SIRsim.m
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function [total, X_out, t_out, gross_I, gross_R] = SIRsim(N,beta,gamma,epsilon, max)
%SIRsim Simulates a household SIR model with three events, intenal infection,
%recovery and external infection. To allow for infinite possible households
%once the third event occurs a new household is added to the model (of size 3).
% TOTAL provides the number of susceptible, infected at any given event
% time (total(1) = susceptible, total(2) = infected)
% X_out records the number of susceptible and infected in each household.
% with each household assigned 2 rows in the matrix
% t_out is a vector that holds the corresponding event times
% gross_I is the gross number of people that have been effected at each
% event time
% gross_R is the gross number of recovered people at each event time
no_events = 3;
% initial conditions
X = [N-1; 1]; % one infected case, total cases, hh1
t = 0;
gross_I = 1; %gross number of infected
gross_R = 0; % gross number of recovered
total = X;
X_out = X;
t_out = 0;
while total(2,end) > 0 % while there exists some infected individuals
household = 1; % initialise each iteration
% step 1. Calculate the rates of each event given the current state.
for hh = 1:length(X)/2 % transition rates for each house
a(1+3*(hh-1)) = beta*X(2*hh)*X(2*hh-1)/(N-1); % infection- logistic growth beta*I*(N-I)/(N-1)
a(2+3*(hh-1)) = gamma*X(2*hh); % recovery
a(3+3*(hh-1)) = epsilon*X(2*hh); % external infection - X(2) or X(4)??
end
a0 = sum(a); % rate until the first event occurs - min(A1,A2,..)
% step 2. Calculate the time to the next event.
t = t - log(rand)/a0;
% step 3. Update the state.
r = rand*a0;
% defining the intervals of events
for i = 1:length(a)
if i == 1
A(1,1) = 0;
A(1,2) = a(1);
else
A(i,1) = sum(a(1:i-1));
A(i,2) = sum(a(1:i));
end
end
for ii = 1:length(A) % number of intervals
if r > A(ii,1) && r < A(ii,2) == 1
e = ii;
end
end
event = e;
while event > 3
event = event - 3;
household = household + 1; % which household the event takes place at
end
S_row = household*2-1;
I_row = household*2;
if event == 1
% infection of household
X(S_row) = X(S_row) - 1;
X(I_row) = X(I_row) + 1;
gross_I = [gross_I gross_I(end)+1];
gross_R = [gross_R gross_R(end)];
elseif event == 2
% recovery of household
X(I_row) = X(I_row) - 1;
gross_I = [gross_I gross_I(end)];
gross_R = [gross_R gross_R(end)+1];
elseif event == 3
% external infection from all households - dont track which
% individual spread it
length_X = length(X);
X(length_X+1) = N-1;
X(length_X+2) = 1;
gross_I = [gross_I gross_I(end)+1];
gross_R = [gross_R gross_R(end)];
end
[length_X_out, ~] = size(X_out);
if length(X) ~= length_X_out
X_out(length(X),:) = 0;
end
% record the time and state after each jump
X_out = [X_out, X];
t_out = [t_out, t];
count = [sum(X(1:2:length(X))); sum(X(2:2:length(X)))];
total = [total count];
if t > max
break
end
end
end