{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "716a1c18",
   "metadata": {},
   "source": [
    "\n",
    "File: 01-testing.py\n",
    "\n",
    "\n",
    "Michel Bierlaire\n",
    "\n",
    "Sat Aug 02 2025, 17:39:56\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdbfd9e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from IPython.core.display_functions import display\n",
    "from biogeme.results_processing import (\n",
    "    get_pandas_correlation_results,\n",
    "    get_pandas_estimated_parameters,\n",
    ")\n",
    "\n",
    "from logit_airline_base import results as res_base\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfaaf704",
   "metadata": {},
   "source": [
    "The objective of this laboratory is to investigate various specification tests."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9225b338",
   "metadata": {},
   "source": [
    "We consider the models developed in an earlier laboratory for the airline itinerary choice. We read the estimation\n",
    "results directly from the specification files available in the same directory."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5726790b",
   "metadata": {},
   "source": [
    "Base model:\n",
    "\\begin{align*}\n",
    "V_class_1 &= \\beta_\\text{fare}  \\text{fare}_1 + \\beta_\\text{legroom}  \\text{legroom}_1\n",
    "+ \\beta_\\text{sd\\_early} \\text{sched\\_delay\\_early}_1  + \\beta_\\text{sd\\_late} \\text{sched\\_delay\\_late}_1\n",
    "+ \\beta_\\text{time} \\text{elapsed\\_time}_1 \\\\\n",
    "V_class_2 &= \\text{cte}_2 + \\beta_\\text{fare}  \\text{fare}_2 + \\beta_\\text{legroom}  \\text{legroom}_2\n",
    "+ \\beta_\\text{sd\\_early} \\text{sched\\_delay\\_early}_2  + \\beta_\\text{sd\\_late} \\text{sched\\_delay\\_late}_2\n",
    "+ \\beta_\\text{time} \\text{elapsed\\_time}_2 \\\\\n",
    "V_3 &= \\text{cte}_3 + \\beta_\\text{fare}  \\text{fare}_3 + \\beta_\\text{legroom}  \\text{legroom}_3\n",
    "+ \\beta_\\text{sd\\_early} \\text{sched\\_delay\\_early}_3  + \\beta_\\text{sd\\_late} \\text{sched\\_delay\\_late}_3\n",
    "+ \\beta_\\text{time} \\text{elapsed\\_time}_3 \\\\\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aea08b11",
   "metadata": {},
   "source": [
    "Alternative specific time coefficient:\n",
    "\\begin{align*}\n",
    "V_class_1 &= \\beta_\\text{fare}  \\text{fare}_1 + \\beta_\\text{legroom}  \\text{legroom}_1\n",
    "+ \\beta_\\text{sd\\_early} \\text{sched\\_delay\\_early}_1  + \\beta_\\text{sd\\_late} \\text{sched\\_delay\\_late}_1\n",
    "+ \\beta_\\text{time, 1} \\text{elapsed\\_time}_1 \\\\\n",
    "V_class_2 &= \\text{cte}_2 + \\beta_\\text{fare}  \\text{fare}_2 + \\beta_\\text{legroom}  \\text{legroom}_2\n",
    "+ \\beta_\\text{sd\\_early} \\text{sched\\_delay\\_early}_2  + \\beta_\\text{sd\\_late} \\text{sched\\_delay\\_late}_2\n",
    "+ \\beta_\\text{time, 2} \\text{elapsed\\_time}_2 \\\\\n",
    "V_3 &= \\text{cte}_3 + \\beta_\\text{fare}  \\text{fare}_3 + \\beta_\\text{legroom}  \\text{legroom}_3\n",
    "+ \\beta_\\text{sd\\_early} \\text{sched\\_delay\\_early}_3  + \\beta_\\text{sd\\_late} \\text{sched\\_delay\\_late}_3\n",
    "+ \\beta_\\text{time, 2} \\text{elapsed\\_time}_3 \\\\\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2482a533",
   "metadata": {},
   "source": [
    "Question 1: test the null hypothesis that `beta_elapsed\\_time_2` is equal to `beta_elapsed\\_time_3`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d577c241",
   "metadata": {},
   "source": [
    "Question 2: test the null hypothesis that, between the two models, the base model is the true model."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94354c90",
   "metadata": {},
   "source": [
    "Consider the model where the fare coefficient varies with income."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0409731d",
   "metadata": {},
   "source": [
    "Question 3: Test the null hypothesis that the fare coefficient does not vary with income."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21ae4736",
   "metadata": {},
   "source": [
    "Consider the model with Box-Cox transform of the time variable."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acfef3d5",
   "metadata": {},
   "source": [
    "Question 4: test the linear specification with alternative specific time parameters, against the nonlinear\n",
    "specification with Box-Cox transform of the time variable."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9ae9f96",
   "metadata": {},
   "source": [
    "Consider the piecewise linear specification."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e3386c2",
   "metadata": {},
   "source": [
    "Question 4: test the piecewise linear specification against the base model."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ed3f6e3",
   "metadata": {},
   "source": [
    "Question 5: test the piecewise linear specification against the specification with the alternative specific time\n",
    "coefficient."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8d4518d",
   "metadata": {},
   "source": [
    "The objects of class `results` contains all the necessary information to perform the tests."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3850041d",
   "metadata": {},
   "source": [
    "General statistics can be obtained from a dictionary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d14aa1a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(res_base.get_general_statistics())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64216af2",
   "metadata": {},
   "source": [
    "The estimates of the parameters and relevant statistics are available from a Pandas data frame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72732aeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(get_pandas_estimated_parameters(estimation_results=res_base))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c3eb1bb",
   "metadata": {},
   "source": [
    "Information about the covariance/correlation between pairs of parameters are also available from a Pandas data frame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "870858a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(get_pandas_correlation_results(estimation_results=res_base))"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
