{"record":{"id":"038854b1ecbf021d","repo":"HKUDS/Vibe-Trading","slug":"sigma-must-be-0-got-sigma","errorCode":null,"errorMessage":"sigma must be >= 0, got {sigma}","messagePattern":"sigma must be >= 0, got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/quantlib/risk.py","lineNumber":561,"sourceCode":"        seed: Seed for ``numpy.random.default_rng``. Keyword-only. Pass an int\n            for a reproducible run; None draws fresh OS entropy and the result\n            is then NOT reproducible.\n        steps_per_year: Steps per year, i.e. ``dt = 1 / steps_per_year``.\n            Defaults to the 252-day trading year.\n\n    Returns:\n        Price matrix of shape ``(n_paths, n_steps + 1)``. Column 0 is exactly\n        ``s0`` on every path, so ``paths[:, -1] / paths[:, 0] - 1`` is the total\n        return over the whole simulation.\n\n    Raises:\n        ValueError: If ``s0`` is not positive, ``sigma`` is negative, or any of\n            ``n_steps`` / ``n_paths`` / ``steps_per_year`` is below 1.\n    \"\"\"\n    if s0 <= 0.0:\n        raise ValueError(f\"s0 must be > 0, got {s0}\")\n    if sigma < 0.0:\n        raise ValueError(f\"sigma must be >= 0, got {sigma}\")\n    if n_steps < 1 or n_paths < 1:\n        raise ValueError(f\"n_steps and n_paths must be >= 1, got {n_steps} and {n_paths}\")\n    if steps_per_year < 1:\n        raise ValueError(f\"steps_per_year must be >= 1, got {steps_per_year}\")\n\n    dt = 1.0 / steps_per_year\n    rng = np.random.default_rng(seed)\n    shocks = rng.standard_normal((n_paths, n_steps))\n    log_returns = (mu - 0.5 * sigma**2) * dt + sigma * np.sqrt(dt) * shocks\n    paths = np.empty((n_paths, n_steps + 1), dtype=float)\n    paths[:, 0] = s0\n    paths[:, 1:] = s0 * np.exp(np.cumsum(log_returns, axis=1))\n    return paths\n\n\ndef analyze_mc_results(paths: np.ndarray, confidence: float = 0.95) -> dict:\n    \"\"\"Summarise the terminal distribution of a simulated price matrix.\n","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/quantlib/risk.py#L543-L579","documentation":"monte_carlo_gbm requires volatility sigma >= 0 because sigma enters the simulation only through sigma**2 and sigma*sqrt(dt); a negative volatility is a sign convention error, not a distinct process, and is rejected as invalid input.","triggerScenarios":"monte_carlo_gbm(s0=100, sigma=-0.2, ...) from a signed estimate, or annualization code that multiplies by a negative scaling factor (e.g. negative time delta).","commonSituations":"Vol computed as covariance with a sign flip; time deltas negative after sorting errors making sqrt-time scaling negative; data entry of -0.2 meaning 'downside vol'.","solutions":["Pass the absolute value: sigma=abs(estimated_sigma)","Fix negative annualization factors (check date sorting before .diff()/dt computations)","Validate sigma >= 0 at the estimation boundary"],"exampleFix":"// before\npaths = monte_carlo_gbm(s0=100, sigma=signed_vol, ...)\n// after\npaths = monte_carlo_gbm(s0=100, sigma=abs(signed_vol), ...)","handlingStrategy":"validation","validationCode":"sigma = abs(sigma)\nassert sigma >= 0","typeGuard":"def is_valid_sigma(x) -> bool:\n    return isinstance(x, (int, float)) and not isinstance(x, bool) and x >= 0","tryCatchPattern":"try:\n    paths = monte_carlo_gbm(s0, mu, sigma, ...)\nexcept ValueError as e:\n    if \"sigma must be >= 0\" in str(e):\n        paths = monte_carlo_gbm(s0, mu, abs(sigma), ...)\n    else:\n        raise","preventionTips":["Take abs() of volatility estimates","Check date ordering before annualizing returns","Validate vol >= 0 in the estimator boundary"],"tags":["quantlib","risk","monte-carlo","gbm","volatility","valueerror"],"backgroundTag":"invalid-domain-value","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}