-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc19sim.cpp
More file actions
224 lines (177 loc) · 5.43 KB
/
c19sim.cpp
File metadata and controls
224 lines (177 loc) · 5.43 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
/* c19sim.cpp
* Copyright (C) 2017-2019 Jake S. Cannell
*/
/* GNU Affero General Public License, Version 3 {{{ */
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <random>
#include <stdarg.h>
#include <time.h>
#include <chrono>
#include <random>
using namespace std;
class CSVRow
{
public:
std::string const& operator[](std::size_t index) const
{
return m_data[index];
}
std::size_t size() const
{
return m_data.size();
}
void readNextRow(std::istream& str)
{
std::string line;
std::getline(str, line);
std::stringstream lineStream(line);
std::string cell;
m_data.clear();
while(std::getline(lineStream, cell, ','))
{
m_data.push_back(cell);
}
// This checks for a trailing comma with no data after it.
if (!lineStream && cell.empty())
{
// If there was a trailing comma then add an empty element.
m_data.push_back("");
}
}
private:
std::vector<std::string> m_data;
};
std::istream& operator>>(std::istream& str, CSVRow& data)
{
data.readNextRow(str);
return str;
}
template <class GT, class DeathTimeDistT>
int sim_run(GT& G, const vector<int>& cases, double IFR, DeathTimeDistT dtd, int today, int obs_deaths)
{
int sim_deaths(0);
uniform_real_distribution<> uni_dist(0.0, 1.0);
for (int i(0); i < cases.size(); i++)
{
double total_infected = cases[i];
//printf("total_infected[%i]: %f \n", i, total_infected);
for (int j(0); j < total_infected; j++)
{
//int infection_time = i - round( ctd(G) );
if (uni_dist(G) < IFR) { // death check
int death_time = i + round( dtd(G) );
//printf("%i ", death_time);
if (death_time <= today) {
++sim_deaths;
//if (++sim_deaths > obs_deaths) return false;
}
}
} // j
//printf("\n");
} // i
return sim_deaths;
} // sim_run
template <class GT, class DeathTimeDistT>
double run_sims(GT& G, const vector<int>& cases, double IFR, DeathTimeDistT dtd, int today, int obs_deaths, int N)
{
int nmatches(0);
for (int i(0); i < N; i++) {
int sim_deaths = sim_run(G,cases,IFR,dtd,today,obs_deaths);
if (sim_deaths == obs_deaths) nmatches++;
}
return double(nmatches) / double(N);
}
void simulate(double IFR, double C_N, int obs_deaths, int shape)
{
std::ifstream file("iceland_cases.csv");
vector<int> cases;
int cases_NUHI(0);
int cases_DGEN(0);
int i_(0);
CSVRow row;
while(file >> row)
{
//printf("%s %s %s \n", row[0].c_str(), row[1].c_str(), row[2].c_str());
if (i_ > 0) {
int n0(0), n1(0);
if (row[1].size() > 2) n0 = stoi( row[1].substr(1,row[1].size()-1) );
if (row[2].size() > 2) n1 = stoi( row[2].substr(1,row[2].size()-1) );
//printf("%i %i \n", n0, n1);
cases.push_back(n0 + n1);
cases_NUHI += n0;
cases_DGEN += n1;
}
i_++;
}
int cases_tot = cases_NUHI + cases_DGEN;
printf("today: %i total cases %i = %i + %i \n", int(cases.size()), cases_tot, cases_NUHI, cases_DGEN);
double extra_infected = cases_tot * (C_N - 1.0);
vector<double> extra_cases;
double ytot = 0.0;
for (int i(0); i < int(cases.size()); i++)
{
double x = double(i);
double sx = 0.3*x -1.0;
double y = exp(sx) / (exp(sx) + 1.0);
//printf(" %f", y);
extra_cases.push_back(y);
ytot += y;
}
//printf("\n");
for (int i(0); i < int(extra_cases.size()); i++)
{
extra_cases[i] = round( extra_cases[i] / ytot * extra_infected );
}
for (int i(0); i < cases.size(); i++)
{
if (shape == 0) cases[i] += extra_infected / double(cases.size());
if (shape == 1) cases[i] += int( extra_cases[i] );
if (shape == 2) cases[i] = int(double(cases[i]) * C_N);
}
random_device rd; //Will be used to obtain a seed for the random number engine
mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
double pval = run_sims(gen, cases, IFR, lognormal_distribution<>(2.8,0.45), cases.size(), obs_deaths, 10000);
printf("%f \n", pval);
}
// ============================ ===================================
int main(int argc, char* argv[])
{
printf("%s\n", argv[1]);
int ND = stoi(argv[1]);
double IFR = stof(argv[2]);
double C_N = stof(argv[3]);
int shape = stoi(argv[4]);
printf("c19sim IFR=%f C_N=%f ND=%i shape=%i \n", IFR, C_N, ND, shape);
simulate(IFR, C_N, ND, shape);
printf("\n");
printf("Done!\n");
}
/*
Age C N C/N
0- 9 21 50K 0.4/K
10-19 76 45K 1.6/K
20-29 158 50K 3.1/K
30-39 171 48K 3.5/K
40-49 226 45K 5.0/K
50-59 190 46K 4.1/K
60-69 131 35K 3.7/K
70-79 37 22K 1.6/K
80-89 4 13K 0.3/K
90+ 6 3K 2.0/K
*/