def run(region, folder): (t_obs, dates, y_obs, n_pop, shutdown_day, u0, _) = data_fetcher.read_region_data(folder, region) y_obs = y_obs.astype(np.float64) u0 = u0.astype(np.float64) # set eqn eqn = Seir() eqn.tau = shutdown_day eqn.population = n_pop eqn.beta = .7 eqn.sigma = 0.8 eqn.gamma = 0.2 eqn.kappa = 0.25 eqn.tint = 25 # set ode solver ti = t_obs[0] tf = t_obs[-1] m = 2 n_steps = m * (tf - ti) rk = RKSolverSeir(ti, tf, n_steps) rk.rk_type = "explicit_euler" rk.output_frequency = m rk.set_output_storing_flag(True) rk.equation = eqn rk.set_initial_condition(u0) rk.set_output_gradient_flag(False) rk.solve() (_, y_sim) = rk.get_outputs() x_dates = [dt.datetime.strptime(date, "%Y-%m-%d").date() for date in dates] fig, ax = plt.subplots() ax.plot(x_dates, y_sim[0, :], color='b', lw=2) ax.plot(x_dates, y_obs, 'x', color='b') import matplotlib.dates as mdates ax.xaxis.set_major_formatter(mdates.DateFormatter('%d %b')) plt.yscale('log') plt.show()
def test_rk4_gradient_computation2(self): n_pop = 7E6 sigma = 1/5.2 gamma = 1/2.28 beta = 2.13*gamma eqn = Seir(beta, sigma, gamma) ti = 0 tf = 218 n_steps = 2*tf rk = RKSolver(ti, tf, n_steps) rk.equation = eqn u0 = np.array([n_pop - 1, 0, 1, 0]) u0 /= n_pop du0_dp = np.zeros((eqn.n_components(), eqn.n_parameters())) rk.set_initial_condition(u0, du0_dp) rk.set_output_gradient_flag(True) rk.solve() (u, du_dp) = rk.state() rk.set_output_gradient_flag(False) epsi = 0.001 # perturb beta0 eqn.beta = beta + epsi rk.solve() u_p1 = rk.state() eqn.beta = beta - epsi rk.solve() u_m1 = rk.state() diff = np.linalg.norm(du_dp[:,0] - (u_p1 - u_m1)/(2*epsi))/np.linalg.norm(u0) np.testing.assert_almost_equal(diff, 0, 5) # reset eqn.beta = beta # perturb sigma eqn.sigma = sigma + epsi rk.solve() u_p1 = rk.state() eqn.sigma = sigma - epsi rk.solve() u_m1 = rk.state() diff = np.linalg.norm(du_dp[:,1] - (u_p1 - u_m1)/(2*epsi))/np.linalg.norm(u0) np.testing.assert_almost_equal(diff, 0, 5) # reset eqn.sigma = sigma # perturb gamma eqn.gamma = gamma + epsi rk.solve() u_p1 = rk.state() eqn.gamma = gamma - epsi rk.solve() u_m1 = rk.state() diff = np.linalg.norm(du_dp[:,2] - (u_p1 - u_m1)/(2*epsi))/np.linalg.norm(u0) np.testing.assert_almost_equal(diff, 0, 5) # reset eqn.gamma = gamma # perturb kappa kappa = 1 eqn.kappa = kappa + epsi rk.solve() u_p1 = rk.state() eqn.kappa = kappa - epsi rk.solve() u_m1 = rk.state() diff = np.linalg.norm(du_dp[:,3] - (u_p1 - u_m1)/(2*epsi))/np.linalg.norm(u0) np.testing.assert_almost_equal(diff, 0, 5) # reset eqn.kappa = kappa