performance-attribution
Performance-attribution decomposes portfolio excess returns into specific sources including sector allocation decisions, stock-selection skill, factor exposures, and market timing through Brinson-Fachler attribution models and factor decomposition frameworks. Use this skill to analyze why a trading strategy or portfolio outperformed or underperformed its benchmark, identifying whether gains came from overweighting outperforming sectors, picking better stocks within sectors, or capturing systematic factor premiums versus manager alpha.
git clone --depth 1 https://github.com/HKUDS/Vibe-Trading /tmp/performance-attribution && cp -r /tmp/performance-attribution/agent/src/skills/performance-attribution ~/.claude/skills/performance-attributionSKILL.md
# Performance Attribution Analysis
## Overview
Decompose portfolio excess returns into explainable sources: sector allocation, stock selection, factor exposure, timing contribution, and more. This helps explain **why** a strategy made or lost money, rather than only **how much** it made or lost.
## Brinson Attribution Model
**Do not retype these formulas into throwaway Python.** They are implemented and
tested in `src/quantlib/attribution.py`; import them.
### Single-Period Brinson-Fachler Model
```
Let w_p,i = portfolio weight of sector i
w_b,i = benchmark weight of sector i
r_p,i = portfolio return of sector i
r_b,i = benchmark return of sector i
R_b = total benchmark return
Allocation_i = (w_p,i - w_b,i) × (r_b,i - R_b)
Selection_i = w_b,i × (r_p,i - r_b,i)
Interaction_i = (w_p,i - w_b,i) × (r_p,i - r_b,i)
Total active return = Σ(Allocation_i) + Σ(Selection_i) + Σ(Interaction_i)
```
**The decomposition itself has no residual term.** The three effects sum to
`R_p - R_b` identically, for any sector returns whatsoever, provided the
portfolio and benchmark weights carry the same total. `brinson_fachler` enforces
the weight-sum precondition and raises rather than returning a decomposition
that does not tie out.
A residual is therefore never a property of the algebra — but it is a real and
expected property of a *reported* attribution, because the inputs are a
snapshot. Intra-period trading, cash drag, corporate actions and FX translation
all move the actual portfolio return away from the one these weights and sector
returns imply. So:
- residual inside the decomposition, given the inputs → **impossible**; if you
see one, the arithmetic or the weight convention is wrong;
- residual between the decomposition and the reported fund return → **normal**;
quantify it and attribute it to its source rather than absorbing it silently
into selection. This is what the `/attrib` reconciliation gate asks for.
```python
from src.quantlib.attribution import brinson_fachler
result = brinson_fachler(
portfolio_weights={"Tech": 0.40, "Financials": 0.10, "Energy": 0.30, "Health": 0.20},
benchmark_weights={"Tech": 0.25, "Financials": 0.30, "Energy": 0.25, "Health": 0.20},
portfolio_returns={"Tech": 0.12, "Financials": 0.04, "Energy": -0.02, "Health": 0.07},
benchmark_returns={"Tech": 0.10, "Financials": 0.05, "Energy": -0.01, "Health": 0.06},
)
result.portfolio_return # 0.0600
result.benchmark_return # 0.0495
result.active_return # 0.0105
result.allocation # 0.0045
result.selection # 0.0015
result.interaction # 0.0045
# 0.0045 + 0.0015 + 0.0045 == 0.0105 exactly (residual ~3e-18, machine epsilon)
for effect in result.sectors:
print(effect.sector, effect.allocation, effect.selection, effect.interaction, effect.total)
```
A sector return may be omitted only where the matching weight is zero. A
benchmark sector you did not own therefore shows zero selection and zero
interaction, and the whole effect lands in allocation — you cannot demonstrate
stock-picking skill in something you never held.
### Example Brinson Attribution
Rendered from the call above, so every figure below is reproducible:
```markdown
### Brinson Sector Attribution
| Sector | Portfolio Weight | Benchmark Weight | Portfolio Return | Benchmark Return | Allocation | Selection | Interaction |
|------|---------|---------|---------|---------|---------|---------|---------|
| Tech | 40% | 25% | 12% | 10% | +0.7575% | +0.50% | +0.30% |
| Financials | 10% | 30% | 4% | 5% | -0.0100% | -0.30% | +0.20% |
| Energy | 30% | 25% | -2% | -1% | -0.2975% | -0.25% | -0.05% |
| Health | 20% | 20% | 7% | 6% | +0.0000% | +0.20% | +0.00% |
| **Total** | 100% | 100% | 6.00% | 4.95% | **+0.45%** | **+0.15%** | **+0.45%** |
Active return 1.05% = allocation 0.45% + selection 0.15% + interaction 0.45%. No residual.
```
### Multi-Period Attribution (Linked Brinson)
Single-period effects add, but returns compound, so simply summing each period's
effects does **not** reproduce the compounded active return. Take the four-sector
period above and two more like it (the exact three are the `_three_periods`
fixture in `tests/quantlib/test_attribution.py`, so you can run them): summing the
three active returns gives 2.8500%, while the compounded active return is 3.0318%
— an 18.2bp error that grows with the horizon and the return level.
Use **Carino logarithmic linking**, implemented as `carino_link`. It is
residual-free, and its per-period scaling factor depends only on that period's
total portfolio and benchmark return — never on the effects being linked — so
linking is deterministic and cannot be steered by how sectors were bucketed.
(Menchero linking is also residual-free but distributes a correction term derived
from the effects themselves; Carino needs less machinery for the same guarantee.)
```
k = (ln(1 + R_P) - ln(1 + R_B)) / (R_P - R_B) # over the whole horizon
k_t = (ln(1 + R_p,t) - ln(1 + R_b,t)) / (R_p,t - R_b,t) # for period t
linked effect = Σ_t (k_t / k) × effect_{i,t}
```
```python
from src.quantlib.attribution import brinson_fachler, carino_link
periods = [brinson_fachler(**month) for month in monthly_inputs]
linked = carino_link(periods)
linked.active_return # compounded, not summed
linked.allocation, linked.selection, linked.interaction
linked.scaling_factors # one k_t / k per period, exposed so a report can be audited
for sector in linked.sectors:
print(sector.sector, sector.total)
# allocation + selection + interaction == linked.active_return exactly
```
Arithmetic linking is acceptable **only** when you explicitly report the residual.
Since `carino_link` costs one function call and leaves none, prefer it.
## Factor Attribution
### Alpha-Beta Decomposition
```
R_p = α + β × R_m + ε
α (alpha): excess return, manager skill
β (beta): market exposure, systematic risk
ε (epsilon): residual, idiosyncratic risk
Regression methodProfessional finance research toolkit — backtesting (10 engines + benchmark comparison panel), factor analysis, Alpha Zoo (462 pre-built alphas across qlib158/alpha101/gtja191/academic/fundamental), options pricing, 90 finance skills, 30 multi-agent swarm teams, Trade Journal analyzer, and Shadow Account (extract → backtest → render) across 25 market-data sources (tushare, yfinance, okx, binance, akshare, baostock, tencent, mootdx, ccxt, futu, mt5, tickerall, local, eastmoney, sina, stooq, yahoo, pykrx, india_broker, qveris, longbridge, plus optional-key finnhub/alphavantage/tiingo/fmp).
ADR/H-share/A-share cross-listing premium analysis — track pricing gaps between US-listed ADRs, HK-listed H-shares, and A-shares for arbitrage signals, dual-listing valuation, and delisting risk assessment.
AKShare financial data aggregator (18k+ stars). Free, no API key. Covers A-shares, US, HK, futures, macro, forex. Primary fallback for tushare and yfinance.
Browse and bench the bundled alpha zoos — prebuilt cross-sectional factor libraries (Kakushadze 101, GTJA 191, Qlib 158, Fama-French / Carhart). Use when the user asks "which alphas exist", wants metadata on a named alpha, or wants to run IC/IR on a whole zoo over a universe.
A 股 ST/*ST 风险预测框架 — 基于最新中报/三季报或业绩预告/快报,预测下一财年是否会因营收、利润、净资产、分红不达标而被风险警示,并将新浪监管处罚记录作为独立证据面纳入风险等级。仅适用于 A 股,不预测财务造假。
Asset allocation theory and optimizer usage — MPT / Black-Litterman / risk budgeting / all-weather strategy, including guides for 5 optimizers and rebalancing rules.
Diagnose failed or underperforming backtests, locate the root cause, and fix the issue
Behavioral finance applications: theories of overreaction and underreaction, behavioral explanations for momentum and reversal, investor sentiment cycles, cognitive-bias checklists, and debiasing quantitative strategies.