-
Notifications
You must be signed in to change notification settings - Fork 3
/
spm_COVID_B.m
395 lines (330 loc) · 16.4 KB
/
spm_COVID_B.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
function T = spm_COVID_B(x,P,r)
% state dependent probability transition matrices
% FORMAT T = spm_COVID_B(x,P,r)
% x - probability distributions (tensor)
% P - model parameters
% r - marginals over regions
%
% T - probability transition matrix
%
% This subroutine creates a transition probability tensors as a function of
% model parameters and the joint density over four factors, each with
% several levels. Crucially the transition probabilities of any one factor
% depend only upon another factor. for example, in the factor modelling
% clinical status, the transition from acute respiratory distress (ARDS) to
% death depends upon infection status (infected or not infected) and
% location (in a critical care unit or not). This version has no absorbing
% states. States such as contributing to daily deaths or tests are modelled
% by remaining in that state for one day and then returning to another
% state.
%__________________________________________________________________________
% Copyright (C) 2020 Wellcome Centre for Human Neuroimaging
% Karl Friston
% $Id: spm_COVID_B.m 7942 2020-09-10 22:00:08Z spm $
# SPDX-License-Identifier: GPL-2.0
% setup
%==========================================================================
% identity matrices
%--------------------------------------------------------------------------
dim = size(x);
I = cell(ndims(x),1);
for i = 1:ndims(x)
I{i} = speye(dim(i));
end
% exponentiate parameters
%--------------------------------------------------------------------------
P = spm_vecfun(P,@exp);
% upper bound probabilities
%--------------------------------------------------------------------------
P.out = min(P.out,1);
P.trn = min(P.trn,1);
P.sev = min(P.sev,1);
P.fat = min(P.fat,1);
P.sur = min(P.sur,1);
Kday = exp(-1);
% probabilistic transitions: location
%==========================================================================
% P.out % P(work | home)
% P.sde % social distancing threshold
% P.cap % bed threshold (per capita)
% social distancing, based on prevalence of infection
%--------------------------------------------------------------------------
q = spm_sum(x,[3 4]);
q = q(1:3,:);
q = q/sum(q(:));
Prev = sum(q(:,2)); % prevalence of infection
Pcco = sum(q(3,:)); % CCU occupancy
% multiregional model
%--------------------------------------------------------------------------
if nargin > 2
% density over regions
%----------------------------------------------------------------------
q = spm_sum(r,[3 4]);
q = q(1:3,:);
q = q/sum(q(:));
Rrev = sum(q(:,2));
% mixture of strategies
%----------------------------------------------------------------------
if isfield(P,'fed')
Prev = Prev*(1 - P.fed) + Rrev*P.fed;
else
Prev = Rrev;
end
end
% lockdown (threshold) strategy
%--------------------------------------------------------------------------
Psde = spm_sigma(Prev,P.sde); % 1 - P(lockdown)
Pout = Psde*P.out; % P(work | home)
% bed availability
%--------------------------------------------------------------------------
Pcca = spm_sigma(Pcco,P.cap); % P(CCU | home, work, ARDS)
Piso = exp(-1/10); % period of self-isolation
b = cell(1,dim(3));
% viral spread
%--------------------------------------------------------------------------
Kspr = exp(-Psde*Prev/P.Tex); % period of exemption
% marginal: location {1} | asymptomatic {3}(1)
%--------------------------------------------------------------------------
% home work hospital removed isolated
%--------------------------------------------------------------------------
b{1} = [(1 - Pout) 1 1 (1 - Kspr) (1 - Piso);
Pout 0 0 0 0;
0 0 0 0 0;
0 0 0 Kspr 0;
0 0 0 0 Piso];
% marginal: location {1} | symptoms {3}(2)
%--------------------------------------------------------------------------
b{2} = [0 0 0 (1 - Kspr) 0;
0 0 0 0 0;
0 0 0 0 0;
0 0 0 Kspr 0;
1 1 1 0 1];
% marginal: location {1} | ARDS {3}(3)
%--------------------------------------------------------------------------
b{3} = [0 0 0 (1 - Kspr) 0;
0 0 0 0 0;
Pcca Pcca 1 0 Pcca;
0 0 0 Kspr 0;
(1 - Pcca) (1 - Pcca) 0 0 (1 - Pcca)];
% marginal: location {1} | deceased {3}(4)
%--------------------------------------------------------------------------
b{4} = [0 0 0 (1 - Kspr) 0;
0 0 0 0 0;
0 0 0 0 0;
1 1 1 Kspr 1;
0 0 0 0 0];
% kroneckor form (taking care to get the order of factors right)
%--------------------------------------------------------------------------
b = spm_cat(spm_diag(b));
b = spm_kron({b,I{2},I{4}});
B{1} = spm_permute_kron(b,dim([1,3,2,4]),[1,3,2,4]);
% stop isolating if asymptomatic and negative : third order dependencies
%--------------------------------------------------------------------------
ij = Bij({5,1:5,1,4},{1,1:5,1,4},dim); B{1}(ij) = 1;
ij = Bij({5,1:5,1,4},{5,1:5,1,4},dim); B{1}(ij) = 0;
% isolate if positive : third order dependencies
%--------------------------------------------------------------------------
ij = Bij({1,1:5,1,3},{5,1:5,1,3},dim); B{1}(ij) = 1;
ij = Bij({1,1:5,1,3},{1,1:5,1,3},dim); B{1}(ij) = 0;
ij = Bij({1,1:5,1,3},{2,1:5,1,3},dim); B{1}(ij) = 0;
% isolate if infected : third order dependencies : efficacy of FTTI
%--------------------------------------------------------------------------
ij = Bij({1,2,1,1},{5,2,1,1},dim); B{1}(ij) = P.ttt;
ij = Bij({1,2,1,1},{1,2,1,1},dim); B{1}(ij) = (1 - Pout)*(1 - P.ttt);
ij = Bij({1,2,1,1},{2,2,1,1},dim); B{1}(ij) = Pout*(1 - P.ttt);
% probabilistic transitions: infection
%==========================================================================
% P.Rin % effective number of contacts: home
% P.Rou % effective number of contacts: work
% P.trn % P(transmission | contact)
% P.Tin % infected (pre-contagious) period
% P.Tcn % infectious (contagious) period
% transmission probabilities
%--------------------------------------------------------------------------
b = cell(1,dim(2));
q = spm_sum(x,[3 4]);
ph = q(1,:)/sum(q(1,:)); % infection probability at home
pw = q(2,:)/sum(q(2,:)); % infection probability at work
Pinh = (1 - P.trn*ph(3))^P.Rin; % P(no transmission) | home
Pinw = (1 - P.trn*pw(3))^P.Rou; % P(no transmission) | work
Kimm = exp(-1/P.Tim/32); % loss of immunity (per 32 days)
Pres = P.res; % proportion of innate immunity
Kinf = exp(-1/P.Tin);
Kcon = exp(-1/P.Tcn);
% marginal: infection {2} | home {1}(1)
%--------------------------------------------------------------------------
% susceptible infected infectious Ab +ve Ab -ve
%--------------------------------------------------------------------------
b{1} = [Pinh 0 0 (1 - Kimm) 0;
(1 - Pinh) Kinf 0 0 0;
0 (1 - Pres)*(1 - Kinf) Kcon 0 0;
0 0 (1 - Kcon) Kimm 0;
0 Pres*(1 - Kinf) 0 0 1];
% marginal: infection {2} | work {1}(2)
%--------------------------------------------------------------------------
b{2} = [Pinw 0 0 (1 - Kimm) 0;
(1 - Pinw) Kinf 0 0 0;
0 (1 - Pres)*(1 - Kinf) Kcon 0 0;
0 0 (1 - Kcon) Kimm 0;
0 Pres*(1 - Kinf) 0 0 1];
% marginal: infection {2} | hospital {1}(3)
%--------------------------------------------------------------------------
b{3} = [1 0 0 (1 - Kimm) 0;
0 Kinf 0 0 0;
0 (1 - Pres)*(1 - Kinf) Kcon 0 0;
0 0 (1 - Kcon) Kimm 0;
0 Pres*(1 - Kinf) 0 0 1];
% marginal: infection {2} | removed {1}(4)
%--------------------------------------------------------------------------
b{4} = b{3};
% marginal: infection {2} | isolated {1}(5)
%--------------------------------------------------------------------------
b{5} = b{3};
% kroneckor form
%--------------------------------------------------------------------------
b = spm_cat(spm_diag(b));
b = spm_kron({b,I{3},I{4}});
B{2} = spm_permute_kron(b,dim([2,1,3,4]),[2,1,3,4]);
% probabilistic transitions: clinical
%==========================================================================
% https://en.wikipedia.org/wiki/List_of_countries_by_life_expectancy
%--------------------------------------------------------------------------
% P.dev % P(developing symptoms | infected)
% P.sev % P(severe symptoms | symptomatic)
% P.Tsy % symptomatic period
% P.Trd % acute RDS period
% P.fat % P(fatality | CCU)
% P.sur % P(survival | home)
% probabilities of developing symptoms
%--------------------------------------------------------------------------
b = cell(1,dim(2));
Psev = P.sev; % P(developing symptoms | infected)
Ksym = exp(-1/P.Tsy); % acute symptomatic rate
Ksev = exp(-1/P.Trd); % acute RDS rate
Kdev = exp(-1/P.Tic); % symptomatic rate
Pfat = 1 - P.sur; % baseline fatality rate
% marginal: clinical {3} | susceptible {2}(1)
%--------------------------------------------------------------------------
% asymptomatic symptomatic ARDS deceased
%--------------------------------------------------------------------------
b{1} = [1 (1 - Ksym) (1 - Ksev)*(1 - Pfat) (1 - Kday);
0 Ksym 0 0;
0 0 Ksev 0;
0 0 (1 - Ksev)*Pfat Kday];
% marginal: clinical {3} | infected {2}(2)
%--------------------------------------------------------------------------
b{2} = [Kdev (1 - Ksym)*(1 - Psev) (1 - Ksev)*(1 - Pfat) (1 - Kday);
(1 - Kdev) Ksym 0 0;
0 (1 - Ksym)*Psev Ksev 0;
0 0 (1 - Ksev)*Pfat Kday];
% marginal: clinical {3} | infectious {2}(3)
%--------------------------------------------------------------------------
b{3} = b{2};
% marginal: clinical {3} | Ab +ve {2}(4)
%--------------------------------------------------------------------------
b{4} = b{1};
% marginal: clinical {3} | Ab -ve {2}(5)
%--------------------------------------------------------------------------
b{5} = b{1};
% kroneckor form
%--------------------------------------------------------------------------
b = spm_cat(spm_diag(b));
b = spm_kron({b,I{1}});
b = spm_permute_kron(b,dim([3,2,1]),[3,2,1]);
% kroneckor form
%--------------------------------------------------------------------------
B{3} = spm_kron({b,I{4}});
% location dependent fatalities (P.fat in CCU): third order dependencies
%--------------------------------------------------------------------------
ij = Bij({3,1:5,3,1:4},{3,1:5,4,1:4},dim); B{3}(ij) = (1 - Ksev)*P.fat;
ij = Bij({3,1:5,3,1:4},{3,1:5,1,1:4},dim); B{3}(ij) = (1 - Ksev)*(1 - P.fat);
% probabilistic transitions: testing
%==========================================================================
% P.bas % probability of being tested
% P.del % delay: testing capacity
% P.tes % relative probability if infected
% test probabilities
%--------------------------------------------------------------------------
b = cell(1,dim(2));
q = spm_sum(x,[2 3 4]);
Pbas = P.bas*(1 - q(4));
Seni = .9; % PCR sensitivity (infected)
Senc = .95; % PCR sensitivity (contagious)
Psen = Pbas/(1 - Prev + P.tes*Prev); % probability of being tested
Ptes = Psen*P.tes; % probability if infected
Kdel = exp(-1/P.del); % exp(-1/waiting period)
% marginal: testing {4} | susceptible {2}(1)
%--------------------------------------------------------------------------
% not tested waiting PCR +ve PCR -ve
%--------------------------------------------------------------------------
b{1} = [(1 - Psen) 0 (1 - Kday) (1 - Kday);
Psen Kdel 0 0;
0 0 Kday 0;
0 (1 - Kdel) 0 Kday];
% marginal: testing {4} | infected {2}(2)
%--------------------------------------------------------------------------
b{2} = [(1 - Ptes) 0 (1 - Kday) (1 - Kday);
Ptes Kdel 0 0;
0 Seni*(1 - Kdel) Kday 0;
0 (1 - Seni)*(1 - Kdel) 0 Kday];
% marginal: testing {4} | infectious {2}(3)
%--------------------------------------------------------------------------
b{3} = [(1 - Ptes) 0 (1 - Kday) (1 - Kday);
Ptes Kdel 0 0;
0 Senc*(1 - Kdel) Kday 0;
0 (1 - Senc)*(1 - Kdel) 0 Kday];
% marginal: testing {4} | Ab +ve {2}(4)
%--------------------------------------------------------------------------
b{4} = b{1};
% marginal: testing {4} | Ab -ve {2}(5)
%--------------------------------------------------------------------------
b{5} = b{1};
% kroneckor form
%--------------------------------------------------------------------------
b = spm_cat(spm_diag(b));
b = spm_kron({b,I{1},I{3}});
B{4} = spm_permute_kron(b,dim([4,2,1,3]),[3,2,4,1]);
% location dependent testing (exempt): third order dependencies
%--------------------------------------------------------------------------
ij = Bij({4,1:5,1:4,1},{4,1:5,1:4,1},dim); B{4}(ij) = 1;
ij = Bij({4,1:5,1:4,1},{4,1:5,1:4,2},dim); B{4}(ij) = 0;
% probability transition matrix
%==========================================================================
T = 1;
for i = 1:numel(B)
T = T*B{i};
end
return
% Auxiliary functions
%__________________________________________________________________________
function ij = Bij(j,i,dim)
% returns linear indices of transition matrix
% j - dimensions of source states
% i - dimensions on target states
%--------------------------------------------------------------------------
nx = prod(dim);
z = zeros(dim); z(j{1},j{2},j{3},j{4}) = 1; j = find(z);
z = zeros(dim); z(i{1},i{2},i{3},i{4}) = 1; i = find(z);
ij = find(sparse(i,j,1,nx,nx));
function p = spm_sigma(x,u,s)
% reverse sigmoid function
% FORMAT p = spm_sigma(p,u,s)
% x - probability
% u - threshold
% s - sensitivity (default four)
%
% p - probability (0 < p < 1)
%
% This function is reverse sigmoid function that scales the input argument
% by the bias and flips the (biased) input. This provides a monotonically
% decreasing sigmoid function of the input that hits 50% at the threshold
% (u). The scaling ensures the probability at x = 0 is about one, for a
% suitably large sensitivity parameter s.
%--------------------------------------------------------------------------
% default sensitivity
%--------------------------------------------------------------------------
if nargin < 3, s = 4; end
% sigmoid function
%--------------------------------------------------------------------------
p = spm_phi(s*(u - x)/u);
return