-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathleakylockdownPod2000_population100000.js
More file actions
480 lines (407 loc) · 15.7 KB
/
Copy pathleakylockdownPod2000_population100000.js
File metadata and controls
480 lines (407 loc) · 15.7 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// *** This is to implement an agent-based modeling of the "leaky Lockdown" model of R0 heterogeneity.
// * See https://docs.google.com/spreadsheets/d/1RRq9oYJDfI7dCq5NTdQbp4TGUHRlzBLi2EGZ6JlBijE/edit#gid=807073296
// The key addition is a "detection pod" analogy.
// There should also be three sectors: unexposed sector, hospital sector, and recovered sector (herd immunity)
console.log("checkLodash")
console.log(_);
var ContactTraceBack;
var ContactTraceForward;
var PodQuarantineProbability;
const PopulationInitial = {
"main" : 1e5,
"infected": 5, // randomly distributed amongst all people.
"hospital" : 0, // The Bureau of Labor Statistics estimates that there were 5.15M people employed in the Hospitals Industry Group in 2018. Scale to US population
"recovered" : 0, // these have herd immunity
"dead" : 0,
}
const PodSize=2000; // the initial unexposed group is partitioned into mutually exclusive sets of this size.
var numberPods = PopulationInitial.main/PodSize;
// const ExposureDistribution = [
// { "percent": 10, "exposuresPerWeek": 10, "name": "lowContact"},
// { "percent": 75, "exposuresPerWeek": 50, "name": "mediumContact"},
// { "percent": 14, "exposuresPerWeek": 100, "name": "highContact"},
// { "percent": 1, "exposuresPerWeek": 1000, "name": "superhighContact"}
// ];
// var populationAverageExposure= ExposureDistribution.reduce((acc,current)=>acc+(current.percent*current.exposuresPerWeek),0)/100;
// console.log(populationAverageExposure);
const asymptomaticProportion= 0.25;
// const TransmissionPerExposureNoCompliance=0.05;
const TransmissionPerExposureNoCompliance=0.05;
// const DistancingComplianceLevels= {
// none: 0,
// lax: 0.5,
// strict: 0.9,
// veryStrict: 0.99,
// perfect: 1.00
// }
// const noHandwashLevels= {
// none: 1,
// lax: 0.5,
// strict: 0.1,
// veryStrict: 0.01,
// perfect: 0
// }
// const DistancingComplianceDistributionConditionalOnExposure = {
// lowContact : [ {none: 0}, {lax: 0.5}, {strict: 0.5}, {veryStrict: 0}, {perfect: 0} ],
// mediumContact : [ {none: 0}, {lax: 0.5}, {strict: 0.5}, {veryStrict: 0}, {perfect: 0} ],
// highContact: [ {none: 0}, {lax: 0.5}, {strict: 0.5}, {veryStrict: 0}, {perfect: 0} ],
// superhighContact : [ {none: 0}, {lax: 0.5}, {strict: 0.5}, {veryStrict: 0}, {perfect: 0} ],
// }
// const PodIntegrityDistribution= [
// { "percent": 0, "inPodProportion": 0.0, "name": "none"},
// { "percent": 10, "inPodProportion": 0.5, "name": "externalInterfacer"},
// { "percent": 75, "inPodProportion": 0.8, "name": "podLoose"},
// { "percent": 14, "inPodProportion": 0.9, "name": "podTight"},
// { "percent": 1, "inPodProportion": 1.0, "name": "podOnly"}
// ]
// const QuarantineComplianceLevels= {
// none: 0,
// lax: 0.5,
// strict: 0.9,
// veryStrict: 0.99,
// perfect: 1.00
// }
const AtRiskPercentage = 0.1; // 10%
const AtRiskDistancingComplianceOverride = "strict"; // none means don't override.
const AtRiskPodIntegrityOverride = "none"
// "none" means AtRisk have podIntegrity from the same distribution. Don't override. If a name is given, then they have that
// inPodProportion.
// const FatalityRates= {
// atRisk: 0.10,
// normal: 0.01
// }
// *** PSEUDOCODE
// Step 0: Initialization: All pods are created. Within each pod, each person has drawn for them:
// atRisk?
// ExposuresPerWeek
// DistancingCompliance | Exposure
// PodIntegrity (temporary)
// AtRisk
// override: AtRiskDistancingComplianceOverride
// override: AtRiskPodIntegrityOverride
// infected: randomly distribute.
//
// infected should include the week of infection.
// model/PodArray
const PodArray= Array.from(
{length: numberPods},
() => Array.from(
{length: PodSize},
() => "temporary" //
)
);
//initialize
var NewInfectionsArray=[];
var Week1InfectionsArray=[];
var Week2InfectionsArray=[];
var RecoveredArray=[];
var DeadArray=[];
var QuarantinesArray=[];
var NewQArray=[];
var Week1QArray=[];
var Week2QArray=[];
var currentWeek=0;
//initialize
function InitializeEverything0(fixedCharacteristics) {
currentWeek=0;
NewInfectionsArray=[];
Week1InfectionsArray=[];
Week2InfectionsArray=[];
RecoveredArray=[];
DeadArray=[];
QuarantinesArray=[];
NewQArray=[];
Week1QArray=[];
Week2QArray=[];
var p;
for (p = 0; p < numberPods; p++) {
var i;
for (i = 0; i < PodSize; i++) {
PodArray[p][i] = {
pod: p,
indexInPod: i,
infection: {
status: "clean", // clean, infected, dead, recovered
infectionWeek: null,
symptomatic: null,
quarantine: null,
}
}
if (fixedCharacteristics) {
PodArray[p][i].fixedCharacteristics= fixedCharacteristics
} else {
PodArray[p][i].fixedCharacteristics= {
atRisk: false,
exposuresPerWeek: 80,
distancingCompliance: 0.5,
quarantineCompliance: 0.9,
podIntegrity: 0.8,
noHandwash: 1.0,
};
}
if (PodArray[p][i].fixedCharacteristics.atRisk) {
if (AtRiskDistancingComplianceOverride !== "none") {
PodArray[p][i].fixedCharacteristics.distancingCompliance=DistancingComplianceLevels[AtRiskDistancingComplianceOverride];
}
if (AtRiskPodIntegrityOverride !== "none") {
PodArray[p][i].fixedCharacteristics.podIntegrity=9999; // have to write function to get the inPodProportion from the name.
// const PodIntegrityDistribution= [
// { "percent": 0, "inPodProportion": 0.0, "name": "none"},
// { "percent": 10, "inPodProportion": 0.5, "name": "externalInterfacer"},
// { "percent": 75, "inPodProportion": 0.8, "name": "podLoose"},
// { "percent": 14, "inPodProportion": 0.9, "name": "podTight"},
// { "percent": 1, "inPodProportion": 1.0, "name": "podOnly"}
// ]
}
}
}
}
/// distributeInitialInfections
var j=0;
while (j<PopulationInitial.infected) {
let person=getRandomPerson({Spec: "none"});
if (person.infection.status!= "infected" && person.infection.status!= "dead") {
person.infection.status= "infected";
person.infection.infectionWeek= 0;
person.infection.symptomatic=false;
person.infection.from= {pod: -1 , indexInPod: -1 };
person.infection.quarantine= false;
NewInfectionsArray.push(person)
j++
}
}
}
// function makeEmptyPerson() {
// /// makes a person according to the parameters above.
// var person = {};
// person.pod = //index of the pod they are in.
// person.indexInPod = // their index in the pod array
// person.fixedCharacteristics = {
// atRisk?
// ExposuresPerWeek
// DistancingCompliance | Exposure
// PodIntegrity (temporary)
// AtRisk
// override: AtRiskDistancingComplianceOverride
// override: AtRiskPodIntegrityOverride
// }
// person.infection = {
// currentStatus = // clean, infected, dead, recovered
// infectionWeek = // week of infection 0 means they were infected in the initialization phase.
// }
// }
//helper
function RandomInt(myNumber) {
// returns int between 0 and myNumber-1
return Math.floor(Math.random()*myNumber)
}
//helper
function getRandomPerson(parameters) {
if (parameters.Spec == "none") {
// find a random pod
// find a random person
let person= PodArray[RandomInt(numberPods)][RandomInt(PodSize)];
return person;
}
if (parameters.Spec == "inPod") {
let person= PodArray[parameters.requester.pod][RandomInt(PodSize)];
// does not exclude drawing oneself
return person
}
if (parameters.Spec == "outOfPod") {
// Get the requesters pod
let notMyPod= RandomInt(myNumber)
parameters.requester.pod;
// find a random person not in this pod
// find random person
// verify not in the same pod
// maybe check if they are alive.
let person= zzz;
return person
}
}
//
//
// Step 1: Iterate through all the exposures of the infected people. Exposures here are 1 way to avoid double counting. 1 ways means they possibly sneeze on them.
// to generate an exposure:
// draw if inPod or out-of pod.
// then look at the distancing compliance and exposure of everyone in that group. Draw a person
// from that distribution. Operationally, pick one random person. And then see if that person
// should be a match by their relative exposure. Pick a random number between 1 and 1000. If it's
// below exposuresPerWeek*(1-QuarnatineComplianceLevels), then it's an exposure.
// if exposuresPerWeek is low (like 10), then that exposure is unlikely.
// if quarantine level is high, then they won't be exposed.
// maximum of 500 tries for exposure
// Whether they are infected or not is determined by a random variable between [0,1] on the distancingCOmpliance
// If compliance is perfect, they don't get infected. If it's none, they get infected. In between, roll the random dice.
// Store this in the variable NewInfectionsArray
// Step 1: Iterate through all the exposures of the infected people. Exposures here are 1 way to avoid double counting. 1 ways means they possibly sneeze on them.
// to generate an exposure:
//timeStep
function exposuresInfections(infectorPerson) {
// console.log("exposing for " + infectorPerson.pod + "---" + infectorPerson.indexInPod );
if (infectorPerson.infection.status === "infected" && infectorPerson.infection.quarantine!==true ) {
let n;
let exposuresWithDistancing= Math.floor(infectorPerson.fixedCharacteristics.exposuresPerWeek*(1-infectorPerson.fixedCharacteristics.distancingCompliance));
for (n=0; n<exposuresWithDistancing; n++) {
// console.log("n is " + n + " for " + infectorPerson.pod + "---" + infectorPerson.indexInPod);
// draw if inPod or out-of pod.
let inPodBoolean=Math.random()<infectorPerson.fixedCharacteristics.podIntegrity;
if (inPodBoolean) {
let exposurePerson = getRandomPerson({Spec: "inPod", requester: infectorPerson});
maybeInfect(exposurePerson, infectorPerson);
} else {
let exposurePerson = getRandomPerson({Spec: "none", requester: infectorPerson});
maybeInfect(exposurePerson, infectorPerson);
}
}
}
}
//timeStep
function maybeInfect(person, infector) {
// console.log("maybe infect" + + person.pod + "---" + person.indexInPod);
if (person.infection.status== "clean" && person.infection.quarantine != true ) {
if (Math.random() < TransmissionPerExposureNoCompliance*person.fixedCharacteristics.noHandwash) {
person.infection.status= "infected";
person.infection.infectionWeek= currentWeek;
person.infection.symptomatic=Math.random()>asymptomaticProportion ;
person.infection.from= {pod: infector.pod , indexInPod: infector.indexInPod };
person.infection.quarantine= false; // maybe overwritten in quarantineFlags
NewInfectionsArray.push(person)
}
}
}
// then look at the distancing compliance and exposure of everyone in that group. Draw a person
// from that distribution. Operationally, pick one random person. And then see if that person
// should be a match by their relative exposure. Pick a random number between 1 and 1000. If it's
// below exposuresPerWeek*(1-QuarnatineComplianceLevels), then it's an exposure.
// if exposuresPerWeek is low (like 10), then that exposure is unlikely.
// if quarantine level is high, then they won't be exposed.
// maximum of 500 tries for exposure
// Whether they are infected or not is determined by a random variable between [0,1] on the distancingCOmpliance
// If compliance is perfect, they don't get infected. If it's none, they get infected. In between, roll the random dice.
// Store this in the variable NewInfectionsArray
// Step 2: Now we have all the infected people this week.
//timeStep
function quarantineFlags(options) {
// search who everyone who is NewlyInfected
if (options.proportion==0) {
"do nothing";
} else {
NewInfectionsArray.forEach((x) => {
if ( options.symptomatic ===true && x.infection.symptomatic===true) {
let initiateQuarantine = (Math.random() < options.proportion);
if (initiateQuarantine) {
x.infection.quarantine = true
QuarantinesArray.push(x);
NewQArray.push(x);
}
if ((options.podQuarantineProbability > Math.random()) && initiateQuarantine) {
PodArray[x.pod].forEach((person) =>{
person.infection.quarantine = true;
QuarantinesArray.push(person);
NewQArray.push(person)
})
}
}
if (options.contactTraceBack===true) {
let infectorPod= x.infection.from.pod ;
let infectorIndexInPod = x.infection.from.indexInPod ;
personTraced = PodArray[infectorPod][infectorIndexInPod]
personTraced.infection.quarantine = true;
QuarantinesArray.push(personTraced);
NewQArray.push(personTraced)
}
});
// contactTraceForward is ignored because if you are quarantined, then you don't infect anyone forward.
}
};
//timeStep
function TimeStep() {
currentWeek=currentWeek+1;
Week2InfectionsArray.forEach((person) => {
person.infection.status = "recovered"; // or dead, do dead later.
})
RecoveredArray=RecoveredArray.concat(Week2InfectionsArray);
Week2InfectionsArray=Week1InfectionsArray;
Week1InfectionsArray=NewInfectionsArray;
NewInfectionsArray=[];
Week1InfectionsArray.forEach(exposuresInfections);
Week2InfectionsArray.forEach(exposuresInfections);
Week2QArray.forEach((person) => {
person.infection.quarantine = null; // or dead, do dead later.
})
Week2QArray=Week1QArray;
Week1QArray=NewQArray;
NewQArray=[];
quarantineFlags({
proportion: GLOBALquarantineCompliance,
symptomatic: true,
contactTraceBack: ContactTraceBack,
contactTraceForward: ContactTraceForward,
podQuarantineProbability: PodQuarantineProbability,
});
}
// Iterate the weekly time step.
// NEW INFECTIONS
// mark them as infected. Include their index in a list of Week1 infected people to iterate through.
// WEEK1 INFECTIONS
// move the week1 infected to week2 infected.
// WEEK2 INFECTIONS
// check if they are at risk or normal, and apply the death rate at the end of the second week.
// at the end of second week, they are recovered or dead.
//
// Repeat Step 1 and Step 2 for the number of weeks we are interested in.
//timeStep
function s() {
console.log('SUMMARY WEEK: '+ currentWeek);
console.log('new :' + NewInfectionsArray.length);
console.log('week1: ' + Week1InfectionsArray.length);
console.log('week2: ' + Week2InfectionsArray.length);
console.log('ThisWeek R0: ' + NewInfectionsArray.length/(Week1InfectionsArray.length + Week2InfectionsArray.length))
console.log('Quarantined (wk1+wk2): ' + Number(Week2QArray.length + Week1QArray.length));
console.log('QuarantineRatio: '+ (Week2QArray.length + Week1QArray.length) /(Week1InfectionsArray.length + Week2InfectionsArray.length))
console.log('recoverd:' + RecoveredArray.length);
console.log('===========');
}
// eventually move tomethod for model/Summary; currently in timeStep
function t(Storage) {
TimeStep();
s();
if (Storage !== undefined) {
Storage.push( {
week: currentWeek,
newInfections: NewInfectionsArray.length,
week1Infections: Week1InfectionsArray.length,
week2Infections: Week2InfectionsArray.length,
r0: Math.round(NewInfectionsArray.length/(Week1InfectionsArray.length + Week2InfectionsArray.length) *100)/100 ,
Recovered: RecoveredArray.length,
Quarantined: QuarantinesArray.length,
PodArray: _.cloneDeep(PodArray), // TODO:
})
}
}
// SAMPLE CODE
// * Copy the above code into node.js, and then run the code below.
// NOTE: This simulation takes about 60 seconds to run.
// console.log(PopulationInitial);
// GLOBALquarantineCompliance=0.0;
// PodQuarantine=false;
// ContactTraceBack=false;
// ContactTraceForward=false;
// var Storage1= [];
// InitializeEverything0({
// atRisk: false,
// exposuresPerWeek: 80,
// distancingCompliance: 0.5,
// quarantineCompliance: 0.0,
// podIntegrity: 0.0,
// noHandwash: 1.0,
// });
// // Run 30 rounds
// for (var i=0; i<30; i++) {
// t(Storage1);
// }
// var Run1PodArray=PodArray;
// // sample person
// PodArray[0][0];