# Associate utility functions with the numbering of alternatives V = {1: V1, 2: V2, 3: V3} # Associate the availability conditions with the alternatives CAR_AV_SP = DefineVariable('CAR_AV_SP',CAR_AV * ( SP != 0 ),database) TRAIN_AV_SP = DefineVariable('TRAIN_AV_SP',TRAIN_AV * ( SP != 0 ),database) av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} #Definition of nests: # 1: nests parameter # 2: list of alternatives existing = MU , [1,3] future = 1.0 , [2] nests = existing,future # The choice model is a nested logit, with availability conditions logprob = models.lognested(V,av,nests,CHOICE) biogeme = bio.BIOGEME(database,logprob) biogeme.modelName = "09nested" results = biogeme.estimate() print("Results=",results)
3: availability3, 4: availability4 } #Definition of nests: # 1: nests parameter # 2: list of alternatives existing = MU, [1, 2] future1 = 1.0, [3] future2 = 1.0, [4] nests = existing, future1, future2 # Definition of the model. This is the contribution of each # observation to the log likelihood function. # The choice model is a nested logit, with availability conditions logprob = models.lognested(choiceset, availability, nests, choice) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed() # Create the Biogeme object biogeme = bio.BIOGEME(database, logprob) biogeme.modelName = "nested car" # Estimate the parameters results = biogeme.estimate() pandasResults = results.getEstimatedParameters()
def build(self): exclude = ((self.PURPOSE != 1) * (self.PURPOSE != 3) + (self.CHOICE == 0)) > 0 self.database.remove(exclude) ASC_CAR = Beta('ASC_CAR', 0, None, None, 0, 'Car cte.') ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0, 'Train cte.') ASC_SM = Beta('ASC_SM', 0, None, None, 1, 'Swissmetro cte.') B_TIME = Beta('B_TIME', 0, None, None, 0, 'Travel time') B_COST = Beta('B_COST', 0, None, None, 0, 'Travel cost') MU = Beta('MU', 2.05, 1, 5, 0) SM_COST = self.SM_CO * (self.GA == 0) TRAIN_COST = self.TRAIN_CO * (self.GA == 0) TRAIN_TT_SCALED = DefineVariable('TRAIN_TT_SCALED', \ self.TRAIN_TT / 100.0, self.database) TRAIN_COST_SCALED = DefineVariable('TRAIN_COST_SCALED', \ TRAIN_COST / 100, self.database) SM_TT_SCALED = DefineVariable('SM_TT_SCALED', self.SM_TT / 100.0, self.database) SM_COST_SCALED = DefineVariable('SM_COST_SCALED', SM_COST / 100, self.database) CAR_TT_SCALED = DefineVariable('CAR_TT_SCALED', self.CAR_TT / 100, self.database) CAR_CO_SCALED = DefineVariable('CAR_CO_SCALED', self.CAR_CO / 100, self.database) self.x0 = np.array([0.0, 0.0, 0.0, 0.0, 2.05]) self.bounds = [(-2, 2), (-2, 2), (-2, 2), (-2, 2), (1, 5)] V1 = ASC_TRAIN + \ B_TIME * TRAIN_TT_SCALED + \ B_COST * TRAIN_COST_SCALED V2 = ASC_SM + \ B_TIME * SM_TT_SCALED + \ B_COST * SM_COST_SCALED V3 = ASC_CAR + \ B_TIME * CAR_TT_SCALED + \ B_COST * CAR_CO_SCALED # Associate utility functions with the numbering of alternatives V = {1: V1, 2: V2, 3: V3} # Associate the availability conditions with the alternatives CAR_AV_SP = DefineVariable('CAR_AV_SP', self.CAR_AV * (self.SP != 0), self.database) TRAIN_AV_SP = DefineVariable('TRAIN_AV_SP', self.TRAIN_AV * (self.SP != 0), self.database) av = {1: TRAIN_AV_SP, 2: self.SM_AV, 3: CAR_AV_SP} # Definition of nests: # 1: nests parameter # 2: list of alternatives existing = MU, [1, 3] future = 1.0, [2] nests = existing, future # The choice model is a nested logit, with availability conditions logprob = models.lognested(V, av, nests, self.CHOICE) self.biogeme = bio.BIOGEME(self.database, logprob) self.biogeme.modelName = "09nested" self.biogeme.generateHtml = False self.biogeme.theC.setData(self.database.data)
# Definition of the nests: # 1: nests parameter MU_NOCAR = Beta('MU_NOCAR', 1.0, 1.0, None, 0) ###Don't Keep this parameter fixed to the initial value #(name_param,defaultvalue,downValue,upperValue,bool) # 2: list of alternatives 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, with availability conditions logprob = models.lognested( V, None, nests, Choice) ###Non prise en compte de la disponibilité des modes (av) """ # Defines an itertor on the data rowIterator('obsIter') #Statistics nullLoglikelihood(av,'obsIter') choiceSet = [0,1,2] cteLoglikelihood(choiceSet,Choice,'obsIter') availabilityStatistics(av,'obsIter') BIOGEME_OBJECT.STATISTICS['Gender: males'] = \ Sum(male,'obsIter') BIOGEME_OBJECT.STATISTICS['Gender: females'] = \ Sum(female,'obsIter')
BETA_DIST_MALE * distance_km_scaled * male + \ BETA_DIST_FEMALE * distance_km_scaled * female + \ 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, with availability conditions logprob = models.lognested(V, None, nests, Choice) # Create the Biogeme object biogeme = bio.BIOGEME(database, logprob) biogeme.modelName = '01nestedEstimation' # Estimate the parameters. Calculate also the standard errors using # bootstrapping. results = biogeme.estimate(bootstrap=100) # Get the results in a pandas table pandasResults = results.getEstimatedParameters() print(pandasResults)
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