{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "66df362c",
   "metadata": {},
   "source": [
    "\n",
    "File: 02-cross_nested.py\n",
    "\n",
    "\n",
    "Michel Bierlaire\n",
    "\n",
    "Mon Aug 04 2025, 10:02:57\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12ea7ddb",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import biogeme.biogeme_logging as blog\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "825121e1",
   "metadata": {},
   "source": [
    "The objective of this laboratory is to compare a logit model, a nested logit and a cross nested logit specifications."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e061f78",
   "metadata": {},
   "source": [
    "We consider the airline case study. It involves a choice set with three alternatives: Non stop, One stop, same\n",
    "airline and One stop, multiples airlines.\n",
    "We consider a specification with the following variables:\n",
    "- square root of the fare,\n",
    "- logarithm of the leg room,\n",
    "- schedule delay upon early arrival,\n",
    "- schedule delay upon late,\n",
    "- logarithm of elapsed time.\n",
    "\n",
    "The fare coefficient is interacted with trip purpose, and all coefficients are\n",
    "generic."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "547c5245",
   "metadata": {},
   "source": [
    "The specification of the model is available in the file `spec_airline.py`. The variables are available from the\n",
    "file `variables.py`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96d50e68",
   "metadata": {},
   "source": [
    "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": "a82bd215",
   "metadata": {},
   "outputs": [],
   "source": [
    "logger = blog.get_screen_logger(level=blog.INFO)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01c9f9fc",
   "metadata": {},
   "source": [
    "1. Estimate the parameters of a logit model with this specification.\n",
    "2. Specify and estimate the parameters of two different nested logit models.\n",
    "3. Specify and estimate the parameters of a cross-nested logit model.\n",
    "4. Which model would you keep, and why?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4d33e34",
   "metadata": {},
   "source": [
    "## Hints"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27082ab6",
   "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,\n",
    "if it exists. It means that, if you need to run your notebook several times, the estimation itself will be done\n",
    "only once."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78840c2f",
   "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
}
