{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8957e432",
   "metadata": {},
   "source": [
    "\n",
    "File: 04-elasticities.py\n",
    "\n",
    "\n",
    "Michel Bierlaire\n",
    "\n",
    "Sun Aug 03 2025, 17:27:36\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "724b52f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from IPython.core.display_functions import display\n",
    "from biogeme.biogeme import BIOGEME\n",
    "from biogeme.models import logit, loglogit\n",
    "from biogeme.results_processing import (\n",
    "    EstimationResults,\n",
    "    get_pandas_estimated_parameters,\n",
    ")\n",
    "\n",
    "from optima_specification import v_base\n",
    "from optima_variables import (\n",
    "    Choice,\n",
    "    database,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "833c41bc",
   "metadata": {},
   "source": [
    "The objective of this laboratory is to use an estimated choice model to calculate elasticities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48566ed5",
   "metadata": {},
   "outputs": [],
   "source": [
    "logprob = loglogit(v_base, None, Choice)\n",
    "model_name = 'optima_base'\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd5d01c0",
   "metadata": {},
   "source": [
    "If the model has already been estimated, we read the results from file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b730bcb",
   "metadata": {},
   "outputs": [],
   "source": [
    "biogeme = BIOGEME(database, logprob)\n",
    "biogeme.model_name = model_name\n",
    "results: EstimationResults = biogeme.estimate(run_bootstrap=True, recycle=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acc86e43",
   "metadata": {},
   "source": [
    "General statistics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42fddf83",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(results.short_summary())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0880899d",
   "metadata": {},
   "source": [
    "Estimated parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8cd74a8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(get_pandas_estimated_parameters(estimation_results=results))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a00cbf5f",
   "metadata": {},
   "source": [
    "# Choice probabilities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a926faf",
   "metadata": {},
   "outputs": [],
   "source": [
    "prob_pt = logit(v_base, None, 0)\n",
    "prob_car = logit(v_base, None, 1)\n",
    "prob_sm = logit(v_base, None, 2)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39ec921a",
   "metadata": {},
   "source": [
    "# Disaggregate elasticities"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73cfacb5",
   "metadata": {},
   "source": [
    "First, calculate the following disaggregate elasticities for all individuals in the sample."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e53cf30b",
   "metadata": {},
   "source": [
    "The direct point elasticity of travel time for public transportation is defined as\n",
    "$$ E_\\text{time}^{P_n(\\text{PT})} = \\frac{\\partial P_n(\\text{PT})}{\\partial \\text{time}}\n",
    "\\frac{\\text{time}}{P_n(\\text{PT})}.$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea67f6af",
   "metadata": {},
   "source": [
    "The derivative of an expression with respect to a literal is calculated using the expression `Derive`.\n",
    "For instance, $\\frac{\\partial P_n(\\text{PT})}{\\partial \\text{time}}$ is calculated as\n",
    "```\n",
    "Derive(prob_pt, 'TimePT')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "176c7aef",
   "metadata": {},
   "source": [
    "The direct point elasticity of travel cost for public transportation is defined as\n",
    "$$ E_\\text{cost}^{P_n(\\text{PT})} = \\frac{\\partial P_n(\\text{PT})}{\\partial \\text{cost}}\n",
    "\\frac{\\text{cost}}{P_n(\\text{PT})}.$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcabcf07",
   "metadata": {},
   "source": [
    "The direct point elasticity of travel time for car is defined as\n",
    "$$ E_\\text{time}^{P_n(\\text{car})} = \\frac{\\partial P_n(\\text{car})}{\\partial \\text{time}}\n",
    "\\frac{\\text{time}}{P_n(\\text{car})}.$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ac70f1e",
   "metadata": {},
   "source": [
    "The direct point elasticity of travel cost for car is defined as\n",
    "$$ E_\\text{cost}^{P_n(\\text{car})} = \\frac{\\partial P_n(\\text{car})}{\\partial \\text{cost}}\n",
    "\\frac{\\text{cost}}{P_n(\\text{car})}.$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a714f44",
   "metadata": {},
   "source": [
    "The direct point elasticity of distance for slow modes is defined as\n",
    "$$ E_\\text{distance}^{P_n(\\text{SM})} = \\frac{\\partial P_n(\\text{SM})}{\\partial \\text{distance}}\n",
    "\\frac{\\text{distance}}{P_n(\\text{SM})}.$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb92cfe7",
   "metadata": {},
   "source": [
    "Analyze more specifically the values for individual number 0 and individual number 2 in the sample."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e34e2dd2",
   "metadata": {},
   "source": [
    "For individual 0:\n",
    "- What would be the probability of choosing public transportation if the travel cost of this mode increases by 1%?\n",
    "- What would be the probability of choosing public transportation if the travel time of this mode increases by 1%?\n",
    "- What would be the probability of choosing slow mode for a trip that is 1% longer?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dc1b485",
   "metadata": {},
   "source": [
    "For individual 2: what would be the probability of choosing public transportation if the travel cost of this mode\n",
    "increases by 1%?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65540441",
   "metadata": {},
   "source": [
    "Aggregate elasticities capture the relative change at the level of the market shares. They can be derived from\n",
    "disaggregate elasticities using the following formula:\n",
    "$$E^{\\widehat{population_shares}(i)}_{x_{jk}}  =\\frac{1}{\\sum_{\\ell=1}^{N} \\omega_\\ell P_\\ell(i)}\\sum_{n=1}^{N}\\omega_n\n",
    "P_n(i) E^{P_n(i)}_{x_{jnk}}.$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "212af7b0",
   "metadata": {},
   "source": [
    "Calculate the aggregate elasticities for travel cost and travel time for public transportation. Can the demand be\n",
    "considered elastic with respect to these two attributes?"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
