Backtest: Portfolio Returns





Kerry Back

  • Form a portfolio each month based on the predictions
  • How many stocks?
  • Short sales? 150/50, 130/30, 100/100, 100/0?
  • How many stocks on long and short sides?
  • How to allocate money across stocks on long and short sides?
    • Equal weight?
    • Weight based on predictions? More on best, etc.?

Portfolio returns

  • Important: before transforming return in preprocessing step, save actual return with a different name (or use different name for the transformed return).
  • Example: df[“actual”] = df[“ret”]

Longs and shorts

numlong = 100
numshort = 100

df["rank_from_top"] = df.groupby("date").predict.rank(
  method="first", 
  ascending=False
)
df["long"] = df.rank_from_top <= numlong

df["rank_from_bottom"] = df.groupby("date").predict.rank(
  method="first"
)
df["short"] = df.rank_from_bottom <= numshort

Equally weighted Monthly returns

df = df[df.index.get_level_values("date") >= dates[0]]

long_ret = df[df.long].groupby("date").actual.mean()
short_ret = df[df.short].groupby("date").actual.mean()

Example 150/50

ret = 1.5*long_ret - 0.5*short_ret
ret
date
2005-01    0.038716
2005-02    0.115622
2005-03    0.001808
2005-04   -0.017554
2005-05    0.056740
             ...   
2021-11    0.028348
2021-12    0.090617
2022-01   -0.023111
2022-02    0.014441
2022-03    0.003977
Name: actual, Length: 207, dtype: float64