Prevents look-ahead bias in cross-sectional asset pricing factor construction. Covers signal timing, portfolio sorts, return alignment, and rebalancing conventions for academic finance research.
Ships in ai-asset-pricing. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
These rules prevent look-ahead bias (LAB) when forming cross-sectional factors. LAB means using information beyond the portfolio formation date — it silently inflates factor returns and invalidates results.
Run this checklist whenever forming portfolios or constructing factors. Flag any violation.
.shift(-1) or forward-looking operation on the signalPreferred approach: Keep signals at their natural date. Lead only the portfolio return series.
Step 1: At date t, sort stocks using signal known at t
Step 2: Compute portfolio return from stock returns at t+1
Step 3: Assign this return to date t+1 (the period it was earned)
Why this is preferred:
Date re-alignment for the output:
# After computing portfolio returns, shift date to earning period
port_ret["date"] = port_ret["date"] + pd.offsets.MonthEnd(1)
import pandas as pd
import numpy as np
def monthly_factor(df, signal_col, date_col="date", id_col="permno",
ret_col="ret", me_col="me", n_groups=5,
weighting="vw"):
"""
Form monthly-rebalanced long-short factor.
df must have: date (month-end), permno, signal (known at date t),
ret (return earned in month t), me (market equity at date t).
Convention: signal_t is used to form portfolios. Returns from t+1
are the out-of-sample returns. Output dated to t+1.
"""
df = df.sort_values([date_col, id_col]).copy()
# Signal must exist at formation date
df = df.dropna(subset=[signal_col])
# Sort into quantile portfolios each month
df["port"] = df.groupby(date_col)[signal_col].transform(
lambda x: pd.qcut(x, n_groups, labels=False, duplicates="drop")
)
# Forward return: use NEXT month's return for each stock
df = df.sort_values([id_col, date_col])
df["fwd_ret"] = df.groupby(id_col)[ret_col].shift(-1)
df["fwd_date"] = df.groupby(id_col)[date_col].shift(-1)
# Gap check: if next date is > 31 days away, set fwd_ret to NaN
day_gap = (df["fwd_date"] - df[date_col]).dt.days
df.loc[day_gap > 31, "fwd_ret"] = np.nan
df = df.dropna(subset=["fwd_ret"])
# Portfolio returns
if weighting == "vw":
# Value-weight using ME at formation date t (NOT t+1)
df["weight"] = df.groupby([date_col, "port"])[me_col].transform(
lambda x: x / x.sum()
)
port_ret = df.groupby([date_col, "port"]).apply(
lambda g: (g["fwd_ret"] * g["weight"]).sum(),
include_groups=False
).unstack("port")
else:
port_ret = df.groupby([date_col, "port"])["fwd_ret"].mean().unstack("port")
# Long-short: top minus bottom
port_ret["hml"] = port_ret[n_groups - 1] - port_ret[0]
# Re-date to earning period
port_ret.index = port_ret.index + pd.offsets.MonthEnd(1)
port_ret.index.name = date_col
return port_ret
# Usage:
# port_ret = monthly_factor(panel, signal_col="bm", n_groups=5, weighting="vw")
Convention: Sort at end of June using signals from the most recent December fiscal year-end (with 4-month lag ensuring availability by April).
Timeline:
Dec t-1: Fiscal year ends → accounting data (BE, etc.)
Apr t: Data becomes available (4-month lag)
Jun t: Sort stocks into portfolios using Dec t-1 accounting + Jun t ME
Jul t - Jun t+1: Hold portfolios, rebalance next June
def annual_factor(df, signal_col, date_col="date", id_col="permno",
ret_col="ret", me_col="me", n_groups=5,
formation_month=6, weighting="vw"):
"""
Annual rebalancing (Fama-French style).
Signal is measured once per year at formation_month (default June).
Portfolios held for 12 months (July through next June).
"""
df = df.sort_values([id_col, date_col]).copy()
# Identify formation months
df["month"] = df[date_col].dt.month
df["year"] = df[date_col].dt.year
# At formation month: assign portfolios using signal
formation = df[df["month"] == formation_month].dropna(subset=[signal_col]).copy()
formation["port"] = formation.groupby("year")[signal_col].transform(
lambda x: pd.qcut(x, n_groups, labels=False, duplicates="drop")
)
# Carry portfolio assignment forward for 12 months
# Merge assignment back to full panel
assign = formation[[id_col, "year", "port"]].copy()
assign = assign.rename(columns={"year": "form_year"})
# Each stock's holding period: Jul of form_year through Jun of form_year+1
df["form_year"] = np.where(
df["month"] <= formation_month, df["year"] - 1, df["year"]
)
df = df.merge(assign, on=[id_col, "form_year"], how="left")
df = df.dropna(subset=["port"])
# Forward return for out-of-sample performance
df = df.sort_values([id_col, date_col])
df["fwd_ret"] = df.groupby(id_col)[ret_col].shift(-1)
df["fwd_date"] = df.groupby(id_col)[date_col].shift(-1)
day_gap = (df["fwd_date"] - df[date_col]).dt.days
df.loc[day_gap > 31, "fwd_ret"] = np.nan
df = df.dropna(subset=["fwd_ret"])
# Portfolio returns (VW using current-month ME)
if weighting == "vw":
df["weight"] = df.groupby([date_col, "port"])[me_col].transform(
lambda x: x / x.sum()
)
port_ret = df.groupby([date_col, "port"]).apply(
lambda g: (g["fwd_ret"] * g["weight"]).sum(),
include_groups=False
).unstack("port")
else:
port_ret = df.groupby([date_col, "port"])["fwd_ret"].mean().unstack("port")
port_ret["hml"] = port_ret[n_groups - 1] - port_ret[0]
port_ret.index = port_ret.index + pd.offsets.MonthEnd(1)
port_ret.index.name = date_col
return port_ret
Key differences from monthly:
The templates above use pd.qcut across all stocks (equal-count portfolios). Fama-French uses NYSE-only breakpoints applied to all stocks:
# NYSE breakpoints (FF convention)
nyse_mask = df["exchcd"] == 1
breakpoints = df.loc[nyse_mask, signal_col].quantile(
[i/n_groups for i in range(1, n_groups)]
)
df["port"] = pd.cut(df[signal_col], bins=[-np.inf] + list(breakpoints) + [np.inf],
labels=False)
This creates unequal-sized portfolios (many small NASDAQ stocks in extreme groups). Always confirm with the user which convention they want — all-stock quantiles vs NYSE breakpoints.
pd.qcut with duplicates='drop' can silently produce fewer groups than requested when many stocks share identical signal values. Always verify the actual number of groups created:
actual_groups = df["port"].nunique()
assert actual_groups == n_groups, f"Expected {n_groups} groups, got {actual_groups}"
Delisting returns must be incorporated. Factor returns that ignore delistings suffer from survivorship bias. In CRSP v2, mthret/dlyret already include delisting returns. In CRSP v1, you must manually compound dlret with the final trading return: ret_adj = (1 + ret) * (1 + dlret) - 1.
After building any factor, run these sanity checks:
t = mean / (std / sqrt(T)). For well-known factors over long samples, expect |t| > 2.df.groupby(['date','port']).size().describe(). Fewer than ~30 stocks per portfolio is a red flag.(panel_date - datadate).median(). Median should be reasonable (e.g., ~10 months for annual BE).What SKILL.md sets; muted values are Claude Code's defaults
38 skills in this plugin
/audit-captionsAudit all table and figure captions for language, notation, and formatting consistency/audit-mathAdversarial audit of mathematical proofs, derivations, and formal environments/audit-sectionDeep audit of a single section -- style, factual accuracy, citations, logical flow/bond-dataReference for the Dickerson corporate bond dataset: column mappings between WRDS and PyBondLab, rating encoding, return definitions, signal clusters, and data gotchas. Auto-apply when loading bond data for PyBondLab analysis./build-contextGenerate guidance/paper-context.md from user-provided .tex, .md, or .pdf files/build-deckCreate and compile Beamer presentations following the Rhetoric of Decks philosophy. Use when making seminar or conference slides./ai-asset-pricing:factor-constructionfrom ai-asset-pricing
ai-asset-pricing@fin-skills is listed by howard-lynn-ye/fin-rsi — review the source before installing
npx claudepluginhub howard-lynn-ye/fin-rsi --plugin ai-asset-pricingOther plugins' skills for the same job