analyze_result.h Source File

CPP API: analyze_result.h Source File
models/abm/analyze_result.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele, Khoa Nguyen
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef ABM_ANALYZE_RESULT_H
21 #define ABM_ANALYZE_RESULT_H
22 
23 #include "abm/parameters.h"
26 
27 #include <vector>
28 
29 namespace mio
30 {
31 namespace abm
32 {
39 template <class Model>
40 std::vector<Model> ensemble_params_percentile(const std::vector<std::vector<Model>>& ensemble_params, ScalarType p)
41 {
42  assert(p > 0.0 && p < 1.0 && "Invalid percentile value.");
43 
44  auto num_runs = ensemble_params.size();
45  auto num_nodes = ensemble_params[0].size();
46  std::vector<ScalarType> single_element_ensemble(num_runs);
47  auto num_groups = (size_t)ensemble_params[0][0].parameters.get_num_groups();
48 
49  // Lambda function that calculates the percentile of a single parameter
50  std::vector<Model> percentile(num_nodes, Model((int)num_groups));
51  auto param_percentile = [&ensemble_params, p, num_runs, &percentile](auto n, auto get_param) mutable {
52  std::vector<ScalarType> single_element(num_runs);
53  for (size_t run = 0; run < num_runs; run++) {
54  auto const& params = ensemble_params[run][n];
55  single_element[run] = get_param(params);
56  }
57  std::sort(single_element.begin(), single_element.end());
58  auto& new_params = get_param(percentile[n]);
59  new_params = single_element[static_cast<size_t>(num_runs * p)];
60  };
61 
62  auto param_percentile_dist = [&ensemble_params, p, num_runs, &percentile](auto n, auto single_element,
63  auto get_param, auto sort_fct) mutable {
64  for (size_t run = 0; run < num_runs; run++) {
65  auto const& params = ensemble_params[run][n];
66  single_element[run] = get_param(params);
67  }
68  std::sort(single_element.begin(), single_element.end(), sort_fct);
69  auto& new_params = get_param(percentile[n]);
70  new_params = single_element[static_cast<size_t>(num_runs * p)];
71  };
72 
73  for (size_t node = 0; node < num_nodes; node++) {
74  for (auto age_group = AgeGroup(0); age_group < AgeGroup(num_groups); age_group++) {
75  for (auto virus_variant : enum_members<VirusVariant>()) {
76  // Global infection parameters
77 
78  //Stay time distributions
79  param_percentile_dist(
80  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
81  [age_group, virus_variant](auto&& model) -> auto& {
82  return model.parameters.template get<TimeExposedToNoSymptoms>()[{virus_variant, age_group}];
83  },
84  [](auto& dist1, auto& dist2) {
85  return dist1 < dist2;
86  });
87  param_percentile_dist(
88  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
89  [age_group, virus_variant](auto&& model) -> auto& {
90  return model.parameters
91  .template get<TimeInfectedNoSymptomsToSymptoms>()[{virus_variant, age_group}];
92  },
93  [](auto& dist1, auto& dist2) {
94  return dist1 < dist2;
95  });
96  param_percentile_dist(
97  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
98  [age_group, virus_variant](auto&& model) -> auto& {
99  return model.parameters
100  .template get<TimeInfectedNoSymptomsToRecovered>()[{virus_variant, age_group}];
101  },
102  [](auto& dist1, auto& dist2) {
103  return dist1 < dist2;
104  });
105  param_percentile_dist(
106  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
107  [age_group, virus_variant](auto&& model) -> auto& {
108  return model.parameters
109  .template get<TimeInfectedSymptomsToSevere>()[{virus_variant, age_group}];
110  },
111  [](auto& dist1, auto& dist2) {
112  return dist1 < dist2;
113  });
114  param_percentile_dist(
115  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
116  [age_group, virus_variant](auto&& model) -> auto& {
117  return model.parameters
118  .template get<TimeInfectedSymptomsToRecovered>()[{virus_variant, age_group}];
119  },
120  [](auto& dist1, auto& dist2) {
121  return dist1 < dist2;
122  });
123  param_percentile_dist(
124  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
125  [age_group, virus_variant](auto&& model) -> auto& {
126  return model.parameters
127  .template get<TimeInfectedSevereToCritical>()[{virus_variant, age_group}];
128  },
129  [](auto& dist1, auto& dist2) {
130  return dist1 < dist2;
131  });
132  param_percentile_dist(
133  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
134  [age_group, virus_variant](auto&& model) -> auto& {
135  return model.parameters
136  .template get<TimeInfectedSevereToRecovered>()[{virus_variant, age_group}];
137  },
138  [](auto& dist1, auto& dist2) {
139  return dist1 < dist2;
140  });
141  param_percentile_dist(
142  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
143  [age_group, virus_variant](auto&& model) -> auto& {
144  return model.parameters.template get<TimeInfectedCriticalToDead>()[{virus_variant, age_group}];
145  },
146  [](auto& dist1, auto& dist2) {
147  return dist1 < dist2;
148  });
149  param_percentile_dist(
150  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
151  [age_group, virus_variant](auto&& model) -> auto& {
152  return model.parameters
153  .template get<TimeInfectedCriticalToRecovered>()[{virus_variant, age_group}];
154  },
155  [](auto& dist1, auto& dist2) {
156  return dist1 < dist2;
157  });
158  // Uncertain values
159  param_percentile(node, [age_group, virus_variant](auto&& model) -> auto& {
160  return model.parameters.template get<SymptomsPerInfectedNoSymptoms>()[{virus_variant, age_group}];
161  });
162  param_percentile(node, [age_group, virus_variant](auto&& model) -> auto& {
163  return model.parameters.template get<SeverePerInfectedSymptoms>()[{virus_variant, age_group}];
164  });
165  param_percentile(node, [age_group, virus_variant](auto&& model) -> auto& {
166  return model.parameters.template get<CriticalPerInfectedSevere>()[{virus_variant, age_group}];
167  });
168  param_percentile(node, [age_group, virus_variant](auto&& model) -> auto& {
169  return model.parameters.template get<DeathsPerInfectedCritical>()[{virus_variant, age_group}];
170  });
171 
172  param_percentile(node, [virus_variant](auto&& model) -> auto& {
173  return model.parameters.template get<AerosolTransmissionRates>()[{virus_variant}];
174  });
175 
176  //Other distributions
177  param_percentile_dist(
178  node, std::vector<ViralLoadDistributionsParameters>(num_runs),
179  [age_group, virus_variant](auto&& model) -> auto& {
180  return model.parameters.template get<ViralLoadDistributions>()[{virus_variant, age_group}];
181  },
182  [](auto& dist1, auto& dist2) {
183  return dist1.viral_load_peak < dist2.viral_load_peak;
184  });
185  param_percentile_dist(
186  node, std::vector<ViralShedTuple>(num_runs),
187  [age_group, virus_variant](auto&& model) -> auto& {
188  return model.parameters.template get<ViralShedParameters>()[{virus_variant, age_group}];
189  },
190  [](auto& dist1, auto& dist2) {
191  return dist1.viral_shed_alpha < dist2.viral_shed_alpha;
192  });
193  param_percentile_dist(
194  node, std::vector<mio::AbstractParameterDistribution>(num_runs),
195  [age_group, virus_variant](auto&& model) -> auto& {
196  return model.parameters.template get<ViralShedFactor>()[{virus_variant, age_group}];
197  },
198  [](auto& dist1, auto& dist2) {
199  return dist1 < dist2;
200  });
201  }
202  param_percentile(node, [age_group](auto&& model) -> auto& {
203  return model.parameters.template get<BasicShoppingRate>()[{age_group}];
204  });
205  param_percentile(node, [age_group](auto&& model) -> auto& {
206  static auto result = model.parameters.template get<GotoWorkTimeMinimum>()[{age_group}].hours();
207  return result;
208  });
209  param_percentile(node, [age_group](auto&& model) -> auto& {
210  static auto result = model.parameters.template get<GotoWorkTimeMaximum>()[{age_group}].hours();
211  return result;
212  });
213  param_percentile(node, [age_group](auto&& model) -> auto& {
214  static auto result = model.parameters.template get<GotoSchoolTimeMinimum>()[{age_group}].hours();
215  return result;
216  });
217  param_percentile(node, [age_group](auto&& model) -> auto& {
218  static auto result = model.parameters.template get<GotoSchoolTimeMaximum>()[{age_group}].hours();
219  return result;
220  });
221  }
222  param_percentile(node, [](auto&& model) -> auto& {
223  return model.parameters.template get<MaskProtection>()[MaskType::Community];
224  });
225  param_percentile(node, [](auto&& model) -> auto& {
226  return model.parameters.template get<MaskProtection>()[MaskType::FFP2];
227  });
228  param_percentile(node, [](auto&& model) -> auto& {
229  return model.parameters.template get<MaskProtection>()[MaskType::Surgical];
230  });
231  param_percentile(node, [](auto&& model) -> auto& {
232  static auto result = model.parameters.template get<LockdownDate>().days();
233  return result;
234  });
235  }
236 
237  return percentile;
238 }
239 
240 } // namespace abm
241 } // namespace mio
242 
243 #endif
The Model of the Simulation.
Definition: abm/model.h:54
double ScalarType
Configuration of memilio library.
Definition: memilio/config.h:30
std::vector< Model > ensemble_params_percentile(const std::vector< std::vector< Model >> &ensemble_params, ScalarType p)
computes the p percentile of the parameters for each node.
Definition: models/abm/analyze_result.h:40
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
The AgeGroup struct is used as a dynamically sized tag for all age dependent categories.
Definition: age_group.h:32