def write_comparison_csv(file_name: str, data, model: Model, parameters: Union[list, dict], model_dt: float, headers: Optional[List[str]] = None): if isinstance(parameters, dict): parameters = [parameters] x0, y0 = find_t0_data_point(data) t_max = find_max_t_data_point(data)[0] x_data = [[t, xt] for t, xt, yt in data] y_data = [[t, yt] for t, xt, yt in data] x_datas = [] y_datas = [] for parameters0 in parameters: model_data = model.get_data(parameters0, x0, y0, 0, t_max, math.ceil(t_max / model_dt + 1)) x_datas.append([[t, xt] for t, xt, yt in model_data]) y_datas.append([[t, yt] for t, xt, yt in model_data]) final_data = x_data for model_x_data in x_datas: final_data = unite_data(final_data, model_x_data) final_data = unite_data(final_data, y_data) for model_y_data in y_datas: final_data = unite_data(final_data, model_y_data) new_headers = None if headers is None else [""] + [ "x " + h for h in headers ] + ["y " + h for h in headers] export_array_array_to_csv(final_data, file_name, headers=new_headers)
def s_squared(data, model: Model, parameters, x_factor=1, y_factor=1): # find datapoint for t=0 x0, y0 = find_t0_data_point(data) x, y = model.get_prediction(parameters, x0, y0) return sum((x(t) - xt)**2 * x_factor + (y(t) - yt)**2 * y_factor for t, xt, yt in data) / len(data)
def write_comparison_csv(file_name: str, data, model: Model, parameters: Union[list, dict], model_dt: float): if isinstance(parameters, dict): parameters = [parameters] t0, x0, y0 = find_t0_data_point(data) t_max = find_max_t_data_point(data)[0] for parameters0 in parameters: model_data = model.get_data(parameters0, x0, y0, 0, t_max, math.ceil(t_max/model_dt+1)) data = unite_data(data, model_data) export_data_to_svg(data, file_name)
def s_squared(data, model: Model, parameters, e1, e2): # find datapoint for t=0 x0, y0 = find_t0_data_point(data) x, y = model.get_prediction(parameters, x0, y0) try: return sum((math.log(max(x(t), 0)+e1)-math.log(max(xt, 0)+e1))**2 + (math.log(max(y(t), 0)+e2)-math.log(max(yt, 0)+e2))**2 for t, xt, yt in data)/len(data) except ValueError: raise ModelException("a value below e1 or e2 occurred!")
y_factor=y_factor, error_out=single_data_errors[2*i+1], debug_mod=debug_mod, minimal_second_gradient=minimal_second_gradient) # write to the output file content string result_file_content += "Using data from {}:\n".format(data_file_names[i]) result_file_content += "Normal gradient descent found the following parameters that gave error {}:\n {}\n".format( single_data_errors[2*i][-1], str(new_parameters1) ) result_file_content += "Improved gradient descent found the following parameters that gave error {}:\n {}\n".format( single_data_errors[2*i + 1][-1], str(new_parameters2) ) result_file_content += "\n" # write some files that are used to create graphs to visually compare the model x0, y0 = find_t0_data_point(datas[i]) t_max = find_max_t_data_point(datas[i])[0] model_data1 = model.get_data(new_parameters1, x0, y0, 0, t_max, math.ceil(t_max/model_graph_dt+1)) model_data2 = model.get_data(new_parameters2, x0, y0, 0, t_max, math.ceil(t_max/model_graph_dt+1)) model_prey_data1 = [data_point[:2] for data_point in model_data1] model_prey_data2 = [data_point[:2] for data_point in model_data2] model_predator_data1 = [[data_point[0], data_point[2]] for data_point in model_data1] model_predator_data2 = [[data_point[0], data_point[2]] for data_point in model_data2] # write prey graph prey_graph_data = [data_point[:2] for data_point in datas[i]] prey_graph_data = unite_data(prey_graph_data, model_prey_data1) prey_graph_data = unite_data(prey_graph_data, model_prey_data2) export_array_array_to_csv(prey_graph_data, output_folder + "/" + single_data_set_prey_graph_format.format(i+1)) # write predator graph