Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(correlation): handle unnamed series in Engle-Granger

Lanre Shittu511e2240f543
diff --git a/agent/src/skills/correlation-analysis/SKILL.md b/agent/src/skills/correlation-analysis/SKILL.md
index 7bb7b63e7..b6bc58ab5 100644
--- a/agent/src/skills/correlation-analysis/SKILL.md
+++ b/agent/src/skills/correlation-analysis/SKILL.md
@@ -239,7 +239,7 @@ def sector_clustering(
### Comparison of Three Linkage Methods
| Method | Feature | Best Use Case | Weakness |
-|------|------|---------|------|
+|------|------|---------|--------|
| Ward | Minimizes within-cluster variance, gives compact clusters | **Default recommendation**, stock-sector discovery | Works best for spherical clusters, weaker for irregular shapes |
| Complete | Uses maximum pairwise distance, conservative | When high within-cluster similarity is required | Can produce elongated clusters |
| Average | Uses average distance, compromise approach | General analysis where compactness is not the top priority | Sensitive to noise |
@@ -369,9 +369,10 @@ def engle_granger_coint(
Test results and spread series
"""
# Step 1: estimate the cointegrating vector with OLS.
- x_const = sm.add_constant(x)
+ x_reg = x.rename("x")
+ x_const = sm.add_constant(x_reg)
ols = sm.OLS(y, x_const).fit()
- hedge_ratio = ols.params[x.name if x.name else "x"]
+ hedge_ratio = ols.params["x"]
intercept = ols.params["const"]
residuals = ols.resid
diff --git a/agent/tests/test_correlation_analysis_skill.py b/agent/tests/test_correlation_analysis_skill.py
new file mode 100644
index 000000000..85c15e6ff
--- /dev/null
+++ b/agent/tests/test_correlation_analysis_skill.py
@@ -0,0 +1,36 @@
+"""Regression tests for the correlation-analysis skill examples."""
+
+from pathlib import Path
+
+import numpy as np
+import pandas as pd
+import pytest
+
+
+SKILL_MD = (
+ Path(__file__).resolve().parents[1]
+ / "src"
+ / "skills"
+ / "correlation-analysis"
+ / "SKILL.md"
+)
+
+
+def _load_engle_granger():
+ text = SKILL_MD.read_text(encoding="utf-8")
+ section = text.split("### Engle-Granger Two-Step Method", 1)[1]
+ code = section.split("```python\n", 1)[1].split("```", 1)[0]
+ namespace = {"pd": pd}
+ exec(code, namespace)
+ return namespace["engle_granger_coint"]
+
+
[email protected]("name", [None, "price_x"])
+def test_engle_granger_handles_series_name(name):
+ rng = np.random.default_rng(42)
+ x = pd.Series(np.cumsum(rng.normal(size=250)), name=name)
+ y = 1.7 * x + pd.Series(rng.normal(scale=0.2, size=250))
+
+ result = _load_engle_granger()(y, x)
+
+ assert result["hedge_ratio"] == pytest.approx(1.7, abs=0.02)