{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Applied Economic Prediction and Classification Projects\n",
        "\n**Opening question:** How can an end-to-end workflow turn housing and passenger data into models that are evaluated honestly and interpreted carefully?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn.linear_model import LinearRegression\n",
        "\n",
        "rng = np.random.default_rng(48)\n",
        "area = rng.uniform(45, 240, 500)\n",
        "price = 85_000 + 3_250 * area + rng.normal(0, 55_000, 500)\n",
        "houses = pd.DataFrame({\"area\": area, \"price\": price})\n",
        "model = LinearRegression().fit(houses[[\"area\"]], houses[\"price\"])\n",
        "print(round(float(model.coef_[0]), 2), len(houses))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The slope is a predictive association in the dataset’s currency units per square metre, not a causal valuation rule.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn.linear_model import LogisticRegression\n",
        "from sklearn.metrics import accuracy_score, precision_score, recall_score\n",
        "from sklearn.model_selection import train_test_split\n",
        "\n",
        "rng = np.random.default_rng(49)\n",
        "n = 800\n",
        "passengers = pd.DataFrame({\n",
        "    \"travel_class\": rng.integers(1, 4, n),\n",
        "    \"age\": rng.normal(36, 14, n).clip(1, 85),\n",
        "    \"fare\": rng.lognormal(3.2, 0.7, n),\n",
        "})\n",
        "score = 1.2 - 0.75 * (passengers[\"travel_class\"] - 1) - 0.018 * (passengers[\"age\"] - 30) + 0.006 * passengers[\"fare\"]\n",
        "probability = 1 / (1 + np.exp(-score))\n",
        "passengers[\"survived\"] = rng.binomial(1, probability)\n",
        "X_train, X_test, y_train, y_test = train_test_split(\n",
        "    passengers.drop(columns=\"survived\"), passengers[\"survived\"], test_size=0.25, random_state=29, stratify=passengers[\"survived\"]\n",
        ")\n",
        "model = LogisticRegression(max_iter=1000).fit(X_train, y_train)\n",
        "prediction = model.predict(X_test)\n",
        "print(\n",
        "    round(accuracy_score(y_test, prediction), 3),\n",
        "    round(precision_score(y_test, prediction), 3),\n",
        "    round(recall_score(y_test, prediction), 3),\n",
        ")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The three metrics illuminate different error trade-offs. Exact values are tied to the documented split and preprocessing.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n84427668.1238 3473\n```\n\n```text\n0.789 0.719 0.744\n```\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3"
    },
    "ceteris_lab": {
      "course_slug": "fundamentals-python-econometrics",
      "source_derived": true,
      "course_title": "Fundamentals of Python for Financial Econometrics",
      "chapter": 48,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
