{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b6c629c4",
   "metadata": {},
   "source": [
    "# Week 3 Lab: Demand\n",
    "\n",
    "Fit the exponential demand equation (Hursh & Silberberg, 2008) to participant\n",
    "consumption data and analyze the results.\n",
    "\n",
    "$$\\log Q = \\log Q_0 + k\\,(e^{-\\alpha\\, Q_0\\, C} - 1)$$\n",
    "\n",
    "In the simplified form used here we fit $\\alpha$ per participant with $Q_0$ taken\n",
    "from consumption at the lowest price and $k$ derived from the span of the data.\n",
    "\n",
    "Work through each task. Code cells are left blank for you to fill in; see the lab\n",
    "page for hints and watch out for the `log(0)` issue."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82122a47",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "Import the libraries you will need: `numpy`, `pandas`, `matplotlib`, `seaborn`, plus `curve_fit` from `scipy.optimize` and the metrics from `scikit-learn`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f91e2a47",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import your libraries here\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59700bd9",
   "metadata": {},
   "source": [
    "## Task 1: Read in the dataset\n",
    "\n",
    "Load `participant_data.csv`. Each row is one participant with consumption at 11 price points, plus the true `Q0` and `a` (alpha) values used to generate the data (with noise added)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7bb64568",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load participant_data.csv\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55b41ebc",
   "metadata": {},
   "source": [
    "## Task 2: Transform the dataset to long form\n",
    "\n",
    "The price points are stored as separate columns (`price_0.01`, `price_0.05`, ...). Use `melt` to reshape so each row is a single price/consumption observation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69b4dfdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reshape from wide to long with df.melt(...)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "837f1705",
   "metadata": {},
   "source": [
    "## Task 3: Transform the price to a numeric value\n",
    "\n",
    "The `variable` column holds strings like `price_5.00`. Strip the `price_` prefix and cast to float so price can be used numerically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff5550a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a numeric 'price' column from the 'variable' strings\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46c09d98",
   "metadata": {},
   "source": [
    "## Task 4: Calculate the parameter k\n",
    "\n",
    "`k` is the span (in log units) of the consumption data. Compute it from the range of non-zero consumption values across the dataset. (You could also choose a fixed constant; here we derive it from the data.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3b97b86",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define calculate_k(df) and compute k for the dataset\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b449067",
   "metadata": {},
   "source": [
    "## Task 5: Create the demand function and a per-participant fitter\n",
    "\n",
    "Write a function that, for one participant, takes `Q0` from consumption at the lowest price, then uses `curve_fit` to estimate `alpha` for the exponential demand equation. Return alpha, Q0, and goodness-of-fit metrics (R-squared, MAE, RMSE)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3e18f96",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define fit_hursh_demand(...) and fit_all_participants(...)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68e90f8a",
   "metadata": {},
   "source": [
    "## Task 6: Apply the function to each participant\n",
    "\n",
    "Run the fitter across all participants, passing the `k` you computed in Task 4."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64d5c6bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fit every participant\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61e68f43",
   "metadata": {},
   "source": [
    "## Task 7: Graph and analyze the results\n",
    "\n",
    "Compare estimated vs. true alpha and look at the distributions of the goodness-of-fit metrics across participants."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0759ddf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Visualize estimated vs true alpha and the error distributions\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccb2a78a",
   "metadata": {},
   "source": [
    "## Task 8: Calculate goodness-of-fit metrics and plot individual fits\n",
    "\n",
    "For each participant, generate predicted consumption from the fitted parameters, compute R-squared and RMSE, and overlay the fitted curve on the observed data (log price axis)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ce0efca",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Loop participants: predict, score (R2/RMSE), and plot observed vs fitted\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "640380fa",
   "metadata": {},
   "source": [
    "## Wrap-up\n",
    "\n",
    "Summarize what you found: How well does the exponential demand equation describe these data? Do the recovered `alpha` values track the true values? What do the goodness-of-fit distributions tell you about where the model struggles?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
