The covariance matrix Σ is symmetric positive-definite by construction (it's a Gram matrix on real returns), so CG is provably optimal — it converges in at most n iterations with no preconditioning, and typically far fewer when eigenvalues cluster.
-
Ensure neural-trader is available:
npm ls neural-trader 2>/dev/null || npm install --ignore-scripts neural-trader
-
Read the current covariance matrix Σ and expected-return vector μ from neural-trader's portfolio API:
npx neural-trader --portfolio current --json
npx neural-trader --portfolio current
mcp__claude-flow__memory_search({ query: "covariance matrix current", namespace: "trading-risk", limit: 1 })
The skill expects the response to include covariance: number[][] (n × n) and expectedReturns: number[] (length n).
-
Solve Σ · x = μ via the SublinearAdapter (preferred path) when RUFLO_NEURAL_TRADER_DISABLE_CG is unset:
import { sublinearAdapter } from '../../src/sublinear-adapter.mjs';
const result = await sublinearAdapter.solveCG(COVARIANCE, EXPECTED_RETURNS, {
tolerance: 1e-6,
maxIterations: 200,
});
The adapter does the dispatch itself: it probes for mcp__ruflo-sublinear__solve on globalThis (and honours RUFLO_SUBLINEAR_NATIVE=1 as a manual override), routes through the native kernel when reachable, and falls back transparently to the embedded ~50-LOC JS CG when not. The math is identical either way — CG, dense form, n × n SPD covariance. The operator reads result.method to know which backend produced the artifact.
The native MCP tool's wire shape (for direct callers who want to bypass the adapter):
mcp__ruflo-sublinear__solve({
matrix: COVARIANCE,
rhs: EXPECTED_RETURNS,
algorithm: "cg",
tolerance: 1e-6,
maxIterations: 200
})
Output:
{ solution: number[], iterations: number, residual: number }
-
Fallback (legacy Neumann) — if step 3 reports degraded: true (non-SPD input, non-square matrix, MCP error) OR if RUFLO_NEURAL_TRADER_DISABLE_CG=1:
npx neural-trader --portfolio optimize
Capture the weights output and tag the artifact metadata with method: 'neumann-fallback' and a reason field.
-
Store the optimal weights to trading-risk namespace with full provenance metadata. Take method and solver straight from the adapter's result so the operator can verify which backend ran:
mcp__claude-flow__memory_store({
key: "portfolio-weights-PORTFOLIO_ID-TIMESTAMP",
namespace: "trading-risk",
value: JSON.stringify({
weights: result.solution, // number[] from step 3 (or weights from step 4 fallback)
method: result.method, // 'cg-sublinear-native' | 'cg-local' | 'neumann-fallback'
solver: result.solver, // 'sublinear-time-solver@1.7.0' | 'local-js-cg' | 'neural-trader-cli'
iterations: result.iterations,
residual: result.residual,
latencyMs: result.latencyMs,
capturedAt: NEW_DATE_ISO,
reason: FALLBACK_REASON || null
})
})
The trading-risk namespace is canonical (ADR-126 Phase 1; the five-namespace alignment). Long-lived — no TTL — because portfolio weights are the audit trail Phase 4 will Ed25519-sign.
-
Cross-check against historical patterns (optional but recommended):
mcp__claude-flow__agentdb_pattern-search({
query: "portfolio weights Sharpe regime:CURRENT_REGIME",
namespace: "trading-risk"
})
If the new weights differ by more than 30% in any single asset from the historical median, flag for human review before applying. This is a guard-rail, not a hard block.