"""
File: 03-cross_nested.py

Michel Bierlaire
Mon Aug 04 2025, 08:52:03
"""

from biogeme.biogeme_logging import INFO, get_screen_logger

# The objective of this laboratory is to compare a nested logit specification with a cross nested logit specification.

# We consider the Swissmetro case study. It involves a choice set with three alternatives: Train, Car and Swissmetro.
# We consider a simple specification with the following variables:
#
# - travel time,
# - travel cost.


# The specification of the model is available in the file `spec_swissmetro.py`. The variables are available from the
# file `variables_swissmetro.py`.


# We set the logger to receive messages from Biogeme during the estimation. Indeed, it may take a while to estimate a cross-nested logit model, and it is useful to monitor the progress of the algorithm.
# Create a logger to display messages from Biogeme. Indeed, it may take a while to estimate a cross-nested logit model,
# and it is useful to monitor the progress of the algorithm.
logger = get_screen_logger(level=INFO)


# 1. Estimate the parameters of a nested logit model, where existing alternatives are included in the same nest.
# 2. Specify and estimate the parameters of a cross-nested logit model, with a nest for existing alternatives, and another nest for public transportation modes.
# 3. Which one of the two models would you keep, and why?

# ## Hints

# As the estimation time may be long, it is advised to "recycle" the estimation results. It can be done by adding the
# argument `recycle=True` to the `estimate` function. It then reads the estimation results from the pickle file, if
# it exists. It means that, if you need to run your notebook several times, the estimation itself will be done only
# once.

# Here is the syntax to specify a cross-nested logit model with two nests, a and b, and three alternatives:
# - Alternative 1, that belongs 0.25 to nest a and 0.75 to nest b
# - Alternative 2, that belongs 0.5 to nest a and 0.5 to nest b
# - Alternative 3, that belongs 0.7 to nest a and 0.3 to nest b.
#
# ```
# mu_nest_a = Beta('mu_nest_a', 1, 1, None, 0)
# mu_nest_b = Beta('mu_nest_b', 1, 1, None, 0)
#
# alpha_nest_a = {1: 0.25, 2: 0.5, 3: 0.7}
# alpha_nest_b = {1: 0.75, 2: 0.5, 3: 0.3}
#
# nest_a = OneNestForCrossNestedLogit(nest_param=mu_nest_a, dict_of_alpha=alpha_nest_a, name='nest a')
# nest_b = OneNestForCrossNestedLogit(nest_param=mu_nest_b, dict_of_alpha=alpha_nest_b, name='nest b')
#
# nests = NestsForCrossNestedLogit(choice_set=[1, 2, 3], tuple_of_nests=(nest_a, nest_b))
# ```
