"""File 02-logit_swissmetro.py


Michel Bierlaire, EPFL
Sat Aug 02 2025, 17:29:00
"""

import pandas as pd
from IPython.core.display_functions import display
from biogeme.biogeme import BIOGEME
from biogeme.database import Database
from biogeme.expressions import Beta, Variable
from biogeme.models import loglogit
from biogeme.results_processing import (
    EstimationResults,
    get_pandas_estimated_parameters,
)
from biogeme.segmentation import DiscreteSegmentationTuple, segmented_beta

# The objective of this exercise is to introduce variations of the choice set across observations, and to investigate
# interactions of socio-economic characteristics and the model parameters.

# We use the Swissmetro data, available as
# [transp-or.epfl.ch/data/swissmetro.dat](http://transp-or.epfl.ch/data/swissmetro.dat). The description of the data
# is available [here](http://transp-or.epfl.ch/documents/technicalReports/CS_SwissmetroDescription.pdf). We advise to
# download the data in your local directory.

# We  read the data and import them into Biogeme's database.
df = pd.read_csv('swissmetro.dat', sep='\t')
database = Database('swissmetro', df)

# We identify the variables that will enter the model specification.
SM_CO = Variable('SM_CO')
TRAIN_CO = Variable('TRAIN_CO')
CAR_CO = Variable('CAR_CO')
SM_TT = Variable('SM_TT')
TRAIN_TT = Variable('TRAIN_TT')
CAR_TT = Variable('CAR_TT')

TRAIN_HE = Variable('TRAIN_HE')
SM_HE = Variable('SM_HE')

SP = Variable('SP')
GA = Variable('GA')
PURPOSE = Variable('PURPOSE')

CHOICE = Variable('CHOICE')
TRAIN_AV = Variable('TRAIN_AV')
SM_AV = Variable('SM_AV')
CAR_AV = Variable('CAR_AV')


# We keep only data corresponding to commuters (`PURPOSE == 1`) and business trips (`PURPOSE == 3`), and remove
# observations where the choice is incorrectly coded (`CHOICE == 0`).
exclude = ((PURPOSE != 1) * (PURPOSE != 3) + (CHOICE == 0)) > 0
database.remove(exclude)

# Definition of new variables
SM_COST = SM_CO * (GA == 0)
TRAIN_COST = TRAIN_CO * (GA == 0)
CAR_AV_SP = CAR_AV * (SP != 0)
TRAIN_AV_SP = TRAIN_AV * (SP != 0)
TRAIN_TT_SCALED = TRAIN_TT / 100
TRAIN_COST_SCALED = TRAIN_COST / 100
SM_TT_SCALED = SM_TT / 100
SM_COST_SCALED = SM_COST / 100
CAR_TT_SCALED = CAR_TT / 100
CAR_CO_SCALED = CAR_CO / 100

# Parameters to be estimated
asc_car = Beta('asc_car', 0, None, None, 0)
asc_train = Beta('asc_train', 0, None, None, 0)
b_time = Beta('b_time', 0, None, None, 0)
b_cost = Beta('b_cost', 0, None, None, 0)
b_headway = Beta('B_HEADWAY', 0, None, None, 0)

# Definition of the utility functions
v_train = (
    asc_train
    + b_time * TRAIN_TT_SCALED
    + b_cost * TRAIN_COST_SCALED
    + b_headway * TRAIN_HE
)
v_swissmetro = b_time * SM_TT_SCALED + b_cost * SM_COST_SCALED + b_headway * SM_HE
v_car = asc_car + b_time * CAR_TT_SCALED + b_cost * CAR_CO_SCALED

# Associate utility functions with the numbering of alternatives
v = {1: v_train, 2: v_swissmetro, 3: v_car}

# The choice set is different from one observation to the next. In order to characterize it, we associate
# availability conditions with each alternative. These conditions are defined in the data file.
av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}

# Definition of the model. This is the contribution of each
# observation to the log likelihood function.
logprob = loglogit(v, av, CHOICE)

# Create the Biogeme object
biogeme = BIOGEME(database, logprob)
biogeme.model_name = 'logit_swissmetro_base'

# Calculate the null log likelihood for reporting.
null_log_likelihood = biogeme.calculate_null_loglikelihood(av)
display(null_log_likelihood)

# Estimate the parameters
results: EstimationResults = biogeme.estimate()

# Summary statistics
print(results.short_summary())

# Get the results in a pandas table
pandas_results = get_pandas_estimated_parameters(estimation_results=results)
display(pandas_results)

# In order to capture the  heterogeneity of the taste parameters, the population can be segmented using socio-economic
# characteristics, and a different parameter associated with each segment.

# Suppose that we want to create a segmentation based on trip purpose. There are only two trip purposes in our sample:
# commuters (`PURPOSE == 1`) and business trips (`PURPOSE == 3`). We define the segmentation using a tuple with two
# entries: a Biogeme variable, and a mapping between the value of the variable and the name of the corresponding
# segment.
purpose_segmentation = DiscreteSegmentationTuple(
    variable=PURPOSE, mapping={1: 'commuter', 3: 'business'}
)

# If we want to define a parameter for each segment, we use the function `segment_parameter`that takes two arguments:
# a parameters, and a list of segmentation objects, as defined above (in this example, the list contains only one
# segmentation).
b_time_car = Beta('b_time_car', 0, None, None, 0)
segmented_b_time_car = segmented_beta(b_time_car, [purpose_segmentation])

# Now, the expression `segmented_B_TIME_CAR` can be included in the utility function like a regular parameter.
v_car = asc_car + segmented_b_time_car * CAR_TT_SCALED + b_cost * CAR_CO_SCALED

# Investigate now segmentations based on:
# - luggage,
# - groups (current rail users, or current ar users),
# - GA (yearly subscription).
#
# Note that the GA segmentation is special, as the cost of public transportation (`TRAIN` or `SM`) is zero for those
# who own a GA. Therefore, there is no way to estimate a cost parameter for that segment.
#
# Investigate also alternative specific parameters.
