"""
File: 03-latent_sequential.py

Michel Bierlaire
Thu Aug 07 2025, 09:02:52
"""

import biogeme.biogeme_logging as blog
import pandas as pd
from biogeme.database import Database
from biogeme.expressions import Variable

# The objective of this series of laboratories is to specify and estimate an hybrid choice model, that is, a choice
# model with latent variables models, using both choice data and psychometric indicators.

# As the estimation time may be long, we ask Biogeme to report the details of the iterations.
logger = blog.get_screen_logger(level=blog.INFO)

# We consider the case study called ``Optima'', about a transportation mode choice
# model in Switzerland.

# # Read the data
df = pd.read_table('optima.dat')
database = Database('optima', df)


# Extract the variables from the data file

# Exclude observations such that the chosen alternative is -1
Choice = Variable('Choice')
database.remove(Choice == -1.0)

# The data contains many psychometric indicators. According to a factor
# analysis, the following indicators seem to be associated with one latent
# variable, that we will call ``active life'':
#
# - `ResidCh01`: *I like living in a neighborhood where a lot of things happen*,
# - `ResidCh04`: *I would like to have access to more services or activities*,
# - `ResidCh05`: *I would like to live in the city center of a big city*,
# - `ResidCh06`: *I would like to live in a town situated in the outskirts of a city*,
# - `LifSty07`: *The pleasure of having something beautiful consists in showing it*,
# - `LifSty10`: *I do not like to be in the same place for too long*.

# # Tasks performed in the previous lab

# - Specify a structural equation for this latent variable,
#   depending on the following variables:
#
#      - a dummy variable that is one if the individual is 30 or less,
#     zero otherwise,
#      - a dummy variable that is one if the individual is male, zero
#     otherwise,
#      - a dummy variable that is one if the individual has children,
#      zero otherwise,
#      - a dummy variable that is one if the individual has high level
#      of education (strictly higher than high school),
#      zero otherwise,
#      - a dummy variable that is one if the individual is artisan,
#      zero otherwise,
#      - a dummy variable that is one if the individual is employee,
#      zero otherwise,
#      - a dummy variable that is one if the main place of residence
#      of the individual when s/he was a kid was in the city center,
#      zero otherwise,
#      - a dummy variable that is one if the main place of residence
#      of the individual when s/he was a kid was in the suburbs,
#      zero otherwise.

# - Specify the measurement equations for the six
#   indicators. Remember to first define an equation for a (latent)
#   continuous indicator, and a second equation relating this latent
#   continuous indicator to the discrete indicators using an ordered
#   probit model.

# - Estimate the parameters of the structural and measurement
#   equations. Note that, in the absence of choice model, the error
#   terms of the two sets of equations are confounded. Therefore, the
#   error terms of the structural equation can be omitted.

# # Tasks
# - Specify a choice model with the following variables:
#     - travel time (PT, car), with an alternative specific
#       coefficient, interacted with the latent variable ``active life'',
#     - waiting time (PT),
#     - cost (PT, car), with a generic coefficient, interacted with
#       the trip purpose, where two categories are considered:
#       home-work-home, and others,
#     - distance (slow modes).
#
#   The interaction with the latent variable is captured by defining the
#   travel time coefficient as follows:
#   $$
#     \beta^\text{ref}_t e^{\beta^\text{AL}_t X^*},
#   $$
#   where $X^*$ is the latent variable capturing the level of ``active
#   life'' of the individual.
#   This specification is designed to avoid that the value of the latent
#   variable modifies the sign of the coefficient.

# - Estimate the choice model sequentially. It means that the
#   values of the coefficients of the structural equations estimated
#   earlier are used. Note that the error term of the structural
#   equation must be included here.
