-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalSizeMultiAgeOut.m
311 lines (302 loc) · 8.1 KB
/
finalSizeMultiAgeOut.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
function [f,g,D]=finalSizeMultiAgeOut(gamma,n,nbar,na,NN,NNbar,NNrep,minNind,maxNind,maxN,Kbar,K1,Cbar,betaS,betaI,betaD,beta3,isdual,solvetype,numseed,eps)
%ZfinalSizeAllMulti2
%isdual: 0=SM, 1=DM, 2=IM
%solvetype: 1=FSC, 2=ODE, 3=SCM
%eps=.3;
if isdual==0
beta=betaS;
elseif isdual==1
beta=betaD;
elseif isdual==2
beta=betaI;
else
beta=beta3;
end
demog=1;
tauend=1;
plotTau=0;
time=(1:tauend);
lt=length(time);
t0=0; tend=360;
mu=1/80;%In ODE code
phi1=1; phi2=0;
NN0=NNrep; NN0(NNrep==0)=1;
Nages=NNbar./NN0;
alpha=1;%TSIR/sub-exp parameter
randic=0;
thetaGeom=0;
rate=eps;
%
%Theta:
%Prow=[0,0,0,1-eps,eps];
Prow=[0,0,0,0,1-eps,eps];
lp=length(Prow);
Prow=Prow/sum(Prow);
Pmat=repmat(Prow,nbar,1);
%v=[5,14,45,16]; vover=1./v; V=kron(vover',ones(n,1)); V=repmat(V,1,lp);
%w=[0,5,14,45]; woverv=w.*vover; W=kron(woverv',ones(n,1)); W=repmat(W,1,lp);
vover=[1/5,1/14,1/45,0];
V=kron(vover',ones(n,1)); V=repmat(V,1,lp);
%
%cen=0;
ages0=0; %maxN=0;
%
Ni=repmat(NNrep,1,nbar); Nj=Ni';
Niover=1./Ni; Niover(Ni==0)=1;
Mj=(Kbar')*NNbar;%C in denom?? .*Cbar %(Kbar')*
Mj(Mj==0)=1;
Mjover=1./Mj;
Mjover=repmat(Mjover',nbar,1);
if isdual==0
D=Kbar.*Mjover.*Cbar.*Nj;
elseif isdual==1
D=(K1.*Mjover)*((Kbar').*Cbar);
D=D.*Nj;
elseif isdual==2
D=Kbar'.*Niover.*Cbar.*Nj;
elseif isdual==3
D=Kbar.*Cbar;
end
%%
%Initial condition:
B=zeros(nbar,lp);
if randic==1
ProwRand=rand(1,lp+1); ProwRand(1)=[]; ProwRand=ProwRand/sum(ProwRand);
Brand=rand(nbar,1).*NNbar./NN0; %Brand=rand(1)*Brand./repmat(sum(Brand,2),1,lp);
B=repmat(ProwRand,nbar,1).*repmat(Brand,1,lp);
end
%}
%
Z0=sum(B(:,2:end),2)*(1-mu);
%Z0(3*n+1:end)=eps*NNbar(3*n+1:end)./NN0(3*n+1:end);
%Z0(2*n+1:3*n)=12/45*eps*NNbar(2*n+1:3*n)./NN0(2*n+1:3*n);
%%
zn=zeros(nbar,1);
A1=zeros(nbar,lt);
A2=A1;
N0=NN; N0(NN==0)=1;
%NNprob=NNbar/sum(NN); NNprob=ones(nbar,1)/sum(NNbar);
seed=numseed;%*NNprob;
seedvec=zeros(nbar,1); seedvec(2*n+1:3*n)=seed*ones(n,1);
%seedvec=seed*ones(nbar,1);
thresh=0;%Remove from ODE solver
for t=1:lt
if solvetype==1
%Final size:
addbit=0;%seed;%tend*
%If seed=0, no epidemic happens - because use ODE solver for initial condition
IC=XODEsolveAllMulti(gamma,NN,n,nbar,NNbar,NNrep,NN0,minNind,maxNind,D,Z0,beta,ages0,t,t0,tend,zn,phi1,phi2,seed,2,thresh,alpha,seedvec); IC=IC./NN0;%New IC here (if ever necessary)
options = optimset('Display','off');
funt=@(Zi)solveZi(Zi,Z0,beta,gamma,D,Nages,addbit);
Zsol=fsolve(funt,IC,options);
%
else%solvetype=2/3
Zsol=XODEsolveAllMulti(gamma,NN,n,nbar,NNbar,NNrep,NN0,minNind,maxNind,D,Z0,beta,ages0,t,t0,tend,zn,phi1,phi2,seed,solvetype,thresh,alpha,seedvec,plotTau);
Zsol=Zsol./NN0;
end
nu=Zsol-Z0;
A1(:,t)=Zsol;%Prop immune for spatial cell (before antigenic drift)
A2(:,t)=nu;%AR for spatial cell
%
if thetaGeom==1
Z0=(1-rate)*(1-mu)*Zsol;
else
%Assign immunity:
Bhat=[B(:,2:end),zeros(nbar,1)];
B=Bhat+repmat(nu,1,lp).*Pmat;
%Age the populations:
if demog==1
%{
BVtake=B.*V; Bsus=sum(BVtake(1:n,2:end),2);
BVadd=[zeros(n,lp);BVtake(1:3*n,:)].*W;
B=B-BVtake+BVadd; B(1:n,1)=B(1:n,1)+Bsus;
%}
Btake=B.*V; Badd=circshift(Btake,n,1);
B(end-n+1:end,:)=B(end-n+1:end,:)*15/16;
B=B-Btake+Badd;
end
Z0=sum(B(:,2:end),2);%*(1-mu);
end
end
%%
%}
f=A1;
g=A2;
end
function f=solveZi(Zi,Z0,beta,gamma,D,Nages,addbit)
lZi=log(Nages-Zi); lZi(Zi>1)=NaN;
f=lZi-log(Nages-Z0)+beta/gamma*D*(Zi-Z0)+addbit;
end
%%
function f=XODEsolveAllMulti(gamma,NN,n,nbar,NNbar,NNrep,NN0,minNind,maxNind,D,ic,beta,ages0,tau,t0,tend,zn,phi1,phi2,seed,solvetype,thresh,alpha,seedvec,plotTau)
icR=ic.*NNrep;
y0=[NNbar-icR;zn;icR];
cond=sum(icR<thresh);
if cond==0
seed=0;
end
%
if solvetype==2
[tout,yout]=ode45(@(t,y)integr8all(t,y,beta,gamma,n,nbar,NN,NN0,D,seed,phi1,phi2,alpha,seedvec),[t0,tend],y0);
%Incidence curve in here:
%
if tau==plotTau
figure
fs=12; lw=2;
Y=yout(:,nbar+1:2*nbar); %Z=yout(:,2*nbar+1:3*nbar);
%Y=sum(Y,2);
%Y1=Y(:,minNind)+Y(:,minNind+n)+Y(:,minNind+2*n)+Y(:,minNind+3*n); Y1=Y1/NN(minNind);
%Y2=Y(:,maxNind)+Y(:,maxNind+n)+Y(:,maxNind+2*n)+Y(:,maxNind+3*n); Y2=Y2/NN(maxNind);
Ysum=sum(Y,2);
Yall=Y(:,1:n)+Y(:,n+1:2*n)+Y(:,2*n+1:3*n)+Y(:,3*n+1:end);
Yall=abs(Yall);%****cheat ;)
%
%Unlogged plots:
hold on
%plot(tout,Y1,'--','linewidth',lw,'color',[.165,.31,.431]);%[.165,.31,.431][.447,.553,.647]
%plot(tout,Y2,'-','linewidth',lw,'color',[.165,.31,.431]);
plot(tout,Ysum,'k','linewidth',lw);
plot(tout,Yall);
%}
%{
%Logged plots:
semilogy(tout,Ysum,'k','linewidth',lw);
hold on
semilogy(tout,Yall);
%}
xlabel('Time (days)','FontSize',fs);
ylabel('Prevalence','FontSize',fs);
set(gca,'FontSize',fs);
maxY=max(Ysum);
%axis([0,tend,0,maxY])
axis ([0,tend,0,maxY])
%legend('Min','Max','location','NE')
grid on
grid minor
hold off
end
%}
rec0=yout(end,2*nbar+1:end)+yout(end,nbar+1:2*nbar);%******** Truncation - add infectious to rercovered?
f=rec0';
elseif solvetype==3
%ic1=round(NNbar/1000); neg=y0(1:nbar)-ic1; neg(neg>0)=0; ic1=ic1+neg;
%ic1=round(y0(1:nbar)/500);
y1=round(y0);%+[-ic1;ic1;zn];
f=stochSim(y1,beta,gamma,n,nbar,NN,NN0,D,seed,phi1,phi2,tau,alpha);
%}
end
end
%%
function f=integr8all(t,y,beta,gamma,n,nbar,NN,NN0,D,seed,phi1,phi2,alpha,seedvec)
mu=1/1800;%5*360=1800
%phi=phi1-phi2*cos(pi*t/180);
phi=1;%+sin(pi*t/180);
S=y(1:nbar);
I=y(nbar+1:2*nbar);
R=y(2*nbar+1:end);
%seed1=seed*S./NN0;%*exp(-t);
seed1=seedvec.*S./NN0;%*exp(-t);
Sfoi=phi*(beta*S.*(D*(I./NN0))+seed1);%seed1);
Sdot=-Sfoi;%+mu*R;
Idot=Sfoi-gamma*I;
Rdot=gamma*I;%-mu*R;
f=[Sdot;Idot;Rdot];
end
%%
function f=stochSim(y,beta,gamma,n,nbar,NN,N0,D,seed,phi1,phi2,tau,alpha)
%Feed in mu if required
factor=6;
tend=360*factor; beta=beta/factor; gamma=gamma/factor;
Vec=zeros(nbar,tend);
%
S=y(1:nbar);
I=y(nbar+1:2*nbar);
R=y(2*nbar+1:end);
%Test seed:
%[Nmax,maxInd]=max(NN);
%ii=4*(maxInd-1)+3;
%seed=0; I(ii)=10; S(ii)=S(ii)-10;
%
%I=floor(N0/10000); S=S-I;
%Different from here:
i=1;
threshold=30;%Number of time-steps for necessary simulation/seed
while i<tend && (i<threshold || sum(I)>0)%At least 30 time-steps
phi=1;%phi1-phi2*cos(pi*i*f1/180);%Just seed here
Sout=1-exp(-phi*(beta*(D*(I./N0).^alpha)+seed*heaviside(threshold-i)));%+mu*R;.^alpha
Sout(Sout>1)=1;
Sout=binornd(S,Sout); Sout(S==0)=0;
S=S-Sout; I=I+Sout;
%
Iout=1-exp(-gamma);
Iout=binornd(I,Iout);
I=I-Iout; R=R+Iout;
Vec(:,i)=I;
i=i+1;
end
%{
Vsum=sum(Vec,1);%/sum(NN);
maxV=max(Vsum);
fs=12; lw=2;
Vtot=Vsum(factor:factor:end);
Vall=Vec(1:n,factor:factor:end)+Vec(n+1:2*n,factor:factor:end)+Vec(2*n+1:3*n,factor:factor:end)+Vec(3*n+1:end,factor:factor:end);
%
%Unlogged plots:
figure
plot(1:tend/factor,Vtot,'-','linewidth',2,'color',[.447,.553,.647]);
plot(1:tend/factor,Vall);
axis([0,tend/factor,0,maxV]);
xlabel('Time (days)'); ylabel('Prevalence'); set(gca,'fontsize',fs)
grid on
grid minor
%}
%
%{
%Logged plots:
figure
semilogy(1:tend/factor,Vtot,'k','linewidth',lw)
hold on
semilogy(1:tend/factor,Vall);
axis([0,tend/factor,0,maxV]);
xlabel('Time (days)'); ylabel('Prevalence'); set(gca,'fontsize',fs)
grid on
grid minor
%}
f=R;
if sum(isnan(R))>0
fuck=1;
print('Somenting is NaN')
end
end
%%
%Incidence curve:
%{
if tau==10
figure
fs=15;
Y=yout(:,nbar+1:2*nbar);
Y=Y(:,1:n)+Y(:,n+1:2*n)+Y(:,2*n+1:3*n)+Y(:,3*n+1:end);
Nover=1./NN; Nover(NN==0)=0; Y=Y.*repmat(Nover',length(tout),1);
X=[NN,Y']; X=sortrows(X,1); Nsort=X(:,1); X=X(:,2:end); X=X';
hold on
k=100; nk=floor(n/k);
cmap=parula(nk);
colormap(cmap);
for i=1:nk
plot(tout,X(:,i*100),'-','linewidth',2,'color',cmap(i,:));
end
xlabel('Time (days)','FontSize',fs);
ylabel('Infectives','FontSize',fs);
set(gca,'FontSize',fs);
axis([0,tend,0,max(max(X))])
end
figure
fs=15;
Y=yout(:,nbar+1:2*nbar);
Y=sum(Y,2);
plot(tout,Y,'-','linewidth',2,'color',[.447,.553,.647]);
xlabel('Time (days)','FontSize',fs);
ylabel('Infectives','FontSize',fs);
set(gca,'FontSize',fs);
axis([0,tend,0,max(Y)])
%}