def setUp(self): self.myData = myData2 self.Person = ex.Variable('Person') self.Variable1 = ex.Variable('Variable1') self.Variable2 = ex.Variable('Variable2') self.Choice = ex.Variable('Choice') self.Av1 = ex.Variable('Av1') self.Av2 = ex.Variable('Av2') self.Av3 = ex.Variable('Av3') self.beta1 = ex.Beta('beta1', 1, None, None, 0) self.beta2 = ex.Beta('beta2', 2, None, None, 0) self.beta3 = ex.Beta('beta3', 3, None, None, 1) self.beta4 = ex.Beta('beta4', 2, None, None, 1)
def train_MNL(data): for mode in modes_list: # availability data[mode+'_avail'] = 1 database = db.Database("MNL_Train", data) beta_dic = {} variables = {} ASC_1 = bioexp.Beta('B___ASC___choice1',0,None,None,1) #fixed ASC_2 = bioexp.Beta('B___ASC___choice2',0,None,None,0) for key in att: beta_dic[key] = {} for var in att[key]: if var not in variables: variables[var] = bioexp.Variable(var) beta_name = 'B___' + var + '___' + key beta_dic[key][beta_name] = bioexp.Beta(beta_name, 0, None, None, 0) V = {key_choice_index['choice1']:ASC_1, key_choice_index['choice2']:ASC_2} AV = {} for key in att: AV[key_choice_index[key]] = bioexp.Variable(key+'_avail') for var in att[key]: beta_name = 'B___' + var + '___' + key V[key_choice_index[key]] += variables[var] * beta_dic[key][beta_name] CHOICE = bioexp.Variable('choice') logprob = bioexp.bioLogLogit(V, AV, CHOICE) formulas = {'loglike': logprob} biogeme = bio.BIOGEME(database, formulas,numberOfThreads = 4) biogeme.modelName = "MNL_Train" results = biogeme.estimate() os.remove("MNL_Train.html") os.remove("MNL_Train.pickle") # Print the estimated values betas = results.getBetaValues() beta={} for k, v in betas.items(): beta[k] = v return beta
def train_MNL(data): for mode in modes_list: # availability data[mode+'_avail'] = 1 database = db.Database("MNL_SGP", data) beta_dic = {} variables = {} ASC_WALK = bioexp.Beta('B___ASC___Walk',0,None,None,1) #fixed ASC_PT = bioexp.Beta('B___ASC___PT',0,None,None,0) ASC_RIDEHAIL = bioexp.Beta('B___ASC___RH',0,None,None,0) ASC_AV = bioexp.Beta('B___ASC___AV',0,None,None,0) ASC_DRIVE = bioexp.Beta('B___ASC___Drive',0,None,None,0) for key in att: beta_dic[key] = {} if key != 'Walk': for var in z_vars: if var not in variables: variables[var] = bioexp.Variable(var) beta_name = 'B___' + var + '___' + key beta_dic[key][beta_name] = bioexp.Beta(beta_name, 0, None, None, 0) for var in att[key]: if var not in variables: variables[var] = bioexp.Variable(var) beta_name = 'B___' + var + '___' + key beta_dic[key][beta_name] = bioexp.Beta(beta_name, 0, None, None, 0) V = {key_choice_index['Walk']:ASC_WALK, key_choice_index['PT']:ASC_PT, key_choice_index['RH']:ASC_RIDEHAIL,key_choice_index['AV']:ASC_AV, key_choice_index['Drive']:ASC_DRIVE} AV = {} for key in att: AV[key_choice_index[key]] = bioexp.Variable(key+'_avail') if key != 'Walk': for var in z_vars: beta_name = 'B___' + var + '___' + key V[key_choice_index[key]] += variables[var] * beta_dic[key][beta_name] for var in att[key]: beta_name = 'B___' + var + '___' + key V[key_choice_index[key]] += variables[var] * beta_dic[key][beta_name] CHOICE = bioexp.Variable('choice') logprob = bioexp.bioLogLogit(V, AV, CHOICE) formulas = {'loglike': logprob} biogeme = bio.BIOGEME(database, formulas,numberOfThreads = 4) biogeme.modelName = "MNL_SGP" results = biogeme.estimate() os.remove("MNL_SGP.html") os.remove("MNL_SGP.pickle") # Print the estimated values betas = results.getBetaValues() beta={} for k, v in betas.items(): beta[k] = v return beta
import pandas as pd import biogeme.version as ver import biogeme.biogeme as bio import biogeme.database as db import biogeme.messaging as msg import biogeme.expressions as be df = pd.read_csv("swissmetro.dat", '\t') database = db.Database("swissmetro", df) # Removing some observations can be done directly using pandas. remove = (((database.data.PURPOSE != 1) & (database.data.PURPOSE != 3)) | (database.data.CHOICE == 0)) database.data.drop(database.data[remove].index, inplace=True) # Parameters to be estimated ASC_CAR = be.Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = be.Beta('ASC_TRAIN', 0, None, None, 0) ASC_SM = be.Beta('ASC_SM', 0, None, None, 1) B_TIME = be.Beta('B_TIME', 0, None, None, 0) B_COST = be.Beta('B_COST', 0, None, None, 0) # Definition of new variables SM_COST = be.Variable('SM_CO') * (be.Variable('GA') == 0) TRAIN_COST = be.Variable('TRAIN_CO') * (be.Variable('GA') == 0) # Definition of new variables: adding columns to the database CAR_AV_SP = be.DefineVariable('CAR_AV_SP', be.Variable('CAR_AV') * (be.Variable('SP') != 0), database) TRAIN_AV_SP = be.DefineVariable('TRAIN_AV_SP', be.Variable('TRAIN_AV') * (be.Variable('SP') != 0), database) TRAIN_TT_SCALED = be.DefineVariable('TRAIN_TT_SCALED', be.Variable('TRAIN_TT') / 100.0, database) TRAIN_COST_SCALED = be.DefineVariable('TRAIN_COST_SCALED', TRAIN_COST / 100, database) SM_TT_SCALED = be.DefineVariable('SM_TT_SCALED', be.Variable('SM_TT') / 100.0, database)
def train_NL(data): for mode in modes_list: # availability data[mode+'_avail'] = 1 database = db.Database("NL_SGP", data) beta_dic = {} variables = {} ASC_WALK = bioexp.Beta('B___ASC___Walk',0,None,None,1) #fixed ASC_PT = bioexp.Beta('B___ASC___PT',0,None,None,0) ASC_RIDEHAIL = bioexp.Beta('B___ASC___RH',0,None,None,0) ASC_AV = bioexp.Beta('B___ASC___AV',0,None,None,0) ASC_DRIVE = bioexp.Beta('B___ASC___Drive',0,None,None,0) for key in att: beta_dic[key] = {} if key != 'Walk': for var in z_vars: if var not in variables: variables[var] = bioexp.Variable(var) beta_name = 'B___' + var + '___' + key beta_dic[key][beta_name] = bioexp.Beta(beta_name, 0, None, None, 0) for var in att[key]: if var not in variables: variables[var] = bioexp.Variable(var) beta_name = 'B___' + var + '___' + key beta_dic[key][beta_name] = bioexp.Beta(beta_name, 0, None, None, 0) V = {key_choice_index['Walk']:ASC_WALK, key_choice_index['PT']:ASC_PT, key_choice_index['RH']:ASC_RIDEHAIL,key_choice_index['AV']:ASC_AV, key_choice_index['Drive']:ASC_DRIVE} AV = {} # availability for key in att: AV[key_choice_index[key]] = bioexp.Variable(key+'_avail') if key != 'Walk': for var in z_vars: beta_name = 'B___' + var + '___' + key V[key_choice_index[key]] += variables[var] * beta_dic[key][beta_name] for var in att[key]: beta_name = 'B___' + var + '___' + key V[key_choice_index[key]] += variables[var] * beta_dic[key][beta_name] CHOICE = bioexp.Variable('choice') MU_car = bioexp.Beta('MU_car', 1, None, None, 0) MU_pt_walk = bioexp.Beta('MU_pt_walk', 1, None, None, 1)#fixed N_car = MU_car, [key_choice_index['RH'],key_choice_index['AV'],key_choice_index['Drive']] N_pt_walk = MU_pt_walk, [key_choice_index['Walk'], key_choice_index['PT']] nests = N_car, N_pt_walk logprob = biomodels.lognested(V, AV, nests, CHOICE) formulas = {'loglike': logprob} biogeme = bio.BIOGEME(database, formulas,numberOfThreads = 4) biogeme.modelName = "NL_SGP" results = biogeme.estimate() os.remove("NL_SGP.html") os.remove("NL_SGP.pickle") # Print the estimated values betas = results.getBetaValues() # beta={} # for k, v in betas.items(): # beta[k] = v biogeme_file={'V':V, 'av':AV, 'nests':nests,'database':database} return betas, biogeme_file