Public
Edited
Jun 6, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
plotKeys(["S","I","R","newI",d=>d.vax.S, d=>d.S - d.vax.S], false, dataWithVax,["S","I","R","newI","S vaxed", "S unvaxed"]);
Insert cell
plotKeys([d=>d.newI/d.N,d=>d.vax.newI/d.vax.N, d=>(d.newI - d.vax.newI)/(d.N - d.vax.N)], false, dataWithVax,["Infection rate total", "Infection rate vaxed", "Infection rate unvaxed"]);
Insert cell
Insert cell
plotKeys([d=>d.vax.newI/d.vax.N/((d.newI - d.vax.newI)/(d.N - d.vax.N))], false, dataWithVax,[" "]);
Insert cell
Insert cell
Insert cell
Insert cell
function genSIR(initial = {}, n = 150, intervention = {}) {
let N = 1000;
const defaultInitial = {
Day: 0,
S:N-1,
I:1,
Infected:1,
R:0,
N:N,
cumulativeI: 1,
Infected: 0,
gamma: .25,
beta: beta,
incubation:1,
};
const defaultIntervention = {
firstDay: 30,
duration: 7,
gamma: 0.45,
beta: beta,
importedCases: 0,
importedCasesDay: 0,
vaccinated: 0,
vaccineEffectivenessAgainstInfection: 0,
};
initial = {...defaultInitial,...initial};
intervention = {...defaultInitial,...defaultIntervention, ...intervention};
if (intervention.vaccinated) {
initial.vax = {
S: intervention.vaccinated,
I: 0,
R: 0,
N: intervention.vaccinated,
cumulativeI: 0,
Infected: 0,
gamma: .25,
beta: beta,
incubation:1,
};
}
else {
initial.vax = {};
}
const a = [initial];
for (let i = 1 ; i <= n ; i++) {
let p = a[i-1];
let interventionActive = i >= intervention.firstDay && i < intervention.firstDay + intervention.duration;
let active = interventionActive ? intervention : initial ;

let incubation = active.incubation;

let pi = a[Math.max(i-incubation,0)];
console.log(incubation,pi);

let beta = active.beta ;
let gamma = active.gamma ;
let Reff = active.beta * p.S / p.N / active.gamma ;
let newI = active.beta * pi.I * p.S / p.N;
let vax = {
...p.vax,
gamma,
beta,
};
if (intervention.vaccinated) {
let ve = i < intervention.vaccinationDay ? 0 : intervention.vaccineEffectivenessAgainstInfection ;
newI -= active.beta * ve * pi.I * vax.S / p.N;
let newIvax = active.beta * (1-ve) * pi.I * vax.S / p.N;
let newRvax = active.gamma * vax.I;
vax.I += newIvax - newRvax;
vax.R += newRvax ;
vax.S -= newIvax;
vax.Infected = vax.I + vax.R;
vax.newI = newIvax;
vax.cumulativeI += newIvax;
}

if (intervention.importedCasesDay === i) {
newI += importedCasesInfectiousness*Reff*intervention.importedCases ;
}
let newR = active.gamma * p.I;
let I = p.I + newI - newR ;
let R = p.R + newR;
a[i] = {
...p,
vax,
gamma,
beta,
Day: i,
S: p.S - newI,
I,
R,
Reff,
newI,
Infected: I + R,
cumulativeI: p.cumulativeI + newI,
};
}
return a;
}
Insert cell
data = genSIR({
gamma: initialGamma,
incubation: incubationPeriod,
},200,{
firstDay: interventionStart,
duration: interventionDays,
gamma: interventionGamma,
});
Insert cell
dataImportedCases = genSIR({
gamma: initialGamma,
incubation: incubationPeriod,
},200,{
firstDay: interventionStart,
duration: interventionDays,
gamma: interventionGamma,
importedCases,
importedCasesDay,
importedCasesInfectiousness,
});
Insert cell
dataWithVax = genSIR({
gamma: initialGamma,
incubation: incubationPeriod,
},200,{
firstDay: 0,
duration: 0,
gamma: initialGamma,
vaccinated,
vaccineEffectivenessAgainstInfection,
vaccinationDay,
});
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more