Esempio n. 1
0
 def solve_best_params_and_predict(optimal_params):
     # Variables Initialization for the ODE system
     alpha, days, r_s, r_dth, p_dth, r_dthdecay, k1, k2, jump, t_jump, std_normal, k3 = optimal_params
     optimal_params = [
         max(alpha, dict_default_reinit_parameters["alpha"]),
         days,
         max(r_s, dict_default_reinit_parameters["r_s"]),
         max(min(r_dth, 1),
             dict_default_reinit_parameters["r_dth"]),
         max(min(p_dth, 1),
             dict_default_reinit_parameters["p_dth"]),
         max(r_dthdecay,
             dict_default_reinit_parameters["r_dthdecay"]),
         max(k1, dict_default_reinit_parameters["k1"]),
         max(k2, dict_default_reinit_parameters["k2"]),
         max(jump, dict_default_reinit_parameters["jump"]),
         max(t_jump, dict_default_reinit_parameters["t_jump"]),
         max(std_normal,
             dict_default_reinit_parameters["std_normal"]),
         max(k3, dict_default_reinit_parameters["k3"]),
     ]
     x_0_cases = get_initial_conditions(
         params_fitted=optimal_params,
         global_params_fixed=GLOBAL_PARAMS_FIXED,
     )
     x_sol_best = solve_ivp(
         fun=model_covid,
         y0=x_0_cases,
         t_span=[t_predictions[0], t_predictions[-1]],
         t_eval=t_predictions,
         args=tuple(optimal_params),
     ).y
     return x_sol_best
Esempio n. 2
0
 def solve_best_params_and_predict(optimal_params):
     # Variables Initialization for the ODE system
     x_0_cases = get_initial_conditions(
         params_fitted=optimal_params,
         global_params_fixed=GLOBAL_PARAMS_FIXED)
     x_sol_best = solve_ivp(
         fun=model_covid_predictions,
         y0=x_0_cases,
         t_span=[t_predictions[0], t_predictions[-1]],
         t_eval=t_predictions,
         args=tuple(optimal_params),
     ).y
     return x_sol_best
Esempio n. 3
0
            def residuals_totalcases(params) -> float:
                """
                Function that makes sure the parameters are in the right range during the fitting process and computes
                the loss function depending on the optimizer that has been chosen for this run as a global variable
                :param params: currently fitted values of the parameters during the fitting process
                :return: the value of the loss function as a float that is optimized against (in our case, minimized)
                """
                # Variables Initialization for the ODE system
                alpha, days, r_s, r_dth, p_dth, r_dthdecay, k1, k2, jump, t_jump, std_normal, k3 = params
                # Force params values to stay in a certain range during the optimization process with re-initializations
                params = (
                    max(alpha, dict_default_reinit_parameters["alpha"]),
                    days,
                    max(r_s, dict_default_reinit_parameters["r_s"]),
                    max(min(r_dth, 1), dict_default_reinit_parameters["r_dth"]),
                    max(min(p_dth, 1), dict_default_reinit_parameters["p_dth"]),
                    max(r_dthdecay, dict_default_reinit_parameters["r_dthdecay"]),
                    max(k1, dict_default_reinit_parameters["k1"]),
                    max(k2, dict_default_reinit_parameters["k2"]),
                    max(jump, dict_default_reinit_parameters["jump"]),
                    max(t_jump, dict_default_reinit_parameters["t_jump"]),
                    max(std_normal, dict_default_reinit_parameters["std_normal"]),
                    max(k3, dict_default_reinit_lower_bounds["k3"]),
                )

                x_0_cases = get_initial_conditions(
                    params_fitted=params, global_params_fixed=GLOBAL_PARAMS_FIXED
                )
                x_sol_total = solve_ivp(
                    fun=model_covid,
                    y0=x_0_cases,
                    t_span=[t_cases[0], t_cases[-1]],
                    t_eval=t_cases,
                    args=tuple(params),
                )
                x_sol = x_sol_total.y
                # weights = list(range(1, len(cases_data_fit) + 1))
                # weights = [(x/len(cases_data_fit))**2 for x in weights]
                if x_sol_total.status == 0:
                    residuals_value = get_residuals_value(
                        optimizer=OPTIMIZER,
                        balance=balance,
                        x_sol=x_sol,
                        cases_data_fit=cases_data_fit,
                        deaths_data_fit=deaths_data_fit,
                        weights=weights,
                        balance_total_difference=balance_total_difference 
                    )
                else:
                    residuals_value = 1e16
                return residuals_value