{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Language Models, Retrieval, and AI Agents for Econometric Research\n",
        "\n**Opening question:** How can a language model answer from a controlled evidence collection, use tools, and still remain subject to verification?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from sklearn.feature_extraction.text import TfidfVectorizer\n",
        "from sklearn.metrics.pairwise import cosine_similarity\n",
        "\n",
        "chunks = [\n",
        "    \"Value at Risk is a loss quantile at a stated horizon and confidence level.\",\n",
        "    \"Expected Shortfall averages losses beyond the VaR threshold.\",\n",
        "    \"An AR model relates a series to its own lags.\",\n",
        "]\n",
        "query = \"What summarizes losses worse than VaR?\"\n",
        "vec = TfidfVectorizer().fit(chunks + [query])\n",
        "X = vec.transform(chunks + [query])\n",
        "scores = cosine_similarity(X[-1], X[:-1]).ravel()\n",
        "print(scores.argmax(), chunks[scores.argmax()])\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The retrieval step is local, inspectable, and requires no paid API. A larger system needs better embeddings and evaluation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "state = {\"step\": 0, \"done\": False}\n",
        "while not state[\"done\"] and state[\"step\"] < 3:\n",
        "    state[\"step\"] += 1\n",
        "    if state[\"step\"] == 2:\n",
        "        state[\"done\"] = True\n",
        "print(state)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. A maximum-step guard prevents an unbounded loop. Real agents also need tool-specific permissions, validation, and human confirmation.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from sklearn.feature_extraction.text import TfidfVectorizer\n",
        "from sklearn.metrics.pairwise import cosine_similarity\n",
        "vec = TfidfVectorizer().fit(chunks + [query])\n",
        "X = vec.transform(chunks + [query])\n",
        "scores = cosine_similarity(X[-1], X[:-1]).ravel()\n",
        "print(scores.argmax(), chunks[scores.argmax()])\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n1 Expected Shortfall averages losses beyond the VaR threshold.\n```\n\n```text\n{'step': 2, 'done': True}\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": 52,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
