print( 'Run first the script 05latentChoiceFull.py in order to generate the file ' '05latentChoiceFull.pickle.') sys.exit() betas = results.getBetaValues() ### Variables # Piecewise linear definition of income ScaledIncome = DefineVariable('ScaledIncome', CalculatedIncome / 1000, database) thresholds = [None, 4, 6, 8, 10, None] formulaIncome = models.piecewiseFormula(ScaledIncome, thresholds, [ betas['beta_ScaledIncome_lessthan_4'], betas['beta_ScaledIncome_4_6'], betas['beta_ScaledIncome_6_8'], betas['beta_ScaledIncome_8_10'], betas['beta_ScaledIncome_10_more'] ]) # 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
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)
df = pd.read_csv('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) # Piecewise linear definition of income ScaledIncome = DefineVariable('ScaledIncome', CalculatedIncome / 1000, database) thresholds = [None, 4, 6, 8, 10, None] 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, 0)
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)
pandas = pd.read_csv("optima.dat", sep='\t') database = db.Database("optima", pandas) globals().update(database.variables) exclude = (Choice == -1.0) database.remove(exclude) # Piecewise linear definition of income # Note the use of the biogeme implementation of min and max, to avoid # ambiguity with the python native functions. ScaledIncome = DefineVariable('ScaledIncome',\ CalculatedIncome / 1000,database) formulaIncome = models.piecewiseFormula(ScaledIncome, [None, 4, 6, 8, 10, None]) 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.0, None, None, 0) coef_age_65_more = Beta('coef_age_65_more', 0.0, None, None, 0)
pandas = pd.read_csv("optima.dat", sep='\t') database = db.Database("optima", pandas) globals().update(database.variables) exclude = (Choice == -1.0) database.remove(exclude) ### Variables ScaledIncome = DefineVariable('ScaledIncome',\ CalculatedIncome / 1000,database) formulaIncome = models.piecewiseFormula( ScaledIncome, [None, 4, 6, 8, 10, None], [ -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)
pandas = pd.read_csv("optima.dat", sep='\t') database = db.Database("optima", pandas) globals().update(database.variables) exclude = (Choice == -1.0) database.remove(exclude) ### Variables ScaledIncome = DefineVariable('ScaledIncome',\ CalculatedIncome / 1000,database) formulaIncome = models.piecewiseFormula( ScaledIncome, [None, 4, 6, 8, 10, None], [ 0.08954209471304636, -0.2209233080453265, 0.2591889240542216, -0.5227805784067027, 0.08430692986645968 ]) 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
pandas = pd.read_csv("optima.dat", sep='\t') database = db.Database("optima", pandas) globals().update(database.variables) exclude = (Choice == -1.0) database.remove(exclude) ### Variables ScaledIncome = DefineVariable('ScaledIncome',\ CalculatedIncome / 1000,database) formulaIncome = models.piecewiseFormula( ScaledIncome, [None, 4, 6, 8, 10, None], [ 0.15012511354122204, -0.287364030650875, 0.3378758520316341, -0.6808138631421441, 0.1191396581172788 ]) 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
def apply_model_to_example(df_persons, betas, output_directory_for_simulation, output_file_name): """ :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 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_are_a_home = Beta( 'b_public_transport_connection_quality_are_a_home', 0, None, None, 1) b_public_transport_connection_quality_are_b_home = Beta( 'b_public_transport_connection_quality_are_b_home', 0, None, None, 1) b_public_transport_connection_quality_are_c_home = Beta( 'b_public_transport_connection_quality_are_c_home', 0, None, None, 1) b_public_transport_connection_quality_are_d_home = Beta( 'b_public_transport_connection_quality_are_d_home', 0, None, None, 1) b_public_transport_connection_quality_are_na_home = Beta( 'b_public_transport_connection_quality_are_na_home', 0, None, None, 0) b_urban_work = Beta('b_urban_work', 0, None, None, 1) b_rural_work = Beta('b_rural_work', 0, None, None, 0) b_intermediate_work = Beta('b_intermediate_work', 0, None, None, 1) 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_employees = Beta('b_employees', 0, None, None, 1) b_executives = Beta('b_executives', 0, None, None, 0) b_german = Beta('b_german', 0, None, None, 0) b_nationality_ch_germany_france_italy_nw_e = Beta( 'b_nationality_ch_germany_france_italy_nw_e', 0, None, None, 1) b_nationality_south_west_europe = Beta('b_nationality_south_west_europe', 0, None, None, 1) b_nationality_southeast_europe = Beta('b_nationality_southeast_europe', 0, None, None, 1) b_hh_income_na = Beta('B_hh_income_na', 0, None, None, 1) b_hh_income_8000_or_less = Beta('b_hh_income_8000_or_less', 0, None, None, 0) b_hh_income_more_than_8000 = Beta('b_hh_income_more_than_8000', 0, None, None, 1) # Definition of new variables no_post_school_educ = ((highest_educ == 1) | (highest_educ == 2) | (highest_educ == 3) | (highest_educ == 4)) secondary_education = ((highest_educ == 5) | (highest_educ == 6) | (highest_educ == 7) | (highest_educ == 8) | (highest_educ == 9) | (highest_educ == 10) | (highest_educ == 11) | (highest_educ == 12)) tertiary_education = ((highest_educ == 13) | (highest_educ == 14) | (highest_educ == 15) | (highest_educ == 16)) university = (highest_educ == 17) male = (sex == 1) public_transport_connection_quality_ARE_A_home = ( public_transport_connection_quality_ARE_home == 1) public_transport_connection_quality_ARE_B_home = ( public_transport_connection_quality_ARE_home == 2) public_transport_connection_quality_ARE_C_home = ( public_transport_connection_quality_ARE_home == 3) public_transport_connection_quality_ARE_D_home = ( public_transport_connection_quality_ARE_home == 4) public_transport_connection_quality_ARE_NA_home = ( public_transport_connection_quality_ARE_home == 5) urban_work = (urban_typology_work == 1) rural_work = (urban_typology_work == 3) intermediate_work = (urban_typology_work == 2) home_work_distance = (home_work_crow_fly_distance * (home_work_crow_fly_distance >= 0.0) / 100000.0) business_sector_agriculture = DefineVariable('business_sector_agriculture', 1 <= noga_08 <= 7, database) business_sector_retail = DefineVariable('business_sector_retail', 47 <= noga_08 <= 47, database) business_sector_gastronomy = DefineVariable('business_sector_gastronomy', 55 <= noga_08 <= 57, database) business_sector_finance = DefineVariable('business_sector_finance', 64 <= noga_08 <= 67, database) business_sector_production = DefineVariable( 'business_sector_production', (10 <= noga_08 <= 35) | (40 <= noga_08 <= 44), database) business_sector_wholesale = DefineVariable('business_sector_wholesale', (45 <= noga_08 <= 45) | (49 <= noga_08 <= 54), database) business_sector_services_fC = DefineVariable( 'business_sector_services_fC', (60 <= noga_08 <= 63) | (69 <= noga_08 <= 83) | (noga_08 == 58), database) business_sector_other_services = DefineVariable( 'business_sector_other_services', (86 <= noga_08 <= 90) | (92 <= noga_08 <= 96) | (noga_08 == 59) | (noga_08 == 68), database) business_sector_others = DefineVariable('business_sector_others', 97 <= noga_08 <= 98, database) business_sector_non_movers = DefineVariable( 'business_sector_non_movers', (8 <= noga_08 <= 9) | (36 <= noga_08 <= 39) | (84 <= noga_08 <= 85) | (noga_08 == 91) | (noga_08 == 99), database) employees = work_position == 2 executives = work_position == 1 german = language == 1 nationality_switzerland = nation == 8100 nationality_germany_austria_lichtenstein = (nation == 8207) + ( nation == 8229) + (nation == 8222) nationality_italy_vatican = (nation == 8218) + (nation == 8241) nationality_france_monaco_san_marino = (nation == 8212) + ( nation == 8226) + (nation == 8233) nationality_northwestern_europe = (nation == 8204) + (nation == 8223) + (nation == 8227) + (nation == 8206) + \ (nation == 8211) + (nation == 8215) + (nation == 8216) + (nation == 8217) + \ (nation == 8228) + (nation == 8234) nationality_south_west_europe = (nation == 8231) + (nation == 8236) + ( nation == 8202) nationality_southeast_europe = (nation == 8224) + (nation == 8201) + (nation == 8214) + (nation == 8256) + \ (nation == 8250) + (nation == 8251) + (nation == 8252) + (nation == 8255) + \ (nation == 8205) + (nation == 8239) + (nation == 8242) + (nation == 8248) + \ (nation == 8254) nationality_eastern_europe = (nation == 8230) + (nation == 8232) + (nation == 8240) + (nation == 8243) + \ (nation == 8244) + (nation == 8263) + (nation == 8265) + (nation == 8266) + \ (nation == 8260) + (nation == 8261) + (nation == 8262) # several_part_time_jobs = full_part_time_job == 3 work_percentage = DefineVariable( 'work_percentage', bioMin( (full_part_time_job == 1) * 100 + percentage_first_part_time_job * (percentage_first_part_time_job > 0), # + # percentage_second_part_time_job * (percentage_second_part_time_job > 0), 100), database) hh_income_na = hh_income == -98 hh_income_less_than_2000 = hh_income == 1 hh_income_2000_to_4000 = hh_income == 2 hh_income_4001_to_6000 = hh_income == 3 hh_income_6001_to_8000 = hh_income == 4 hh_income_8001_to_10000 = hh_income == 5 hh_income_10001_to_12000 = hh_income == 6 hh_income_12001_to_14000 = hh_income == 7 hh_income_14001_to_16000 = hh_income == 8 hh_income_more_than_16000 = hh_income == 9 # Utility U = alternative_specific_constant + \ b_executives * executives + \ b_employees * employees + \ 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_are_a_home * public_transport_connection_quality_ARE_A_home + \ b_public_transport_connection_quality_are_b_home * public_transport_connection_quality_ARE_B_home + \ b_public_transport_connection_quality_are_c_home * public_transport_connection_quality_ARE_C_home + \ b_public_transport_connection_quality_are_d_home * public_transport_connection_quality_ARE_D_home + \ b_public_transport_connection_quality_are_na_home * public_transport_connection_quality_ARE_NA_home + \ b_urban_work * urban_work + \ b_rural_work * rural_work + \ b_intermediate_work * intermediate_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_lichtenstein + \ b_nationality_ch_germany_france_italy_nw_e * nationality_italy_vatican + \ b_nationality_ch_germany_france_italy_nw_e * nationality_france_monaco_san_marino + \ b_nationality_ch_germany_france_italy_nw_e * nationality_northwestern_europe + \ b_nationality_south_west_europe * nationality_south_west_europe + \ b_nationality_southeast_europe * nationality_southeast_europe + \ b_nationality_ch_germany_france_italy_nw_e * nationality_eastern_europe + \ models.piecewiseFormula(work_percentage, [0, 90, 101]) + \ b_hh_income_na * hh_income_na + \ b_hh_income_8000_or_less * hh_income_less_than_2000 + \ b_hh_income_8000_or_less * hh_income_2000_to_4000 + \ b_hh_income_8000_or_less * hh_income_4001_to_6000 + \ b_hh_income_8000_or_less * hh_income_6001_to_8000 + \ b_hh_income_more_than_8000 * hh_income_8001_to_10000 + \ b_hh_income_more_than_8000 * hh_income_10001_to_12000 + \ b_hh_income_more_than_8000 * hh_income_12001_to_14000 + \ b_hh_income_more_than_8000 * hh_income_14001_to_16000 + \ b_hh_income_more_than_8000 * hh_income_more_than_16000 U_No_telecommuting = 0 # Associate utility functions with the numbering of alternatives V = { 1: U, # Yes or sometimes 0: U_No_telecommuting } # No av = {1: 1, 0: 1} # The choice model is a logit, with availability conditions prob_telecommuting = models.logit(V, av, 1) prob_no_telecommuting = models.logit(V, av, 0) 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' # 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) ''' Save the file ''' df_persons.to_csv(output_directory_for_simulation / output_file_name, sep=',', index=False)
def run_estimation(data_file_directory, data_file_name, output_directory, output_file_name='logit_telecommuting'): """ :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 df = pd.read_csv(data_file_directory / data_file_name, ';') 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_university = Beta('b_university', 0, None, None, 1) b_male = Beta('b_male', 0, None, None, 1) b_single_household = Beta('b_single_household', 0, None, None, 1) b_couple_without_children = Beta('b_couple_without_children', 0, None, None, 0) b_couple_with_children = Beta('b_couple_with_children', 0, None, None, 1) b_single_parent_with_children = Beta('b_single_parent_with_children', 0, None, None, 1) b_not_family_household = Beta('b_not_family_household', 0, None, None, 1) b_public_transport_connection_quality_are_a_home = Beta( 'b_public_transport_connection_quality_are_a_home', 0, None, None, 1) b_public_transport_connection_quality_are_b_home = Beta( 'b_public_transport_connection_quality_are_b_home', 0, None, None, 1) b_public_transport_connection_quality_are_c_home = Beta( 'b_public_transport_connection_quality_are_c_home', 0, None, None, 1) b_public_transport_connection_quality_are_d_home = Beta( 'b_public_transport_connection_quality_are_d_home', 0, None, None, 1) b_public_transport_connection_quality_are_na_home = Beta( 'b_public_transport_connection_quality_are_na_home', 0, None, None, 0) b_public_transport_connection_quality_are_a_work = Beta( 'b_public_transport_connection_quality_are_a_work', 0, None, None, 1) b_public_transport_connection_quality_are_b_work = Beta( 'b_public_transport_connection_quality_are_b_work', 0, None, None, 1) b_public_transport_connection_quality_are_c_work = Beta( 'b_public_transport_connection_quality_are_c_work', 0, None, None, 1) b_public_transport_connection_quality_are_d_work = Beta( 'b_public_transport_connection_quality_are_d_work', 0, None, None, 1) b_public_transport_connection_quality_are_na_work = Beta( 'b_public_transport_connection_quality_are_na_work', 0, None, None, 1) b_urban_home = Beta('b_urban_home', 0, None, None, 1) b_rural_home = Beta('b_rural_home', 0, None, None, 1) b_intermediate_home = Beta('b_intermediate_home', 0, None, None, 1) b_urban_work = Beta('b_urban_work', 0, None, None, 1) b_rural_work = Beta('b_rural_work', 0, None, None, 1) b_intermediate_work = Beta('b_intermediate_work', 0, None, None, 1) 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 = Beta('b_business_sector_agriculture', 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 = Beta('b_business_sector_services_fc', 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_employees = Beta('b_employees', 0, None, None, 1) b_executives = Beta('b_executives', 0, None, None, 0) b_german = Beta('b_german', 0, None, None, 0) b_nationality_ch_germany_france_italy_nw_e = Beta( 'b_nationality_ch_germany_france_italy_nw_e', 0, None, None, 1) b_nationality_south_west_europe = Beta('b_nationality_south_west_europe', 0, None, None, 1) b_nationality_southeast_europe = Beta('b_nationality_southeast_europe', 0, None, None, 1) # b_several_part_time_jobs = Beta('b_several_part_time_jobs', 0, None, None, 1) 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_hh_income_more_than_8000 = Beta('b_hh_income_more_than_8000', 0, None, None, 1) b_general_abo = Beta('b_general_abo', 0, None, None, 0) b_regional_abo = Beta('b_regional_abo', 0, None, None, 1) b_regional_abo_na = Beta('b_regional_abo_na', 0, None, None, 1) b_half_fare_abo = Beta('b_half_fare_abo', 0, None, None, 1) b_half_fare_abo_na = Beta('b_half_fare_abo_na', 0, None, None, 1) b_car_avail = Beta('b_car_avail', 0, None, None, 1) b_car_avail_na = Beta('b_car_avail_na', 0, None, None, 1) b_mobility_resource_na = Beta('b_mobility_resource_na', 0, None, None, 0) b_mobility_resource_car_general_abo = Beta( 'b_mobility_resource_car_general_abo', 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 = Beta('b_mobility_resource_car', 0, None, None, 1) b_mobility_resource_general_abo = Beta('b_mobility_resource_general_abo', 0, None, None, 1) b_mobility_resource_half_fare_abo = Beta( 'b_mobility_resource_half_fare_abo', 0, None, None, 1) b_mobility_resource_none = Beta('b_mobility_resource_none', 0, None, None, 1) b_mobility_resource_car_half_fare_regional_abo = Beta( 'b_mobility_resource_car_half_fare_regional_abo', 0, None, None, 1) b_mobility_resource_car_regional_abo = Beta( 'b_mobility_resource_car_regional_abo', 0, None, None, 1) b_mobility_resource_half_fare_regional_abo = Beta( 'b_mobility_resource_half_fare_regional_abo', 0, None, None, 1) b_mobility_resource_regional_abo = Beta('b_mobility_resource_regional_abo', 0, None, None, 1) # Definition of new variables male = DefineVariable('male', sex == 1, database) single_household = DefineVariable('single_household', hh_type == 10, database) couple_without_children = DefineVariable('couple_without_children', hh_type == 210, database) couple_with_children = DefineVariable('couple_with_children', hh_type == 220, database) single_parent_with_children = DefineVariable('single_parent_with_children', hh_type == 230, database) not_family_household = DefineVariable('not_family_household', hh_type == 30, database) public_transport_connection_quality_ARE_A_home = DefineVariable( 'public_transport_connection_quality_ARE_A_home', public_transport_connection_quality_ARE_home == 1, database) public_transport_connection_quality_ARE_B_home = DefineVariable( 'public_transport_connection_quality_ARE_B_home', public_transport_connection_quality_ARE_home == 2, database) public_transport_connection_quality_ARE_C_home = DefineVariable( 'public_transport_connection_quality_ARE_C_home', public_transport_connection_quality_ARE_home == 3, database) public_transport_connection_quality_ARE_D_home = DefineVariable( 'public_transport_connection_quality_ARE_D_home', public_transport_connection_quality_ARE_home == 4, database) public_transport_connection_quality_ARE_NA_home = DefineVariable( 'public_transport_connection_quality_ARE_NA_home', public_transport_connection_quality_ARE_home == 5, database) public_transport_connection_quality_ARE_A_work = DefineVariable( 'public_transport_connection_quality_ARE_A_work', public_transport_connection_quality_ARE_work == 1, database) public_transport_connection_quality_ARE_B_work = DefineVariable( 'public_transport_connection_quality_ARE_B_work', public_transport_connection_quality_ARE_work == 2, database) public_transport_connection_quality_ARE_C_work = DefineVariable( 'public_transport_connection_quality_ARE_C_work', public_transport_connection_quality_ARE_work == 3, database) public_transport_connection_quality_ARE_D_work = DefineVariable( 'public_transport_connection_quality_ARE_D_work', public_transport_connection_quality_ARE_work == 4, database) public_transport_connection_quality_ARE_NA_work = DefineVariable( 'public_transport_connection_quality_ARE_NA_work', public_transport_connection_quality_ARE_work == 5, database) urban_home = DefineVariable('urban_home', urban_typology_home == 1, database) rural_home = DefineVariable('rural_home', urban_typology_home == 3, database) intermediate_home = DefineVariable('intermediate_home', urban_typology_home == 2, database) urban_work = DefineVariable('urban_work', urban_typology_work == 1, database) rural_work = DefineVariable('rural_work', urban_typology_work == 3, database) intermediate_work = DefineVariable('intermediate_work', urban_typology_work == 2, 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) employees = DefineVariable('employees', work_position == 2, database) executives = DefineVariable('executives', work_position == 1, database) german = DefineVariable('german', language == 1, database) nationality_switzerland = DefineVariable('nationality_switzerland', nation == 8100, database) nationality_germany_austria_lichtenstein = DefineVariable( 'nationality_germany_austria_lichtenstein', (nation == 8207) | (nation == 8229) | (nation == 8222), database) nationality_italy_vatican = DefineVariable( 'nationality_italy_vatican', (nation == 8218) | (nation == 8241), database) nationality_france_monaco_san_marino = DefineVariable( 'nationality_france_monaco_san_marino', (nation == 8212) | (nation == 8226) | (nation == 8233), database) nationality_northwestern_europe = DefineVariable( 'nationality_northwestern_europe', (nation == 8204) | # Belgium (nation == 8223) | # Luxembourg (nation == 8227) | # Netherlands (nation == 8206) | # Denmark (nation == 8211) | # Finland (nation == 8215) | # United Kingdom (nation == 8216) | # Ireland (nation == 8217) | # Iceland (nation == 8228) | # Norway (nation == 8234), # Sweden database) nationality_south_west_europe = DefineVariable( 'nationality_south_west_europe', (nation == 8231) | # Portugal (nation == 8236) | # Spain (nation == 8202), # Andorra database) nationality_southeast_europe = DefineVariable( 'nationality_southeast_europe', (nation == 8224) | # Malta (nation == 8201) | # Albania (nation == 8214) | # Greece (nation == 8256) | # Kosovo (nation == 8250) | # Croatia (nation == 8251) | # Slovenia (nation == 8252) | # Bosnia and Herzegovina (nation == 8255) | # Macedonia (nation == 8205) | # Bulgaria (nation == 8239) | # Turkey (nation == 8242) | # Cyprus (nation == 8248) | # Serbia (nation == 8254), # Montenegro database) nationality_eastern_europe = DefineVariable( 'nationality_eastern_europe', (nation == 8230) | # Poland (nation == 8232) | # Rumania (nation == 8240) | # Hungary (nation == 8243) | # Slovakia (nation == 8244) | # Czech Republic (nation == 8263) | # Moldavia (nation == 8265) | # Ukraine (nation == 8266) | # Belarus (nation == 8260) | # Estonia (nation == 8261) | # Latvia (nation == 8262), # Lithuania database) # several_part_time_jobs = DefineVariable('several_part_time_jobs', full_part_time_job == 3, database) hh_income_na = DefineVariable('hh_income_na', hh_income < 0, database) hh_income_less_than_2000 = DefineVariable('hh_income_less_than_4000', hh_income == 1, database) hh_income_2000_to_4000 = DefineVariable('hh_income_2000_to_4000', hh_income == 2, database) hh_income_4001_to_6000 = DefineVariable('hh_income_4001_to_6000', hh_income == 3, database) hh_income_6001_to_8000 = DefineVariable('hh_income_6001_to_8000', hh_income == 4, database) hh_income_8001_to_10000 = DefineVariable('hh_income_8001_to_10000', hh_income == 5, database) hh_income_10001_to_12000 = DefineVariable('hh_income_10001_to_12000', hh_income == 6, database) hh_income_12001_to_14000 = DefineVariable('hh_income_12001_to_14000', hh_income == 7, database) hh_income_14001_to_16000 = DefineVariable('hh_income_14001_to_16000', hh_income == 8, database) hh_income_more_than_16000 = DefineVariable('hh_income_more_than_16000', hh_income == 9, database) general_abo = DefineVariable('general_abo', GA_ticket == 1, database) regional_abo = DefineVariable('regional_abo', Verbund_Abo == 1, database) half_fare_abo = DefineVariable('half_fare_abo', halbtax_ticket == 1, database) car_avail_always_or_on_demand = DefineVariable( 'car_avail_always_or_on_demand', (car_avail == 1) | (car_avail == 2), database) regional_abo_na = DefineVariable('regional_abo_na', Verbund_Abo < 0, database) half_fare_abo_na = DefineVariable('half_fare_abo_na', halbtax_ticket < 0, database) car_avail_na = DefineVariable('car_avail_na', car_avail < 0, database) mobility_resource_na = DefineVariable('mobility_resource_na', mobility_resources == -98, database) mobility_resource_car_general_abo = DefineVariable( 'mobility_resource_car_general_abo', mobility_resources == 1, database) mobility_resource_car_half_fare_abo = DefineVariable( 'mobility_resource_car_half_fare_abo', mobility_resources == 2, database) mobility_resource_car = DefineVariable('mobility_resource_car', mobility_resources == 3, database) mobility_resource_general_abo = DefineVariable( 'mobility_resource_general_abo', mobility_resources == 4, database) mobility_resource_half_fare_abo = DefineVariable( 'mobility_resource_half_fare_abo', mobility_resources == 5, database) mobility_resource_none = DefineVariable('mobility_resource_none', mobility_resources == 6, database) mobility_resource_car_half_fare_regional_abo = DefineVariable( 'mobility_resource_car_half_fare_regional_abo', mobility_resources == 20, database) mobility_resource_car_regional_abo = DefineVariable( 'mobility_resource_car_regional_abo', mobility_resources == 30, database) mobility_resource_half_fare_regional_abo = DefineVariable( 'mobility_resource_half_fare_regional_abo', mobility_resources == 50, database) mobility_resource_regional_abo = DefineVariable( 'mobility_resource_regional_abo', mobility_resources == 60, database) # Utility U = alternative_specific_constant + \ b_executives * executives + \ b_employees * employees + \ 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_single_household * single_household + \ b_couple_without_children * couple_without_children + \ b_couple_with_children * couple_with_children + \ b_single_parent_with_children * single_parent_with_children + \ b_not_family_household * not_family_household + \ b_public_transport_connection_quality_are_a_home * public_transport_connection_quality_ARE_A_home + \ b_public_transport_connection_quality_are_b_home * public_transport_connection_quality_ARE_B_home + \ b_public_transport_connection_quality_are_c_home * public_transport_connection_quality_ARE_C_home + \ b_public_transport_connection_quality_are_d_home * public_transport_connection_quality_ARE_D_home + \ b_public_transport_connection_quality_are_na_home * public_transport_connection_quality_ARE_NA_home + \ b_public_transport_connection_quality_are_a_work * public_transport_connection_quality_ARE_A_work + \ b_public_transport_connection_quality_are_b_work * public_transport_connection_quality_ARE_B_work + \ b_public_transport_connection_quality_are_c_work * public_transport_connection_quality_ARE_C_work + \ b_public_transport_connection_quality_are_d_work * public_transport_connection_quality_ARE_D_work + \ b_public_transport_connection_quality_are_na_work * public_transport_connection_quality_ARE_NA_work + \ b_urban_home * urban_home + \ b_rural_home * rural_home + \ b_intermediate_home * intermediate_home + \ b_urban_work * urban_work + \ b_rural_work * rural_work + \ b_intermediate_work * intermediate_work + \ 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_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_lichtenstein + \ b_nationality_ch_germany_france_italy_nw_e * nationality_italy_vatican + \ b_nationality_ch_germany_france_italy_nw_e * nationality_france_monaco_san_marino + \ b_nationality_ch_germany_france_italy_nw_e * nationality_northwestern_europe + \ b_nationality_south_west_europe * nationality_south_west_europe + \ b_nationality_southeast_europe * nationality_southeast_europe + \ b_nationality_ch_germany_france_italy_nw_e * nationality_eastern_europe + \ models.piecewiseFormula(work_percentage, [0, 90, 101]) + \ b_hh_income_na * hh_income_na + \ b_hh_income_8000_or_less * hh_income_less_than_2000 + \ b_hh_income_8000_or_less * hh_income_2000_to_4000 + \ b_hh_income_8000_or_less * hh_income_4001_to_6000 + \ b_hh_income_8000_or_less * hh_income_6001_to_8000 + \ b_hh_income_more_than_8000 * hh_income_8001_to_10000 + \ b_hh_income_more_than_8000 * hh_income_10001_to_12000 + \ b_hh_income_more_than_8000 * hh_income_12001_to_14000 + \ b_hh_income_more_than_8000 * hh_income_14001_to_16000 + \ b_hh_income_more_than_8000 * hh_income_more_than_16000 + \ b_general_abo * general_abo + \ b_regional_abo * regional_abo + \ b_half_fare_abo * half_fare_abo + \ b_car_avail * car_avail_always_or_on_demand + \ b_regional_abo_na * regional_abo_na + \ b_half_fare_abo_na * half_fare_abo_na + \ b_car_avail_na * car_avail_na + \ b_mobility_resource_na * mobility_resource_na + \ b_mobility_resource_car_general_abo * mobility_resource_car_general_abo + \ b_mobility_resource_car_half_fare_abo * mobility_resource_car_half_fare_abo + \ b_mobility_resource_car * mobility_resource_car + \ b_mobility_resource_general_abo * mobility_resource_general_abo + \ b_mobility_resource_half_fare_abo * mobility_resource_half_fare_abo + \ b_mobility_resource_none * mobility_resource_none + \ b_mobility_resource_car_half_fare_regional_abo * mobility_resource_car_half_fare_regional_abo + \ b_mobility_resource_car_regional_abo * mobility_resource_car_regional_abo + \ b_mobility_resource_half_fare_regional_abo * mobility_resource_half_fare_regional_abo + \ b_mobility_resource_regional_abo * mobility_resource_regional_abo U_no_telecommuting = 0 # Associate utility functions with the numbering of alternatives V = { 1: 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() os.chdir(output_directory) # Create the Biogeme object biogeme = bio.BIOGEME(database, logprob) 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)