# Chapter 52: Language Models, Retrieval, and AI Agents for Econometric Research
# Fundamentals of Python for Financial Econometrics - Language Models, Retrieval, and AI Agents for Econometric Research
# Dataset: Ceteris Lab teaching sample

# Language Models, Retrieval, and AI Agents for Econometric Research
#
# **Opening question:** How can a language model answer from a controlled evidence collection, use tools, and still remain subject to verification?

# %% Cell 2
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

chunks = [
    "Value at Risk is a loss quantile at a stated horizon and confidence level.",
    "Expected Shortfall averages losses beyond the VaR threshold.",
    "An AR model relates a series to its own lags.",
]
query = "What summarizes losses worse than VaR?"
vec = TfidfVectorizer().fit(chunks + [query])
X = vec.transform(chunks + [query])
scores = cosine_similarity(X[-1], X[:-1]).ravel()
print(scores.argmax(), chunks[scores.argmax()])

# **Interpretation check:** Interpretation. The retrieval step is local, inspectable, and requires no paid API. A larger system needs better embeddings and evaluation.

# %% Cell 4
state = {"step": 0, "done": False}
while not state["done"] and state["step"] < 3:
    state["step"] += 1
    if state["step"] == 2:
        state["done"] = True
print(state)

# **Interpretation check:** Interpretation. A maximum-step guard prevents an unbounded loop. Real agents also need tool-specific permissions, validation, and human confirmation.

# %% Cell 6
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
vec = TfidfVectorizer().fit(chunks + [query])
X = vec.transform(chunks + [query])
scores = cosine_similarity(X[-1], X[:-1]).ravel()
print(scores.argmax(), chunks[scores.argmax()])

# Verified source output
#
# ```text
# 1 Expected Shortfall averages losses beyond the VaR threshold.
# ```
#
# ```text
# {'step': 2, 'done': True}
# ```
