def _convert_to_long_format(self):
        """
        Convert data to the long format expected by pylogit
        """
        model = self.model
        (alt_specific_vars,
         availability_vars) = self._encode_alternatives_as_integers()
        # Create observation number column as a range of integers from 1
        model.data[_OBSERVATION_COL] = np.arange(model.data.shape[0],
                                                 dtype=int)+1

        # Use pylogit routine to convert to long format
        self.long_data = pl.convert_wide_to_long(
            wide_data=model.data,
            ind_vars=model.alternative_independent_variables,
            alt_specific_vars=alt_specific_vars,
            availability_vars=availability_vars,
            obs_id_col=_OBSERVATION_COL,
            choice_col=_CHOICE_COL,
            new_alt_id_name=_CHOICE_ID_COL
            )

        # Remove choice bool and observation_id column from wide data
        model.data.drop(columns=[_CHOICE_COL, _OBSERVATION_COL],
                        inplace=True)
def convert_wide_to_long():
    # Print a status message
    print("Beginning Conversion Process.")

    # Load the wide format car data
    wide_car = pd.read_csv("../../data/raw/car_wide_format.csv")

    # Create the list of individual specific variables
    ind_variables = wide_car.columns.tolist()[1:4]

    # Specify the variables that vary across individuals and some or all
    # alternatives.

    # The keys are the column names that will be used in the long format df.
    # The values are dictionaries whose key-value pairs are the alternative id
    # and the column name of the corresponding column that encodes that
    # variable for the given alternative. Examples below.
    new_name_to_old_base = {
        'body_type': 'type{}',
        'fuel_type': 'fuel{}',
        'price_over_log_income': 'price{}',
        'range': 'range{}',
        'acceleration': 'acc{}',
        'top_speed': 'speed{}',
        'pollution': 'pollution{}',
        'vehicle_size': 'size{}',
        'luggage_space': 'space{}',
        'cents_per_mile': 'cost{}',
        'station_availability': 'station{}'
    }

    alt_varying_variables =\
        {k: dict([(x, v.format(x)) for x in range(1, 7)])
         for k, v in list(new_name_to_old_base.items())}

    # Specify the availability variables
    # Note that the keys of the dictionary are the alternative id's.
    # The values are the columns denoting the availability for the
    # given mode in the dataset.
    availability_variables =\
        {x: 'avail_{}'.format(x) for x in range(1, 7)}
    for col in availability_variables.values():
        wide_car[col] = 1

    ##########
    # Determine the columns for: alternative ids, the observation ids, and the choice
    ##########
    # The 'custom_alt_id' is the name of a column to be created in the long-
    # format data. It will identify the alternative associated with each row.
    custom_alt_id = "alt_id"

    # Create a custom id column that ignores the fact that this is a
    # panel/repeated-observations dataset. Note the +1 ensures the id's start
    # at one.
    obs_id_column = "obs_id"
    wide_car[obs_id_column] = np.arange(1, wide_car.shape[0] + 1, dtype=int)

    # Create a variable recording the choice column
    choice_column = "choice"
    # Store the original choice column in a new variable
    wide_car['orig_choices'] = wide_car['choice'].values
    # Alter the original choice column
    choice_str_to_value = {'choice{}'.format(x): x for x in range(1, 7)}
    wide_car[choice_column] = wide_car[choice_column].map(choice_str_to_value)

    # Convert the wide-format data to long format
    long_car =\
        pl.convert_wide_to_long(wide_data=wide_car,
                                ind_vars=ind_variables,
                                alt_specific_vars=alt_varying_variables,
                                availability_vars=availability_variables,
                                obs_id_col=obs_id_column,
                                choice_col=choice_column,
                                new_alt_id_name=custom_alt_id)

    # Save the long-format data to file.
    long_car.to_csv("../../data/interim/car_long_format.csv", index=False)

    # Print a status message.
    print("Finished Conversion Process.")
    return None
    # Simulate data without parents
    sim_bike_data_no_parent = sim.sim_node_no_parent(
        params_dict=bike_data_params, size=sim_size)

    sim_bike_data_wide = copy.deepcopy(sim_bike_data_no_parent)

    # Simulate nodes based on causal graphs
    exec(SIMULATE_NODES_WIDE)

    # Converting Data from Wide to Long
    print("Converting data from wide to long...")
    long_sim_data = pl.convert_wide_to_long(
        wide_data=sim_bike_data_wide,
        ind_vars=IND_VARIABLES,
        alt_specific_vars=ALT_VARYING_VARIABLES,
        availability_vars=AVAILABILITY_VARIABLES,
        obs_id_col=OBS_ID_COL,
        choice_col=CHOICE_COL,
        new_alt_id_name=CUSTOM_ALT_ID,
    )

    # Create a cars per licensed drivers column
    long_sim_data["cars_per_licensed_drivers"] = 0
    long_sim_data.loc[
        long_sim_data.num_licensed_drivers > 0,
        "cars_per_licensed_drivers"] = long_sim_data.num_cars / long_sim_data.num_licensed_drivers.astype(
            float)

    # Add a variable representing cost divided by distance
    long_sim_data["cost_per_distance"] = 0
    long_sim_data.loc[
Пример #4
0
def convert_wide_to_long(*args, **kwargs):
    return pylogit.convert_wide_to_long(*args, **kwargs)
Пример #5
0
fishing_ind_variables = ["site_choice", "boat", "kids", "ones"]

fishing_availability_variables = OrderedDict()
for alt_id, var in zip(range(1, 101),
                       ["availability_{}".format(i) for i in range(1, 101)]):
    fishing_availability_variables[alt_id] = var

# In[105]:

##########
# Actually perform the conversion to long format
##########
long_fishing_df = pl.convert_wide_to_long(
    wide_data=data,
    ind_vars=fishing_ind_variables,
    alt_specific_vars=fishing_alt_varying_variables,
    availability_vars=fishing_availability_variables,
    obs_id_col=fishing_obs_id_col,
    choice_col=fishing_choice_column,
    new_alt_id_name=fishing_alt_id)
# 240400 rows × 14 columns
# Look at the long format Heating data
long_fishing_df.head()
long_fishing_df.to_csv("long_data.csv")

# In[84]:

long_fishing_df

# In[ ]:

# In[ ]:
Пример #6
0
alt_varying_variables = {
    u'walk_time':
    dict([(2, 'tt_walk'), (3, 'walkTime_PT')]),
    u'vehicle_time':
    dict([(0, 'tt_drive'), (3, 'transitTime_PT')]),
    u'cycle_time':
    dict([(1, 'tt_cycle')]),
    u'cost':
    dict([(0, 'cost_drive'), (1, 'cost_cycle'), (2, 'cost_walk'),
          (3, 'cost_PT')])
}

longSimPop = pl.convert_wide_to_long(simPop,
                                     ind_variables,
                                     alt_varying_variables,
                                     availability_variables,
                                     obs_id_column,
                                     choice_column,
                                     new_alt_id_name=custom_alt_id)

#####################  Create the model specification #####################

basic_specification = OrderedDict()
basic_names = OrderedDict()

basic_specification["intercept"] = [1, 2, 3]
basic_names["intercept"] = ['ASC Walk', 'ASC Cycle', 'ASC Transit']

#basic_specification["lwBalance_home"] = [1, 2, 3]
#basic_names["lwBalance_home"] = [
#                          'lwBalance_home (Cycle)',
    u'mpg':
    dict([(1, 'EV_mpg'), (2, 'PHEV_mpg'), (3, 'CONV_mpg')]),
    u'annual_cost':
    dict([(1, 'EV_annual_cost'), (2, 'PHEV_annual_cost'),
          (3, 'CONV_annual_cost')]),
    u'parking_avail':
    dict([(1, 'EV_parking_avail'), (2, 'PHEV_parking_avail'),
          (3, 'CONV_parking_avail')]),
    u'charging_hrs':
    dict([(1, 'EV_charging_hrs'), (2, 'PHEV_charging_hrs'),
          (3, 'CONV_charging_hrs')])
}
availability_variables = {1: 'EV_AV', 2: 'PHEV_AV', 3: 'CONV_AV'}

custom_alt_id = "mode_id"
obs_id_column = "custom_id"
dataWide[obs_id_column] = np.arange(dataWide.shape[0], dtype=int) + 1

choice_column = "Choice"
dataWide[choice_column] = dataWide[choice_column].astype('int')

dataLong = pl.convert_wide_to_long(dataWide,
                                   ind_variables,
                                   alt_varying_variables,
                                   availability_variables,
                                   obs_id_column,
                                   choice_column,
                                   new_alt_id_name=custom_alt_id)

# TODO: ADD YOUR MODEL ESTIMATION SCRIPT HERE
Пример #8
0

def missingToUsable(missing, len_):
    return_arr = np.ones(len_)
    return_arr[list(missing)] = 0
    return_idx = np.where(return_arr == 1)[0]
    return (return_idx)


missing = missingIndexes()
usable = missingToUsable(missing, len(raw_data))

long_data = pl.convert_wide_to_long(raw_data,
                                    columns,
                                    alt_varying_variables,
                                    availability_variables,
                                    obs_id_column,
                                    choice_column,
                                    new_alt_id_name=custom_alt_id)

missing_long = missingIndexes(stack=long_data)
usable_long = missingToUsable(missing_long, len(long_data))


def addVariable(var_name,
                modes,
                desc_array,
                names=names,
                specification=specification):
    specification[var_name] = modes
    names[var_name] = desc_array
Пример #9
0
    def unpack_data_for_pylogit(self, df, entnames):
        """
        Unpacks a Multi-indexed pandas DataFrame in a way that can be consumed
        by pylogit in long-format.

        Inputs:
        -------
        df: DataFrame
            DataFrame with multi-index where each index is an entity and object
            of comparison made, for this code to be triggered these would also
            contain some features.

        entnames: list of strings
            The name of the columns that contain the entity names.


        Returns:
        --------
        long_x_comb: DataFrame
            The DataFrame ready for pylogit modelling in long-format
        """

        availability_vars = {}

        # Get the distinct entities
        all_unique_indices = get_distinct_entities(df)
        x_comb = df.reset_index()

        ind_variables = df.drop([self.target_col_name],
                                axis=1).columns.tolist()

        for name in entnames:
            x_comb[name] = x_comb[name].map(self.rplc_lkp)

        for i in all_unique_indices:
            colname = str(i) + '_AV'
            x_comb[colname] = np.where(
                (x_comb[entnames[0]] == self.rplc_lkp[i]) |
                (x_comb[entnames[1]] == self.rplc_lkp[i]), 1, 0)
            availability_vars[self.rplc_lkp[i]] = colname

        x_comb['CHOICE'] = np.where(x_comb[self.target_col_name] == 1,
                                    x_comb[entnames[0]], x_comb[entnames[1]])

        x_comb.drop(self.target_col_name, axis=1, inplace=True)

        custom_alt_id = 'entity'
        obs_id_column = 'observation'
        x_comb[obs_id_column] = np.arange(x_comb.shape[0], dtype=int) + 1
        choice_column = "CHOICE"

        x_comb.drop(entnames, axis=1, inplace=True)

        long_x_comb = pl.convert_wide_to_long(x_comb,
                                              ind_variables, {},
                                              availability_vars,
                                              obs_id_column,
                                              choice_column,
                                              new_alt_id_name=custom_alt_id)

        return long_x_comb
Пример #10
0
    6: 'K_AV',
    7: 'W_AV'
}

alt_id_col = 'ALT_ID'

# 'ID' is the decision-maker, but there are multiple observations (choice scenarios) for each
df['OBS_ID'] = np.arange(df.shape[0], dtype=int) + 1
obs_id_col = 'OBS_ID'

choice_col = 'CHOICE'

data = pylogit.convert_wide_to_long(df,
                                    ind_vars,
                                    alt_varying_vars,
                                    availability_vars,
                                    obs_id_col,
                                    choice_col,
                                    new_alt_id_name=alt_id_col)

# Specify important columns

ind_id_col = 'ID'
obs_id_col = 'OBS_ID'
alt_id_col = 'ALT_ID'
choice_col = 'CHOICE'

# base case is alt_id = 7 (walking)

spec = OrderedDict([('intercept', [1, 2, 3, 4, 5, 6]),
                    ('travel_time', [[1, 2], [3, 4, 5], [6, 7]]),
def generate_data(train=True):
    wide_lpmc = pd.read_table("lpmc.dat", sep="\t")

    #%%
    # define travel time for public transport
    #########################################
    wide_lpmc["dur_pt"] = wide_lpmc["dur_pt_access"] + wide_lpmc[
        "dur_pt_rail"] + wide_lpmc["dur_pt_bus"] + wide_lpmc["dur_pt_int"]

    # define cost for driving alternative
    #####################################
    wide_lpmc['cost_driving'] = wide_lpmc['cost_driving_fuel'] + wide_lpmc[
        'cost_driving_ccharge']

    # define avaiblity columns
    ##########################
    wide_lpmc['AV'] = np.ones(len(wide_lpmc['cost_driving']))

    ###############################################
    # SEGMENTATION WRT GENDER, PURPOSE, AGE, SEASON
    ###############################################
    '''
        we copy the columns into segmented ones and put 0 
        when the gender does not match the gender considered in the column.
    '''

    # Driving cost male:
    wide_lpmc['cost_driving_male_age1_cold_purpose1'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age1_cold_purpose2'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age1_cold_purpose3'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age1_cold_purpose4'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age1_cold_purpose5'] = wide_lpmc[
        'cost_driving']

    wide_lpmc['cost_driving_male_age1_warm_purpose1'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age1_warm_purpose2'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age1_warm_purpose3'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age1_warm_purpose4'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age1_warm_purpose5'] = wide_lpmc[
        'cost_driving']

    wide_lpmc['cost_driving_male_age2_cold_purpose1'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age2_cold_purpose2'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age2_cold_purpose3'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age2_cold_purpose4'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age2_cold_purpose5'] = wide_lpmc[
        'cost_driving']

    wide_lpmc['cost_driving_male_age2_warm_purpose1'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age2_warm_purpose2'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age2_warm_purpose3'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age2_warm_purpose4'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age2_warm_purpose5'] = wide_lpmc[
        'cost_driving']

    wide_lpmc['cost_driving_male_age3_cold_purpose1'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age3_cold_purpose2'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age3_cold_purpose3'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age3_cold_purpose4'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age3_cold_purpose5'] = wide_lpmc[
        'cost_driving']

    wide_lpmc['cost_driving_male_age3_warm_purpose1'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age3_warm_purpose2'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age3_warm_purpose3'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age3_warm_purpose4'] = wide_lpmc[
        'cost_driving']
    wide_lpmc['cost_driving_male_age3_warm_purpose5'] = wide_lpmc[
        'cost_driving']

    # Public transport cost male:
    wide_lpmc['cost_pt_male_age1_cold_purpose1'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age1_cold_purpose2'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age1_cold_purpose3'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age1_cold_purpose4'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age1_cold_purpose5'] = wide_lpmc['cost_transit']

    wide_lpmc['cost_pt_male_age1_warm_purpose1'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age1_warm_purpose2'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age1_warm_purpose3'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age1_warm_purpose4'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age1_warm_purpose5'] = wide_lpmc['cost_transit']

    wide_lpmc['cost_pt_male_age2_cold_purpose1'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age2_cold_purpose2'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age2_cold_purpose3'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age2_cold_purpose4'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age2_cold_purpose5'] = wide_lpmc['cost_transit']

    wide_lpmc['cost_pt_male_age2_warm_purpose1'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age2_warm_purpose2'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age2_warm_purpose3'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age2_warm_purpose4'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age2_warm_purpose5'] = wide_lpmc['cost_transit']

    wide_lpmc['cost_pt_male_age3_cold_purpose1'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age3_cold_purpose2'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age3_cold_purpose3'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age3_cold_purpose4'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age3_cold_purpose5'] = wide_lpmc['cost_transit']

    wide_lpmc['cost_pt_male_age3_warm_purpose1'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age3_warm_purpose2'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age3_warm_purpose3'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age3_warm_purpose4'] = wide_lpmc['cost_transit']
    wide_lpmc['cost_pt_male_age3_warm_purpose5'] = wide_lpmc['cost_transit']

    # Driving time male:
    wide_lpmc['dur_driving_male_age1_cold_purpose1'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age1_cold_purpose2'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age1_cold_purpose3'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age1_cold_purpose4'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age1_cold_purpose5'] = wide_lpmc['dur_driving']

    wide_lpmc['dur_driving_male_age1_warm_purpose1'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age1_warm_purpose2'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age1_warm_purpose3'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age1_warm_purpose4'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age1_warm_purpose5'] = wide_lpmc['dur_driving']

    wide_lpmc['dur_driving_male_age2_cold_purpose1'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age2_cold_purpose2'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age2_cold_purpose3'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age2_cold_purpose4'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age2_cold_purpose5'] = wide_lpmc['dur_driving']

    wide_lpmc['dur_driving_male_age2_warm_purpose1'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age2_warm_purpose2'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age2_warm_purpose3'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age2_warm_purpose4'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age2_warm_purpose5'] = wide_lpmc['dur_driving']

    wide_lpmc['dur_driving_male_age3_cold_purpose1'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age3_cold_purpose2'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age3_cold_purpose3'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age3_cold_purpose4'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age3_cold_purpose5'] = wide_lpmc['dur_driving']

    wide_lpmc['dur_driving_male_age3_warm_purpose1'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age3_warm_purpose2'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age3_warm_purpose3'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age3_warm_purpose4'] = wide_lpmc['dur_driving']
    wide_lpmc['dur_driving_male_age3_warm_purpose5'] = wide_lpmc['dur_driving']

    # Public transport time male:
    wide_lpmc['dur_pt_male_age1_cold_purpose1'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age1_cold_purpose2'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age1_cold_purpose3'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age1_cold_purpose4'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age1_cold_purpose5'] = wide_lpmc['dur_pt']

    wide_lpmc['dur_pt_male_age1_warm_purpose1'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age1_warm_purpose2'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age1_warm_purpose3'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age1_warm_purpose4'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age1_warm_purpose5'] = wide_lpmc['dur_pt']

    wide_lpmc['dur_pt_male_age2_cold_purpose1'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age2_cold_purpose2'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age2_cold_purpose3'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age2_cold_purpose4'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age2_cold_purpose5'] = wide_lpmc['dur_pt']

    wide_lpmc['dur_pt_male_age2_warm_purpose1'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age2_warm_purpose2'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age2_warm_purpose3'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age2_warm_purpose4'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age2_warm_purpose5'] = wide_lpmc['dur_pt']

    wide_lpmc['dur_pt_male_age3_cold_purpose1'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age3_cold_purpose2'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age3_cold_purpose3'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age3_cold_purpose4'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age3_cold_purpose5'] = wide_lpmc['dur_pt']

    wide_lpmc['dur_pt_male_age3_warm_purpose1'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age3_warm_purpose2'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age3_warm_purpose3'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age3_warm_purpose4'] = wide_lpmc['dur_pt']
    wide_lpmc['dur_pt_male_age3_warm_purpose5'] = wide_lpmc['dur_pt']

    # Walking time male:
    wide_lpmc['dur_walking_male_age1_cold_purpose1'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age1_cold_purpose2'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age1_cold_purpose3'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age1_cold_purpose4'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age1_cold_purpose5'] = wide_lpmc['dur_walking']

    wide_lpmc['dur_walking_male_age1_warm_purpose1'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age1_warm_purpose2'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age1_warm_purpose3'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age1_warm_purpose4'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age1_warm_purpose5'] = wide_lpmc['dur_walking']

    wide_lpmc['dur_walking_male_age2_cold_purpose1'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age2_cold_purpose2'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age2_cold_purpose3'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age2_cold_purpose4'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age2_cold_purpose5'] = wide_lpmc['dur_walking']

    wide_lpmc['dur_walking_male_age2_warm_purpose1'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age2_warm_purpose2'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age2_warm_purpose3'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age2_warm_purpose4'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age2_warm_purpose5'] = wide_lpmc['dur_walking']

    wide_lpmc['dur_walking_male_age3_cold_purpose1'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age3_cold_purpose2'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age3_cold_purpose3'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age3_cold_purpose4'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age3_cold_purpose5'] = wide_lpmc['dur_walking']

    wide_lpmc['dur_walking_male_age3_warm_purpose1'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age3_warm_purpose2'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age3_warm_purpose3'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age3_warm_purpose4'] = wide_lpmc['dur_walking']
    wide_lpmc['dur_walking_male_age3_warm_purpose5'] = wide_lpmc['dur_walking']

    # Cycling time male:
    wide_lpmc['dur_cycling_male_age1_cold_purpose1'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age1_cold_purpose2'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age1_cold_purpose3'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age1_cold_purpose4'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age1_cold_purpose5'] = wide_lpmc['dur_cycling']

    wide_lpmc['dur_cycling_male_age1_warm_purpose1'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age1_warm_purpose2'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age1_warm_purpose3'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age1_warm_purpose4'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age1_warm_purpose5'] = wide_lpmc['dur_cycling']

    wide_lpmc['dur_cycling_male_age2_cold_purpose1'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age2_cold_purpose2'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age2_cold_purpose3'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age2_cold_purpose4'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age2_cold_purpose5'] = wide_lpmc['dur_cycling']

    wide_lpmc['dur_cycling_male_age2_warm_purpose1'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age2_warm_purpose2'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age2_warm_purpose3'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age2_warm_purpose4'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age2_warm_purpose5'] = wide_lpmc['dur_cycling']

    wide_lpmc['dur_cycling_male_age3_cold_purpose1'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age3_cold_purpose2'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age3_cold_purpose3'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age3_cold_purpose4'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age3_cold_purpose5'] = wide_lpmc['dur_cycling']

    wide_lpmc['dur_cycling_male_age3_warm_purpose1'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age3_warm_purpose2'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age3_warm_purpose3'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age3_warm_purpose4'] = wide_lpmc['dur_cycling']
    wide_lpmc['dur_cycling_male_age3_warm_purpose5'] = wide_lpmc['dur_cycling']

    # Distance male:
    wide_lpmc['distance_male_age1_cold_purpose1'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age1_cold_purpose2'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age1_cold_purpose3'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age1_cold_purpose4'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age1_cold_purpose5'] = wide_lpmc['distance']

    wide_lpmc['distance_male_age1_warm_purpose1'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age1_warm_purpose2'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age1_warm_purpose3'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age1_warm_purpose4'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age1_warm_purpose5'] = wide_lpmc['distance']

    wide_lpmc['distance_male_age2_cold_purpose1'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age2_cold_purpose2'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age2_cold_purpose3'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age2_cold_purpose4'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age2_cold_purpose5'] = wide_lpmc['distance']

    wide_lpmc['distance_male_age2_warm_purpose1'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age2_warm_purpose2'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age2_warm_purpose3'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age2_warm_purpose4'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age2_warm_purpose5'] = wide_lpmc['distance']

    wide_lpmc['distance_male_age3_cold_purpose1'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age3_cold_purpose2'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age3_cold_purpose3'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age3_cold_purpose4'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age3_cold_purpose5'] = wide_lpmc['distance']

    wide_lpmc['distance_male_age3_warm_purpose1'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age3_warm_purpose2'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age3_warm_purpose3'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age3_warm_purpose4'] = wide_lpmc['distance']
    wide_lpmc['distance_male_age3_warm_purpose5'] = wide_lpmc['distance']

    ###########
    ## Def Lambdas
    ######

    # Distance male:
    wide_lpmc['lambda_male_age1_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age1_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age1_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age1_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age1_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_male_age1_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age1_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age1_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age1_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age1_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_male_age2_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age2_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age2_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age2_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age2_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_male_age2_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age2_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age2_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age2_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age2_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_male_age3_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age3_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age3_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age3_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age3_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_male_age3_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age3_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age3_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age3_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_male_age3_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_time_male_age1_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age1_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age1_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age1_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age1_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_time_male_age1_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age1_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age1_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age1_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age1_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_time_male_age2_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age2_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age2_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age2_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age2_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_time_male_age2_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age2_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age2_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age2_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age2_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_time_male_age3_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age3_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age3_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age3_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age3_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_time_male_age3_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age3_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age3_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age3_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_time_male_age3_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_cost_male_age1_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age1_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age1_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age1_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age1_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_cost_male_age1_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age1_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age1_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age1_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age1_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_cost_male_age2_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age2_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age2_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age2_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age2_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_cost_male_age2_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age2_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age2_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age2_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age2_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_cost_male_age3_cold_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age3_cold_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age3_cold_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age3_cold_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age3_cold_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    wide_lpmc['lambda_cost_male_age3_warm_purpose1'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age3_warm_purpose2'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age3_warm_purpose3'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age3_warm_purpose4'] = np.ones(
        len(wide_lpmc['cost_driving']))
    wide_lpmc['lambda_cost_male_age3_warm_purpose5'] = np.ones(
        len(wide_lpmc['cost_driving']))

    #%% FILTERS

    filter_age1 = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("age1")]
    filter_age2 = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("age2")]
    filter_age3 = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("age3")]

    wide_lpmc.loc[(wide_lpmc['age'] > 18), filter_age1] = 0
    wide_lpmc.loc[((wide_lpmc['age'] <= 18) & (wide_lpmc['age'] > 64)),
                  filter_age2] = 0
    wide_lpmc.loc[(wide_lpmc['age'] <= 64), filter_age3] = 0

    filter_warm = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("warm")]
    filter_cold = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("cold")]

    wide_lpmc.loc[((wide_lpmc['travel_month'] != 12) &
                   (wide_lpmc['travel_month'] > 2)), filter_cold] = 0
    wide_lpmc.loc[((wide_lpmc['travel_month'] == 12) |
                   (wide_lpmc['travel_month'] == 1) |
                   (wide_lpmc['travel_month'] == 2)), filter_warm] = 0

    filter_p1 = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("purpose1")]
    filter_p2 = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("purpose2")]
    filter_p3 = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("purpose3")]
    filter_p4 = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("purpose4")]
    filter_p5 = wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("purpose5")]

    wide_lpmc.loc[(wide_lpmc['purpose'] != 1), filter_p1] = 0
    wide_lpmc.loc[(wide_lpmc['purpose'] != 2), filter_p2] = 0
    wide_lpmc.loc[(wide_lpmc['purpose'] != 3), filter_p3] = 0
    wide_lpmc.loc[(wide_lpmc['purpose'] != 4), filter_p4] = 0
    wide_lpmc.loc[(wide_lpmc['purpose'] != 5), filter_p5] = 0

    #%%
    # delete useless columns (can be discussed)
    ##############################################
    del wide_lpmc['dur_pt_access']
    del wide_lpmc['dur_pt_rail']
    del wide_lpmc['dur_pt_bus']
    del wide_lpmc['dur_pt_int']
    del wide_lpmc['cost_driving_fuel']
    del wide_lpmc['cost_driving_ccharge']
    del wide_lpmc['driving_license']
    del wide_lpmc['car_ownership']
    del wide_lpmc['trip_id']
    del wide_lpmc['household_id']
    del wide_lpmc['person_n']
    del wide_lpmc['trip_n']
    del wide_lpmc['purpose']
    del wide_lpmc['fueltype']
    del wide_lpmc['faretype']
    del wide_lpmc['bus_scale']
    del wide_lpmc['female']
    del wide_lpmc['pt_interchanges']
    del wide_lpmc['driving_traffic_percent']
    if (train):
        wide_lpmc = wide_lpmc.drop(
            wide_lpmc[wide_lpmc['survey_year'] != 3].index)
    else:
        wide_lpmc = wide_lpmc.drop(
            wide_lpmc[wide_lpmc['survey_year'] == 3].index)
    del wide_lpmc['survey_year']
    del wide_lpmc['travel_year']
    del wide_lpmc['travel_month']
    del wide_lpmc['travel_date']
    del wide_lpmc['day_of_week']
    del wide_lpmc['start_time']
    del wide_lpmc['dur_driving']
    del wide_lpmc['dur_walking']
    del wide_lpmc['dur_pt']
    del wide_lpmc['dur_cycling']
    del wide_lpmc['distance']
    del wide_lpmc['cost_driving']
    del wide_lpmc['cost_transit']
    del wide_lpmc['age']

    #%%
    ''' ind_vars : list of strings.
        Each element should be a column heading in `wide_data` that denotes a
        variable that varies across observations but not across alternatives.
    '''

    ind_variables = list(wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("distance")])
    ind_variables = ind_variables + list(wide_lpmc.columns[pd.Series(
        wide_lpmc.columns).str.contains("lambda")])
    #ind_variables=ind_variables+list(wide_lpmc['survey_year'])
    #%%

    alt_varying_variables = {
        u'travel_time_male_age1_cold_purpose1':
        dict([(1, 'dur_walking_male_age1_cold_purpose1'),
              (2, 'dur_cycling_male_age1_cold_purpose1'),
              (3, 'dur_pt_male_age1_cold_purpose1'),
              (4, 'dur_driving_male_age1_cold_purpose1')]),
        u'travel_time_male_age1_cold_purpose2':
        dict([(1, 'dur_walking_male_age1_cold_purpose2'),
              (2, 'dur_cycling_male_age1_cold_purpose2'),
              (3, 'dur_pt_male_age1_cold_purpose2'),
              (4, 'dur_driving_male_age1_cold_purpose2')]),
        u'travel_time_male_age1_cold_purpose3':
        dict([(1, 'dur_walking_male_age1_cold_purpose3'),
              (2, 'dur_cycling_male_age1_cold_purpose3'),
              (3, 'dur_pt_male_age1_cold_purpose3'),
              (4, 'dur_driving_male_age1_cold_purpose3')]),
        u'travel_time_male_age1_cold_purpose4':
        dict([(1, 'dur_walking_male_age1_cold_purpose4'),
              (2, 'dur_cycling_male_age1_cold_purpose4'),
              (3, 'dur_pt_male_age1_cold_purpose4'),
              (4, 'dur_driving_male_age1_cold_purpose4')]),
        u'travel_time_male_age1_cold_purpose5':
        dict([(1, 'dur_walking_male_age1_cold_purpose5'),
              (2, 'dur_cycling_male_age1_cold_purpose5'),
              (3, 'dur_pt_male_age1_cold_purpose5'),
              (4, 'dur_driving_male_age1_cold_purpose5')]),
        u'travel_time_male_age1_warm_purpose1':
        dict([(1, 'dur_walking_male_age1_warm_purpose1'),
              (2, 'dur_cycling_male_age1_warm_purpose1'),
              (3, 'dur_pt_male_age1_warm_purpose1'),
              (4, 'dur_driving_male_age1_warm_purpose1')]),
        u'travel_time_male_age1_warm_purpose2':
        dict([(1, 'dur_walking_male_age1_warm_purpose2'),
              (2, 'dur_cycling_male_age1_warm_purpose2'),
              (3, 'dur_pt_male_age1_warm_purpose2'),
              (4, 'dur_driving_male_age1_warm_purpose2')]),
        u'travel_time_male_age1_warm_purpose3':
        dict([(1, 'dur_walking_male_age1_warm_purpose3'),
              (2, 'dur_cycling_male_age1_warm_purpose3'),
              (3, 'dur_pt_male_age1_warm_purpose3'),
              (4, 'dur_driving_male_age1_warm_purpose3')]),
        u'travel_time_male_age1_warm_purpose4':
        dict([(1, 'dur_walking_male_age1_warm_purpose4'),
              (2, 'dur_cycling_male_age1_warm_purpose4'),
              (3, 'dur_pt_male_age1_warm_purpose4'),
              (4, 'dur_driving_male_age1_warm_purpose4')]),
        u'travel_time_male_age1_warm_purpose5':
        dict([(1, 'dur_walking_male_age1_warm_purpose5'),
              (2, 'dur_cycling_male_age1_warm_purpose5'),
              (3, 'dur_pt_male_age1_warm_purpose5'),
              (4, 'dur_driving_male_age1_warm_purpose5')]),
        u'travel_time_male_age2_cold_purpose1':
        dict([(1, 'dur_walking_male_age2_cold_purpose1'),
              (2, 'dur_cycling_male_age2_cold_purpose1'),
              (3, 'dur_pt_male_age2_cold_purpose1'),
              (4, 'dur_driving_male_age2_cold_purpose1')]),
        u'travel_time_male_age2_cold_purpose2':
        dict([(1, 'dur_walking_male_age2_cold_purpose2'),
              (2, 'dur_cycling_male_age2_cold_purpose2'),
              (3, 'dur_pt_male_age2_cold_purpose2'),
              (4, 'dur_driving_male_age2_cold_purpose2')]),
        u'travel_time_male_age2_cold_purpose3':
        dict([(1, 'dur_walking_male_age2_cold_purpose3'),
              (2, 'dur_cycling_male_age2_cold_purpose3'),
              (3, 'dur_pt_male_age2_cold_purpose3'),
              (4, 'dur_driving_male_age2_cold_purpose3')]),
        u'travel_time_male_age2_cold_purpose4':
        dict([(1, 'dur_walking_male_age2_cold_purpose4'),
              (2, 'dur_cycling_male_age2_cold_purpose4'),
              (3, 'dur_pt_male_age2_cold_purpose4'),
              (4, 'dur_driving_male_age2_cold_purpose4')]),
        u'travel_time_male_age2_cold_purpose5':
        dict([(1, 'dur_walking_male_age2_cold_purpose5'),
              (2, 'dur_cycling_male_age2_cold_purpose5'),
              (3, 'dur_pt_male_age2_cold_purpose5'),
              (4, 'dur_driving_male_age2_cold_purpose5')]),
        u'travel_time_male_age2_warm_purpose1':
        dict([(1, 'dur_walking_male_age2_warm_purpose1'),
              (2, 'dur_cycling_male_age2_warm_purpose1'),
              (3, 'dur_pt_male_age2_warm_purpose1'),
              (4, 'dur_driving_male_age2_warm_purpose1')]),
        u'travel_time_male_age2_warm_purpose2':
        dict([(1, 'dur_walking_male_age2_warm_purpose2'),
              (2, 'dur_cycling_male_age2_warm_purpose2'),
              (3, 'dur_pt_male_age2_warm_purpose2'),
              (4, 'dur_driving_male_age2_warm_purpose2')]),
        u'travel_time_male_age2_warm_purpose3':
        dict([(1, 'dur_walking_male_age2_warm_purpose3'),
              (2, 'dur_cycling_male_age2_warm_purpose3'),
              (3, 'dur_pt_male_age2_warm_purpose3'),
              (4, 'dur_driving_male_age2_warm_purpose3')]),
        u'travel_time_male_age2_warm_purpose4':
        dict([(1, 'dur_walking_male_age2_warm_purpose4'),
              (2, 'dur_cycling_male_age2_warm_purpose4'),
              (3, 'dur_pt_male_age2_warm_purpose4'),
              (4, 'dur_driving_male_age2_warm_purpose4')]),
        u'travel_time_male_age2_warm_purpose5':
        dict([(1, 'dur_walking_male_age2_warm_purpose5'),
              (2, 'dur_cycling_male_age2_warm_purpose5'),
              (3, 'dur_pt_male_age2_warm_purpose5'),
              (4, 'dur_driving_male_age2_warm_purpose5')]),
        u'travel_time_male_age3_cold_purpose1':
        dict([(1, 'dur_walking_male_age3_cold_purpose1'),
              (2, 'dur_cycling_male_age3_cold_purpose1'),
              (3, 'dur_pt_male_age3_cold_purpose1'),
              (4, 'dur_driving_male_age3_cold_purpose1')]),
        u'travel_time_male_age3_cold_purpose2':
        dict([(1, 'dur_walking_male_age3_cold_purpose2'),
              (2, 'dur_cycling_male_age3_cold_purpose2'),
              (3, 'dur_pt_male_age3_cold_purpose2'),
              (4, 'dur_driving_male_age3_cold_purpose2')]),
        u'travel_time_male_age3_cold_purpose3':
        dict([(1, 'dur_walking_male_age3_cold_purpose3'),
              (2, 'dur_cycling_male_age3_cold_purpose3'),
              (3, 'dur_pt_male_age3_cold_purpose3'),
              (4, 'dur_driving_male_age3_cold_purpose3')]),
        u'travel_time_male_age3_cold_purpose4':
        dict([(1, 'dur_walking_male_age3_cold_purpose4'),
              (2, 'dur_cycling_male_age3_cold_purpose4'),
              (3, 'dur_pt_male_age3_cold_purpose4'),
              (4, 'dur_driving_male_age3_cold_purpose4')]),
        u'travel_time_male_age3_cold_purpose5':
        dict([(1, 'dur_walking_male_age3_cold_purpose5'),
              (2, 'dur_cycling_male_age3_cold_purpose5'),
              (3, 'dur_pt_male_age3_cold_purpose5'),
              (4, 'dur_driving_male_age3_cold_purpose5')]),
        u'travel_time_male_age3_warm_purpose1':
        dict([(1, 'dur_walking_male_age3_warm_purpose1'),
              (2, 'dur_cycling_male_age3_warm_purpose1'),
              (3, 'dur_pt_male_age3_warm_purpose1'),
              (4, 'dur_driving_male_age3_warm_purpose1')]),
        u'travel_time_male_age3_warm_purpose2':
        dict([(1, 'dur_walking_male_age3_warm_purpose2'),
              (2, 'dur_cycling_male_age3_warm_purpose2'),
              (3, 'dur_pt_male_age3_warm_purpose2'),
              (4, 'dur_driving_male_age3_warm_purpose2')]),
        u'travel_time_male_age3_warm_purpose3':
        dict([(1, 'dur_walking_male_age3_warm_purpose3'),
              (2, 'dur_cycling_male_age3_warm_purpose3'),
              (3, 'dur_pt_male_age3_warm_purpose3'),
              (4, 'dur_driving_male_age3_warm_purpose3')]),
        u'travel_time_male_age3_warm_purpose4':
        dict([(1, 'dur_walking_male_age3_warm_purpose4'),
              (2, 'dur_cycling_male_age3_warm_purpose4'),
              (3, 'dur_pt_male_age3_warm_purpose4'),
              (4, 'dur_driving_male_age3_warm_purpose4')]),
        u'travel_time_male_age3_warm_purpose5':
        dict([(1, 'dur_walking_male_age3_warm_purpose5'),
              (2, 'dur_cycling_male_age3_warm_purpose5'),
              (3, 'dur_pt_male_age3_warm_purpose5'),
              (4, 'dur_driving_male_age3_warm_purpose5')]),
        u'travel_cost_male_age1_cold_purpose1':
        dict([(3, 'cost_pt_male_age1_cold_purpose1'),
              (4, 'cost_driving_male_age1_cold_purpose1')]),
        u'travel_cost_male_age1_cold_purpose2':
        dict([(3, 'cost_pt_male_age1_cold_purpose2'),
              (4, 'cost_driving_male_age1_cold_purpose2')]),
        u'travel_cost_male_age1_cold_purpose3':
        dict([(3, 'cost_pt_male_age1_cold_purpose3'),
              (4, 'cost_driving_male_age1_cold_purpose3')]),
        u'travel_cost_male_age1_cold_purpose4':
        dict([(3, 'cost_pt_male_age1_cold_purpose4'),
              (4, 'cost_driving_male_age1_cold_purpose4')]),
        u'travel_cost_male_age1_cold_purpose5':
        dict([(3, 'cost_pt_male_age1_cold_purpose5'),
              (4, 'cost_driving_male_age1_cold_purpose5')]),
        u'travel_cost_male_age1_warm_purpose1':
        dict([(3, 'cost_pt_male_age1_warm_purpose1'),
              (4, 'cost_driving_male_age1_warm_purpose1')]),
        u'travel_cost_male_age1_warm_purpose2':
        dict([(3, 'cost_pt_male_age1_warm_purpose2'),
              (4, 'cost_driving_male_age1_warm_purpose2')]),
        u'travel_cost_male_age1_warm_purpose3':
        dict([(3, 'cost_pt_male_age1_warm_purpose3'),
              (4, 'cost_driving_male_age1_warm_purpose3')]),
        u'travel_cost_male_age1_warm_purpose4':
        dict([(3, 'cost_pt_male_age1_warm_purpose4'),
              (4, 'cost_driving_male_age1_warm_purpose4')]),
        u'travel_cost_male_age1_warm_purpose5':
        dict([(3, 'cost_pt_male_age1_warm_purpose5'),
              (4, 'cost_driving_male_age1_warm_purpose5')]),
        u'travel_cost_male_age2_cold_purpose1':
        dict([(3, 'cost_pt_male_age2_cold_purpose1'),
              (4, 'cost_driving_male_age2_cold_purpose1')]),
        u'travel_cost_male_age2_cold_purpose2':
        dict([(3, 'cost_pt_male_age2_cold_purpose2'),
              (4, 'cost_driving_male_age2_cold_purpose2')]),
        u'travel_cost_male_age2_cold_purpose3':
        dict([(3, 'cost_pt_male_age2_cold_purpose3'),
              (4, 'cost_driving_male_age2_cold_purpose3')]),
        u'travel_cost_male_age2_cold_purpose4':
        dict([(3, 'cost_pt_male_age2_cold_purpose4'),
              (4, 'cost_driving_male_age2_cold_purpose4')]),
        u'travel_cost_male_age2_cold_purpose5':
        dict([(3, 'cost_pt_male_age2_cold_purpose5'),
              (4, 'cost_driving_male_age2_cold_purpose5')]),
        u'travel_cost_male_age2_warm_purpose1':
        dict([(3, 'cost_pt_male_age2_warm_purpose1'),
              (4, 'cost_driving_male_age2_warm_purpose1')]),
        u'travel_cost_male_age2_warm_purpose2':
        dict([(3, 'cost_pt_male_age2_warm_purpose2'),
              (4, 'cost_driving_male_age2_warm_purpose2')]),
        u'travel_cost_male_age2_warm_purpose3':
        dict([(3, 'cost_pt_male_age2_warm_purpose3'),
              (4, 'cost_driving_male_age2_warm_purpose3')]),
        u'travel_cost_male_age2_warm_purpose4':
        dict([(3, 'cost_pt_male_age2_warm_purpose4'),
              (4, 'cost_driving_male_age2_warm_purpose4')]),
        u'travel_cost_male_age2_warm_purpose5':
        dict([(3, 'cost_pt_male_age2_warm_purpose5'),
              (4, 'cost_driving_male_age2_warm_purpose5')]),
        u'travel_cost_male_age3_cold_purpose1':
        dict([(3, 'cost_pt_male_age3_cold_purpose1'),
              (4, 'cost_driving_male_age3_cold_purpose1')]),
        u'travel_cost_male_age3_cold_purpose2':
        dict([(3, 'cost_pt_male_age3_cold_purpose2'),
              (4, 'cost_driving_male_age3_cold_purpose2')]),
        u'travel_cost_male_age3_cold_purpose3':
        dict([(3, 'cost_pt_male_age3_cold_purpose3'),
              (4, 'cost_driving_male_age3_cold_purpose3')]),
        u'travel_cost_male_age3_cold_purpose4':
        dict([(3, 'cost_pt_male_age3_cold_purpose4'),
              (4, 'cost_driving_male_age3_cold_purpose4')]),
        u'travel_cost_male_age3_cold_purpose5':
        dict([(3, 'cost_pt_male_age3_cold_purpose5'),
              (4, 'cost_driving_male_age3_cold_purpose5')]),
        u'travel_cost_male_age3_warm_purpose1':
        dict([(3, 'cost_pt_male_age3_warm_purpose1'),
              (4, 'cost_driving_male_age3_warm_purpose1')]),
        u'travel_cost_male_age3_warm_purpose2':
        dict([(3, 'cost_pt_male_age3_warm_purpose2'),
              (4, 'cost_driving_male_age3_warm_purpose2')]),
        u'travel_cost_male_age3_warm_purpose3':
        dict([(3, 'cost_pt_male_age3_warm_purpose3'),
              (4, 'cost_driving_male_age3_warm_purpose3')]),
        u'travel_cost_male_age3_warm_purpose4':
        dict([(3, 'cost_pt_male_age3_warm_purpose4'),
              (4, 'cost_driving_male_age3_warm_purpose4')]),
        u'travel_cost_male_age3_warm_purpose5':
        dict([(3, 'cost_pt_male_age3_warm_purpose5'),
              (4, 'cost_driving_male_age3_warm_purpose5')]),
    }

    #%%

    availability_variables = {1: 'AV', 2: 'AV', 3: 'AV', 4: 'AV'}

    custom_alt_id = "mode_id"

    # obs_id_column = "trip_id"
    # wide_lpmc["trip_id"] =wide_lpmc["trip_id"]+1

    obs_id_column = "custom_id"
    wide_lpmc[obs_id_column] = np.arange(wide_lpmc.shape[0], dtype=int) + 1

    # Create a variable recording the choice column
    choice_column = "travel_mode"

    long_lpmc = pl.convert_wide_to_long(wide_lpmc,
                                        ind_variables,
                                        alt_varying_variables,
                                        availability_variables,
                                        obs_id_column,
                                        choice_column,
                                        new_alt_id_name=custom_alt_id)

    return long_lpmc