DEV Community

Volamind
Volamind

Posted on

Separating deterministic portfolio math from multi-agent LLM stock research in TypeScript

When self-directed stock investors paste a brokerage table into a general-purpose chat model and ask "Am I over-concentrated?", two things routinely go wrong:

  1. Tokenized arithmetic drifts across turns. Ask a model to trim a 32% position back to a 25% cap while keeping total portfolio capital constant, and prose reasoning frequently confuses the simple excess above cap with the post-sale denominator.
  2. Single-prompt research agrees with the prompt's framing. Ask why a stock looks cheap and you get a bull pitch. Ask why it looks dangerous and you get a bear pitch. Neither is anchored to a repeatable position-sizing check.

While building Volamind, we drew a hard line in the TypeScript architecture: LLMs never compute portfolio weights, concentration thresholds, or rebalance dollar amounts. Deterministic functions run the math first, and specialist research agents only read the resulting structured snapshot alongside SEC 10-K and 10-Q filings.

Today we extracted that deterministic layer into a standalone, zero-login calculator anyone can test in the browser: Volamind Portfolio Concentration Calculator.

Here is the math behind the three checks that catch hidden concentration fastest.

1. Counting effective independent bets with inverse HHI

Owning 10 tickers does not mean owning 10 diversified bets. If one winner has compounded to 48% of your account and nine smaller positions split the remaining 52%, your portfolio behaves closer to three equal-sized positions than ten.

In industrial organization and portfolio construction, the cleanest way to measure this is the Herfindahl-Hirschman Index (HHI) and its reciprocal, the effective number of positions:

export function computeEffectiveBets(dollarValues: number[]): {
  totalValue: number;
  weights: number[];
  hhi: number;
  effectivePositionCount: number;
} {
  const clean = dollarValues.filter((v) => Number.isFinite(v) && v > 0);
  const totalValue = clean.reduce((sum, v) => sum + v, 0);
  if (totalValue <= 0) {
    return { totalValue: 0, weights: [], hhi: 0, effectivePositionCount: 0 };
  }

  const weights = clean.map((v) => v / totalValue);
  const hhi = weights.reduce((sum, w) => sum + w * w, 0);

  return {
    totalValue,
    weights,
    hhi,
    effectivePositionCount: hhi > 0 ? 1 / hhi : 0,
  };
}
Enter fullscreen mode Exit fullscreen mode

Check the boundary cases:

  • Equal weights: Five $10,000 positions each carry $w_i = 0.20$. Then $\sum w_i^2 = 5 \times 0.04 = 0.20$, and $1 / 0.20 = 5.0$ effective bets.
  • Top-heavy weights: One $32,000 position and four $4,500 positions total $50,000. Weights are $0.64$ and four copies of $0.09$. Then $\sum w_i^2 = 0.4096 + 4(0.0081) = 0.442$, so $1 / 0.442 \approx 2.26$ effective bets out of 5 tickers.

Showing "2.3 effective bets across 5 holdings" communicates concentration risk far faster than a pie chart.

2. Rebalance shift vs outright sale

Suppose a $100,000 portfolio holds $32,000 in NVDA (32.0%) against a 25.0% single-stock cap ($L = 0.25$):

  • Reinvested shift (keeping total portfolio value $V$ constant): Moving $V \times (w_i - L) = \$100,000 \times (0.32 - 0.25) = \$7,000$ out of NVDA and into diversified holdings brings NVDA directly to $25,000 / 100,000 = 25.0\%$.
  • Cash withdrawal sale (shrinking total portfolio value): If instead you sell $x$ dollars and withdraw the cash from the portfolio, the new weight is $(32,000 - x) / (100,000 - x) = 0.25$, which requires selling $x = \$9,333.33$.

Chat models conflate these two operations constantly. In our calculator we compute the constant-capital shift explicitly and state the assumption right next to the dollar figure:

const limitFraction = positionLimitPct / 100;
const excessPct = Math.max(0, weightPct - positionLimitPct);
const dollarShiftToCap =
  excessPct > 0
    ? Math.max(0, marketValue - totalValue * limitFraction)
    : 0;
Enter fullscreen mode Exit fullscreen mode

3. Catching single-sector clustering (the 40% guardrail)

Individual position caps miss correlated drawdowns. Holding 18% NVDA, 16% AMD, 15% AVGO, and 14% MSFT passes a 25% single-stock rule on every row, yet puts 63% of total equity in Technology.

Aggregating weights by GICS sector and flagging any sector above 40% catches disguised one-theme portfolios before an earnings miss reprices the entire group together.

Pairing deterministic risk checks with 12-agent SEC memos

Once the deterministic math computes exact weights, sector totals, and over-limit flags, our research pipeline handles the qualitative work that code formulas cannot: reading SEC 10-K and 10-Q filings, separating fundamentals from sentiment, and running an explicit bull-versus-bear debate before issuing an Overweight, Hold, or Underweight stance.

If you want to inspect both halves live in the browser (neither requires creating an account):

If you build financial or analytics interfaces with LLMs, curious where else you draw the boundary between deterministic TypeScript functions and agentic reasoning.

Top comments (0)