{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Machine-Learning Workflow and Gradient Descent for Economic Data\n",
        "\n**Opening question:** How can a model be trained without letting information from the future or test set leak into the learning process?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "x = np.array([1., 2., 3., 4.])\n",
        "y = 2.5 * x\n",
        "slope = 0.0\n",
        "learning_rate = 0.02\n",
        "for _ in range(500):\n",
        "    gradient = -2 * np.mean(x * (y - slope*x))\n",
        "    slope -= learning_rate * gradient\n",
        "print(round(slope, 4))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The iterative slope converges to the least-squares solution for this noiseless one-parameter problem.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from sklearn.pipeline import Pipeline\n",
        "from sklearn.preprocessing import StandardScaler\n",
        "from sklearn.linear_model import Ridge\n",
        "\n",
        "pipeline = Pipeline([\n",
        "    (\"scale\", StandardScaler()),\n",
        "    (\"model\", Ridge(alpha=1.0)),\n",
        "])\n",
        "print([name for name, _ in pipeline.steps])\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. When the pipeline is fitted inside cross-validation, scaling parameters are learned only from each training fold.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "x = np.array([1., 2., 3., 4.])\n",
        "gradient = -2 * np.mean(x * (y - slope*x))\n",
        "print(round(slope, 4))\n",
        "from sklearn.pipeline import Pipeline\n",
        "from sklearn.preprocessing import StandardScaler\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n2.5\n```\n\n```text\n['scale', 'model']\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": 47,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
