analyze_result.h Source File

CPP API: analyze_result.h Source File
models/ode_secir/analyze_result.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele
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 ODESECIR_ANALYZE_RESULT_H
21 #define ODESECIR_ANALYZE_RESULT_H
22 
24 #include "ode_secir/parameters.h"
25 
26 namespace mio
27 {
28 
29 namespace osecir
30 {
31 
38 template <typename FP, class Model>
39 std::vector<Model> ensemble_params_percentile(const std::vector<std::vector<Model>>& ensemble_params, FP p)
40 {
41  assert(p > 0.0 && p < 1.0 && "Invalid percentile value.");
42 
43  auto num_runs = ensemble_params.size();
44  auto num_nodes = ensemble_params[0].size();
45  auto num_groups = (size_t)ensemble_params[0][0].parameters.get_num_groups();
46 
47  std::vector<FP> single_element_ensemble(num_runs);
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_percentil = [&ensemble_params, p, num_runs, &percentile](auto n, auto get_param) mutable {
52  std::vector<FP> 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  for (size_t node = 0; node < num_nodes; node++) {
63  for (auto i = AgeGroup(0); i < AgeGroup(num_groups); i++) {
64  //Population
65  for (size_t compart = 0; compart < (size_t)InfectionState::Count; ++compart) {
66  param_percentil(node, [compart, i](auto&& model) -> auto& {
67  return model.populations[{i, (InfectionState)compart}];
68  });
69  }
70  // times
71  param_percentil(node, [i](auto&& model) -> auto& {
72  return model.parameters.template get<TimeExposed<FP>>()[i];
73  });
74  param_percentil(node, [i](auto&& model) -> auto& {
75  return model.parameters.template get<TimeInfectedNoSymptoms<FP>>()[i];
76  });
77  param_percentil(node, [i](auto&& model) -> auto& {
78  return model.parameters.template get<TimeInfectedSymptoms<FP>>()[i];
79  });
80  param_percentil(node, [i](auto&& model) -> auto& {
81  return model.parameters.template get<TimeInfectedSevere<FP>>()[i];
82  });
83  param_percentil(node, [i](auto&& model) -> auto& {
84  return model.parameters.template get<TimeInfectedCritical<FP>>()[i];
85  });
86  //probs
87  param_percentil(node, [i](auto&& model) -> auto& {
88  return model.parameters.template get<TransmissionProbabilityOnContact<FP>>()[i];
89  });
90  param_percentil(node, [i](auto&& model) -> auto& {
91  return model.parameters.template get<RelativeTransmissionNoSymptoms<FP>>()[i];
92  });
93  param_percentil(node, [i](auto&& model) -> auto& {
94  return model.parameters.template get<RiskOfInfectionFromSymptomatic<FP>>()[i];
95  });
96  param_percentil(node, [i](auto&& model) -> auto& {
97  return model.parameters.template get<MaxRiskOfInfectionFromSymptomatic<FP>>()[i];
98  });
99  param_percentil(node, [i](auto&& model) -> auto& {
100  return model.parameters.template get<RecoveredPerInfectedNoSymptoms<FP>>()[i];
101  });
102  param_percentil(node, [i](auto&& model) -> auto& {
103  return model.parameters.template get<SeverePerInfectedSymptoms<FP>>()[i];
104  });
105  param_percentil(node, [i](auto&& model) -> auto& {
106  return model.parameters.template get<CriticalPerSevere<FP>>()[i];
107  });
108  param_percentil(node, [i](auto&& model) -> auto& {
109  return model.parameters.template get<DeathsPerCritical<FP>>()[i];
110  });
111  }
112  // group independent params
113  param_percentil(node, [](auto&& model) -> auto& {
114  return model.parameters.template get<Seasonality<FP>>();
115  });
116  param_percentil(node, [](auto&& model) -> auto& {
117  return model.parameters.template get<TestAndTraceCapacity<FP>>();
118  });
119  param_percentil(node, [](auto&& model) -> auto& {
120  return model.parameters.template get<DynamicNPIsImplementationDelay<FP>>();
121  });
122 
123  for (size_t run = 0; run < num_runs; run++) {
124  auto const& params = ensemble_params[run][node];
125  single_element_ensemble[run] =
126  params.parameters.template get<ICUCapacity<FP>>() * params.populations.get_total();
127  }
128  std::sort(single_element_ensemble.begin(), single_element_ensemble.end());
129  percentile[node].parameters.template set<ICUCapacity<FP>>(
130  single_element_ensemble[static_cast<size_t>(num_runs * p)]);
131  }
132  return percentile;
133 }
134 
135 } // namespace osecir
136 } // namespace mio
137 
138 #endif //ODESECIR_ANALYZE_RESULT_H
Definition: ode_secir/model.h:64
InfectionState
The InfectionState enum describes the possible categories for the infectious state of persons.
Definition: ode_secir/infection_state.h:34
std::vector< Model > ensemble_params_percentile(const std::vector< std::vector< Model >> &ensemble_params, FP p)
computes the p percentile of the parameters for each node.
Definition: models/ode_secir/analyze_result.h:39
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
auto i
Definition: io.h:809
The AgeGroup struct is used as a dynamically sized tag for all age dependent categories.
Definition: age_group.h:32