trader-portfolio-cg
What it does
This skill calculates portfolio allocation weights using a faster mathematical method called conjugate gradient. It is for people analysing how to spread investments across assets, using their own data.
When to use it
- ✓When you want portfolio weights computed from a covariance matrix.
- ✓When the older portfolio method is too slow for your data.
- ✓When you want to compare the faster method with the older one.
Skill link
https://github.com/ruvnet/ruflo/blob/main/plugins/ruflo-neural-trader/skills/trader-portfolio-cg/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Compute the mean-variance weights for my five holdings using the faster method.”
5 new skills a week. 1 minute to read.
How it works
The skill solves the portfolio equation with the conjugate gradient method, records which method was used, and falls back to a local calculation if a native path fails. A setting can turn the faster path off for comparison.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Solve the mean-variance optimization Σ · x = μ via Conjugate Gradient instead of the legacy Neumann series.
Why CG instead of Neumann (ADR-123 Wedge 8):
- Neumann series: ~50 µs at n=256 (legacy
npx neural-trader --portfolio optimize) - Conjugate Gradient: ~816 ns at n=256 (this skill)
- Measured speedup: 40-60×; parity within 1e-4 on a fixed seed.
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.
Disable flag: set RUFLO_NEURAL_TRADER_DISABLE_CG=1 to skip the CG path entirely and fall through to step 4's legacy Neumann route. Useful for A/B validation or when an upstream covariance regression breaks SPD.
Native dispatch flag: set RUFLO_SUBLINEAR_NATIVE=1 to force the adapter to attempt the native mcp__ruflo-sublinear__solve path even when globalThis doesn't expose the tool (e.g. when the harness mounts it via a different transport). On any native-dispatch failure the adapter cleanly falls back to the local JS CG and records method: 'cg-local' in the artifact metadata — so the regression is auditable.
Steps:
Ensure neural-trader is available:
npm ls neural-trader 2>/dev/null || npm install --ignore-scripts neural-traderRead the current covariance matrix Σ and expected-return vector μ from neural-trader's portfolio API:
# neural-trader 2.8.11 does NOT return Σ or μ: `--portfolio current` runs the equal-weight demo # (symbols, weights, a risk figure and a note that the returns matrix is not wired). # Take Σ and μ from the user (a returns matrix they provide), or from AgentDB if a prior run stored them:mcp__plugin_ruflo-core_ruflo__memory_search({ query: "covariance matrix current", namespace: "trading-risk", limit: 1 })The skill expects the response to include
covariance: number[][](n × n) andexpectedReturns: number[](length n).Solve Σ · x = μ via the SublinearAdapter (preferred path) when
RUFLO_NEURAL_TRADER_DISABLE_CGis unset:import { sublinearAdapter } from '../../src/sublinear-adapter.mjs'; const result = await sublinearAdapter.solveCG(COVARIANCE, EXPECTED_RETURNS, { tolerance: 1e-6, maxIterations: 200, }); // result.solution — optimal weights (number[]) // result.iterations — CG iterations executed // result.residual — final ||A·x − b||₂ // result.latencyMs — wall-clock latency // result.method — 'cg-sublinear-native' | 'cg-local' <-- READ THIS // result.solver — 'sublinear-time-solver@1.7.0' | 'local-js-cg' // result.degraded — true if input failed SPD checks (fall back to step 4)The adapter does the dispatch itself: it probes for
mcp__ruflo-sublinear__solveonglobalThis(and honoursRUFLO_SUBLINEAR_NATIVE=1as 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 readsresult.methodto 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 ifRUFLO_NEURAL_TRADER_DISABLE_CG=1:npx neural-trader --portfolio optimizeCapture the weights output and tag the artifact metadata with
method: 'neumann-fallback'and areasonfield.Store the optimal weights to
trading-risknamespace with full provenance metadata. Takemethodandsolverstraight from the adapter's result so the operator can verify which backend ran:mcp__plugin_ruflo-core_ruflo__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,
FAQ
Does this tell me what to buy?
It calculates weights from the data you provide. Deciding what to act on is up to you.
How do I turn off the faster method?
A setting can disable it, which falls back to the older method for comparison.
What if the covariance data is bad?
The method assumes a valid symmetric matrix, so bad input data can break the results.
Details
Original description by the author
Mean-variance portfolio optimization via Conjugate Gradient — 40-60× faster than the legacy Neumann path (ADR-126 Phase 3, ADR-123 Wedge 8)
repo: ruvnet/ruflo
Popularity
- GitHub stars
- 81k
- Ranking position
- #1670
- License
- MIT
- Updated
- 11/2/2026
Comments
- …
More skills from this repository
agent-adaptive-coordinator
81kHelps your agent split and rebalance a group of AI agents as the workload changes, choosing a layout that fits the task.
agent-agent
81kTeaches your agent to turn a big goal into an ordered plan of actions, and to replan when conditions change.
agent-agentic-payments
81kHelps your agent manage spending mandates for AI shopping, with spend caps, time windows, merchant rules and signed approvals.
agent-analyze-code-quality
81kHelps your agent review code for quality, finding code smells, technical debt and refactoring ideas without changing the files.
Similar skills
gitcrawl
394kLets your agent search a saved archive of GitHub issues and pull requests, group duplicates, and check their current status on GitHub.
slacrawl
394kLets your agent search and count messages in a local Slack archive, check how fresh that archive is, and refresh it when needed.
goplaces
394kHelps your agent search for places, get their details and reviews, and resolve place names using Google Places, with JSON output for scripts.