def predict_NL_2(betas, biogeme_file, data): for mode in modes_list: # availability data[mode+'_avail'] = 1 database = db.Database("NL_SGP", data) # The choice model is a nested logit prob_Walk = biomodels.nested(biogeme_file['V'], biogeme_file['av'], biogeme_file['nests'], key_choice_index['Walk']) prob_PT = biomodels.nested(biogeme_file['V'], biogeme_file['av'], biogeme_file['nests'], key_choice_index['PT']) prob_RH = biomodels.nested(biogeme_file['V'], biogeme_file['av'], biogeme_file['nests'], key_choice_index['RH']) prob_AV = biomodels.nested(biogeme_file['V'], biogeme_file['av'], biogeme_file['nests'], key_choice_index['AV']) prob_Drive = biomodels.nested(biogeme_file['V'], biogeme_file['av'], biogeme_file['nests'], key_choice_index['Drive']) simulate = {'prob_Walk': prob_Walk, 'prob_PT': prob_PT, 'prob_RH': prob_RH, 'prob_AV':prob_AV, 'prob_Drive':prob_Drive} biogeme = bio.BIOGEME(database, simulate) # Extract the values that are necessary betaValues = betas # simulatedValues is a Panda dataframe with the same number of rows as # the database, and as many columns as formulas to simulate. simulatedValues = biogeme.simulate(betaValues) prob_list = list(simulatedValues.columns) data_test = data for key in prob_list: data_test[key] = 0 data_test.loc[:,prob_list] = simulatedValues.loc[:, prob_list] data_test['max_prob'] = data_test[prob_list].max(axis=1) data_test['CHOOSE'] = 0 for mode in key_choice_index: col_nameprob = 'prob_' + mode data_test.loc[data_test[col_nameprob]==data_test['max_prob'],'CHOOSE'] = key_choice_index[mode] acc = len(data_test.loc[data_test['CHOOSE']==data_test['choice']])/len(data_test) return acc, data_test
BETA_DIST_UNREPORTED * distance_km_scaled * unreportedGender # Associate utility functions with the numbering of alternatives V = {0: V_PT, 1: V_CAR, 2: V_SM} # Definition of the nests: # 1: nests parameter # 2: list of alternatives MU_NOCAR = Beta('MU_NOCAR', 1.0, 1.0, None, 0) CAR_NEST = 1.0, [1] NO_CAR_NEST = MU_NOCAR, [0, 2] nests = CAR_NEST, NO_CAR_NEST # The choice model is a nested logit prob_pt = models.nested(V, None, nests, 0) prob_car = models.nested(V, None, nests, 1) prob_sm = models.nested(V, None, nests, 2) # We investigate a scenario where the distance increases by one kilometer. delta_dist = 1.0 distance_km_scaled_after = (distance_km + delta_dist) / 5 # Utility of the slow mode whem the distance increases by 1 kilometer. V_SM_after = ASC_SM + \ BETA_DIST_MALE * distance_km_scaled_after * male + \ BETA_DIST_FEMALE * distance_km_scaled_after * female + \ BETA_DIST_UNREPORTED * distance_km_scaled_after * unreportedGender # Associate utility functions with the numbering of alternatives V_after = {0: V_PT, 1: V_CAR, 2: V_SM_after}
def scenario(scale): """Simulate a scenarios modifying the price of public transportation :param scale: price multiplier. :type scale: float :return: simulated revenues :rtype: float """ # This is the only variable that depends on scale MarginalCostScenario = MarginalCostPT * scale MarginalCostPT_scaled = MarginalCostScenario / 10 # The rest of the model is the same for all scenarios ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_PT = Beta('ASC_PT', 0, None, None, 1) ASC_SM = Beta('ASC_SM', 0, None, None, 0) BETA_TIME_FULLTIME = Beta('BETA_TIME_FULLTIME', 0, None, None, 0) BETA_TIME_OTHER = Beta('BETA_TIME_OTHER', 0, None, None, 0) BETA_DIST_MALE = Beta('BETA_DIST_MALE', 0, None, None, 0) BETA_DIST_FEMALE = Beta('BETA_DIST_FEMALE', 0, None, None, 0) BETA_DIST_UNREPORTED = Beta('BETA_DIST_UNREPORTED', 0, None, None, 0) BETA_COST = Beta('BETA_COST', 0, None, None, 0) # Utility functions V_PT = ASC_PT + BETA_TIME_FULLTIME * TimePT_scaled * fulltime + \ BETA_TIME_OTHER * TimePT_scaled * notfulltime + \ BETA_COST * MarginalCostPT_scaled V_CAR = ASC_CAR + \ BETA_TIME_FULLTIME * TimeCar_scaled * fulltime + \ BETA_TIME_OTHER * TimeCar_scaled * notfulltime + \ BETA_COST * CostCarCHF_scaled V_SM = ASC_SM + \ BETA_DIST_MALE * distance_km_scaled * male + \ BETA_DIST_FEMALE * distance_km_scaled * female + \ BETA_DIST_UNREPORTED * distance_km_scaled * unreportedGender V = {0: V_PT, 1: V_CAR, 2: V_SM} MU_NOCAR = Beta('MU_NOCAR', 1.0, 1.0, None, 0) CAR_NEST = 1.0, [1] NO_CAR_NEST = MU_NOCAR, [0, 2] nests = CAR_NEST, NO_CAR_NEST prob_pt = models.nested(V, None, nests, 0) simulate = { 'weight': normalizedWeight, 'Revenue public transportation': prob_pt * MarginalCostScenario } biogeme = bio.BIOGEME(database, simulate) biogeme.modelName = '02nestedPlot' # Read the estimation results from the file try: results = res.bioResults(pickleFile='01nestedEstimation.pickle') except FileNotFoundError: sys.exit( 'Run first the script 01nestedEstimation.py in order to generate ' 'the file 01nestedEstimation.pickle.') # Simulation simulatedValues = biogeme.simulate(results.getBetaValues()) # We calculate the sum for all individuals of the generated revenues. revenues_pt = (simulatedValues['Revenue public transportation'] * simulatedValues['weight']).sum() return revenues_pt