Class BacktestAttribution

java.lang.Object
com.quantfinlib.alpha.BacktestAttribution

public final class BacktestAttribution extends Object
P&L attribution of a traced backtest over time: factor, specific, trading, cost and financing components per bar, with long/short leg and sector splits.

For step s (bar b → b+1) with book h held over the bar, cross-sectional factor returns f and specific returns u from a FactorRiskModel regression of that bar (exposures X at close(b)):

  factor_k  = (Σ_i h_i X_ik) f_k
  specific  = Σ_i h_i u_i
  trading   = Σ_i pnl_i − factor − specific
  net       = factor + specific + trading + cost + financing      (exact)

This is the holdings-based decomposition with a trading residual (Menchero and Hu 2006, "Portfolio Risk Attribution", J. Performance Measurement 10(3), for the factor/specific split; the trading term is the transaction-based correction of Brinson-style attribution). The trading term holds intrabar fills (a next-open fill earns a different bar return than the book held at the close), terminal events of names outside the regression and compounding across the open. With close fills it is zero to rounding for names inside the regression.

Legs and sectors split Σ_i pnl_i: a name is long or short by the sign of its weight over the bar, or after the fill when it was not held. Components are fractions of equity at the start of each bar; totals are arithmetic sums over bars.

  • Method Details

    • attribute

      public static BacktestAttribution attribute(AlphaContext ctx, AlphaBacktester.Traced traced, String[] sectors, AlphaRiskModels.Spec spec)
      Attributes a traced run. The factor model is a single ex-post estimate over the traced bars, so each bar's factor returns come from that bar's own cross-section. The bars before the trace must cover the style lookbacks of spec.
      Parameters:
      sectors - sector label per symbol in context order (null = one sector)
      spec - style lookbacks and model settings (its window is replaced by the trace length)
    • factorNames

      public List<String> factorNames()
    • sectorNames

      public List<String> sectorNames()
    • steps

      public int steps()
    • factor

      public double factor(int s, int k)
      P&L from factor k in step s.
    • factorTotal

      public double factorTotal(int s)
      P&L from all factors in step s.
    • specific

      public double specific(int s)
    • trading

      public double trading(int s)
    • cost

      public double cost(int s)
    • financing

      public double financing(int s)
    • longLeg

      public double longLeg(int s)
    • shortLeg

      public double shortLeg(int s)
    • sector

      public double sector(int s, int j)
      P&L of sector j (index into sectorNames()) in step s.
    • net

      public double net(int s)
      Net return of step s.
    • cumulativeFactor

      public double cumulativeFactor(int k)
      Sum over steps of factor k's P&L.
    • cumulativeSpecific

      public double cumulativeSpecific()
    • cumulativeTrading

      public double cumulativeTrading()
    • cumulativeCost

      public double cumulativeCost()
    • cumulativeFinancing

      public double cumulativeFinancing()
    • cumulativeLong

      public double cumulativeLong()
    • cumulativeShort

      public double cumulativeShort()
    • cumulativeSector

      public double cumulativeSector(int j)
    • cumulativeNet

      public double cumulativeNet()
    • format

      public String format()
      A plain-text table of the cumulative components.