{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Computer Vision and Spatial Economic Measurement\n",
        "\n**Opening question:** How does a model transform pixels into useful local patterns without being told the exact edge or texture to search for?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "image = np.zeros((8, 8))\n",
        "image[:, 4:] = 1\n",
        "kernel = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])\n",
        "padded = np.pad(image, 1, mode=\"symmetric\")\n",
        "windows = np.lib.stride_tricks.sliding_window_view(padded, (3, 3))\n",
        "feature = np.einsum(\"ijkl,kl->ij\", windows, kernel)\n",
        "print(float(np.abs(feature).max()), feature.shape)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The vertical edge creates a strong response where intensity changes across columns.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "rng = np.random.default_rng(50)\n",
        "batch = rng.normal(size=(4, 3, 16, 16))\n",
        "kernels = rng.normal(size=(6, 3, 3, 3))\n",
        "padded = np.pad(batch, ((0, 0), (0, 0), (1, 1), (1, 1)))\n",
        "windows = np.lib.stride_tricks.sliding_window_view(padded, (3, 3), axis=(2, 3))\n",
        "feature_maps = np.einsum(\"bchwkl,ockl->bohw\", windows, kernels)\n",
        "print(\"Input batch:\", batch.shape)\n",
        "print(\"Feature maps:\", feature_maps.shape)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. Padding preserves height and width, while eight learned filters replace the three input channels.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n3.0 (8, 8)\n```\n\n```text\n(16, 8, 64, 64)\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": 50,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
