def plot_model(contact_matrix, susceptible, infectious_rate, days, reference_cases=None, measure_factor: float = 1, measure_day=0, reference_hospital=None, offset=0, first_patient_age=38, name='model', scenario=None, cap_ic=False, title='') -> Model: model = Model(contact_matrix, susceptible, infectious_rate, measure_factor=measure_factor, measure_day=measure_day, first_patient_age=first_patient_age, scenario=scenario, cap_ic=cap_ic) model.run(days) y = [ model.infected_data, model.recovered_data, model.exposed_data, model.hospital_data, model.ic_data, model.dead_data ] labels = ['infected', 'recovered', 'exposed', 'hospitalized', 'ic', 'dead'] plot(y, labels, name, 'Amount', title=title) if reference_cases is not None: y = [reference_cases, model.case_data] labels = ['Reference Cases', 'Model Cases'] plot(y, labels, 'ref_cases', 'Amount', title='Verschil model werkelijkheid') if reference_hospital is not None: y = [ np.append(np.zeros(offset), reference_hospital), model.hospital_data + model.ic_data ] labels = ['Reference Hospital + ic', 'Model Hospital + ic'] plot(y, labels, 'ref_hospital', 'Amount', title='Verschil model werkelijkheid') return model
def optimal_fit(reference_infected, infectious_rate, contact_matrix, susceptible, num_days): # initial model, do once with higher infectious rate, once with lower, continue with best fit inf_rate_diff = 0.001 model = Model(contact_matrix, susceptible, infectious_rate + inf_rate_diff) model.run(num_days) residual = np.absolute(model.case_data - reference_infected) pos_sum_of_squares = np.sum(residual**2) inf_rate_diff = -0.001 model = Model(contact_matrix, susceptible, infectious_rate + inf_rate_diff) model.run(num_days) residual = np.absolute(model.case_data - reference_infected) neg_sum_of_squares = np.sum(residual**2) if neg_sum_of_squares < pos_sum_of_squares: infectious_rate -= 0.001 prev_sum_of_squares = neg_sum_of_squares while True: infectious_rate -= 0.001 model = Model(contact_matrix, susceptible, infectious_rate) model.run(num_days) residual = np.absolute(model.case_data - reference_infected) sum_of_squares = np.sum(residual**2) if sum_of_squares > prev_sum_of_squares or infectious_rate <= 0 or infectious_rate >= 1: break else: prev_sum_of_squares = sum_of_squares else: infectious_rate += 0.001 prev_sum_of_squares = pos_sum_of_squares while True: infectious_rate += 0.001 model = Model(contact_matrix, susceptible, infectious_rate) model.run(num_days) residual = np.absolute(model.case_data - reference_infected) sum_of_squares = np.sum(residual**2) if sum_of_squares > prev_sum_of_squares or infectious_rate <= 0 or infectious_rate >= 1: break else: prev_sum_of_squares = sum_of_squares return infectious_rate
def calculate_sum_of_squares(contact_matrix, days, infectious_rate, measure_day, offset, reference_hospital, susceptible, first_patient_age): num_days = days + offset factor = 0.2 factor_diff = 0.001 model = Model(contact_matrix, susceptible, infectious_rate, measure_factor=factor + factor_diff, measure_day=measure_day, first_patient_age=first_patient_age) model.run(num_days) temp = model.hospital_data + model.ic_data residual = np.absolute(temp[offset:] - reference_hospital) pos_sum_of_squares = np.sum(residual**2) factor_diff = -0.001 model = Model(contact_matrix, susceptible, infectious_rate, measure_factor=factor + factor_diff, measure_day=measure_day, first_patient_age=first_patient_age) model.run(num_days) temp = model.hospital_data + model.ic_data residual = np.absolute(temp[offset:] - reference_hospital) neg_sum_of_squares = np.sum(residual**2) if neg_sum_of_squares < pos_sum_of_squares: factor_diff -= 0.001 prev_sum_of_squares = neg_sum_of_squares while True: factor_diff -= 0.001 model = Model(contact_matrix, susceptible, infectious_rate, measure_factor=factor + factor_diff, measure_day=measure_day, first_patient_age=first_patient_age) model.run(num_days) temp = model.hospital_data + model.ic_data residual = np.absolute(temp[offset:] - reference_hospital) sum_of_squares = np.sum(residual**2) if sum_of_squares > prev_sum_of_squares or infectious_rate <= 0 or infectious_rate >= 1: factor_diff += 0.001 break else: prev_sum_of_squares = sum_of_squares else: factor_diff += 0.001 prev_sum_of_squares = pos_sum_of_squares while True: factor_diff += 0.001 model = Model(contact_matrix, susceptible, infectious_rate, measure_factor=factor + factor_diff, measure_day=measure_day, first_patient_age=first_patient_age) model.run(num_days) temp = model.hospital_data + model.ic_data residual = np.absolute(temp[offset:] - reference_hospital) sum_of_squares = np.sum(residual**2) if sum_of_squares > prev_sum_of_squares: factor_diff -= 0.001 break else: prev_sum_of_squares = sum_of_squares return factor + factor_diff, prev_sum_of_squares