def setUp(self): np.random.seed(90267) rnd.seed(90267) Choice = Variable('Choice') Variable1 = Variable('Variable1') Variable2 = Variable('Variable2') beta1 = Beta('beta1', 0, None, None, 0) beta2 = Beta('beta2', 0, None, None, 0) V1 = beta1 * Variable1 V2 = beta2 * Variable2 V3 = 0 V = {1: V1, 2: V2, 3: V3} likelihood = models.loglogit(V, av=None, i=Choice) self.myBiogeme = bio.BIOGEME(myData1, likelihood) self.myBiogeme.modelName = 'simpleExample' self.theFunction = rosenbrock()
def setUp(self): np.random.seed(90267) rnd.seed(90267) Variable1 = Variable('Variable1') Variable2 = Variable('Variable2') beta1 = Beta('beta1', -1.0, -3, 3, 0) beta2 = Beta('beta2', 2.0, -3, 10, 0) likelihood = -beta1**2 * Variable1 - exp( beta2 * beta1) * Variable2 - beta2**4 simul = beta1 / Variable1 + beta2 / Variable2 dictOfExpressions = { 'loglike': likelihood, 'beta1': beta1, 'simul': simul } self.myBiogeme = bio.BIOGEME(myData1, dictOfExpressions) self.myBiogeme.modelName = 'simpleExample'
def testEstimation(self): AutoTime = Variable('AutoTime') TransitTime = Variable('TransitTime') Choice = Variable('Choice') ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) B_TIME = Beta('B_TIME', 0, None, None, 0) V = {0: ASC_CAR + B_TIME * AutoTime, 1: B_TIME * TransitTime} av = {0: 1, 1: 1} logprob = models.loglogit(V, av, Choice) biogeme = bio.BIOGEME(self.database, logprob) biogeme.modelName = 'test' biogeme.generateHtml = False results = biogeme.estimate() self.assertAlmostEqual(results.data.logLike, -6.166042, 2)
def run_simulation(data_file_directory_for_simulation, data_file_name_for_simulation, output_directory_for_simulation, betas, household_income_limit): """ :author: Antonin Danalet, based on the example '01logit_simul.py' by Michel Bierlaire, EPFL, on biogeme.epfl.ch Simulation with a binary logit model. Two alternatives: work from home at least some times, or not.""" # Read the data df_persons = pd.read_csv(data_file_directory_for_simulation / data_file_name_for_simulation, ';') database = db.Database('persons', df_persons) # The following statement allows you to use the names of the variable as Python variable. globals().update(database.variables) # Parameters to be estimated alternative_specific_constant = Beta('alternative_specific_constant', 0, None, None, 0) b_no_post_school_education = Beta('b_no_post_school_education', 0, None, None, 0) b_secondary_education = Beta('b_secondary_education', 0, None, None, 0) b_tertiary_education = Beta('b_tertiary_education', 0, None, None, 0) b_university = Beta('b_university', 0, None, None, 1) b_male = Beta('b_male', 0, None, None, 0) b_public_transport_connection_quality_na_home = Beta('b_public_transport_connection_quality_na_home', 0, None, None, 0) b_public_transport_connection_quality_a_work = Beta('b_public_transport_connection_quality_are_a_work', 0, None, None, 1) b_rural_work = Beta('b_rural_work', 0, None, None, 0) b_home_work_distance = Beta('b_home_work_distance', 0, None, None, 0) b_business_sector_agriculture = Beta('b_business_sector_agriculture', 0, None, None, 0) b_business_sector_production = Beta('b_business_sector_production', 0, None, None, 0) b_business_sector_wholesale = Beta('b_business_sector_wholesale', 0, None, None, 1) b_business_sector_retail = Beta('b_business_sector_retail', 0, None, None, 0) b_business_sector_gastronomy = Beta('b_business_sector_gastronomy', 0, None, None, 0) b_business_sector_finance = Beta('b_business_sector_finance', 0, None, None, 1) b_business_sector_services_fc = Beta('b_business_sector_services_fc', 0, None, None, 0) b_business_sector_other_services = Beta('b_business_sector_other_services', 0, None, None, 1) b_business_sector_others = Beta('b_business_sector_others', 0, None, None, 1) b_business_sector_non_movers = Beta('b_business_sector_non_movers', 0, None, None, 0) b_executives = Beta('b_executives', 0, None, None, 0) b_german = Beta('b_german', 0, None, None, 0) b_hh_income_8000_or_less = Beta('b_hh_income_8000_or_less', 0, None, None, 0) # Definition of new variables no_post_school_educ = education == 1 secondary_education = education == 2 tertiary_education = education == 3 university = education == 4 male = (sex == 1) public_transport_quality_NA_home = (public_transport_connection_quality_ARE_home == 5) public_transport_quality_A_work = (public_transport_connection_quality_ARE_work == 1) home_work_distance = (home_work_crow_fly_distance * (home_work_crow_fly_distance >= 0.0) / 100000.0) business_sector_agriculture = type_1 == 1 business_sector_retail = type_1 == 4 business_sector_gastronomy = type_1 == 5 business_sector_finance = type_1 == 6 business_sector_production = type_1 == 2 business_sector_wholesale = type_1 == 3 business_sector_services_fC = type_1 == 7 business_sector_other_services = type_1 == 8 business_sector_others = type_1 == 9 business_sector_non_movers = type_1 == 10 german = language == 1 nationality_switzerland = nation == 0 nationality_germany_austria = nation == 1 nationality_italy_vatican = nation == 2 nationality_france_monaco_s_marino = nation == 3 nationality_northwestern_europe = nation == 4 nationality_eastern_europe = nation == 7 hh_income_8000_or_less = hh_income < household_income_limit executives = (0 < position_in_bus) * (position_in_bus < 19) rural_work = urban_rural_typology_work == 3 # Utility utility_function_telecommuting = alternative_specific_constant + \ b_executives * executives + \ b_no_post_school_education * no_post_school_educ + \ b_secondary_education * secondary_education + \ b_tertiary_education * tertiary_education + \ b_university * university + \ b_male * male + \ b_public_transport_connection_quality_na_home * public_transport_quality_NA_home + \ b_public_transport_connection_quality_a_work * public_transport_quality_A_work + \ b_rural_work * rural_work + \ b_home_work_distance * home_work_distance + \ models.piecewiseFormula(age, [0, 20, 35, 75, 200]) + \ b_business_sector_agriculture * business_sector_agriculture + \ b_business_sector_retail * business_sector_retail + \ b_business_sector_gastronomy * business_sector_gastronomy + \ b_business_sector_finance * business_sector_finance + \ b_business_sector_production * business_sector_production + \ b_business_sector_wholesale * business_sector_wholesale + \ b_business_sector_services_fc * business_sector_services_fC + \ b_business_sector_other_services * business_sector_other_services + \ b_business_sector_others * business_sector_others + \ b_business_sector_non_movers * business_sector_non_movers + \ b_german * german + \ b_nationality_ch_germany_france_italy_nw_e * nationality_switzerland + \ b_nationality_ch_germany_france_italy_nw_e * nationality_germany_austria + \ b_nationality_ch_germany_france_italy_nw_e * nationality_italy_vatican + \ b_nationality_ch_germany_france_italy_nw_e * nationality_france_monaco_s_marino + \ b_nationality_ch_germany_france_italy_nw_e * nationality_northwestern_europe + \ b_nationality_ch_germany_france_italy_nw_e * nationality_eastern_europe + \ models.piecewiseFormula(work_percentage, [0, 90, 101]) + \ b_hh_income_8000_or_less * hh_income_8000_or_less utility_function_no_telecommuting = 0 # Associate utility functions with the numbering of alternatives utility_functions_with_numbering_of_alternatives = {1: utility_function_telecommuting, # Yes or sometimes 3: utility_function_no_telecommuting} # No availability_conditions = {1: 1, # Always available 3: 1} # Always available # The choice model is a logit, with availability conditions prob_telecommuting = models.logit(utility_functions_with_numbering_of_alternatives, availability_conditions, 1) prob_no_telecommuting = models.logit(utility_functions_with_numbering_of_alternatives, availability_conditions, 3) simulate = {'Prob. telecommuting': prob_telecommuting, 'Prob. no telecommuting': prob_no_telecommuting} # Create the Biogeme object biogeme = bio.BIOGEME(database, simulate) biogeme.modelName = 'logit_telecommuting_simul' # Define level of verbosity logger = msg.bioMessage() # logger.setSilent() logger.setWarning() # logger.setGeneral() # logger.setDetailed() # Get the betas from the estimation (without corrections) # path_to_estimation_folder = Path('../data/output/models/estimation/') # if os.path.isfile(path_to_estimation_folder / 'logit_telecommuting~00.pickle'): # raise Exception('There are several model outputs! Careful.') # results = res.bioResults(pickleFile=path_to_estimation_folder / 'logit_telecommuting.pickle') # betas_without_correction = results.getBetaValues() # Change the working directory, so that biogeme writes in the correct folder, i.e., where this file is standard_directory = os.getcwd() os.chdir(output_directory_for_simulation) results = biogeme.simulate(theBetaValues=betas) # print(results.describe()) df_persons = pd.concat([df_persons, results], axis=1) # Go back to the normal working directory os.chdir(standard_directory) # For unemployed people, fix probability of doing some home office to 0 (and probability of not doing to 1). df_persons.loc[df_persons.employed == 0, 'Prob. telecommuting'] = 0.0 # Unemployed people df_persons.loc[df_persons.employed == 0, 'Prob. no telecommuting'] = 1.0 # Unemployed people df_persons.loc[df_persons.employed == -99, 'Prob. telecommuting'] = 0.0 # Other people df_persons.loc[df_persons.employed == -99, 'Prob. no telecommuting'] = 1.0 # Other people # By definition, apprentices don't work from home (because they were not asked in the MTMC) df_persons.loc[df_persons.position_in_bus == 3, 'Prob. telecommuting'] = 0.0 df_persons.loc[df_persons.position_in_bus == 3, 'Prob. no telecommuting'] = 1.0 # Add a realisation of the probability df_persons['random 0/1'] = np.random.rand(len(df_persons)) df_persons['telecommuting_model'] = np.where(df_persons['random 0/1'] < df_persons['Prob. telecommuting'], 1, 0) del df_persons['random 0/1'] ''' Save the file ''' data_file_name = 'persons_from_SynPop_with_probability_telecommuting.csv' df_persons.to_csv(output_directory_for_simulation / data_file_name, sep=',', index=False)
# The following statement allows you to use the names of the variable # as Python variable. globals().update(database.variables) # 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) # Here we use the "biogeme" way for backward compatibility exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0 database.remove(exclude) # Parameters to be estimated ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_SM = Beta('ASC_SM', 0, None, None, 1) B_TIME = Beta('B_TIME', 0, None, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) # If the lower bound is set to zero, the model cannot be evaluated. # Therefore, we set the lower bound to a small number, strictly larger than zero. MU = Beta('MU', 0.5, 0.000001, 1.0, 0) # Definition of new variables SM_COST = SM_CO * (GA == 0) TRAIN_COST = TRAIN_CO * (GA == 0) # Definition of new variables: adding columns to the database CAR_AV_SP = DefineVariable('CAR_AV_SP', CAR_AV * (SP != 0), database)
# The Pandas data structure is available as database.data. Use all the # Pandas functions to invesigate the database #print(database.data.describe()) globals().update(database.variables) # 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) # Here we use the "biogeme" way for backward compatibility exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0 database.remove(exclude) ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_SM = Beta('ASC_SM', 0, None, None, 1) B_TIME = Beta('B_TIME', 0, None, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) B_TIME_S = Beta('B_TIME_S', 0, None, None, 0) # Define a random parameter, normally distirbuted, designed to be used # for Monte-Carlo simulation B_TIME_RND = B_TIME + B_TIME_S * bioDraws('B_TIME_RND', 'NORMAL') SM_COST = SM_CO * (GA == 0) TRAIN_COST = TRAIN_CO * (GA == 0) TRAIN_TT_SCALED = DefineVariable('TRAIN_TT_SCALED',\
age_65_more = DefineVariable('age_65_more', age >= Numeric(65), database) moreThanOneCar = DefineVariable('moreThanOneCar', NbCar > 1, database) moreThanOneBike = DefineVariable('moreThanOneBike', NbBicy > 1, database) individualHouse = DefineVariable('individualHouse',\ HouseType == 1,database) male = DefineVariable('male', Gender == 1, database) haveChildren = DefineVariable('haveChildren',\ ((FamilSitu == 3)+(FamilSitu == 4)) > 0,database) haveGA = DefineVariable('haveGA', GenAbST == 1, database) highEducation = DefineVariable('highEducation', Education >= 6, database) ### Coefficients # Read the estimates from the structural equation estimation, and use # them as starting values coef_age_65_more = Beta('coef_age_65_more', 0.03623576587917444, None, None, 0) coef_haveChildren = Beta('coef_haveChildren', -0.027979246306050195, None, None, 0) coef_haveGA = Beta('coef_haveGA', -0.749193972835575, None, None, 0) coef_highEducation = Beta('coef_highEducation', -0.25961437893330336, None, None, 0) coef_individualHouse = Beta('coef_individualHouse', -0.1162986643952338, None, None, 0) coef_intercept = Beta('coef_intercept', 0.3534782074218524, None, None, 0) coef_male = Beta('coef_male', 0.07950619870640736, None, None, 0) coef_moreThanOneBike = Beta('coef_moreThanOneBike', -0.36347727873174407, None, None, 0) coef_moreThanOneCar = Beta('coef_moreThanOneCar', 0.714788103177542, None, None, 0) ### Latent variable: structural equation
coef_highEducation = -0.24726576867313482 coef_individualHouse = -0.08887159771570047 coef_intercept = 0.40149819890908217 coef_male = 0.0661412838697794 coef_moreThanOneBike = -0.2776091744681671 coef_moreThanOneCar = 0.5335541575826122 ### Latent variable: structural equation # Note that the expression must be on a single line. In order to # write it across several lines, each line must terminate with # the \ symbol omega = RandomVariable('omega') density = dist.normalpdf(omega) sigma_s = Beta('sigma_s', 0.8625193422179722, None, None, 0) CARLOVERS = \ coef_intercept +\ coef_age_65_more * age_65_more +\ formulaIncome+\ coef_moreThanOneCar * moreThanOneCar +\ coef_moreThanOneBike * moreThanOneBike +\ coef_individualHouse * individualHouse +\ coef_male * male +\ coef_haveChildren * haveChildren +\ coef_haveGA * haveGA +\ coef_highEducation * highEducation +\ sigma_s * omega
from biogeme.expressions import Beta, DefineVariable, bioLinearUtility from statistics import * # Read the data df = pd.read_csv('../data/optima.dat', sep='\t') database = db.Database('optima', df) # The following statement allows you to use the names of the variable # as Python variable. globals().update(database.variables) # Exclude observations such that the chosen alternative is -1 database.remove(Choice == -1.0) # List of parameters to be estimated 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) # Definition of variables: # For numerical reasons, it is good practice to scale the data to # that the values of the parameters are around 1.0. ##Method 1 from python """ TimePT_scaled = TimePT / 200
formulaIncome = models.piecewiseFormula(ScaledIncome, thresholds, [0.0, 0.0, 0.0, 0.0, 0.0]) # Definition of other variables age_65_more = DefineVariable('age_65_more', age >= 65, database) moreThanOneCar = DefineVariable('moreThanOneCar', NbCar > 1, database) moreThanOneBike = DefineVariable('moreThanOneBike', NbBicy > 1, database) individualHouse = DefineVariable('individualHouse', HouseType == 1, database) male = DefineVariable('male', Gender == 1, database) haveChildren = DefineVariable('haveChildren', \ ((FamilSitu == 3) + (FamilSitu == 4)) > 0, database) haveGA = DefineVariable('haveGA', GenAbST == 1, database) highEducation = DefineVariable('highEducation', Education >= 6, database) # Parameters to be estimated coef_intercept = Beta('coef_intercept', 0.0, None, None, 1) coef_age_65_more = Beta('coef_age_65_more', 0.0, None, None, 0) coef_age_unknown = Beta('coef_age_unknown', 0.0, None, None, 0) coef_haveGA = Beta('coef_haveGA', 0.0, None, None, 0) coef_moreThanOneCar = Beta('coef_moreThanOneCar', 0.0, None, None, 0) coef_moreThanOneBike = Beta('coef_moreThanOneBike', 0.0, None, None, 0) coef_individualHouse = Beta('coef_individualHouse', 0.0, None, None, 0) coef_male = Beta('coef_male', 0.0, None, None, 0) coef_haveChildren = Beta('coef_haveChildren', 0.0, None, None, 0) coef_highEducation = Beta('coef_highEducation', 0.0, None, None, 0) ### Latent variable: structural equation # Note that the expression must be on a single line. In order to # write it across several lines, each line must terminate with # the \ symbol
thresholds, [0.0, 0.0, 0.0, 0.0, 0.0]) # Definition of other variables age_65_more = DefineVariable('age_65_more', age >= 65, database) moreThanOneCar = DefineVariable('moreThanOneCar', NbCar > 1, database) moreThanOneBike = DefineVariable('moreThanOneBike', NbBicy > 1, database) individualHouse = DefineVariable('individualHouse', HouseType == 1, database) male = DefineVariable('male', Gender == 1, database) haveChildren = DefineVariable('haveChildren', \ ((FamilSitu == 3) + (FamilSitu == 4)) > 0, database) haveGA = DefineVariable('haveGA', GenAbST == 1, database) highEducation = DefineVariable('highEducation', Education >= 6, database) # Parameters to be estimated coef_intercept = Beta('coef_intercept', 0.0, None, None, 0) coef_age_65_more = Beta('coef_age_65_more', 0.0, None, None, 0) coef_age_unknown = Beta('coef_age_unknown', 0.0, None, None, 0) coef_haveGA = Beta('coef_haveGA', 0.0, None, None, 0) coef_moreThanOneCar = Beta('coef_moreThanOneCar', 0.0, None, None, 0) coef_moreThanOneBike = Beta('coef_moreThanOneBike', 0.0, None, None, 0) coef_individualHouse = Beta('coef_individualHouse', 0.0, None, None, 0) coef_male = Beta('coef_male', 0.0, None, None, 0) coef_haveChildren = Beta('coef_haveChildren', 0.0, None, None, 0) coef_highEducation = Beta('coef_highEducation', 0.0, None, None, 0) ### Latent variable: structural equation # Note that the expression must be on a single line. In order to # write it across several lines, each line must terminate with # the \ symbol
# The following statement allows you to use the names of the variable # as Python variable. globals().update(database.variables) # 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) # Here we use the "biogeme" way for backward compatibility exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0 database.remove(exclude) # Parameters to be estimated ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_SM = Beta('ASC_SM', 0, None, None, 1) B_TIME = Beta('B_TIME', 0, None, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) Scale_group3 = Beta('Scale_group3', 1, 0.001, None, 0) # Definition of new variables SM_COST = SM_CO * (GA == 0) TRAIN_COST = TRAIN_CO * (GA == 0) # Definition of new variables: adding columns to the database CAR_AV_SP = DefineVariable('CAR_AV_SP', CAR_AV * (SP != 0), database) TRAIN_AV_SP = DefineVariable('TRAIN_AV_SP', TRAIN_AV * (SP != 0), database) TRAIN_TT_SCALED = DefineVariable('TRAIN_TT_SCALED', TRAIN_TT / 100.0, database) TRAIN_COST_SCALED = DefineVariable('TRAIN_COST_SCALED', TRAIN_COST / 100,
# Definition of other variables age_65_more = DefineVariable('age_65_more', age >= Numeric(65), database) moreThanOneCar = DefineVariable('moreThanOneCar', NbCar > 1, database) moreThanOneBike = DefineVariable('moreThanOneBike', NbBicy > 1, database) individualHouse = DefineVariable('individualHouse',\ HouseType == 1,database) male = DefineVariable('male', Gender == 1, database) haveChildren = DefineVariable('haveChildren',\ ((FamilSitu == 3)+(FamilSitu == 4)) > 0,database) haveGA = DefineVariable('haveGA', GenAbST == 1, database) highEducation = DefineVariable('highEducation', Education >= 6, database) ### Coefficients coef_intercept = Beta('coef_intercept', 0, None, None, 0) coef_age_65_more = Beta('coef_age_65_more', 0, None, None, 0) coef_haveGA = Beta('coef_haveGA', 0, None, None, 0) coef_ContIncome_0_4000 = Beta('coef_ContIncome_0_4000', 0, None, None, 0) coef_ContIncome_4000_6000 = Beta('coef_ContIncome_4000_6000', 0, None, None, 0) coef_ContIncome_6000_8000 = Beta('coef_ContIncome_6000_8000', 0, None, None, 0) coef_ContIncome_8000_10000 = Beta('coef_ContIncome_8000_10000', 0, None, None, 0) coef_ContIncome_10000_more = Beta('coef_ContIncome_10000_more', 0, None, None, 0) coef_moreThanOneCar = Beta('coef_moreThanOneCar', 0, None, None, 0) coef_moreThanOneBike = Beta('coef_moreThanOneBike', 0, None, None, 0) coef_individualHouse = Beta('coef_individualHouse', 0, None, None, 0) coef_male = Beta('coef_male', 0, None, None, 0) coef_haveChildren = Beta('coef_haveChildren', 0, None, None, 0) coef_highEducation = Beta('coef_highEducation', 0, None, None, 0)
# The Pandas data structure is available as database.data. Use all the # Pandas functions to invesigate the database #print(database.data.describe()) globals().update(database.variables) # 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) # Here we use the "biogeme" way for backward compatibility exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0 database.remove(exclude) ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_SM = Beta('ASC_SM', 0, None, None, 1) B_TIME = Beta('B_TIME', 0, None, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) MU = Beta('MU', 1, 0, 1, 0) MU_EXISTING = Beta('MU_EXISTING', 1, 1, None, 1) MU_PUBLIC = Beta('MU_PUBLIC', 1, 1, None, 0) ALPHA_EXISTING = Beta('ALPHA_EXISTING', 0.5, 0, 1, 0) ALPHA_PUBLIC = 1 - ALPHA_EXISTING SM_COST = SM_CO * (GA == 0) TRAIN_COST = TRAIN_CO * (GA == 0) TRAIN_TT_SCALED = TRAIN_TT / 100.0
import biogeme.models as models import biogeme.messaging as msg from biogeme.expressions import Beta, DefineVariable, bioDraws, \ PanelLikelihoodTrajectory, MonteCarlo, log # Read the data df = pd.read_csv('comboall.dat', '\t') database = db.Database('comboall', df) # They are organized as panel data. The variable ID identifies each individual. database.panel("ID") globals().update(database.variables) # Parameters to be estimated. One version for each latent class. numberOfClasses = 2 ASC_1 = [Beta(f'ASC_1{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] ASC_11 = [ Beta(f'ASC_11{i}', 0.1, None, None, 0) for i in range(numberOfClasses) ] ASC_2 = [Beta(f'ASC_2{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] ASC_21 = [ Beta(f'ASC_21{i}', 0.1, None, None, 0) for i in range(numberOfClasses) ] ASC_3 = [Beta(f'ASC_3{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] ASC_4 = [Beta(f'ASC_4{i}', 0.1, None, None, 1) for i in range(numberOfClasses)] beta_shcosttaxi1 = [ Beta(f'beta_shcosttaxi1{i}', 0.1, None, None, 0) for i in range(numberOfClasses) ] beta_shcostsharedcar = [
globals().update(database.variables) # 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) # Here we use the "biogeme" way for backward compatibility exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0 database.remove(exclude) # Parameters to be estimated. One version for each latent class. numberOfClasses = 2 B_COST = [ Beta(f'B_COST_class{i}', 0, None, None, 0) for i in range(numberOfClasses) ] # Define a random parameter, normally distributed across individuals, # designed to be used for Monte-Carlo simulation B_TIME = [ Beta(f'B_TIME_class{i}', 0, None, None, 0) for i in range(numberOfClasses) ] # It is advised not to use 0 as starting value for the following parameter. B_TIME_S = [ Beta(f'B_TIME_S_class{i}', 1, None, None, 0) for i in range(numberOfClasses) ] B_TIME_RND = [ B_TIME[i] + B_TIME_S[i] * bioDraws(f'B_TIME_RND_class{i}', 'NORMAL_ANTI')
coef_moreThanOneCar = structBetas['coef_moreThanOneCar'] coef_moreThanOneBike = structBetas['coef_moreThanOneBike'] coef_individualHouse = structBetas['coef_individualHouse'] coef_male = structBetas['coef_male'] coef_haveChildren = structBetas['coef_haveChildren'] coef_highEducation = structBetas['coef_highEducation'] ### Latent variable: structural equation # Note that the expression must be on a single line. In order to # write it across several lines, each line must terminate with # the \ symbol # Define a random parameter, normally distributed, designed to be used # for numerical integration sigma_s = Beta('sigma_s', 1, None, None, 0) CARLOVERS = coef_intercept + \ coef_age_65_more * age_65_more + \ formulaIncome + \ coef_moreThanOneCar * moreThanOneCar + \ coef_moreThanOneBike * moreThanOneBike + \ coef_individualHouse * individualHouse + \ coef_male * male + \ coef_haveChildren * haveChildren + \ coef_haveGA * haveGA + \ coef_highEducation * highEducation + \ sigma_s * bioDraws('EC', 'NORMAL_MLHS') # Choice model ASC_CAR = Beta('ASC_CAR', 0, None, None, 0)
import biogeme.models as models import biogeme.messaging as msg from biogeme.expressions import Beta, DefineVariable, bioDraws, \ PanelLikelihoodTrajectory, MonteCarlo, log # Read the data df = pd.read_csv('outside.dat', '\t') database = db.Database('outside', df) # They are organized as panel data. The variable ID identifies each individual. database.panel("ID") globals().update(database.variables) # Parameters to be estimated. One version for each latent class. numberOfClasses = 2 ASC_1 = [Beta(f'ASC_1{i}', 0.1, None, None, 1) for i in range(numberOfClasses)] ASC_2 = [Beta(f'ASC_2{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] ASC_3 = [Beta(f'ASC_3{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_shcostperdist1 = [Beta(f'beta_shcostperdist1{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_shtime = [Beta(f'beta_shtime{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_sharedist = [Beta(f'beta_sharedist{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_maascostperdist2 = [Beta(f'beta_maascostperdist2{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_maastime1 = [Beta(f'beta_maastime1{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_maastime2 = [Beta(f'beta_maastime2{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_extra = [Beta(f'beta_extra{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_maasdist = [Beta(f'beta_maasdist{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_maasdisbike = [Beta(f'beta_maasdisbike{i}', 0.1, None, None, 0) for i in range(numberOfClasses)] beta_totcostperdist = [Beta(f'beta_totcostperdist{i}', 0.1, None, None, 0) for i in range(numberOfClasses)]
def run_estimation_2015_2020(): """ :author: Antonin Danalet, based on the example '01logit.py' by Michel Bierlaire, EPFL, on biogeme.epfl.ch A binary logit model on the possibility to work from home at least some times.""" # Read the data data_file_directory = Path('../data/output/data/estimation/2015_2020/') df = pd.read_csv(data_file_directory / 'persons.csv', ';') database = db.Database('persons', df) # The following statement allows you to use the names of the variable as Python variable. globals().update(database.variables) # Parameters to be estimated alternative_specific_constant = Beta('alternative_specific_constant', 0, None, None, 0) b_no_post_school_education = Beta('b_no_post_school_education', 0, None, None, 0) b_secondary_education = Beta('b_secondary_education', 0, None, None, 0) b_tertiary_education = Beta('b_tertiary_education', 0, None, None, 0) b_male_2020 = Beta('b_male_2020', 0, None, None, 1) b_single_household_2020 = Beta('b_single_household_2020', 0, None, None, 1) b_couple_without_children_2015 = Beta('b_couple_without_children_2015', 0, None, None, 0) b_couple_without_children_2020 = Beta('b_couple_without_children_2020', 0, None, None, 0) b_couple_with_children_2020 = Beta('b_couple_with_children_2020', 0, None, None, 1) b_single_parent_with_children_2020 = Beta( 'b_single_parent_with_children_2020', 0, None, None, 1) b_not_family_household_2020 = Beta('b_not_family_household_2020', 0, None, None, 1) b_public_transport_connection_quality_abc_home_2020 = Beta( 'b_public_transport_connection_quality_abc_home_2020', 0, None, None, 1) b_public_transport_connection_quality_na_home_2015 = Beta( 'b_public_transport_connection_quality_na_home_2015', 0, None, None, 0) b_public_transport_connection_quality_na_home_2020 = Beta( 'b_public_transport_connection_quality_na_home_2020', 0, None, None, 1) b_public_transport_connection_quality_abcd_work_2020 = Beta( 'b_public_transport_connection_quality_abcd_work_2020', 0, None, None, 1) b_urban_home_2020 = Beta('b_urban_home_2020', 0, None, None, 1) b_rural_home_2020 = Beta('b_rural_home_2020', 0, None, None, 1) b_intermediate_home_2020 = Beta('b_intermediate_home_2020', 0, None, None, 1) b_urban_work_2020 = Beta('b_urban_work_2020', 0, None, None, 1) b_rural_work_2020 = Beta('b_rural_work_2020', 0, None, None, 1) b_intermediate_work_2020 = Beta('b_intermediate_work_2020', 0, None, None, 0) b_home_work_distance = Beta('b_home_work_distance', 0, None, None, 0) b_home_work_distance_zero = Beta('b_home_work_distance_zero', 0, None, None, 0) b_home_work_distance_na = Beta('b_home_work_distance_na', 0, None, None, 0) b_business_sector_agriculture_2020 = Beta( 'b_business_sector_agriculture_2020', 0, None, None, 1) b_business_sector_production = Beta('b_business_sector_production', 0, None, None, 0) b_business_sector_wholesale = Beta('b_business_sector_wholesale', 0, None, None, 0) b_business_sector_retail = Beta('b_business_sector_retail', 0, None, None, 0) b_business_sector_gastronomy = Beta('b_business_sector_gastronomy', 0, None, None, 0) b_business_sector_finance = Beta('b_business_sector_finance', 0, None, None, 0) b_business_sector_services_fc_2020 = Beta( 'b_business_sector_services_fc_2020', 0, None, None, 1) b_business_sector_other_services = Beta('b_business_sector_other_services', 0, None, None, 0) b_business_sector_others = Beta('b_business_sector_others', 0, None, None, 0) b_business_sector_non_movers = Beta('b_business_sector_non_movers', 0, None, None, 0) b_executives = Beta('b_executives', 0, None, None, 0) b_german = Beta('b_german', 0, None, None, 0) b_hh_income_na = Beta('b_hh_income_na', 0, None, None, 0) b_hh_income_8000_or_less = Beta('b_hh_income_8000_or_less', 0, None, None, 0) b_owning_a_general_abo = Beta('b_owning_a_general_abo', 0, None, None, 0) b_regional_abo_2020 = Beta('b_regional_abo_2020', 0, None, None, 1) b_regional_abo_na_2020 = Beta('b_regional_abo_na_2020', 0, None, None, 1) b_half_fare_abo_2020 = Beta('b_half_fare_abo_2020', 0, None, None, 1) b_half_fare_abo_na_2020 = Beta('b_half_fare_abo_na_2020', 0, None, None, 1) b_car_avail_2020 = Beta('b_car_avail_2020', 0, None, None, 1) b_car_avail_na_2020 = Beta('b_car_avail_na_2020', 0, None, None, 1) b_mobility_resource_na = Beta('b_mobility_resource_na', 0, None, None, 0) b_mobility_resource_car_general_abo_2020 = Beta( 'b_mobility_resource_car_general_abo_2020', 0, None, None, 1) b_mobility_resource_car_half_fare_abo = Beta( 'b_mobility_resource_car_half_fare_abo', 0, None, None, 0) b_mobility_resource_car_2020 = Beta('b_mobility_resource_car_2020', 0, None, None, 1) b_mobility_resource_general_abo_no_car_2020 = Beta( 'b_mobility_resource_general_no_car_abo_2020', 0, None, None, 0) b_mobility_resource_half_fare_abo_2020 = Beta( 'b_mobility_resource_half_fare_abo_2020', 0, None, None, 1) b_mobility_resource_none_2020 = Beta('b_mobility_resource_none_2020', 0, None, None, 1) b_mobility_resource_car_half_fare_regional_abo_2020 = Beta( 'b_mobility_resource_car_half_fare_regional_abo_2020', 0, None, None, 1) b_mobility_resource_car_regional_abo_2020 = Beta( 'b_mobility_resource_car_regional_abo_2020', 0, None, None, 1) b_mobility_resource_half_fare_regional_abo_2020 = Beta( 'b_mobility_resource_half_fare_regional_abo_2020', 0, None, None, 1) b_mobility_resource_regional_abo_2020 = Beta( 'b_mobility_resource_regional_abo_2020', 0, None, None, 1) scale_2020 = Beta('scale_2020', 1, 0.001, None, 0) ''' Definition of new variables ''' male_2020 = DefineVariable('male', (sex == 1) * (year == 2020), database) single_household_2020 = DefineVariable('single_household_2020', (hh_type == 10) * (year == 2020), database) couple_without_children_2015 = DefineVariable( 'couple_without_children_2015', (hh_type == 210) * (year == 2015), database) couple_without_children_2020 = DefineVariable( 'couple_without_children_2020', (hh_type == 210) * (year == 2020), database) couple_with_children_2020 = DefineVariable( 'couple_with_children_2020', (hh_type == 220) * (year == 2020), database) single_parent_with_children_2020 = DefineVariable( 'single_parent_with_children_2020', (hh_type == 230) * (year == 2020), database) not_family_household_2020 = DefineVariable( 'not_family_household_2020', (hh_type == 30) * (year == 2020), database) public_transport_connection_quality_abc_home_2020 = \ DefineVariable('public_transport_connection_quality_abc_home_2020', ((public_transport_connection_quality_ARE_home == 1) + (public_transport_connection_quality_ARE_home == 2) + (public_transport_connection_quality_ARE_home == 3)) * (year == 2020), database) public_transport_connection_quality_na_home_2015 = \ DefineVariable('public_transport_connection_quality_NA_home_2015', (public_transport_connection_quality_ARE_home == 5) * (year == 2015), database) public_transport_connection_quality_na_home_2020 = \ DefineVariable('public_transport_connection_quality_NA_home_2020', (public_transport_connection_quality_ARE_home == 5) * (year == 2020), database) public_transport_connection_quality_abcd_work_2020 = \ DefineVariable('public_transport_connection_quality_abc_work_2020', ((public_transport_connection_quality_ARE_work == 1) + (public_transport_connection_quality_ARE_work == 2) + (public_transport_connection_quality_ARE_work == 3) + (public_transport_connection_quality_ARE_work == 4)) * (year == 2020), database) urban_home_2020 = DefineVariable( 'urban_home_2020', (urban_typology_home == 1) * (year == 2020), database) rural_home_2020 = DefineVariable( 'rural_home_2020', (urban_typology_home == 3) * (year == 2020), database) intermediate_home_2020 = DefineVariable( 'intermediate_home_2020', (urban_typology_home == 2) * (year == 2020), database) urban_work_2020 = DefineVariable( 'urban_work_2020', (urban_typology_work == 1) * (year == 2020), database) rural_work_2020 = DefineVariable( 'rural_work_2020', (urban_typology_work == 3) * (year == 2020), database) intermediate_work_2020 = DefineVariable( 'intermediate_work_2020', (urban_typology_work == 2) * (year == 2020), database) home_work_distance = DefineVariable( 'home_work_distance', home_work_crow_fly_distance * (home_work_crow_fly_distance >= 0.0) / 100000.0, database) home_work_distance_zero = DefineVariable( 'home_work_distance_zero', home_work_crow_fly_distance == 0.0, database) home_work_distance_na = DefineVariable('home_work_distance_na', home_work_crow_fly_distance == -999, database) executives = DefineVariable('executives', work_position == 1, database) german = DefineVariable('german', language == 1, database) hh_income_na = DefineVariable('hh_income_na', hh_income < 0, database) hh_income_8000_or_less = DefineVariable( 'hh_income_8000_or_less', (hh_income == 1) + (hh_income == 2) + (hh_income == 3) + (hh_income == 4), database) owning_a_general_abo = DefineVariable('owning_a_general_abo', GA_ticket == 1, database) regional_abo_2020 = DefineVariable('regional_abo_2020', (Verbund_Abo == 1) * (year == 2020), database) half_fare_abo_2020 = DefineVariable('half_fare_abo_2020', (halbtax_ticket == 1) * (year == 2020), database) car_avail_always_or_on_demand_2020 = DefineVariable( 'car_avail_always_or_on_demand_2020', ((car_avail == 1) + (car_avail == 2)) * (year == 2020), database) regional_abo_na_2020 = DefineVariable('regional_abo_na_2020', (Verbund_Abo < 0) * (year == 2020), database) half_fare_abo_na_2020 = DefineVariable( 'half_fare_abo_na_2020', (halbtax_ticket < 0) * (year == 2020), database) car_avail_na_2020 = DefineVariable('car_avail_na_2020', (car_avail < 0) * (year == 2020), database) mobility_resource_na = DefineVariable('mobility_resource_na', mobility_resources == -98, database) mobility_resource_car_general_abo_2020 = DefineVariable( 'mobility_resource_car_general_abo_2020', (mobility_resources == 1) * (year == 2020), database) mobility_resource_car_half_fare_abo = DefineVariable( 'mobility_resource_car_half_fare_abo', mobility_resources == 2, database) mobility_resource_car_2020 = DefineVariable('mobility_resource_car_2020', (mobility_resources == 3) * (year == 2020), database) mobility_resource_general_abo_no_car_2020 = DefineVariable( 'mobility_resource_general_abo_no_car_2020', (mobility_resources == 4) * (year == 2020), database) mobility_resource_half_fare_abo_2020 = DefineVariable( 'mobility_resource_half_fare_abo_2020', (mobility_resources == 5) * (year == 2020), database) mobility_resource_none_2020 = DefineVariable('mobility_resource_none_2020', (mobility_resources == 6) * (year == 2020), database) mobility_resource_car_half_fare_regional_abo_2020 = \ DefineVariable('mobility_resource_car_half_fare_regional_abo_2020', (mobility_resources == 20) * (year == 2020), database) mobility_resource_car_regional_abo_2020 = DefineVariable( 'mobility_resource_car_regional_abo_2020', (mobility_resources == 30) * (year == 2020), database) mobility_resource_half_fare_regional_abo_2020 = DefineVariable( 'mobility_resource_half_fare_regional_abo_2020', (mobility_resources == 50) * (year == 2020), database) mobility_resource_regional_abo_2020 = DefineVariable( 'mobility_resource_regional_abo_2020', (mobility_resources == 60) * (year == 2020), database) business_sector_agriculture_2020 = DefineVariable( 'business_sector_agriculture_2020', business_sector_agriculture * (year == 2020), database) business_sector_services_fc_2020 = DefineVariable( 'business_sector_services_fc_2020', business_sector_services_fc * (year == 2020), database) # Utility U = alternative_specific_constant + \ b_executives * executives + \ b_no_post_school_education * no_post_school_educ + \ b_secondary_education * secondary_education + \ b_tertiary_education * tertiary_education + \ b_couple_without_children_2015 * couple_without_children_2015 + \ b_couple_without_children_2020 * couple_without_children_2020 + \ b_public_transport_connection_quality_na_home_2015 * public_transport_connection_quality_na_home_2015 + \ b_public_transport_connection_quality_na_home_2020 * public_transport_connection_quality_na_home_2020 + \ b_home_work_distance * home_work_distance + \ b_home_work_distance_zero * home_work_distance_zero + \ b_home_work_distance_na * home_work_distance_na + \ models.piecewiseFormula(age, [15, 19, 31, 79, 85]) + \ b_business_sector_retail * business_sector_retail + \ b_business_sector_gastronomy * business_sector_gastronomy + \ b_business_sector_finance * business_sector_finance + \ b_business_sector_production * business_sector_production + \ b_business_sector_wholesale * business_sector_wholesale + \ b_business_sector_other_services * business_sector_other_services + \ b_business_sector_others * business_sector_others + \ b_business_sector_non_movers * business_sector_non_movers + \ b_german * german + \ models.piecewiseFormula(work_percentage, [0, 90, 101]) + \ b_hh_income_na * hh_income_na + \ b_hh_income_8000_or_less * hh_income_8000_or_less + \ b_owning_a_general_abo * owning_a_general_abo + \ b_mobility_resource_na * mobility_resource_na + \ b_mobility_resource_car_half_fare_abo * mobility_resource_car_half_fare_abo + \ b_male_2020 * male_2020 + \ b_single_household_2020 * single_household_2020 + \ b_couple_with_children_2020 * couple_with_children_2020 + \ b_single_parent_with_children_2020 * single_parent_with_children_2020 + \ b_not_family_household_2020 * not_family_household_2020 + \ b_public_transport_connection_quality_abc_home_2020 * public_transport_connection_quality_abc_home_2020 + \ b_public_transport_connection_quality_abcd_work_2020 * public_transport_connection_quality_abcd_work_2020 + \ b_urban_home_2020 * urban_home_2020 + \ b_rural_home_2020 * rural_home_2020 + \ b_intermediate_home_2020 * intermediate_home_2020 + \ b_urban_work_2020 * urban_work_2020 + \ b_rural_work_2020 * rural_work_2020 + \ b_intermediate_work_2020 * intermediate_work_2020 + \ b_business_sector_agriculture_2020 * business_sector_agriculture_2020 + \ b_business_sector_services_fc_2020 * business_sector_services_fc_2020 + \ b_regional_abo_2020 * regional_abo_2020 + \ b_regional_abo_na_2020 * regional_abo_na_2020 + \ b_half_fare_abo_2020 * half_fare_abo_2020 + \ b_half_fare_abo_na_2020 * half_fare_abo_na_2020 + \ b_car_avail_2020 * car_avail_always_or_on_demand_2020 + \ b_car_avail_na_2020 * car_avail_na_2020 + \ b_mobility_resource_car_general_abo_2020 * mobility_resource_car_general_abo_2020 + \ b_mobility_resource_car_2020 * mobility_resource_car_2020 + \ b_mobility_resource_general_abo_no_car_2020 * mobility_resource_general_abo_no_car_2020 + \ b_mobility_resource_half_fare_abo_2020 * mobility_resource_half_fare_abo_2020 + \ b_mobility_resource_none_2020 * mobility_resource_none_2020 + \ b_mobility_resource_car_half_fare_regional_abo_2020 * mobility_resource_car_half_fare_regional_abo_2020 + \ b_mobility_resource_car_regional_abo_2020 * mobility_resource_car_regional_abo_2020 + \ b_mobility_resource_half_fare_regional_abo_2020 * mobility_resource_half_fare_regional_abo_2020 + \ b_mobility_resource_regional_abo_2020 * mobility_resource_regional_abo_2020 U_no_telecommuting = 0 # Scale associated with 2020 is estimated scale = (year == 2015) + (year == 2020) * scale_2020 # Associate utility functions with the numbering of alternatives V = { 1: scale * U, # Yes or sometimes 0: U_no_telecommuting } # No av = {1: 1, 0: 1} # Definition of the model. This is the contribution of each # observation to the log likelihood function. logprob = models.loglogit( V, av, # All alternatives are supposed to be always available telecommuting) # Choice variable # Change the working directory, so that biogeme writes in the correct folder standard_directory = os.getcwd() output_directory = '../data/output/models/estimation/2015_2020/' os.chdir(output_directory) # Create the Biogeme object biogeme = bio.BIOGEME(database, logprob) output_file_name = 'logit_telecommuting_2015_2020' biogeme.modelName = output_file_name # Estimate the parameters results = biogeme.estimate() # Get the results in LaTeX results.writeLaTeX() # Go back to the normal working directory os.chdir(standard_directory)
import biogeme.database as db import biogeme.biogeme as bio import biogeme.models as models import biogeme.messaging as msg from biogeme.expressions import Beta, DefineVariable, bioDraws, \ PanelLikelihoodTrajectory, MonteCarlo, log # Read the data df = pd.read_csv('outside.dat', '\t') database = db.Database('outside', df) database.panel("ID") # They are organized as panel data. The variable ID identifies each individual. globals().update(database.variables) # Parameters to be estimated ASC_1 = Beta('ASC_1', 0, None, None, 1) ASC_11 = Beta('ASC_11', 0, None, None, 0) ASC_2 = Beta('ASC_2', 0, None, None, 0) ASC_21 = Beta('ASC_21', 0, None, None, 0) ASC_3 = Beta('ASC_3', 0, None, None, 0) ASC_31 = Beta('ASC_31', 0, None, None, 0) ASC_4 = Beta('ASC_4', 0, None, None, 0) # Shared error parameters, fix the mean-parameter to 0 SIGMA_SH_MAAS_M = Beta('SIGMA_SH_MAAS_M', 0, None, None, 1) SIGMA_SH_MAAS_STD = Beta('SIGMA_SH_MAAS_STD', 0, None, None, 0) SIGMA_SH_MAASRND = SIGMA_SH_MAAS_M + SIGMA_SH_MAAS_STD * bioDraws( 'SIGMA_SH_MAASRND', 'NORMAL') beta_fam_package = Beta('beta_fam_package', 0, None, None, 0) beta_fam_private = Beta('beta_fam_private', 0, None, None, 0) beta_age_package = Beta('beta_age_package', 0, None, None, 0)
numberOfRows = database.data.shape[0] normalizedWeight = Weight * numberOfRows / sumWeight # Calculate the number of accurences of a value in the database numberOfMales = database.count('Gender', 1) print(f'Number of males: {numberOfMales}') numberOfFemales = database.count('Gender', 2) print(f'Number of females: {numberOfFemales}') # For more complex conditions, we use Pandas unreportedGender = database.data[(database.data['Gender'] != 1) & \ (database.data['Gender'] != 2)].count()['Gender'] print(f'Unreported gender: {unreportedGender}') # List of parameters. Their value will be set later. 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) # Define new variables. Must be consistent with estimation results. TimePT_scaled = TimePT / 200 TimeCar_scaled = TimeCar / 200 MarginalCostPT_scaled = MarginalCostPT / 10 CostCarCHF_scaled = CostCarCHF / 10 distance_km_scaled = distance_km / 5
# The Pandas data structure is available as database.data. Use all the # Pandas functions to invesigate the database #print(database.data.describe()) globals().update(database.variables) # 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) # Here we use the "biogeme" way for backward compatibility exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0 database.remove(exclude) ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_SM = Beta('ASC_SM', 0, None, None, 1) B_TIME = Beta('B_TIME', 0, None, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) SIGMA_CAR = Beta('SIGMA_CAR', 0, None, None, 0) SIGMA_SM = Beta('SIGMA_SM', 0, None, None, 1) SIGMA_TRAIN = Beta('SIGMA_TRAIN', 0, None, None, 0) # Define a random parameter, normally distirbuted, designed to be used # for Monte-Carlo simulation EC_CAR = SIGMA_CAR * bioDraws('EC_CAR', 'NORMAL') EC_SM = SIGMA_SM * bioDraws('EC_SM', 'NORMAL') EC_TRAIN = SIGMA_TRAIN * bioDraws('EC_TRAIN', 'NORMAL')
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
-0.33669944071374086, 0.2177481824894475, -0.6224104823788724, 1.1592372281254595, -0.35530173883216076 ]) age_65_more = DefineVariable('age_65_more', age >= Numeric(65), database) moreThanOneCar = DefineVariable('moreThanOneCar', NbCar > 1, database) moreThanOneBike = DefineVariable('moreThanOneBike', NbBicy > 1, database) individualHouse = DefineVariable('individualHouse',\ HouseType == 1,database) male = DefineVariable('male', Gender == 1, database) haveChildren = DefineVariable('haveChildren',\ ((FamilSitu == 3)+(FamilSitu == 4)) > 0,database) haveGA = DefineVariable('haveGA', GenAbST == 1, database) highEducation = DefineVariable('highEducation', Education >= 6, database) ASC_CAR = Beta('ASC_CAR', 0.4118057751313065, None, None, 0) ASC_SM = Beta('ASC_SM', 1.0134144912346523, None, None, 0) BETA_COST_HWH = Beta('BETA_COST_HWH', -1.7754249429871691, None, None, 0) BETA_COST_OTHER = Beta('BETA_COST_OTHER', -1.5427606901901116, None, None, 0) BETA_DIST = Beta('BETA_DIST', -4.952750818319787, None, None, 0) BETA_TIME_CAR_CL = Beta('BETA_TIME_CAR_CL', -0.1412781305192878, None, None, 0) BETA_TIME_CAR_REF = Beta('BETA_TIME_CAR_REF', -26.647254885078237, None, None, 0) BETA_TIME_PT_CL = Beta('BETA_TIME_PT_CL', -0.5048865862522101, None, None, 0) BETA_TIME_PT_REF = Beta('BETA_TIME_PT_REF', -4.883762313381118, None, None, 0) BETA_WAITING_TIME = Beta('BETA_WAITING_TIME', -0.052434936015492456, None, None, 0) coef_age_65_more = Beta('coef_age_65_more', 1.1187240773542817, None, None, 0) coef_haveChildren = Beta('coef_haveChildren', 0.18090193116052988, None, None, 0) coef_haveGA = Beta('coef_haveGA', 4.657846388649241, None, None, 0)
# Exclude exclude = (ArrivalTimeHours_1 == -1) database.remove(exclude) # Choice chosenAlternative = (BestAlternative_1 * 1) + (BestAlternative_2 * 2) + (BestAlternative_3 * 3) # Parameters to be estimated # Arguments: # 1 Name for report. Typically, the same as the variable # 2 Starting value # 3 Lower bound # 4 Upper bound # 5 0: estimate the parameter, 1: keep it fixed Constant1 = Beta('Constant1', 0, None, None, 1) Constant2 = Beta('Constant2', 0, None, None, 0) Constant3 = Beta('Constant3', 0, None, None, 0) LogFare = Beta('LogFare', 0, None, None, 0) Legroom = Beta('Legroom', 0, None, None, 0) SchedDE = Beta('SchedDE', 0, None, None, 0) SchedDL = Beta('SchedDL', 0, None, None, 0) Total_TT1 = Beta('Total_TT1', 0, None, None, 0) Total_TT2 = Beta('Total_TT2', 0, None, None, 0) Total_TT3 = Beta('Total_TT3', 0, None, None, 0) # Define here arithmetic expressions for variables that are not directly available from the data DepTimeSensitive = DefineVariable('DepTimeSensitive', q11_DepartureOrArrivalIsImportant == 1, database) ArrTimeSensitive = DefineVariable('ArrTimeSensitive',
# The Pandas data structure is available as database.data. Use all the # Pandas functions to invesigate the database #print(database.data.describe()) # The following statement allows you to use the names of the variable # as Python variable. globals().update(database.variables) # 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_1 = Beta('ASC_1', 0, None, None, 0) ASC_11 = Beta('ASC_11', 0, None, None, 0) ASC_2 = Beta('ASC_2', 0, None, None, 0) ASC_21 = Beta('ASC_21', 0, None, None, 0) ASC_3 = Beta('ASC_3', 0, None, None, 0) ASC_31 = Beta('ASC_31', 0, None, None, 0) ASC_4 = Beta('ASC_4', 0, None, None, 1) ASC_41 = Beta('ASC_41', 0, None, None, 1) beta_shcosttaxi1 = Beta('beta_shcosttaxi1', 0, None, None, 0) beta_shcosttaxi2 = Beta('beta_shcosttaxi2', 0, None, None, 0) beta_shcostsharedcar = Beta('beta_shcostsharedcar', 0, None, None, 0) beta_shcostsharedcar2 = Beta('beta_shcostsharedcar2', 0, None, None, 0) beta_shtime_taxi = Beta('beta_shtimetaxi', 0, None, None, 0) beta_shtime_taxi1 = Beta('beta_shtimetaxi1', 0, None, None, 0) beta_shtime_share = Beta('beta_shtimeshare', 0, None, None, 0)
]) # Definition of other variables age_65_more = DefineVariable('age_65_more', age >= 65, database) moreThanOneCar = DefineVariable('moreThanOneCar', NbCar > 1, database) moreThanOneBike = DefineVariable('moreThanOneBike', NbBicy > 1, database) individualHouse = DefineVariable('individualHouse', HouseType == 1, database) male = DefineVariable('male', Gender == 1, database) haveChildren = DefineVariable('haveChildren', \ ((FamilSitu == 3) + (FamilSitu == 4)) > 0, database) haveGA = DefineVariable('haveGA', GenAbST == 1, database) highEducation = DefineVariable('highEducation', Education >= 6, database) ### Coefficients coef_intercept = Beta('coef_intercept', betas['coef_intercept'], None, None, 0) coef_age_65_more = Beta('coef_age_65_more', betas['coef_age_65_more'], None, None, 0) coef_haveGA = Beta('coef_haveGA', betas['coef_haveGA'], None, None, 0) coef_moreThanOneCar = Beta('coef_moreThanOneCar', betas['coef_moreThanOneCar'], None, None, 0) coef_moreThanOneBike = Beta('coef_moreThanOneBike', betas['coef_moreThanOneBike'], None, None, 0) coef_individualHouse = Beta('coef_individualHouse', betas['coef_individualHouse'], None, None, 0) coef_male = Beta('coef_male', betas['coef_male'], None, None, 0) coef_haveChildren = Beta('coef_haveChildren', betas['coef_haveChildren'], None, None, 0) coef_highEducation = Beta('coef_highEducation', betas['coef_highEducation'], None, None, 0)
# The following statement allows you to use the names of the variable # as Python variable. globals().update(database.variables) # 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) # Here we use the "biogeme" way for backward compatibility exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0 database.remove(exclude) # Parameters to be estimated B_TIME = Beta('B_TIME', 0, None, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) # Parameters for the ordered logit. # tau1 <= 0 tau1 = Beta('tau1', -1, None, 0, 0) # delta2 >= 0 delta2 = Beta('delta2', 2, 0, None, 0) tau2 = tau1 + delta2 # Definition of new variables 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)
# The following statement allows you to use the names of the variable # as Python variable. globals().update(database.variables) # 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) # Here we use the "biogeme" way for backward compatibility exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0 database.remove(exclude) # Parameters to be estimated ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_SM = Beta('ASC_SM', 0, None, None, 1) B_TIME = Beta('B_TIME', 0, None, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) LAMBDA = Beta('LAMBDA', 1.5, 0.0001, 5, 0) # Definition of new variables SM_COST = SM_CO * (GA == 0) TRAIN_COST = TRAIN_CO * (GA == 0) # Definition of new variables: adding columns to the database CAR_AV_SP = DefineVariable('CAR_AV_SP', CAR_AV * (SP != 0), database) TRAIN_AV_SP = DefineVariable('TRAIN_AV_SP', TRAIN_AV * (SP != 0), database) TRAIN_TT_SCALED = DefineVariable('TRAIN_TT_SCALED', TRAIN_TT / 100.0, database) TRAIN_COST_SCALED = DefineVariable('TRAIN_COST_SCALED', TRAIN_COST / 100,