Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(correlation): publish undefined pairs as null, not 0.0 (#1933)

Zigen Hanbf37bbdaef27
diff --git a/agent/backtest/correlation.py b/agent/backtest/correlation.py
index 2185bc7df..bf8fa81d3 100644
--- a/agent/backtest/correlation.py
+++ b/agent/backtest/correlation.py
@@ -175,7 +175,7 @@ def _rolling_correlation_matrix(
price_series: Dict[str, pd.DataFrame],
window: int,
method: Literal["pearson", "spearman"],
-) -> tuple[list[str], list[list[float]]]:
+) -> tuple[list[str], list[list[float | None]]]:
"""Compute correlation matrix for multiple price series.
Args:
@@ -185,7 +185,7 @@ def _rolling_correlation_matrix(
Returns:
(labels, matrix) where labels is the sorted list of codes and matrix
- is a symmetric NxN matrix of correlation coefficients.
+ is a symmetric NxN matrix, with None for undefined off-diagonal pairs.
"""
if not price_series:
return [], []
@@ -244,10 +244,17 @@ def _rolling_correlation_matrix(
corr, _ = spearmanr(xi, xj)
else:
corr = np.corrcoef(xi, xj)[0, 1]
+ # Undefined pairs (zero-variance leg: halted series, pegged
+ # stablecoin, fresh listing) are missing evidence, not a claim of
+ # no linear relationship. Publishing them as a real 0.0 silently
+ # drags portfolio-level readings ("diversified basket") that an
+ # operator may act on — the sibling regime path (regime.py
+ # compute_edge_density) already chose the conservative reading,
+ # so the two endpoints must agree on what unmeasurable means.
if np.isnan(corr):
- corr = 0.0
- matrix[i][j] = round(corr, 4)
- matrix[j][i] = round(corr, 4)
+ corr = None
+ matrix[i][j] = round(corr, 4) if corr is not None else None
+ matrix[j][i] = matrix[i][j]
return labels, matrix
diff --git a/agent/tests/test_correlation.py b/agent/tests/test_correlation.py
index 949765e04..aea5ffdce 100644
--- a/agent/tests/test_correlation.py
+++ b/agent/tests/test_correlation.py
@@ -311,6 +311,29 @@ class TestRollingCorrelationMatrix:
assert labels == []
assert matrix == []
+ def test_undefined_pairs_publish_null_not_zero(self):
+ # Regression for #1932: a zero-variance leg (halted every session,
+ # pegged stablecoin, fresh listing inside the window) makes the
+ # correlation mathematically undefined. Publishing a real 0.0 claims
+ # "no linear relationship" from what is actually missing evidence,
+ # and silently drags portfolio-level "diversified" readings. The
+ # regime path (regime.py compute_edge_density) already keeps NaN as
+ # missing; the matrix must agree on what unmeasurable means.
+ moving = [100.0, 102.0, 101.0, 103.5, 99.5, 104.0, 106.0, 101.5]
+ pegged = [1.0] * len(moving) # zero variance: correlation undefined
+ price_series = {
+ "PEGGED": self._make_price_df(pegged),
+ "MOVER": self._make_price_df(moving),
+ }
+ for method in ("pearson", "spearman"):
+ labels, matrix = _rolling_correlation_matrix(price_series, window=30, method=method)
+ i, j = labels.index("PEGGED"), labels.index("MOVER")
+ assert matrix[i][j] is None, f"{method}: undefined pair must be None, got {matrix[i][j]!r}"
+ assert matrix[j][i] is None, f"{method}: matrix must stay symmetric"
+ # Diagonal stays 1.0 — self-correlation is always defined.
+ assert matrix[i][i] == 1.0
+ assert matrix[j][j] == 1.0
+
def test_missing_close_column_raises(self):
df = pd.DataFrame({"open": [1, 2, 3]})
with pytest.raises(ValueError, match="No 'close' column"):
diff --git a/frontend/src/components/charts/CorrelationMatrix.tsx b/frontend/src/components/charts/CorrelationMatrix.tsx
index 6ecd7a997..916e5d1f2 100644
--- a/frontend/src/components/charts/CorrelationMatrix.tsx
+++ b/frontend/src/components/charts/CorrelationMatrix.tsx
@@ -6,7 +6,7 @@ import { useChartLifecycle } from "@/hooks/useChartLifecycle";
interface Props {
labels: string[];
- matrix: number[][];
+ matrix: (number | null)[][];
height?: number;
}
@@ -33,11 +33,19 @@ export function CorrelationMatrix({ labels, matrix, height = 500 }: Props) {
useChartLifecycle(ref, () => {
const t = getChartTheme();
- // Build heatmap data: [xIdx, yIdx, value] with per-cell label color for readability on saturated cells
- const data: { value: [number, number, number]; label: { color: string } }[] = [];
+ // Build heatmap data: [xIdx, yIdx, value] with per-cell label color for readability on saturated cells.
+ // Undefined pairs (null — zero-variance leg such as a halted series) are missing
+ // evidence, not 0.0: render them as a muted "—" cell instead of cooling the
+ // heatmap with a fabricated "no linear relationship" reading.
+ const data: { value: [number, number, number]; label: { color: string }; itemStyle?: { color: string; opacity: number } }[] = [];
for (let i = 0; i < labels.length; i++) {
for (let j = 0; j < labels.length; j++) {
- const val = parseFloat((matrix[i]?.[j] ?? 0).toFixed(4));
+ const raw = matrix[i]?.[j] ?? null;
+ if (raw === null) {
+ data.push({ value: [j, i, 0], label: { color: t.textColor }, itemStyle: { color: t.gridColor, opacity: 0.6 } });
+ continue;
+ }
+ const val = parseFloat(raw.toFixed(4));
data.push({ value: [j, i, val], label: { color: cellTextColor(val) } });
}
}
@@ -55,7 +63,9 @@ export function CorrelationMatrix({ labels, matrix, height = 500 }: Props) {
formatter: (params: unknown) => {
const p = params as { data: { value: [number, number, number] } };
const [x, y, v] = p.data.value;
- return `<b>${escapeHtml(labels[x] ?? "")}</b> vs <b>${escapeHtml(labels[y] ?? "")}</b><br/>r = <b>${v.toFixed(4)}</b>`;
+ const rawCell = matrix[y]?.[x] ?? null;
+ const rText = rawCell === null ? i18n.t("charts.correlationUndefined") : v.toFixed(4);
+ return `<b>${escapeHtml(labels[x] ?? "")}</b> vs <b>${escapeHtml(labels[y] ?? "")}</b><br/>r = <b>${escapeHtml(rText)}</b>`;
},
},
grid: { left: "3%", right: "8%", top: "8%", bottom: "12%", containLabel: true },
@@ -101,7 +111,9 @@ export function CorrelationMatrix({ labels, matrix, height = 500 }: Props) {
fontSize: 10,
formatter: (params: unknown) => {
const p = params as { value: [number, number, number] };
- return p.value[2].toFixed(2);
+ const [x, y] = p.value;
+ const rawCell = matrix[y]?.[x] ?? null;
+ return rawCell === null ? "—" : p.value[2].toFixed(2);
},
},
emphasis: {
diff --git a/frontend/src/components/charts/__tests__/CorrelationMatrix.test.tsx b/frontend/src/components/charts/__tests__/CorrelationMatrix.test.tsx
new file mode 100644
index 000000000..0cf6421ee
--- /dev/null
+++ b/frontend/src/components/charts/__tests__/CorrelationMatrix.test.tsx
@@ -0,0 +1,23 @@
+import { render } from "@testing-library/react";
+import { CorrelationMatrix } from "../CorrelationMatrix";
+
+const captured = vi.hoisted(() => ({ option: null as any }));
+vi.mock("@/hooks/useChartLifecycle", () => ({
+ useChartLifecycle: (_ref: unknown, build: () => unknown) => { captured.option = build(); },
+}));
+
+it("keeps unknown cells distinct from measured zero in labels and escaped tooltips", () => {
+ render(<CorrelationMatrix labels={["<A>", "B", "C"]} matrix={[
+ [1, null, 0], [null, 1, 0.5], [0, 0.5, 1],
+ ]} />);
+ const option = captured.option;
+ const series = option.series[0];
+ const unknown = series.data.find((d: any) => d.value[0] === 1 && d.value[1] === 0);
+ const zero = series.data.find((d: any) => d.value[0] === 2 && d.value[1] === 0);
+ expect(series.label.formatter(unknown)).toBe("—");
+ expect(series.label.formatter(zero)).toBe("0.00");
+ expect(unknown.itemStyle.opacity).toBeLessThan(1);
+ expect(option.tooltip.formatter({ data: unknown })).toContain("undefined");
+ expect(option.tooltip.formatter({ data: unknown })).toContain("&lt;A&gt;");
+ expect(option.tooltip.formatter({ data: zero })).toContain("0.0000");
+});
diff --git a/frontend/src/i18n/locales/ar.json b/frontend/src/i18n/locales/ar.json
index e04143788..d59c3bc82 100644
--- a/frontend/src/i18n/locales/ar.json
+++ b/frontend/src/i18n/locales/ar.json
@@ -1943,7 +1943,8 @@
"charts": {
"noPriceData": "لا توجد بيانات أسعار لهذا الرمز حتى الآن",
"noCorrelationData": "اختر رمزين على الأقل للمقارنة",
- "noEquityData": "لا يوجد منحنى لقيمة الحساب حتى الآن — شغّل اختبارًا عكسيًا أولًا"
+ "noEquityData": "لا يوجد منحنى لقيمة الحساب حتى الآن — شغّل اختبارًا عكسيًا أولًا",
+ "correlationUndefined": "غير معرّفة (لا يوجد تغير في السعر خلال النافذة)"
},
"toolProgress": {
"step": "الخطوة {{step}} · {{tool}}",
diff --git a/frontend/src/i18n/locales/de.json b/frontend/src/i18n/locales/de.json
index 7059dd4dc..64e343daf 100644
--- a/frontend/src/i18n/locales/de.json
+++ b/frontend/src/i18n/locales/de.json
@@ -5,6 +5,7 @@
"charts": {
"noPriceData": "Noch keine Kursdaten für dieses Symbol",
"noCorrelationData": "Wählen Sie mindestens zwei Symbole zum Vergleichen",
+ "correlationUndefined": "undefiniert (keine Preisbewegung im Zeitraum)",
"noEquityData": "Noch keine Kontowert-Kurve — führen Sie zuerst einen Backtest durch"
},
"navigation": {
diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.json
index dee0f71c9..76328abca 100644
--- a/frontend/src/i18n/locales/en.json
+++ b/frontend/src/i18n/locales/en.json
@@ -5,6 +5,7 @@
"charts": {
"noPriceData": "No price data for this symbol yet",
"noCorrelationData": "Pick at least two symbols to compare",
+ "correlationUndefined": "undefined (no price variation in window)",
"noEquityData": "No account-value curve yet — run a backtest first"
},
"navigation": {
diff --git a/frontend/src/i18n/locales/es.json b/frontend/src/i18n/locales/es.json
index 83831db53..d747beeb6 100644
--- a/frontend/src/i18n/locales/es.json
+++ b/frontend/src/i18n/locales/es.json
@@ -5,6 +5,7 @@
"charts": {
"noPriceData": "Aún no hay datos de precio para este símbolo",
"noCorrelationData": "Elige al menos dos símbolos para comparar",
+ "correlationUndefined": "indefinida (sin variación de precio en la ventana)",
"noEquityData": "Aún no hay curva de valor de cuenta — ejecuta primero un backtest"
},
"navigation": {
diff --git a/frontend/src/i18n/locales/fa.json b/frontend/src/i18n/locales/fa.json
index 384cf77f2..3228c8f6b 100644
--- a/frontend/src/i18n/locales/fa.json
+++ b/frontend/src/i18n/locales/fa.json
@@ -5,6 +5,7 @@
"charts": {
"noPriceData": "هنوز داده قیمتی برای این نماد وجود ندارد",
"noCorrelationData": "برای مقایسه حداقل دو نماد انتخاب کنید",
+ "correlationUndefined": "تعریف‌نشده (بدون نوسان قیمت در بازه)",
"noEquityData": "هنوز منحنی ارزش حسابی وجود ندارد — ابتدا یک بک‌تست اجرا کنید"
},
"navigation": {
diff --git a/frontend/src/i18n/locales/id.json b/frontend/src/i18n/locales/id.json
index cd3f1a708..5daf85376 100644
--- a/frontend/src/i18n/locales/id.json
+++ b/frontend/src/i18n/locales/id.json
@@ -5,6 +5,7 @@
"charts": {
"noPriceData": "Belum ada data harga untuk simbol ini",
"noCorrelationData": "Pilih setidaknya dua simbol untuk dibandingkan",
+ "correlationUndefined": "belum terdefinisi (tidak ada variasi harga pada jendela)",
"noEquityData": "Belum ada kurva nilai akun — jalankan backtest terlebih dahulu"
},
"navigation": {
diff --git a/frontend/src/i18n/locales/ja.json b/frontend/src/i18n/locales/ja.json
index 5c2e29875..507f74e13 100644
--- a/frontend/src/i18n/locales/ja.json
+++ b/frontend/src/i18n/locales/ja.json
@@ -1943,7 +1943,8 @@
"charts": {
"noPriceData": "このシンボルにはまだ価格データがありません",
"noCorrelationData": "比較するシンボルを2つ以上選択してください",
- "noEquityData": "口座価値の推移はまだありません。先にバックテストを実行してください"
+ "noEquityData": "口座価値の推移はまだありません。先にバックテストを実行してください",
+ "correlationUndefined": "未定義(期間内の価格変動なし)"
},
"toolProgress": {
"step": "ステップ {{step}} · {{tool}}",
diff --git a/frontend/src/i18n/locales/ko.json b/frontend/src/i18n/locales/ko.json
index fb9a8c750..07a29f6dc 100644
--- a/frontend/src/i18n/locales/ko.json
+++ b/frontend/src/i18n/locales/ko.json
@@ -1943,7 +1943,8 @@
"charts": {
"noPriceData": "이 종목에는 아직 가격 데이터가 없습니다",
"noCorrelationData": "비교할 종목을 두 개 이상 선택하세요",
- "noEquityData": "계정 가치 곡선이 아직 없습니다. 먼저 백테스트를 실행하세요"
+ "noEquityData": "계정 가치 곡선이 아직 없습니다. 먼저 백테스트를 실행하세요",
+ "correlationUndefined": "정의되지 않음(기간 내 가격 변동 없음)"
},
"toolProgress": {
"step": "단계 {{step}} · {{tool}}",
diff --git a/frontend/src/i18n/locales/pt-BR.json b/frontend/src/i18n/locales/pt-BR.json
index 8bf5c1d7b..7589029ec 100644
--- a/frontend/src/i18n/locales/pt-BR.json
+++ b/frontend/src/i18n/locales/pt-BR.json
@@ -5,6 +5,7 @@
"charts": {
"noPriceData": "Ainda não há dados de preço para este símbolo",
"noCorrelationData": "Escolha pelo menos dois símbolos para comparar",
+ "correlationUndefined": "indefinida (sem variação de preço na janela)",
"noEquityData": "Ainda não há curva de valor da conta – execute primeiro um backtest"
},
"navigation": {
diff --git a/frontend/src/i18n/locales/tr.json b/frontend/src/i18n/locales/tr.json
index c099099a3..6236923be 100644
--- a/frontend/src/i18n/locales/tr.json
+++ b/frontend/src/i18n/locales/tr.json
@@ -5,6 +5,7 @@
"charts": {
"noPriceData": "Bu sembol için henüz fiyat verisi yok",
"noCorrelationData": "Karşılaştırmak için en az iki sembol seçin",
+ "correlationUndefined": "tanımsız (pencerede fiyat değişimi yok)",
"noEquityData": "Henüz hesap değeri eğrisi yok; önce geriye dönük test çalıştırın"
},
"layout": {
diff --git a/frontend/src/i18n/locales/zh-CN.json b/frontend/src/i18n/locales/zh-CN.json
index 5a00a8f78..60d731055 100644
--- a/frontend/src/i18n/locales/zh-CN.json
+++ b/frontend/src/i18n/locales/zh-CN.json
@@ -1943,6 +1943,7 @@
"charts": {
"noPriceData": "这个标的还没有价格数据",
"noCorrelationData": "至少选两个标的才能比较",
+ "correlationUndefined": "无法计算(该标的在窗口内价格无波动)",
"noEquityData": "还没有净值曲线——先跑一次回测"
},
"toolProgress": {
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 764d977c4..1ce6afcce 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -89,7 +89,10 @@ export function isAuthRequiredError(error: unknown): boolean {
export interface CorrelationResponse {
labels: string[];
- matrix: number[][];
+ // Undefined pairs (zero-variance leg — halted series, pegged stablecoin,
+ // fresh listing) come back as null: missing evidence, not 0.0. Mirrors the
+ // regime endpoint's (number | null)[] density convention.
+ matrix: (number | null)[][];
}
export interface CorrelationAnalysisResponse {