def plot(ground_truth, estimation_exp, estimator_exp, estimation_mnl, estimator_mnl, transactions): print('') print('Log-likelihood RND: %.4f' % RandomChoiceModel.simple_random(ground_truth.products).log_likelihood_for(transactions)) print('Log-likelihood EXP: %.4f' % estimation_exp.log_likelihood_for(transactions)) print('Log-likelihood MNL: %.4f' % estimation_mnl.log_likelihood_for(transactions)) print('') print('Soft RMSE RND: %.4f' % ground_truth.soft_rmse_for(RandomChoiceModel.simple_random(ground_truth.products))) print('Soft RMSE EXP: %.4f' % ground_truth.soft_rmse_for(estimation_exp)) print('Soft RMSE MNL: %.4f' % ground_truth.soft_rmse_for(estimation_mnl)) print('') iterations_exp = estimator_exp.profiler().iterations() iterations_mnl = estimator_mnl.profiler().iterations() y_data_exp = [-i.value() for i in iterations_exp] x_data_exp = [i.stop_time() - iterations_exp[0].start_time() for i in iterations_exp] y_data_mnl = [-i.value() for i in iterations_mnl] x_data_mnl = [i.stop_time() - iterations_mnl[0].start_time() for i in iterations_mnl] plt.plot(x_data_exp, y_data_exp, x_data_mnl, y_data_mnl) plt.title('MNL vs EXP (Ground truth MNL)') plt.xlabel('Time (seconds)') plt.ylabel('Log-likelihood') plt.legend(['exp', 'mnl']) plt.savefig('output/MNL vs EXP (Ground truth MNL).png')
def plot(ground_truth, estimator_max, estimator_em, estimator_mkv2, estimation_max, estimation_em, estimation_mkv2, transactions): print('') print('Log-likelihood RND: %.4f' % RandomChoiceModel.simple_random( ground_truth.products).log_likelihood_for(transactions)) print('Log-likelihood MAX: %.4f' % estimation_max.log_likelihood_for(transactions)) print('Log-likelihood EM: %.4f' % estimation_em.log_likelihood_for(transactions)) print('Log-likelihood R2: %.4f' % estimation_mkv2.log_likelihood_for(transactions)) print('') print('Soft RMSE RND: %.4f' % ground_truth.soft_rmse_for( RandomChoiceModel.simple_random(ground_truth.products))) print('Soft RMSE MAX: %.4f' % ground_truth.soft_rmse_for(estimation_max)) print('Soft RMSE EM: %.4f' % ground_truth.soft_rmse_for(estimation_em)) print('Soft RMSE R2: %.4f' % ground_truth.soft_rmse_for(estimation_mkv2)) print('') iterations_max = estimator_max.profiler().iterations()[2:] iterations_em = estimator_em.profiler().iterations()[2:] iterations_mkv2 = estimator_mkv2.profiler().iterations()[2:] y_mkv2_data = [-i.value() for i in iterations_mkv2] x_mkv2_data = [ i.stop_time() - iterations_mkv2[0].start_time() for i in iterations_mkv2 ] y_max_data = [-i.value() for i in iterations_max] x_max_data = [ i.stop_time() - iterations_max[0].start_time() for i in iterations_max ] y_em_data = [i.value() for i in iterations_em] x_em_data = [ i.stop_time() - iterations_em[0].start_time() for i in iterations_em ] plt.plot(x_max_data, y_max_data, x_em_data, y_em_data, x_mkv2_data, y_mkv2_data) plt.title('Markov Chain method comparison') plt.xlabel('Time (seconds)') plt.ylabel('Log-likelihood') plt.legend(['max', 'em', 'rank2']) plt.savefig('output/Markov Chain method comparison.png')
def main(): Settings.new( linear_solver_partial_time_limit=300.0, non_linear_solver_partial_time_limit=300.0, solver_total_time_limit=1800.0, ) ground_truth, transactions = read_synthetic_instance( 'instances/100_lists_300_periods_1_instance.json') products = ground_truth.products nests = [{ 'products': [0], 'lambda': 0.8 }, { 'products': [i for i in range(1, len(products)) if i % 2 == 0], 'lambda': 0.8 }, { 'products': [i for i in range(1, len(products)) if i % 2 == 1], 'lambda': 0.8 }] lists = [[i] + list(sorted(set(products) - {i})) for i in range(len(products))] models_to_run = [ (ExponomialModel.simple_deterministic(products), MaximumLikelihoodEstimator()), (MultinomialLogitModel.simple_deterministic(products), MaximumLikelihoodEstimator()), (RandomChoiceModel.simple_deterministic(products), RandomChoiceModelMaximumLikelihoodEstimator()), (LatentClassModel.simple_deterministic(products, 1), LatentClassFrankWolfeEstimator()), (MarkovChainModel.simple_deterministic(products), MarkovChainExpectationMaximizationEstimator()), (MarkovChainRank2Model.simple_deterministic(products), MaximumLikelihoodEstimator()), (MixedLogitModel.simple_deterministic(products), MaximumLikelihoodEstimator()), (NestedLogitModel.simple_deterministic(products, nests=nests), MaximumLikelihoodEstimator()), (RankedListModel.simple_deterministic(products, ranked_lists=lists), RankedListExpectationMaximizationEstimator(MIPMarketExplorer())), ] results = [] for initial_solution, estimator in models_to_run: if hasattr(estimator, 'estimate_with_market_discovery'): model = estimator.estimate_with_market_discovery( initial_solution, transactions) else: model = estimator.estimate(initial_solution, transactions) soft_rmse = model.soft_rmse_for(ground_truth) results.append((model, estimator.profiler(), soft_rmse)) plot(results)
def plot(ground_truth, estimator_max, estimator_em, estimator_mip, estimation_max, estimation_em, estimation_mip, transactions): print('') print('Log-likelihood RND: %.4f' % RandomChoiceModel.simple_random( ground_truth.products).log_likelihood_for(transactions)) print('Log-likelihood MAX: %.4f' % estimation_max.log_likelihood_for(transactions)) print('Log-likelihood EM: %.4f' % estimation_em.log_likelihood_for(transactions)) print('Log-likelihood MIP: %.4f' % estimation_mip.log_likelihood_for(transactions)) print('') print('Soft RMSE RND: %.4f' % ground_truth.soft_rmse_for( RandomChoiceModel.simple_random(ground_truth.products))) print('Soft RMSE MAX: %.4f' % ground_truth.soft_rmse_for(estimation_max)) print('Soft RMSE EM: %.4f' % ground_truth.soft_rmse_for(estimation_em)) print('Soft RMSE MIP: %.4f' % ground_truth.soft_rmse_for(estimation_mip)) print('') iterations_em = estimator_em.profiler().iterations() iterations_max = estimator_max.profiler().iterations() iterations_mip = estimator_mip.profiler().iterations() y_data_max = [-i.value() for i in iterations_max] x_data_max = [ i.stop_time() - iterations_max[0].start_time() for i in iterations_max ] y_data_em = [i.value() for i in iterations_em] x_data_em = [ i.stop_time() - iterations_em[0].start_time() for i in iterations_em ] y_data_mip = [i.value() for i in iterations_mip] x_data_mip = [ i.stop_time() - iterations_mip[0].start_time() for i in iterations_mip ] plt.plot(x_data_max, y_data_max, x_data_em, y_data_em, x_data_mip, y_data_mip) plt.title('Ranked List Method comparison') plt.xlabel('Time (seconds)') plt.ylabel('Log-likelihood') plt.legend(['max (has lists)', 'em (has lists)', 'mip (discovers lists)']) plt.savefig('output/Ranked List Method comparison.png')
def plot(ground_truth, estimation_em, estimation_max, estimation_fw, estimator_em, estimator_max, estimator_fw, transactions): print('') print('Log-likelihood RND: %.4f' % RandomChoiceModel.simple_random( ground_truth.products).log_likelihood_for(transactions)) print('Log-likelihood MAX: %.4f' % estimation_max.log_likelihood_for(transactions)) print('Log-likelihood EM: %.4f' % estimation_em.log_likelihood_for(transactions)) print('Log-likelihood FW: %.4f' % estimation_fw.log_likelihood_for(transactions)) print('') print('Soft RMSE RND: %.4f' % ground_truth.soft_rmse_for( RandomChoiceModel.simple_random(ground_truth.products))) print('Soft RMSE MAX: %.4f' % ground_truth.soft_rmse_for(estimation_max)) print('Soft RMSE EM: %.4f' % ground_truth.soft_rmse_for(estimation_em)) print('Soft RMSE FW: %.4f' % ground_truth.soft_rmse_for(estimation_fw)) print('') iterations_fw = estimator_fw.profiler().iterations() iterations_max = estimator_max.profiler().iterations() iterations_em = estimator_em.profiler().iterations() # Frank wolfe has two subproblems on each iteration. # - 1st problem finds new MNLs to add to the Latent class: (always negative objective function) # - 2nd problem corrects weights for each class (always positive objective function, basically a NLL) # Not very elegant: # - Plot only NLL after each iteration. nlls_iterations = [] for i in range(1, len(iterations_fw)): # If went from positive to negative, then changed from 2nd problem to 1st problem. Save NLL. if iterations_fw[i - 1].value() >= 0 and iterations_fw[i].value() < 0: nlls_iterations.append((-iterations_fw[i - 1].value(), iterations_fw[i - 1].stop_time() - iterations_fw[0].start_time())) if iterations_fw[-1].value() >= 0: nlls_iterations.append( (-iterations_fw[-1].value(), iterations_fw[-1].stop_time() - iterations_fw[0].start_time())) y_data_fw = [ x[0] * len(set(transactions)) / len(transactions) for x in nlls_iterations ] x_data_fw = [x[1] for x in nlls_iterations] y_data_max = [-i.value() for i in iterations_max] x_data_max = [ i.stop_time() - iterations_max[0].start_time() for i in iterations_max ] y_data_em = [i.value() for i in iterations_em] x_data_em = [ i.stop_time() - iterations_em[0].start_time() for i in iterations_em ] plt.plot(x_data_fw, y_data_fw, x_data_max, y_data_max, x_data_em, y_data_em) plt.title('Latent Class method comparison') plt.xlabel('Time (seconds)') plt.ylabel('Log-likelihood') plt.legend([ 'CG (%s classes discovered)' % len(estimation_fw.gammas), 'MAX (%s classes given)' % len(estimation_max.gammas), 'EM (%s classes given)' % len(estimation_em.gammas) ]) plt.savefig('output/Latent Class method comparison.png')
'mx': { 'estimator': MaximumLikelihoodEstimator(), 'model_class': lambda products: MixedLogitModel.simple_deterministic(products ), 'name': 'Mixed Logit', 'settings': NORMAL_SETTINGS }, 'rnd': { 'estimator': RandomChoiceModelMaximumLikelihoodEstimator(), 'model_class': lambda products: RandomChoiceModel.simple_deterministic( products), 'name': 'Random Choice', 'settings': NORMAL_SETTINGS }, 'rl': { 'estimator': RankedListMaximumLikelihoodEstimator.with_this( MIPMarketExplorer()), 'model_class': lambda products: RankedListModel. simple_deterministic_independent(products), 'name': 'Ranked List', 'settings':