{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de1be440",
   "metadata": {},
   "source": [
    "\n",
    "File: 05-wtp.py\n",
    "\n",
    "\n",
    "Michel Bierlaire\n",
    "\n",
    "Sun Aug 03 2025, 17:27:41\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cc7b0c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from IPython.core.display_functions import display\n",
    "from biogeme.biogeme import BIOGEME\n",
    "from biogeme.expressions import Beta, logzero\n",
    "from biogeme.models import loglogit\n",
    "from biogeme.results_processing import (\n",
    "    EstimationResults,\n",
    "    get_pandas_estimated_parameters,\n",
    ")\n",
    "\n",
    "from optima_variables import (\n",
    "    Choice,\n",
    "    CostCarCHF,\n",
    "    MarginalCostPT,\n",
    "    NbTransf,\n",
    "    TimeCar,\n",
    "    TimePT,\n",
    "    database,\n",
    "    distance_km,\n",
    "    female,\n",
    "    fulltime,\n",
    "    male,\n",
    "    notfulltime,\n",
    "    unreportedGender,\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ed2d937",
   "metadata": {},
   "source": [
    "The objective of this laboratory is to calculate the willingness to pay for the improvement of some attributes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f95a1163",
   "metadata": {},
   "source": [
    "Consider the two models introduced below. For each of them, perform the following calculations:\n",
    "\n",
    "- Calculate the value of time for each individual in the sample for the public transportation alternative, and plot the distribution.\n",
    "- Calculate the value of time for each individual in the sample for the car alternative, and plot the distribution.\n",
    "- Calculate the average value in the population of these two quantities.\n",
    "- Calculate the willingness to pay to save one transfer for each individual in the sample, and plot the distribution."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bf619ef",
   "metadata": {},
   "source": [
    "Hints:\n",
    "- Take into account that the variable 'NbTransf' is discrete, and not continuous.\n",
    "- The non linear model involves logarithms, that require a positive argument."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3a61ba7",
   "metadata": {},
   "source": [
    "# First model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bbe739a",
   "metadata": {},
   "source": [
    "In the first model, the variables involved for the calculation of the willingness to pay appear linearly in the\n",
    "utility functions."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c4a142c",
   "metadata": {},
   "source": [
    "Parameters to be estimated."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e2dc261",
   "metadata": {},
   "outputs": [],
   "source": [
    "asc_car = Beta('asc_car', 0, None, None, 0)\n",
    "asc_sm = Beta('asc_sm', 0, None, None, 0)\n",
    "beta_time_fulltime_pt = Beta('beta_time_fulltime_pt', 0, None, None, 0)\n",
    "beta_time_other_pt = Beta('beta_time_other_pt', 0, None, None, 0)\n",
    "beta_time_fulltime_car = Beta('beta_time_fulltime_car', 0, None, None, 0)\n",
    "beta_time_other_car = Beta('beta_time_other_car', 0, None, None, 0)\n",
    "\n",
    "beta_dist_male = Beta('beta_dist_male', 0, None, None, 0)\n",
    "beta_dist_female = Beta('beta_dist_female', 0, None, None, 0)\n",
    "beta_dist_unreported = Beta('beta_dist_unreported', 0, None, None, 0)\n",
    "beta_cost = Beta('beta_cost', 0, None, None, 0)\n",
    "beta_transf = Beta('beta_transf', 0, None, None, 0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93fab534",
   "metadata": {},
   "source": [
    "Utility functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1415e4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "v_pt = (\n",
    "    beta_time_fulltime_pt * TimePT * fulltime\n",
    "    + beta_time_other_pt * TimePT * notfulltime\n",
    "    + beta_cost * MarginalCostPT\n",
    "    + beta_transf * NbTransf\n",
    ")\n",
    "v_car = (\n",
    "    asc_car\n",
    "    + beta_time_fulltime_car * TimeCar * fulltime\n",
    "    + beta_time_other_car * TimeCar * notfulltime\n",
    "    + beta_cost * CostCarCHF\n",
    ")\n",
    "v_sm = (\n",
    "    asc_sm\n",
    "    + beta_dist_male * distance_km * male\n",
    "    + beta_dist_female * distance_km * female\n",
    "    + beta_dist_unreported * distance_km * unreportedGender\n",
    ")\n",
    "v = {0: v_pt, 1: v_car, 2: v_sm}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "681ace38",
   "metadata": {},
   "source": [
    "Estimation of the parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c30c902c",
   "metadata": {},
   "outputs": [],
   "source": [
    "logprob = loglogit(v, None, Choice)\n",
    "biogeme = BIOGEME(database, logprob)\n",
    "biogeme.model_name = 'wtp_first_model'\n",
    "results: EstimationResults = biogeme.estimate()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0a3626b",
   "metadata": {},
   "source": [
    "General statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "577c2c11",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(results.short_summary())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "220672e3",
   "metadata": {},
   "source": [
    "Estimated parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99744f02",
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = get_pandas_estimated_parameters(estimation_results=results)\n",
    "display(parameters)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "661e965c",
   "metadata": {},
   "source": [
    "# Second model: linear model in willingness to pay space."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eec65062",
   "metadata": {},
   "source": [
    "Consider the moneymetric specification of the above model, and use it to calculate the same willingness to pay\n",
    "indicators."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "347964a6",
   "metadata": {},
   "source": [
    "# Third model: non linear transformation of the variables"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e051079",
   "metadata": {},
   "source": [
    "We use a Biogeme expression that returns the log of a variable if it is non zero, and 0 otherwise.\n",
    "The variable is assumed to be non negative."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ef4e403",
   "metadata": {},
   "outputs": [],
   "source": [
    "v_pt = (\n",
    "    beta_time_fulltime_pt * logzero(TimePT) * fulltime\n",
    "    + beta_time_other_pt * logzero(TimePT) * notfulltime\n",
    "    + beta_cost * logzero(MarginalCostPT)\n",
    "    + beta_transf * logzero(NbTransf)\n",
    ")\n",
    "v_car = (\n",
    "    asc_car\n",
    "    + beta_time_fulltime_car * logzero(TimeCar) * fulltime\n",
    "    + beta_time_other_car * logzero(TimeCar) * notfulltime\n",
    "    + beta_cost * logzero(CostCarCHF)\n",
    ")\n",
    "v_sm = (\n",
    "    asc_sm\n",
    "    + beta_dist_male * distance_km * male\n",
    "    + beta_dist_female * distance_km * female\n",
    "    + beta_dist_unreported * distance_km * unreportedGender\n",
    ")\n",
    "v = {0: v_pt, 1: v_car, 2: v_sm}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "640de989",
   "metadata": {},
   "source": [
    "Estimation of the parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c315a48c",
   "metadata": {},
   "outputs": [],
   "source": [
    "logprob = loglogit(v, None, Choice)\n",
    "biogeme = BIOGEME(database, logprob)\n",
    "biogeme.model_name = 'wtp_second_model'\n",
    "results_third: EstimationResults = biogeme.estimate()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "560888cb",
   "metadata": {},
   "source": [
    "General statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5e312cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(results_third.short_summary())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aabd8efa",
   "metadata": {},
   "source": [
    "Estimated parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd96331e",
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters_third = get_pandas_estimated_parameters(estimation_results=results_third)\n",
    "display(parameters_third)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
