"""
File: 02-latent_structural.py

Michel Bierlaire
Thu Aug 07 2025, 08:59:52
"""

import biogeme.biogeme_logging as blog
import pandas as pd
from biogeme.database import Database
from biogeme.expressions import (
    Variable,
)

# 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)

# 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.

# 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

# - 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.
