跳到正文
Skillpedia
分析与电子表格81k · #1670

trader-portfolio-cg

它能做什么

这项技能使用一种名为共轭梯度法的更快数学方法,计算投资组合的配置权重。适合用自己的数据分析如何在不同资产之间分配投资的人。

适用场景

  • ✓当你想根据协方差矩阵计算投资组合权重时。
  • ✓当旧的投资组合方法对你的数据来说太慢时。
  • ✓当你想比较新方法与旧方法的结果时。

技能链接

https://github.com/ruvnet/ruflo/blob/main/plugins/ruflo-neural-trader/skills/trader-portfolio-cg/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/ruvnet/ruflo/blob/main/plugins/ruflo-neural-trader/skills/trader-portfolio-cg/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请用更快的方法计算我五项持仓的均值方差权重。”

每周 5 个新技能,1 分钟读完。

工作原理

这项技能用共轭梯度法求解投资组合方程,记录所用的方法;如果原生路径失败,则退回到本地计算。一个设置可以关闭较快的路径,以便比较。

技术内容(原始 SKILL.md)

作者在 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:

  1. Ensure neural-trader is available:

    npm ls neural-trader 2>/dev/null || npm install --ignore-scripts neural-trader
    
  2. Read 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) and expectedReturns: number[] (length n).

  3. 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,
    });
    // 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__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 }
    
  4. 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.

  5. 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__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,
    
常见问题

这会告诉我该买什么吗?

它只根据你提供的数据计算权重。决定如何行动由你自己负责。

如何关闭较快的方法?

一个设置可以将其禁用,从而退回到旧方法进行比较。

如果协方差数据有问题怎么办?

该方法假设矩阵有效且对称,因此错误的输入数据可能导致结果失效。

详情

作者的原始描述

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

热度

GitHub 星标
81k
排名
#1670
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能