"""
File: 02-cross_nested.py

Michel Bierlaire
Mon Aug 04 2025, 10:02:57
"""

import biogeme.biogeme_logging as blog

# The objective of this laboratory is to compare a logit model, a nested logit and a cross nested logit specifications.


# We consider the airline case study. It involves a choice set with three alternatives: Non stop, One stop, same
# airline and One stop, multiples airlines.
# We consider a specification with the following variables:
# - square root of the fare,
# - logarithm of the leg room,
# - schedule delay upon early arrival,
# - schedule delay upon late,
# - logarithm of elapsed time.
#
# The fare coefficient is interacted with trip purpose, and all coefficients are
# generic.

# The specification of the model is available in the file `spec_airline.py`. The variables are available from the
# file `variables.py`.


# 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 = blog.get_screen_logger(level=blog.INFO)


# 1. Estimate the parameters of a logit model with this specification.
# 2. Specify and estimate the parameters of two different nested logit models.
# 3. Specify and estimate the parameters of a cross-nested logit model.
# 4. Which model 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))
# ```
