def estimate_wrap(V, av, CHOICE, database, panelEst, ModNameSeqence, Draws): ''' # # # ''' if panelEst: #database.panel('ID') # Conditional to B_TIME_RND, the likelihood of one observation is # given by the logit model (called the kernel) obsprob = exp(models.loglogit(V, av, CHOICE)) # Conditional to B_TIME_RND, the likelihood of all observations for # one individual (the trajectory) is the product of the likelihood of # each observation. condprobIndiv = PanelLikelihoodTrajectory(obsprob) if Draws > 0: condprobIndiv = MonteCarlo(condprobIndiv) logprob = log(condprobIndiv) else: prob = exp(models.loglogit(V, av, CHOICE)) if Draws > 0: prob = MonteCarlo(prob) logprob = log(prob) if Draws > 0: biogeme = bio.BIOGEME(database, logprob, numberOfDraws=Draws) else: biogeme = bio.BIOGEME(database, logprob) biogeme.modelName = ModNameSeqence return biogeme.estimate()
def mixedloglikelihood(prob): """Compute a simulated loglikelihood function :param prob: An expression providing the value of the probability. Although it is not formally necessary, the expression should contain one or more random variables of a given distribution, and therefore is defined as .. math:: P(i|\\xi_1,\\ldots,\\xi_L) :type prob: biogeme.expressions.Expression :return: the simulated loglikelihood, given by .. math:: \\ln\\left(\\sum_{r=1}^R P(i|\\xi^r_1,\\ldots,\\xi^r_L) \\right) where :math:`R` is the number of draws, and :math:`\\xi_j^r` is the rth draw of the random variable :math:`\\xi_j`. :rtype: biogeme.expressions.Expression """ l = MonteCarlo(prob) return log(l)
V2 = ASC_SM + \ B_TIME_RND * SM_TT_SCALED + \ B_COST * SM_COST_SCALED V3 = ASC_CAR + \ B_TIME_RND * 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', 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} obsprob = models.logit(V, av, CHOICE) condprobIndiv = PanelLikelihoodTrajectory(obsprob) logprob = log(MonteCarlo(condprobIndiv)) class test_12(unittest.TestCase): def testEstimation(self): biogeme = bio.BIOGEME(database, logprob, numberOfDraws=5, seed=10) results = biogeme.estimate() self.assertAlmostEqual(results.data.logLike, -4705.638407401872, 2) if __name__ == '__main__': unittest.main()
V2 = ASC_SM + \ B_TIME_RND * SM_TT_SCALED + \ B_COST * SM_COST_SCALED V3 = ASC_CAR + \ B_TIME_RND * 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 av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} # The choice model is a logit, with availability conditions prob = models.logit(V, av, CHOICE) logprob = log(MonteCarlo(prob)) R = 2000 biogeme = bio.BIOGEME(database, logprob, numberOfDraws=R) biogeme.modelName = '07estimationMonteCarlo_mlhs' results = biogeme.estimate() # Get the results in a pandas table pandasResults = results.getEstimatedParameters() print(pandasResults)
# the kernel) for the choice condprob = models.logit(V, None, Choice) # Conditional to the random parameters, we have the product of ordered # probit for the indicators. condlike = P_Envir01 * \ P_Envir02 * \ P_Envir03 * \ P_Mobil11 * \ P_Mobil14 * \ P_Mobil16 * \ P_Mobil17 * \ condprob # We integrate over omega using Monte-Carlo integration loglike = log(MonteCarlo(condlike)) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed() # Create the Biogeme object biogeme = bio.BIOGEME(database, loglike, numberOfDraws=20000) biogeme.modelName = '06serialCorrelation' # Estimate the parameters results = biogeme.estimate() print(f'Estimated betas: {len(results.data.betaValues)}')
V = {1: V1, 2: V2, 3: V3} # Associate the availability conditions with the alternatives av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} # Conditional to B_TIME_RND, we have a logit model (called the kernel) prob = models.logit(V, av, CHOICE) # Add Panel Structure pnl_prob = PanelLikelihoodTrajectory(prob) # We integrate over B_TIME_RND using Monte-Carlo logprob = log(MonteCarlo(pnl_prob)) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed() # These notes will be included as such in the report file. userNotes = ('Example of a mixture of logit models with three alternatives, ' 'approximated using Monte-Carlo integration.') # Create the Biogeme object biogeme = bio.BIOGEME(database, logprob, numberOfDraws=1500, userNotes=userNotes) biogeme.modelName = '05normalMixture'
exp(BETA_TIME_CAR_CL * CARLOVERS) V1 = ASC_CAR + \ BETA_TIME_CAR * TimeCar_scaled + \ BETA_COST_HWH * CostCarCHF_scaled * PurpHWH + \ BETA_COST_OTHER * CostCarCHF_scaled * PurpOther V2 = ASC_SM + BETA_DIST * distance_km_scaled # Associate utility functions with the numbering of alternatives V = {0: V0, 1: V1, 2: V2} # Conditional to omega, we have a logit model (called the kernel) condprob = models.logit(V, None, Choice) # We integrate over omega using numerical integration loglike = log(MonteCarlo(condprob)) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed() # Create the Biogeme object biogeme = bio.BIOGEME(database, loglike, numberOfDraws=10000) biogeme.modelName = '04latentChoiceSeq_mc' # Estimate the parameters fname = '__04iterations.txt' biogeme.loadSavedIteration(filename=fname)
# Associate the availability conditions with the alternatives av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} # The choice model is a discrete mixture of logit, with availability conditions # We calculate the conditional probability for each class prob = [ PanelLikelihoodTrajectory(models.logit(V[i], av, CHOICE)) for i in range(numberOfClasses) ] # Conditional to the random variables, likelihood for the individual. probIndiv = PROB_class0 * prob[0] + PROB_class1 * prob[1] # We integrate over the random variables using Monte-Carlo logprob = log(MonteCarlo(probIndiv)) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed() # Create the Biogeme object biogeme = bio.BIOGEME(database, logprob, numberOfDraws=100000) biogeme.modelName = '15panelDiscrete' # As the estimation may take a while and risk to be interrupted, we save the iterations, # and restore them before the estimation.
database = db.Database('fakeDatabase', pandas) def halton13(sampleSize, numberOfDraws): """ The user can define new draws. For example, Halton draws with base 13, skipping the first 10 draws. """ return draws.getHaltonDraws(sampleSize, numberOfDraws, base=13, skip=10) mydraws = {'HALTON13': (halton13, 'Halton draws, base 13, skipping 10')} database.setRandomNumberGenerators(mydraws) integrand = exp(bioDraws('U', 'UNIFORM')) simulatedI = MonteCarlo(integrand) integrand_halton = exp(bioDraws('U_halton', 'UNIFORM_HALTON2')) simulatedI_halton = MonteCarlo(integrand_halton) integrand_halton13 = exp(bioDraws('U_halton13', 'HALTON13')) simulatedI_halton13 = MonteCarlo(integrand_halton13) integrand_mlhs = exp(bioDraws('U_mlhs', 'UNIFORM_MLHS')) simulatedI_mlhs = MonteCarlo(integrand_mlhs) trueI = exp(1.0) - 1.0 R = 20000 sampleVariance = MonteCarlo(integrand * integrand) - simulatedI * simulatedI
# The estimation results are read from the pickle file try: results = res.bioResults(pickleFile='05normalMixture.pickle') except FileNotFoundError: print( 'Run first the script 05normalMixture.py in order to generate the file ' '05normalMixture.pickle.') sys.exit() # Conditional to B_TIME_RND, we have a logit model (called the kernel) prob = models.logit(V, av, CHOICE) # We calculate the integration error. Note that this formula assumes # independent draws, and is not valid for Haltom or antithetic draws. numberOfDraws = 100000 integral = MonteCarlo(prob) integralSquare = MonteCarlo(prob * prob) variance = integralSquare - integral * integral error = (variance / 2.0)**0.5 # And the value of the individual parameters numerator = MonteCarlo(B_TIME_RND * prob) denominator = integral simulate = { 'Numerator': numerator, 'Denominator': denominator, 'Integral': integral, 'Integration error': error }
V2 = [ASC_SM_RND[i] + B_TIME_RND[i] * SM_TT_SCALED + B_COST[i] * SM_COST_SCALED for i in range(numberOfClasses)] V3 = [ASC_CAR_RND[i] + B_TIME_RND[i] * CAR_TT_SCALED + B_COST[i] * CAR_CO_SCALED for i in range(numberOfClasses)] V = [{1: V1[i], 2: V2[i], 3: V3[i]} for i in range(numberOfClasses)] # Associate the availability conditions with the alternatives av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} # The choice model is a discrete mixture of logit, with availability conditions # We calculate the conditional probability for each class prob = [MonteCarlo(PanelLikelihoodTrajectory(models.logit(V[i], av, CHOICE))) for i in range(numberOfClasses)] # Conditional to the random variables, likelihood for the individual. probIndiv = PROB_class0 * prob[0] + PROB_class1 * prob[1] # We integrate over the random variables using Monte-Carlo logprob = log(probIndiv) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed()
""" d = draws.getHaltonDraws(sampleSize, int(numberOfDraws / 2), base=13, skip=10) return np.concatenate((d, 1 - d), axis=1) mydraws = { 'HALTON13_ANTI': (halton13_anti, 'Antithetic Halton draws, base 13, skipping 10') } database.setRandomNumberGenerators(mydraws) integrand = exp(bioDraws('U', 'UNIFORM_ANTI')) simulatedI = MonteCarlo(integrand) integrand_halton13 = exp(bioDraws('U_halton13', 'HALTON13_ANTI')) simulatedI_halton13 = MonteCarlo(integrand_halton13) integrand_mlhs = exp(bioDraws('U_mlhs', 'UNIFORM_MLHS_ANTI')) simulatedI_mlhs = MonteCarlo(integrand_mlhs) trueI = exp(1.0) - 1.0 R = 20000 error = simulatedI - trueI error_halton13 = simulatedI_halton13 - trueI
def cv_estimate_model(V, Draws, ModName, train, myRandomNumberGenerators, COST_SCALE_CAR=100, COST_SCALE_PUB=100): db_train = db.Database("swissmetro_train", train) db_train.setRandomNumberGenerators(myRandomNumberGenerators) globals().update(db_train.variables) #locals().update(db_train.variables) db_train.panel('ID') #variables CAR_AV_SP = DefineVariable('CAR_AV_SP', CAR_AV * (SP != 0), db_train) TRAIN_AV_SP = DefineVariable('TRAIN_AV_SP', TRAIN_AV * (SP != 0), db_train) SM_COST = SM_CO * (GA == 0) TRAIN_COST = TRAIN_CO * (GA == 0) ###Parameters to be estimated (Note not all parameters are used in all models!) ##Attributes #Alternative specific constants ASC_CAR = Beta('ASC_CAR', 0, None, None, 1) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_SM = Beta('ASC_SM', 0, None, None, 0) #Cost (Note: Assumed generic) B_COST = Beta('B_COST', 0, None, None, 0) B_COST_BUSINESS = Beta('B_COST_BUSINESS', 0, None, None, 0) B_COST_PRIVATE = Beta('B_COST_PRIVATE', 0, None, None, 0) #Time B_TIME = Beta('B_TIME', 0, None, None, 0) B_TIME_CAR = Beta('B_TIME_CAR', 0, None, None, 0) B_TIME_TRAIN = Beta('B_TIME_TRAIN', 0, None, None, 0) B_TIME_SM = Beta('B_TIME_SM', 0, None, None, 0) B_TIME_PUB = Beta('B_TIME_PUB', 0, None, None, 0) B_TIME_CAR_BUSINESS = Beta('B_TIME_CAR_BUSINESS', 0, None, None, 0) B_TIME_TRAIN_BUSINESS = Beta('B_TIME_TRAIN_BUSINESS', 0, None, None, 0) B_TIME_SM_BUSINESS = Beta('B_TIME_SM_BUSINESS', 0, None, None, 0) B_TIME_PUB_BUSINESS = Beta('B_TIME_PUB_BUSINESS', 0, None, None, 0) B_TIME_CAR_PRIVATE = Beta('B_TIME_CAR_PRIVATE', 0, None, None, 0) B_TIME_TRAIN_PRIVATE = Beta('B_TIME_TRAIN_PRIVATE', 0, None, None, 0) B_TIME_SM_PRIVATE = Beta('B_TIME_SM_PRIVATE', 0, None, None, 0) B_TIME_PUB_PRIVATE = Beta('B_TIME_PUB_PRIVATE', 0, None, None, 0) #HE (Note: Not available for car) B_HE = Beta('B_HE', 0, None, None, 0) B_HE_TRAIN = Beta('B_HE_TRAIN', 0, None, None, 0) B_HE_SM = Beta('B_HE_SM', 0, None, None, 0) B_HE_BUSINESS = Beta('B_HE_BUSINESS', 0, None, None, 0) B_HE_TRAIN_BUSINESS = Beta('B_HE_TRAIN_BUSINESS', 0, None, None, 0) B_HE_SM_BUSINESS = Beta('B_HE_SM_BUSINESS', 0, None, None, 0) B_HE_PRIVATE = Beta('B_HE_PRIVATE', 0, None, None, 0) B_HE_TRAIN_PRIVATE = Beta('B_HE_TRAIN_PRIVATE', 0, None, None, 0) B_HE_SM_PRIVATE = Beta('B_HE_SM_PRIVATE', 0, None, None, 0) #Seats (Note: Only avaliable for SM) B_SEATS = Beta('B_SEATS', 0, None, None, 0) ##Characteristics #Age B_AGE_1_TRAIN = Beta('B_AGE_1_TRAIN', 0, None, None, 1) #Note: Reference B_AGE_2_TRAIN = Beta('B_AGE_2_TRAIN', 0, None, None, 0) B_AGE_3_TRAIN = Beta('B_AGE_3_TRAIN', 0, None, None, 0) B_AGE_4_TRAIN = Beta('B_AGE_4_TRAIN', 0, None, None, 0) B_AGE_5_TRAIN = Beta('B_AGE_5_TRAIN', 0, None, None, 0) B_AGE_6_TRAIN = Beta('B_AGE_6_TRAIN', 0, None, None, 0) B_AGE_1_SM = Beta('B_AGE_1_SM', 0, None, None, 1) #Note: Reference B_AGE_2_SM = Beta('B_AGE_2_SM', 0, None, None, 0) B_AGE_3_SM = Beta('B_AGE_3_SM', 0, None, None, 0) B_AGE_4_SM = Beta('B_AGE_4_SM', 0, None, None, 0) B_AGE_5_SM = Beta('B_AGE_5_SM', 0, None, None, 0) B_AGE_6_SM = Beta('B_AGE_6_SM', 0, None, None, 0) B_AGE_1_PUB = Beta('B_AGE_1_PUB', 0, None, None, 1) #Note: Reference B_AGE_2_PUB = Beta('B_AGE_2_PUB', 0, None, None, 0) B_AGE_3_PUB = Beta('B_AGE_3_PUB', 0, None, None, 0) B_AGE_4_PUB = Beta('B_AGE_4_PUB', 0, None, None, 0) B_AGE_5_PUB = Beta('B_AGE_5_PUB', 0, None, None, 0) B_AGE_6_PUB = Beta('B_AGE_6_PUB', 0, None, None, 0) B_AGE_ADULTS_TRAIN = Beta('B_AGE_TRAIN_ADULTS', 0, None, None, 0) B_AGE_ADULTS_SM = Beta('B_AGE_ADULTS_SM', 0, None, None, 0) B_AGE_ADULTS_PUB = Beta('B_AGE_ADULTS_PUB', 0, None, None, 0) #Luggage B_LUGGAGE_TRAIN = Beta('B_LUGGAGE_TRAIN', 0, None, None, 0) B_LUGGAGE_SM = Beta('B_LUGGAGE_SM', 0, None, None, 0) B_LUGGAGE_PUB = Beta('B_LUGGAGE_PUB', 0, None, None, 0) #Gender B_MALE_TRAIN = Beta('B_MALE_TRAIN', 0, None, None, 0) B_MALE_SM = Beta('B_MALE_SM', 0, None, None, 0) B_MALE_PUB = Beta('B_MALE_PUB', 0, None, None, 0) #Purpose B_BUSINESS = Beta('B_BUSINESS', 0, None, None, 0) B_BUSINESS_TRAIN = Beta('B_BUSINESS_TRAIN', 0, None, None, 0) B_BUSINESS_SM = Beta('B_BUSINESS_SM', 0, None, None, 0) B_PRIVATE = Beta('B_PRIVATE', 0, None, None, 0) B_PRIVATE_TRAIN = Beta('B_PRIVATE_TRAIN', 0, None, None, 0) B_PRIVATE_SM = Beta('B_PRIVATE_SM', 0, None, None, 0) B_COMMUTER = Beta('B_COMMUTER', 0, None, None, 0) B_COMMUTER_TRAIN = Beta('B_COMMUTER_TRAIN', 0, None, None, 0) B_COMMUTER_SM = Beta('B_COMMUTER_SM', 0, None, None, 0) #GA B_GA = Beta('B_GA', 0, None, None, 0) B_GA_TRAIN = Beta('B_GA_TRAIN', 0, None, None, 0) B_GA_SM = Beta('B_GA_SM', 0, None, None, 0) #First B_FIRST_TRAIN = Beta('B_FIRST_TRAIN', 0, None, None, 0) B_FIRST_SM = Beta('B_FIRST_SM', 0, None, None, 0) B_FIRST = Beta('B_FIRST', 0, None, None, 0) ##Non linearization #Cost q_COST = Beta('q_COST', 1, None, None, 0) #Time q_TIME = Beta('q_TIME', 1, None, None, 0) q_TIME_TRAIN = Beta('q_TIME_TRAIN', 1, None, None, 0) q_TIME_SM = Beta('q_TIME_SM', 1, None, None, 0) q_TIME_CAR = Beta('q_TIME_CAR', 1, None, None, 0) q_TIME_PUB = Beta('q_TIME_PUB', 1, None, None, 0) #HE q_HE = Beta('q_HE', 1, None, None, 0) ##Nesting parameter MU = Beta('MU', 1, 0, 1, 0) ##ML RANDOM GENERIC TIME LOGNORMAL BETA_TIME_mean = Beta('BETA_TIME_mean', 0, None, None, 0) BETA_TIME_std = Beta('BETA_TIME_std', 1, None, None, 0) BETA_TIME_random = -exp(BETA_TIME_mean + BETA_TIME_std * bioDraws('BETA_TIME_random', 'NORMAL')) ##ML RANDOM SPECIFIC TIME TRAIN LOGNORMAL BETA_TIME_TRAIN_mean = Beta('BETA_TIME_TRAIN_mean', 0, None, None, 0) BETA_TIME_TRAIN_std = Beta('BETA_TIME_TRAIN_std', 1, None, None, 0) BETA_TIME_TRAIN_random = -exp(BETA_TIME_TRAIN_mean + BETA_TIME_TRAIN_std * bioDraws('BETA_TIME_TRAIN_random', 'NORMAL')) ##ML RANDOM SPECIFIC TIME SM LOGNORMAL BETA_TIME_SM_mean = Beta('BETA_TIME_SM_mean', 0, None, None, 0) BETA_TIME_SM_std = Beta('BETA_TIME_SM_std', 1, None, None, 0) BETA_TIME_SM_random = -exp(BETA_TIME_SM_mean + BETA_TIME_SM_std * bioDraws('BETA_TIME_SM_random', 'NORMAL')) ##ML RANDOM SPECIFIC TIME CAR LOGNORMAL BETA_TIME_CAR_mean = Beta('BETA_TIME_CAR_mean', 0, None, None, 0) BETA_TIME_CAR_std = Beta('BETA_TIME_CAR_std', 1, None, None, 0) BETA_TIME_CAR_random = -exp(BETA_TIME_CAR_mean + BETA_TIME_CAR_std * bioDraws('BETA_TIME_CAR_random', 'NORMAL')) ##ML RANDOM GENERIC COST LOGNORMAL BETA_COST_mean = Beta('BETA_COST_mean', 0, None, None, 0) BETA_COST_std = Beta('BETA_COST_std', 1, None, None, 0) BETA_COST_random = -exp(BETA_COST_mean + BETA_COST_std * bioDraws('BETA_COST_random', 'NORMAL')) ##ML RANDOM GENERIC HE LOGNORMAL BETA_HE_mean = Beta('BETA_HE_mean', 0, None, None, 0) BETA_HE_std = Beta('BETA_HE_std', 1, None, None, 0) BETA_HE_random = -exp(BETA_HE_mean + BETA_HE_std * bioDraws('BETA_HE_random', 'NORMAL')) ##ML RANDOM GENERIC TIME NORMAL BETA_TIME_mean_Norm = Beta('BETA_TIME_mean_Norm', 0, None, None, 0) BETA_TIME_std_Norm = Beta('BETA_TIME_std_Norm', 1, None, None, 0) BETA_TIME_random_Norm = BETA_TIME_mean_Norm + BETA_TIME_std_Norm * bioDraws( 'BETA_TIME_random_Norm', 'NORMAL') ##ML RANDOM SPECIFIC TIME TRAIN LOGNORMAL BETA_TIME_TRAIN_mean_Norm = Beta('BETA_TIME_TRAIN_mean_Norm', 0, None, None, 0) BETA_TIME_TRAIN_std_Norm = Beta('BETA_TIME_TRAIN_std_Norm', 1, None, None, 0) BETA_TIME_TRAIN_random_Norm = BETA_TIME_TRAIN_mean_Norm + BETA_TIME_TRAIN_std_Norm * bioDraws( 'BETA_TIME_TRAIN_random_Norm', 'NORMAL') ##ML RANDOM SPECIFIC TIME SM NORMAL BETA_TIME_SM_mean_Norm = Beta('BETA_TIME_SM_mean_Norm', 0, None, None, 0) BETA_TIME_SM_std_Norm = Beta('BETA_TIME_SM_std_Norm', 1, None, None, 0) BETA_TIME_SM_random_Norm = BETA_TIME_SM_mean_Norm + BETA_TIME_SM_std_Norm * bioDraws( 'BETA_TIME_SM_random_Norm', 'NORMAL') ##ML RANDOM SPECIFIC TIME CAR NORMAL BETA_TIME_CAR_mean_Norm = Beta('BETA_TIME_CAR_mean_Norm', 0, None, None, 0) BETA_TIME_CAR_std_Norm = Beta('BETA_TIME_CAR_std_Norm', 1, None, None, 0) BETA_TIME_CAR_random_Norm = BETA_TIME_CAR_mean_Norm + BETA_TIME_CAR_std_Norm * bioDraws( 'BETA_TIME_CAR_random_Norm', 'NORMAL') ##ML RANDOM GENERIC COST LOGNORMAL BETA_COST_PUB_mean = Beta('BBETA_COST_PUB_mean', 0, None, None, 0) BETA_COST_PUB_std = Beta('BBETA_COST_PUB_std', 1, None, None, 0) BETA_COST_PUB_random = -exp(BETA_COST_PUB_mean + BETA_COST_PUB_std * bioDraws('BBETA_COST_PUB_random', 'NORMAL')) ##ML RANDOM GENERIC COST NORMAL BETA_COST_mean_Norm = Beta('BETA_COST_mean_Norm', 0, None, None, 0) BETA_COST_std_Norm = Beta('BETA_COST_std_Norm', 1, None, None, 0) BETA_COST_random_Norm = BETA_COST_mean_Norm + BETA_COST_std_Norm * bioDraws( 'BETA_COST_random_Norm', 'NORMAL') ##ML RANDOM GENERIC HE NORMAL BETA_HE_mean_Norm = Beta('BETA_HE_mean_Norm', 0, None, None, 0) BETA_HE_std_Norm = Beta('BETA_HE_std_Norm', 1, None, None, 0) BETA_HE_random_Norm = BETA_HE_mean_Norm + BETA_HE_std_Norm * bioDraws( 'BETA_HE_random_Norm', 'NORMAL') ''' *********************************************************************************************** ''' #PUBLIC TRAIN_COST_SCALED = DefineVariable('TRAIN_COST_SCALED',\ TRAIN_COST / COST_SCALE_PUB,db_train) SM_COST_SCALED = DefineVariable('SM_COST_SCALED', SM_COST / COST_SCALE_PUB, db_train) #CAR CAR_COST_SCALED = DefineVariable('CAR_COST_SCALED', CAR_CO / COST_SCALE_CAR, db_train) ''' *********************************************************************************************** ''' ##Scaling 'COST', 'TRAVEL-TIME' and 'HE' by a factor of 100 and adding the scaled variables to the database TRAIN_TT_SCALED = DefineVariable('TRAIN_TT_SCALED',\ TRAIN_TT / 100.0,db_train) TRAIN_HE_SCALED = DefineVariable('TRAIN_HE_SCALED',\ TRAIN_HE / 100, db_train) SM_TT_SCALED = DefineVariable('SM_TT_SCALED', SM_TT / 100.0, db_train) SM_HE_SCALED = DefineVariable('SM_HE_SCALED',\ SM_HE / 100, db_train) CAR_TT_SCALED = DefineVariable('CAR_TT_SCALED', CAR_TT / 100, db_train) ###Defining new variables and adding columns to the database #Age AGE_1 = DefineVariable('AGE_1', (AGE == 1), db_train) #don't scale because is cathegorical AGE_2 = DefineVariable('AGE_2', (AGE == 2), db_train) AGE_3 = DefineVariable('AGE_3', (AGE == 3), db_train) AGE_4 = DefineVariable('AGE_4', (AGE == 4), db_train) AGE_5 = DefineVariable('AGE_5', (AGE == 5), db_train) AGE_6 = DefineVariable('AGE_6', (AGE == 6), db_train) #Purpose PRIVATE = DefineVariable("PRIVATE", (PURPOSE == 1), db_train) COMMUTER = DefineVariable("COMMUTER", (PURPOSE == 2), db_train) BUSINESS = DefineVariable("BUSINESS", (PURPOSE == 3), db_train) #Model Estimation av = {3: CAR_AV_SP, 1: TRAIN_AV_SP, 2: SM_AV} obsprob = exp(models.loglogit(V, av, CHOICE)) condprobIndiv = PanelLikelihoodTrajectory(obsprob) logprob = log(MonteCarlo(condprobIndiv)) bg = bio.BIOGEME(db_train, logprob, numberOfDraws=Draws) bg.modelName = ModName result = bg.estimate() return result
# pylint: disable=invalid-name, undefined-variable import pandas as pd import biogeme.database as db import biogeme.biogeme as bio from biogeme.expressions import exp, bioDraws, MonteCarlo # We create a fake database with one entry, as it is required # to store the draws pandas = pd.DataFrame() pandas['FakeColumn'] = [1.0] database = db.Database('fakeDatabase', pandas) integrand = exp(bioDraws('U', 'UNIFORM')) simulatedI = MonteCarlo(integrand) trueI = exp(1.0) - 1.0 R = 2000 sampleVariance = MonteCarlo(integrand * integrand) - simulatedI * simulatedI stderr = (sampleVariance / R)**0.5 error = simulatedI - trueI simulate = { 'Analytical Integral': trueI, 'Simulated Integral': simulatedI, 'Sample variance ': sampleVariance, 'Std Error ': stderr, 'Error ': error
THE_B_TIME_RND * 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 av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} # The choice model is a logit, with availability conditions integrand = models.logit(V, av, CHOICE) return integrand numericalI = Integrate(logit(B_TIME_RND) * density, 'omega') normal = MonteCarlo(logit(B_TIME_RND_normal)) anti = MonteCarlo(logit(B_TIME_RND_anti)) halton = MonteCarlo(logit(B_TIME_RND_halton)) mlhs = MonteCarlo(logit(B_TIME_RND_mlhs)) antimlhs = MonteCarlo(logit(B_TIME_RND_antimlhs)) simulate = { 'Numerical': numericalI, 'MonteCarlo': normal, 'Antithetic': anti, 'Halton': halton, 'MLHS': mlhs, 'Antithetic MLHS': antimlhs } R = 20000