{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73ac4594",
   "metadata": {},
   "source": [
    "\n",
    "File: 03-cross_nested.py\n",
    "\n",
    "\n",
    "Michel Bierlaire\n",
    "\n",
    "Mon Aug 04 2025, 08:52:03\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9b02a56",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from biogeme.biogeme_logging import INFO, get_screen_logger\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a896a86",
   "metadata": {},
   "source": [
    "The objective of this laboratory is to compare a nested logit specification with a cross nested logit specification."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0314495d",
   "metadata": {},
   "source": [
    "We consider the Swissmetro case study. It involves a choice set with three alternatives: Train, Car and Swissmetro.\n",
    "We consider a simple specification with the following variables:\n",
    "\n",
    "- travel time,\n",
    "- travel cost."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad0a41d6",
   "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": "51fa8920",
   "metadata": {},
   "source": [
    "We set the logger to receive messages from Biogeme during the estimation. Indeed, it may take a while to estimate a cross-nested logit model, and it is useful to monitor the progress of the algorithm.\n",
    "Create a logger to display messages from Biogeme. Indeed, it may take a while to estimate a cross-nested logit model,\n",
    "and it is useful to monitor the progress of the algorithm."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f89de7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "logger = get_screen_logger(level=INFO)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e07be686",
   "metadata": {},
   "source": [
    "1. Estimate the parameters of a nested logit model, where existing alternatives are included in the same nest.\n",
    "2. Specify and estimate the parameters of a cross-nested logit model, with a nest for existing alternatives, and another nest for public transportation modes.\n",
    "3. Which one of the two models would you keep, and why?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b051a781",
   "metadata": {},
   "source": [
    "## Hints"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3be15a24",
   "metadata": {},
   "source": [
    "As the estimation time may be long, it is advised to \"recycle\" the estimation results. It can be done by adding the\n",
    "argument `recycle=True` to the `estimate` function. It then reads the estimation results from the pickle file, if\n",
    "it exists. It means that, if you need to run your notebook several times, the estimation itself will be done only\n",
    "once."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7958f459",
   "metadata": {},
   "source": [
    "Here is the syntax to specify a cross-nested logit model with two nests, a and b, and three alternatives:\n",
    "- Alternative 1, that belongs 0.25 to nest a and 0.75 to nest b\n",
    "- Alternative 2, that belongs 0.5 to nest a and 0.5 to nest b\n",
    "- Alternative 3, that belongs 0.7 to nest a and 0.3 to nest b.\n",
    "\n",
    "```\n",
    "mu_nest_a = Beta('mu_nest_a', 1, 1, None, 0)\n",
    "mu_nest_b = Beta('mu_nest_b', 1, 1, None, 0)\n",
    "\n",
    "alpha_nest_a = {1: 0.25, 2: 0.5, 3: 0.7}\n",
    "alpha_nest_b = {1: 0.75, 2: 0.5, 3: 0.3}\n",
    "\n",
    "nest_a = OneNestForCrossNestedLogit(nest_param=mu_nest_a, dict_of_alpha=alpha_nest_a, name='nest a')\n",
    "nest_b = OneNestForCrossNestedLogit(nest_param=mu_nest_b, dict_of_alpha=alpha_nest_b, name='nest b')\n",
    "\n",
    "nests = NestsForCrossNestedLogit(choice_set=[1, 2, 3], tuple_of_nests=(nest_a, nest_b))\n",
    "```"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
