def one_run(t): params = prepare_params(t) # t days **before** lockdown (Jan 23) # simulation finishes at Jan 27 (after lockdown for 5 days) # t=1, 2, 3, ... means patient zero appeared in Jan 22, 21, 20 p0_time = lockdown_date - timedelta(days=t) total, _, _, _, stats = do_simulation(t + 5, bed_info, params, p0_time=p0_time, verbose=0) pred_I = np.array([total[-1, STATE.I]]) pred_E = np.array([total[-1, STATE.E]]) pred_M = np.array([total[-1, STATE.M]]) mse_I = mean_absolute_error(actual_I, pred_I) mse_E = mean_absolute_error(actual_E, pred_E) mse_M = mean_absolute_error(actual_M, pred_M) mse_IM = mean_absolute_error(actual_M + actual_I, pred_M + pred_I) mse_IEM = mean_absolute_error(actual_I + actual_E + actual_M, pred_I + pred_E + pred_M) return (t, actual_I[0], pred_I[0], mse_I, mse_E, mse_M, mse_IM, mse_IEM, stats)
def one_run(initial_num_I, I2E_factor, I2M_factor, alpha, beta, mu_ei, mu_mo, k_days, x0_pt): initial_num_E = initial_num_I * I2E_factor initial_num_M = initial_num_I * I2M_factor initial_num_M = min(bed_info[0][1], initial_num_M) params = Params(initial_num_I=initial_num_I, initial_num_E=initial_num_E, initial_num_M=initial_num_M, alpha=alpha, beta=beta, mu_ei=mu_ei, mu_mo=mu_mo, k_days=k_days, x0_pt=x0_pt) total, delta, increase, trans_data, ax = do_simulation(total_days + 3, bed_info, params, p0_time=start_date) I_mae = mean_absolute_error(I_true, increase[1:(total_days + 1), STATE.I]) O_mae = mean_absolute_error(O_true, increase[1:(total_days + 1), STATE.O]) is_decreasing_after_Feb09 = True for i in range((T('09/02/2020') - start_date).days, total.shape[0] - 1): if total[i, STATE.I] < total[i + 1, STATE.I]: is_decreasing_after_Feb09 = False row = (initial_num_I, initial_num_E, initial_num_M, alpha, beta, k_days, mu_ei, mu_mo, x0_pt, I_mae, O_mae, is_decreasing_after_Feb09) return row
def one_run(params_after, bed_info, delta_t, total_days=360): delta = timedelta(days=delta_t) assumed_ld_date = lockdown_time + delta days_to_p0 = (assumed_ld_date - p0_time).days print('lockdown date', assumed_ld_date) params = Params(total_population=params_before.total_population, initial_num_E=params_before.initial_num_E, initial_num_I=params_before.initial_num_I, initial_num_M=params_before.initial_num_M, alpha=[(0, params_before.alpha), (days_to_p0, params_after.alpha)], beta=[(0, params_before.beta), (days_to_p0, params_after.beta)], mu_ei=params_after.mu_ei, mu_mo=params_after.mu_mo, k_days=params_after.k_days, x0_pt=params_after.x0_pt, stages=[days_to_p0]) total, delta, increase, trans, stats = do_simulation(total_days, bed_info, params, p0_time=p0_time) bundle = [total, delta, increase, trans] return assumed_ld_date, delta_t, bundle, stats
def test_equivalence(): p0_time, total_days, bed_info, params = get_input() total_actual, delta_actual, increase_actual, trans_data_actual, stats_actual = do_simulation( total_days, bed_info, params, p0_time=p0_time, verbose=0, show_bar=True) total_expected, delta_expected, increase_expected, trans_data_expected, stats_expected = do_simulation_old( total_days, bed_info, params, p0_time=p0_time, verbose=0, show_bar=True) assert_areq(total_expected, total_actual) assert_areq(delta_expected, delta_actual) assert_areq(increase_expected, increase_actual) assert_areq(trans_data_expected, trans_data_actual) assert stats_actual == stats_expected
if d <= end_date] params = Params(initial_num_I=br.initial_num_I, initial_num_E=br.initial_num_E, initial_num_M=br.initial_num_M, alpha=br.alpha, beta=br.beta, mu_ei=br.mu_ei, mu_mo=br.mu_mo, k_days=int(br.k_days), x0_pt=br.x0_pt) pkl.dump(params, open('output/params_after_lockdown.pkl', 'wb')) total, delta, increase, trans_data, aux = do_simulation(total_days + 60, bed_info, params, p0_time=start_date) I_true_all = df[(df['date'] > start_date)]['infected'].values I_pred_all = increase[1:len(I_true_all) + 1, STATE.I] I_pred = increase[1:(len(I_true) + 1), STATE.I] dates = pd.date_range(start_date + timedelta(days=1), end_date + timedelta(days=2)) data = { 'date': dates, 'true_I': I_true_all, 'pred_I': I_pred_all, 'abs_error': np.abs(I_true_all - I_pred_all), 'squared_error': np.power(I_true_all - I_pred_all, 2), 'used_in_fitting': [(d >= start_date) & (d <= end_date) for d in dates]
initial_num_E=params_before.initial_num_E, initial_num_I=params_before.initial_num_I, initial_num_M=params_before.initial_num_M, mu_ei=params_after.mu_ei, mu_mo=params_after.mu_mo, k_days=params_after.k_days, x0_pt=params_after.x0_pt, alpha=fine_grained_alpha, beta=fine_grained_beta, stages=[days_before_ld] + [(days_before_ld + i) for i in range(offset, offset * n_offsets + 1, offset)]) total, delta, increase, trans_data, stats = do_simulation(total_days, bed_info, params, p0_time=p0_time, verbose=0) # In[12]: stats # In[13]: params # In[14]: stats
makedir_if_not_there('output/tbl/p0-time/') df.to_csv('output/tbl/p0-time/error.csv', index=False) pkl.dump(p0_time, open('output/p0_time.pkl', 'wb')) params = copy(params_jan27) params.alpha = infection_factor * params.alpha params.beta = infection_factor * params.beta params.initial_num_E = 1 params.initial_num_I = 0 params.initial_num_M = 0 total, delta, increase, trans, stats = do_simulation(best_t, bed_info, params, verbose=0, p0_time=p0_time) makedir_if_not_there('output/tbl/before-lockdown/') save_bundle([total, delta, increase, trans], p0_time, best_t, 'output/tbl/before-lockdown') save_to_json(stats, 'output/tbl/before-lockdown/stat.txt') pkl.dump(params, open('output/params_before_lockdown.pkl', 'wb')) fig, ax = plot_total(total, p0_time, best_t) makedir_if_not_there('figs/') fig.savefig('figs/before-lockdown.pdf')