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