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 lognormalpdf(x, mu=0.0, s=1.0): """ Log normal pdf Probability density function of a log normal distribution .. math:: f(x;\\mu, \\sigma) = \\frac{1}{x\\sigma \\sqrt{2\\pi}} \\exp{-\\frac{(\\ln x-\\mu)^2}{2\\sigma^2}} :param x: location parameter :math:`\\mu` of the lognormal distribution. Default: 0. :type x: float or biogeme.expression :param s: scale parameter :math:`\\sigma` of the lognormal distribution. Default: 1. :type s: float or biogeme.expression :note: It is assumed that :math:`\\sigma > 0`, but it is not verified by the code. :return: value of the lognormal pdf. :rtype: float or biogeme.expression """ d = -(log(x) - mu) * (log(x) - mu) n = 2.0 * s * s a = d / n num = exp(a) den = x * s * 2.506628275 p = (x > 0) * num / den return p
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)
def loglikelihoodregression(meas, model, sigma): """Computes log likelihood function of a regression model. :param meas: An expression providing the value :math:`y` of the measure for the current observation. :type meas: biogeme.expressions.Expression :param model: An expression providing the output :math:`m` of the model for the current observation. :type model: biogeme.expressions.Expression :param sigma: An expression (typically, a parameter) providing the standard error :math:`\\sigma` of the error term. :type sigma: biogeme.expressions.Expression :return: the likelihood of the regression, assuming a normal distribution, that is .. math:: -\\left( \\frac{(y-m)^2}{2\\sigma^2} \\right) - \\log(\\sigma) - \\frac{1}{2}\\log(2\\pi) :rtype: biogeme.expressions.Expression """ t = (meas - model) / sigma f = -(t**2) / 2 - log(sigma) - 0.9189385332 return f
def loglikelihood(prob): """ Simply computes the log of the probability :param prob: An expression providing the value of the probability. :type prob: biogeme.expressions.Expression :return: the logarithm of the probability. :rtype: biogeme.expressions.Expression """ return log(prob)
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(Integrate(condprobIndiv * density, 'omega')) class test_12integral(unittest.TestCase): def testEstimation(self): biogeme = bio.BIOGEME(database, logprob) results = biogeme.estimate() self.assertAlmostEqual(results.data.logLike, -4359.520517074624, 2) if __name__ == '__main__': unittest.main()
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} # Conditional to omega, we have a logit model (called the kernel) condprob = models.logit(V, av, CHOICE) # We integrate over omega using numerical integration logprob = log(Integrate(condprob * dx / (b - a), 'omega')) # 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 = '06unifMixtureIntegral' # Estimate the parameters results = biogeme.estimate() pandasResults = results.getEstimatedParameters()
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)
# Conditional to omega, we have a logit model (called the kernel) for the choice condprob = models.logit(V, None, Choice) # Conditional to omega, 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 numerical integration loglike = log(Integrate(condlike * density, 'omega')) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed() # Create the Biogeme object biogeme = bio.BIOGEME(database, loglike) biogeme.modelName = '05latentChoiceFull' # Estimate the parameters results = biogeme.estimate()
CAR_CO_SCALED = DefineVariable('CAR_CO_SCALED', CAR_CO / 100, database) # Definition of the utility functions # We estimate a binary probit model. There are only two alternatives. V1 = B_TIME * TRAIN_TT_SCALED + \ B_COST * TRAIN_COST_SCALED V3 = ASC_CAR + \ B_TIME * CAR_TT_SCALED + \ B_COST * CAR_CO_SCALED # Associate choice probability with the numbering of alternatives P = {1: bioNormalCdf(V1 - V3), 3: bioNormalCdf(V3 - V1)} # Definition of the model. This is the contribution of each # observation to the log likelihood function. logprob = log(Elem(P, 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 = '21probit' # Estimate the parameters results = biogeme.estimate() pandasResults = results.getEstimatedParameters()
# 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.
V1 = ASC_TRAIN + B_TIME_RND * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED 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} # The choice model is a logit, with availability conditions condprob = models.logit(V, av, CHOICE) prob = Integrate(condprob * dx / (b - a), 'omega') logprob = log(prob) class test_06integral(unittest.TestCase): def testEstimation(self): biogeme = bio.BIOGEME(database, logprob) results = biogeme.estimate() self.assertAlmostEqual(results.data.logLike, -5215.072, 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)
SM_COST = SM_CO * (GA == 0) TRAIN_COST = TRAIN_CO * (GA == 0) TRAIN_TT_SCALED = DefineVariable('TRAIN_TT_SCALED',\ TRAIN_TT / 100.0,database) TRAIN_COST_SCALED = DefineVariable('TRAIN_COST_SCALED',\ TRAIN_COST / 100,database) SM_TT_SCALED = DefineVariable('SM_TT_SCALED', SM_TT / 100.0, database) SM_COST_SCALED = DefineVariable('SM_COST_SCALED', SM_COST / 100, database) CAR_TT_SCALED = DefineVariable('CAR_TT_SCALED', CAR_TT / 100, database) CAR_CO_SCALED = DefineVariable('CAR_CO_SCALED', CAR_CO / 100, database) # Biogeme cannot compute the log of 0. Therefore, whenever the cost # is 0, the log of 1 computed instead. LOG_CAR_COST = DefineVariable('LOG_CAR_COST', (CAR_CO_SCALED != 0) * log(CAR_CO_SCALED + 1 * (CAR_CO_SCALED == 0)), database) LOG_TRAIN_COST = DefineVariable('LOG_TRAIN_COST', (TRAIN_COST_SCALED != 0) * log(TRAIN_COST_SCALED + 1 * (TRAIN_COST_SCALED == 0)), database) LOG_SM_COST = DefineVariable('LOG_SM_COST', (SM_COST_SCALED != 0) * log(SM_COST_SCALED + 1 * (SM_COST_SCALED == 0)), database) V1 = ASC_TRAIN + B_TIME * TRAIN_TT_SCALED + B_COST * LOG_TRAIN_COST V2 = ASC_SM + B_TIME * SM_TT_SCALED + B_COST * LOG_SM_COST V3 = ASC_CAR + B_TIME * CAR_TT_SCALED + B_COST * LOG_CAR_COST # Associate utility functions with the numbering of alternatives V = {1: V1, 2: V2, 3: V3}
def testEstimation(self): biogeme = bio.BIOGEME(database, log(prob)) results = biogeme.estimate() self.assertAlmostEqual(results.data.logLike, -986.1888, 2)
# 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() # Create the Biogeme object biogeme = bio.BIOGEME(database, logprob, numberOfDraws=100000) biogeme.modelName = '15panelDiscreteBis' # As the estimation may take a while and risk to be interrupted, we save the iterations, # and restore them before the estimation. fname = "__15panelDiscreteBis.iters"
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} # Associate the availability conditions with the alternatives. # In this example all alternatives are available # for each individual. av = {0: 1, 1: 1, 2: 1} # The choice model is a logit, conditional to # the value of the latent variable condprob = models.logit(V, av, Choice) prob = Integrate(condprob * density, 'omega') loglike = log(prob) class test_03(unittest.TestCase): def testEstimation(self): biogeme = bio.BIOGEME(database, loglike) results = biogeme.estimate() self.assertAlmostEqual(results.data.logLike, -1076.6955020804955, 2) if __name__ == '__main__': unittest.main()
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
Opt2_SchedDelayEarly = DefineVariable('Opt2_SchedDelayEarly', (-SchedDelay_2 * (SchedDelay_2 < 0)) / 60, database) Opt3_SchedDelayEarly = DefineVariable('Opt3_SchedDelayEarly', (-SchedDelay_3 * (SchedDelay_3 < 0)) / 60, database) Opt1_SchedDelayLate = DefineVariable('Opt1_SchedDelayLate', (SchedDelay_1 * (SchedDelay_1 > 0)) / 60, database) Opt2_SchedDelayLate = DefineVariable('Opt2_SchedDelayLate', (SchedDelay_2 * (SchedDelay_2 > 0)) / 60, database) Opt3_SchedDelayLate = DefineVariable('Opt3_SchedDelayLate', (SchedDelay_3 * (SchedDelay_3 > 0)) / 60, database) LogFare_1 = DefineVariable('LogFare_1', log(Fare_1), database) LogFare_2 = DefineVariable('LogFare_2', log(Fare_2), database) LogFare_3 = DefineVariable('LogFare_3', log(Fare_3), database) # Utilities Opt1 = Constant1 + LogFare * LogFare_1 + Legroom * Legroom_1 + SchedDE * Opt1_SchedDelayEarly + SchedDL * Opt1_SchedDelayLate + Total_TT1 * TripTimeHours_1 Opt2 = Constant2 + LogFare * LogFare_2 + Legroom * Legroom_2 + SchedDE * Opt2_SchedDelayEarly + SchedDL * Opt2_SchedDelayLate + Total_TT2 * TripTimeHours_2 Opt3 = Constant3 + LogFare * LogFare_3 + Legroom * Legroom_3 + SchedDE * Opt3_SchedDelayEarly + SchedDL * Opt3_SchedDelayLate + Total_TT3 * TripTimeHours_3 V = {1: Opt1, 2: Opt2, 3: Opt3} av = {1: 1, 2: 1, 3: 1} # The choice model is a logit, with availability conditions logprob = loglogit(V, av, chosenAlternative) biogeme = bio.BIOGEME(database, logprob) biogeme.modelName = "logit_airline_log" results = biogeme.estimate()
Mobil17_tau_4 = (tau_4 - MODEL_Mobil17) / SIGMA_STAR_Mobil17 IndMobil17 = { 1: bioNormalCdf(Mobil17_tau_1), 2: bioNormalCdf(Mobil17_tau_2) - bioNormalCdf(Mobil17_tau_1), 3: bioNormalCdf(Mobil17_tau_3) - bioNormalCdf(Mobil17_tau_2), 4: bioNormalCdf(Mobil17_tau_4) - bioNormalCdf(Mobil17_tau_3), 5: 1 - bioNormalCdf(Mobil17_tau_4), 6: 1.0, -1: 1.0, -2: 1.0 } P_Mobil17 = Elem(IndMobil17, Mobil17) loglike = log(P_Envir01) + \ log(P_Envir02) + \ log(P_Envir03) + \ log(P_Mobil11) + \ log(P_Mobil14) + \ log(P_Mobil16) + \ log(P_Mobil17) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed() # Create the Biogeme object
# 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)}')
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(Integrate(condprob * density, 'omega')) # Define level of verbosity logger = msg.bioMessage() #logger.setSilent() #logger.setWarning() logger.setGeneral() #logger.setDetailed() # Create the Biogeme object biogeme = bio.BIOGEME(database, loglike) biogeme.modelName = '04latentChoiceSeq' # Estimate the parameters results = biogeme.estimate()
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()
U = B_TIME * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED # Associate each discrete indicator with an interval. # 1: -infinity -> tau1 # 2: tau1 -> tau2 # 3: tau2 -> +infinity ChoiceProba = { 1: 1 - dist.logisticcdf(U - tau1), 2: dist.logisticcdf(U - tau1) - dist.logisticcdf(U - tau2), 3: dist.logisticcdf(U - tau2) } # Definition of the model. This is the contribution of each # observation to the log likelihood function. logprob = log(Elem(ChoiceProba, 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 = '18ordinalLogit' # Estimate the parameters results = biogeme.estimate() pandasResults = results.getEstimatedParameters()