{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b81d6321",
   "metadata": {},
   "source": [
    "\n",
    "File: 02-aggregation.py\n",
    "\n",
    "\n",
    "Michel Bierlaire\n",
    "\n",
    "Sat Aug 02 2025, 18:41:06\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "518f3560",
   "metadata": {},
   "source": [
    "The objective of this laboratory is to calculate aggregated indicators from a choice model. We consider the model\n",
    "specification available in the file `netherlands_model.py`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ffe32ee",
   "metadata": {},
   "source": [
    "We want to calculate the market shares (and corresponding confidence intervals) for each of the two alternatives in\n",
    "the population using sample enumeration."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7b1b2f7",
   "metadata": {},
   "source": [
    "We consider four segments of the population, characterized by two age categories  (41 or older, and 40 or younger),\n",
    "and gender."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1ec5e68",
   "metadata": {},
   "source": [
    "The census data report the following number $N_g$ of individuals in each segment $g$ of the population."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5772f766",
   "metadata": {},
   "outputs": [],
   "source": [
    "census = {\n",
    "    'male_41_more': 4092390,\n",
    "    'male_40_less': 4151092,\n",
    "    'female_41_more': 3984028,\n",
    "    'female_40_less': 4428289,\n",
    "}\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "720b3369",
   "metadata": {},
   "source": [
    "Question 1: calculate a weight for each entry in the file `netherlands.dat` so that the sample is representative\n",
    "of the population."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a771bfa",
   "metadata": {},
   "source": [
    "Question 2: calculate the market shares for each of the two alternatives. First, the model should be obtained\n",
    "from the specification file and the parameters should be estimated. To calculate the choice probability of each\n",
    "of the two alternatives, we can use the syntax below:\n",
    "```\n",
    "simulate = {\n",
    "'Weight': Weight,\n",
    "'Prob. rail': prob_rail,\n",
    "'Prob. car': prob_car,\n",
    "}\n",
    "\n",
    "biosim = BIOGEME(database, simulate)\n",
    "simulated_values = biosim.simulate(results.getBetaValues())\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61185c0c",
   "metadata": {},
   "source": [
    "Question 3: calculate confidence intervals on the market shares calculated above. Note that, to do so, the model must\n",
    "be estimated using bootstrapping as shown below:\n",
    "```\n",
    "results_bootstrapping = biogeme.estimate(bootstrap=100)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8895901e",
   "metadata": {},
   "source": [
    "We obtain a sample of values for the parameters. Using simulation, we can calculate the 90% confidence intervals on\n",
    "the simulated quantities as follows:\n",
    "```\n",
    "betas = biogeme.freeBetaNames()\n",
    "b = results_bootstrapping.getBetasForSensitivityAnalysis(betas)\n",
    "left, right = biosim.confidenceIntervals(b, 0.9)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f9a5e21",
   "metadata": {},
   "source": [
    "Question 4: consider a scenario where the cost of rail is decreased by 10%. What would be the market shares (and the\n",
    "confidence intervals) under this scenario? To do so, we should first write the choice probability under the proposed\n",
    "scenario and then perform the simulation as shown in the previous steps."
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
