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(
67  node, [ compart, i ](auto&& model) -> auto& {
68  return model.populations[{i, (InfectionState)compart}];
69  });
70  }
71  // times
72  param_percentil(
73  node, [i](auto&& model) -> auto& { return model.parameters.template get<TimeExposed<FP>>()[i]; });
74  param_percentil(
75  node, [i](auto&& model) -> auto& {
76  return model.parameters.template get<TimeInfectedNoSymptoms<FP>>()[i];
77  });
78  param_percentil(
79  node, [i](auto&& model) -> auto& {
80  return model.parameters.template get<TimeInfectedSymptoms<FP>>()[i];
81  });
82  param_percentil(
83  node, [i](auto&& model) -> auto& {
84  return model.parameters.template get<TimeInfectedSevere<FP>>()[i];
85  });
86  param_percentil(
87  node, [i](auto&& model) -> auto& {
88  return model.parameters.template get<TimeInfectedCritical<FP>>()[i];
89  });
90  //probs
91  param_percentil(
92  node, [i](auto&& model) -> auto& {
93  return model.parameters.template get<TransmissionProbabilityOnContact<FP>>()[i];
94  });
95  param_percentil(
96  node, [i](auto&& model) -> auto& {
97  return model.parameters.template get<RelativeTransmissionNoSymptoms<FP>>()[i];
98  });
99  param_percentil(
100  node, [i](auto&& model) -> auto& {
101  return model.parameters.template get<RiskOfInfectionFromSymptomatic<FP>>()[i];
102  });
103  param_percentil(
104  node, [i](auto&& model) -> auto& {
105  return model.parameters.template get<MaxRiskOfInfectionFromSymptomatic<FP>>()[i];
106  });
107  param_percentil(
108  node, [i](auto&& model) -> auto& {
109  return model.parameters.template get<RecoveredPerInfectedNoSymptoms<FP>>()[i];
110  });
111  param_percentil(
112  node, [i](auto&& model) -> auto& {
113  return model.parameters.template get<SeverePerInfectedSymptoms<FP>>()[i];
114  });
115  param_percentil(
116  node, [i](auto&& model) -> auto& { return model.parameters.template get<CriticalPerSevere<FP>>()[i]; });
117  param_percentil(
118  node, [i](auto&& model) -> auto& { return model.parameters.template get<DeathsPerCritical<FP>>()[i]; });
119  }
120  // group independent params
121  param_percentil(
122  node, [](auto&& model) -> auto& { return model.parameters.template get<Seasonality<FP>>(); });
123  param_percentil(
124  node, [](auto&& model) -> auto& { return model.parameters.template get<TestAndTraceCapacity<FP>>(); });
125 
126  for (size_t run = 0; run < num_runs; run++) {
127  auto const& params = ensemble_params[run][node];
128  single_element_ensemble[run] =
129  params.parameters.template get<ICUCapacity<FP>>() * params.populations.get_total();
130  }
131  std::sort(single_element_ensemble.begin(), single_element_ensemble.end());
132  percentile[node].parameters.template set<ICUCapacity<FP>>(
133  single_element_ensemble[static_cast<size_t>(num_runs * p)]);
134  }
135  return percentile;
136 }
137 
138 } // namespace osecir
139 } // namespace mio
140 
141 #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:810
Typesafe index representing an age group.
Definition: age_group.h:40