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