def estimate(self, model, transactions): likelihood_loss_function_coefficients = self.likelihood_loss_function_coefficients( transactions) new_likelihood = model.log_likelihood_for(transactions) max_iterations = len(likelihood_loss_function_coefficients) cpu_time = time_for_optimization( partial_time=Settings.instance( ).non_linear_solver_partial_time_limit(), total_time=Settings.instance().solver_total_time_limit(), profiler=self.profiler()) start_time = time.time() for _ in range(max_iterations): old_likelihood = new_likelihood possible_mnl_model = self.look_for_new_mnl_model( model, likelihood_loss_function_coefficients) model.add_new_class_with(possible_mnl_model) self.update_weights_for(model, likelihood_loss_function_coefficients) new_likelihood = model.log_likelihood_for(transactions) likelihood_does_not_increase = new_likelihood < old_likelihood likelihood_does_not_increase_enough = abs( new_likelihood - old_likelihood) / len(transactions) < 1e-7 time_limit = (time.time() - start_time) > cpu_time if likelihood_does_not_increase or likelihood_does_not_increase_enough or time_limit: break return model
def estimate(self, model, transactions): self.profiler().reset_convergence_criteria() self.profiler().update_time() model = self.custom_initial_solution(model, transactions) cpu_time = time_for_optimization(partial_time=Settings.instance().non_linear_solver_partial_time_limit(), total_time=Settings.instance().solver_total_time_limit(), profiler=self.profiler()) start_time = time.time() while True: self.profiler().start_iteration() model = self.one_step(model, transactions) likelihood = model.log_likelihood_for(transactions) self.profiler().stop_iteration(likelihood) if self.profiler().should_stop() or (time.time() - start_time) > cpu_time: break return model
def cpu_time(self, profiler): return time_for_optimization( partial_time=Settings.instance().linear_solver_partial_time_limit( ), total_time=Settings.instance().solver_total_time_limit(), profiler=profiler)