"""
File: 01-nested.py

Michel Bierlaire
Mon Aug 04 2025, 09:52:59
"""

from biogeme.results_processing import EstimationResults

# The objective of this laboratory is to compare a nested logit specification with a logit specification.


# We consider the Swissmetro case study. It involves a choice set with three alternatives: Train, Car and Swissmetro.
# We consider a specification with the following variables:
#
# - the logarithm of the travel time,
# - the square root of the headway,
#
# and the following interactions:
# - all alternative specific constants interacted with gender,
# - all cost coefficients interacted with the travel class,
# - the time coefficient of Train and Swissmetro interacted with the ownership of a yearly subscription (GA),
# - the headway coefficient of Train and Swissmetro interacted with age.

# The specification of the model is available in the file `spec_swissmetro.py`. The variables are available from the
# file `variables_swissmetro.py`.


# 1. Estimate the parameters of a logit model with this specification.
# 2. Propose two different nested logit models, and estimate their parameters.
# 3. Which one of the three models would you keep, and why?

# ## Hints

# ### Syntax for the definition of the nests with Biogeme.

# Consider an example with four alternatives (1, 2, 3, 4), and two nests (A and B). The definition of the nests is
# done as follows:
# ```
# mu_a = Beta('mu_a', 1, 0, None, 0)
# mu_b = Beta('mu_b', 1, 0, None, 0)
# nest_a = OneNestForNestedLogit(nest_param=mu_a, list_of_alternatives=[1, 2], name='nest a')
# nest_b = OneNestForNestedLogit(nest_param=mu_b, list_of_alternatives=[3, 4], name='nest b')
# nests = NestsForNestedLogit(choice_set=list(V), tuple_of_nests=(nest_a, nest_b))
# ```

# The nest definition is then transmitted to the nested logit model:
# ```
# logprob = lognested(V, av, nests, CHOICE)
# ```
# When a nest contains only one alternative, the nest parameter is not identified and must be set to 1.

# ### Likelihood ratio test

# It is possible to perform a likelihood ratio test directly from the estimation results.
help(EstimationResults.likelihood_ratio_test)


# For instance, the test can be performed as follows:
# ```
# nested_results.likelihood_ratio_test(logit_results, significance_level=0.05)
# ```
