charlieviettq/awesome-agent-skill / .claude/skills/algo-risk-var
"algo-risk-var"
Verified"Calculate Value at Risk to estimate maximum portfolio loss at a given confidence level. Use this skill when the user needs to quantify downside risk, set risk limits, or report regulatory risk measures — even if they say 'worst case loss', 'portfolio risk', or 'how much could we lose'.".
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 4.6 KB |
| examples/sample_scenario.md | markdown | 4.3 KB |
| references/backtesting.md | markdown | 7.6 KB |
| references/expected-shortfall.md | markdown | 9.2 KB |
examples/sample_scenario.md
View on GitHub# Example: 台灣科技股投資組合的日 VaR 計算
## Scenario
**公司:** 元鼎資產管理(虛構)
**日期:** 2026-03-31
**請求者:** 風控部門主管 Linda Chen
Linda 管理一個 5,000 萬台幣的科技股投資組合,持有 TSMC、聯發科、鴻海三檔股票。法遵部門要求在 Q1 結算前提交 Basel III 風控報告,需要 95% 信賴水準、1 日時間窗口的 VaR 數字,並附上回測結果。Linda 問:「最近市場波動大,我們最壞情況一天會虧多少?」
**投資組合:**
| 股票 | 持股市值 (TWD) | 權重 |
|------|--------------|------|
| TSMC (2330) | 25,000,000 | 50% |
| 聯發科 (2454) | 15,000,000 | 30% |
| 鴻海 (2317) | 10,000,000 | 20% |
| **合計** | **50,000,000** | **100%** |
使用過去 250 個交易日(2025-04-01 至 2026-03-31)的每日報酬率,採用歷史模擬法。
---
## Analysis
### Phase 1:輸入驗證
- ✅ 歷史資料:250 天,符合最低門檻
- ✅ 部位以當日收盤價評價(NAV = TWD 50,000,000)
- ✅ 信賴水準:95%(α = 0.05)
- ✅ 時間窗口:1 日
**注意:** 2025 年 8 月台股曾出現單週急跌 9% 的事件,此段歷史已包含在樣本中,對歷史模擬法的 VaR 有拉高效果。
---
### Phase 2:計算每日投資組合損益
每日投資組合報酬率 = 0.50 × r_TSMC + 0.30 × r_MTK + 0.20 × r_HH
從 250 天資料中,將投資組合日報酬率由小到大排序,取得損失分布。
**部分排序後的報酬率(最差的 15 筆):**
| 排序 | 日期 | 投組日報酬 | 損失金額 (TWD) |
|------|------|-----------|--------------|
| 1 | 2025-08-05 | -6.82% | 3,410,000 |
| 2 | 2025-08-06 | -5.91% | 2,955,000 |
| 3 | 2025-11-12 | -4.73% | 2,365,000 |
| 4 | 2025-09-18 | -4.21% | 2,105,000 |
| 5 | 2026-01-07 | -3.88% | 1,940,000 |
| 6 | 2025-10-03 | -3.64% | 1,820,000 |
| 7 | 2025-12-15 | -3.29% | 1,645,000 |
| 8 | 2026-02-19 | -3.11% | 1,555,000 |
| 9 | 2025-07-22 | -2.97% | 1,485,000 |
| 10 | 2026-03-10 | -2.84% | 1,420,000 |
| 11 | 2025-06-30 | -2.76% | 1,380,000 |
| 12 | 2025-09-04 | -2.68% | 1,340,000 |
| **13** | **2025-05-20** | **-2.54%** | **1,270,000** |
| 14 | 2026-01-28 | -2.41% | 1,205,000 |
| 15 | 2025-08-19 | -2.33% | 1,165,000 |
**VaR 計算:**
- 250 天 × 5% = **12.5 → 取第 13 筆**(floor(250 × 0.05) + 1)
- 第 13 筆日報酬 = **-2.54%**
- **1-day 95% VaR = TWD 50,000,000 × 2.54% = TWD 1,270,000**
---
### CVaR(Expected Shortfall)計算
取排序 1–12 筆(比 VaR 更壞)的平均損失:
```
平均損失 = (3,410,000 + 2,955,000 + 2,365,000 + 2,105,000 +
1,940,000 + 1,820,000 + 1,645,000 + 1,555,000 +
1,485,000 + 1,420,000 + 1,380,000 + 1,340,000) ÷ 12
= 23,420,000 ÷ 12
= TWD 1,951,667
```
**CVaR ≈ TWD 1,952,000**
驗證:CVaR (1,952,000) > VaR (1,270,000) ✅
---
### Phase 3:回測驗證
回測期間:250 交易日
實際損失超過 VaR(1,270,000)的天數 = **13 天**
預期超過天數 = 250 × 5% = **12.5 天**
超越率 = 13/250 = **5.2%**(預期 5.0%)
**Kupiec 檢定:** p-value = 0.87 → 無法拒絕虛無假設 → **回測通過** ✅
---
## Result
```json
{
"var": {
"amount": 1270000,
"confidence": 0.95,
"horizon_days": 1,
"currency": "TWD"
},
"cvar": {
"amount": 1952000
},
"backtest": {
"exceedances": 13,
"expected": 12.5,
"days_tested": 250,
"pass": true
},
"metadata": {
"method": "historical_simulation",
"portfolio_value": 50000000,
"reference_date": "2026-03-31",
"history_start": "2025-04-01"
}
}
```
**給 Linda 的風控摘要:**
> 在 95% 信賴水準下,此投資組合單日最大預期損失為 **TWD 127 萬**(佔 NAV 2.54%)。回測顯示模型表現良好(超越率 5.2%,Kupiec 通過)。
>
> ⚠️ **關鍵警示(Iron Law):** VaR 只告訴你「95% 的情況不會超過 127 萬」。**最壞的 5% 情況平均虧損為 CVaR = TWD 195 萬**,且 2025 年 8 月 5 日單日曾虧損 341 萬。VaR 合規,但尾端風險不可忽視——建議在季報中同步揭露 CVaR。
>
> 若需要 10 日 VaR(Basel III 市場風險資本要求),可套用 √10 縮放:1,270,000 × √10 ≈ **TWD 4,017,000**(僅適用於報酬率獨立且近似常態的情境)。
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |