{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0770ee3e",
   "metadata": {},
   "source": [
    "File 02-binary_netherlands-heterogeneity\n",
    "\n",
    "\n",
    "Michel Bierlaire\n",
    "\n",
    "Sat Aug 02 2025, 16:59:07\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "632785b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import pandas as pd\n",
    "from IPython.core.display_functions import display\n",
    "from biogeme.biogeme import BIOGEME\n",
    "from biogeme.database import Database\n",
    "from biogeme.expressions import Beta, Variable, exp, log\n",
    "from biogeme.results_processing import (\n",
    "    EstimationResults,\n",
    "    get_pandas_estimated_parameters,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2ff0d5b",
   "metadata": {},
   "source": [
    "The goal of this computer session is to investigate the\n",
    "heterogeneity of taste in the population."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1f3cfcc",
   "metadata": {},
   "source": [
    "We are using the binary transportation mode choice data, collected in\n",
    "the Netherlands. The data set is available as\n",
    "http://transp-or.epfl.ch/data/netherlands.dat\n",
    "and its description is available\n",
    "http://transp-or.epfl.ch/documents/technicalReports/CS_NetherlandsDescription.pdf."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50bd3a9e",
   "metadata": {},
   "source": [
    "# Data preparation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10b3cba3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('netherlands.dat', sep='\\t')\n",
    "database = Database('netherlands', df)\n",
    "\n",
    "sp = Variable('sp')\n",
    "rail_ivtt = Variable('rail_ivtt')\n",
    "rail_acc_time = Variable('rail_acc_time')\n",
    "rail_egr_time = Variable('rail_egr_time')\n",
    "car_ivtt = Variable('car_ivtt')\n",
    "car_walk_time = Variable('car_walk_time')\n",
    "car_cost = Variable('car_cost')\n",
    "rail_cost = Variable('rail_cost')\n",
    "choice = Variable('choice')\n",
    "\n",
    "exclude = sp != 0\n",
    "database.remove(exclude)\n",
    "\n",
    "rail_time = rail_ivtt + rail_acc_time + rail_egr_time\n",
    "car_time = car_ivtt + car_walk_time\n",
    "DUTCH_GUILDERS_TO_EUROS = 0.44378022\n",
    "car_cost_euro = car_cost * DUTCH_GUILDERS_TO_EUROS\n",
    "rail_cost_euro = rail_cost * DUTCH_GUILDERS_TO_EUROS\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4c92e24",
   "metadata": {},
   "source": [
    "# Base model\n",
    "We first define the base model, where no heterogeneity of taste is considered."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3a53312",
   "metadata": {},
   "outputs": [],
   "source": [
    "asc_car = Beta('asc_car', 0, None, None, 0)\n",
    "beta_cost = Beta('beta_cost', 0, None, None, 0)\n",
    "beta_time_car = Beta('beta_time_car', 0, None, None, 0)\n",
    "beta_time_rail = Beta('beta_time_rail', 0, None, None, 0)\n",
    "\n",
    "v_car = asc_car + beta_cost * car_cost_euro + beta_time_car * car_time\n",
    "v_rail = beta_cost * rail_cost_euro + beta_time_rail * rail_time\n",
    "prob_car = 1 / (1 + exp(v_rail - v_car))\n",
    "prob_rail = 1 - prob_car\n",
    "prob_observation = prob_car * (choice == 0) + prob_rail * (choice == 1)\n",
    "logprob = log(prob_observation)\n",
    "biogeme = BIOGEME(database, logprob)\n",
    "biogeme.model_name = 'binary_netherlands_socio_eco_base'\n",
    "results_base: EstimationResults = biogeme.estimate()\n",
    "\n",
    "print(results_base.short_summary())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74d69baf",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(get_pandas_estimated_parameters(estimation_results=results_base))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74369570",
   "metadata": {},
   "source": [
    "# Questions\n",
    "1. Split the database into two parts: one with only business trips, and one with only non business trips, and\n",
    "re-estimate the base model on each of them.\n",
    "2. Write a model that includes two set of parameters, one for business and one for other purposes, and estimate it\n",
    "on the full data set. Compare the results with the models estimated on the separate data sets.\n",
    "3. Now, impose the coefficient of travel time by car to be the same for business trip and non business trips. Which\n",
    "model would you prefer?"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
