import numpy as np
import pandas as pd
from pandas.tseries.holiday import (AbstractHolidayCalendar, GoodFriday, Holiday,
USLaborDay, USMartinLutherKingJr, USMemorialDay,
USPresidentsDay, USThanksgivingDay,
nearest_workday, sunday_to_monday)
SEED = 2026
N_STOCKS, N_SECTORS = 500, 10
WARMUP_MONTHS, STUDY_MONTHS = 24, 240 # 2004-2005 give the signals a history, then 2006-2025
BASE_RETURN = 0.008 # expected monthly return in the middle of both rankings
VALUE_PREMIUM = 0.003 # extra expected monthly return, top minus bottom of the value ranking
MOMENTUM_PREMIUM = 0.003 # the same for the momentum ranking
MARKET_SD, SECTOR_SD, STOCK_SD = 0.045, 0.025, 0.06 # monthly standard deviations of the shocks
ROE_MEAN, ROE_SD = 0.10, 0.04 # return on equity: level at a price-to-book of 2.5, yearly shock
PB_TO_ROE = 0.2 # extra return on equity per unit of log price-to-book above 2.5
LOG_PB_MEAN = np.log(2.5)
DAILY_SD = 0.015 # standard deviation of the daily draws
# period_range lists the 264 months from January 2004 as monthly periods, such as 2004-01
months = pd.period_range("2004-01", periods=WARMUP_MONTHS + STUDY_MONTHS, freq="M")
# Tickers S001 to S500: the numbers 1 to 500 as text, filled with zeros to three digits
tickers = "S" + pd.Series(range(1, N_STOCKS + 1)).astype(str).str.zfill(3)
# default_rng(SEED) starts the random number generator, so every run draws the same numbers;
# rng.integers(a, b, n) draws n whole numbers from a to b - 1, rng.uniform(a, b, n) n numbers
# equally likely anywhere between a and b, and rng.standard_normal(n) n normal draws with a
# mean of 0 and a standard deviation of 1; rng.choice(list, n) draws n items from the list
rng = np.random.default_rng(SEED)
sector = rng.integers(0, N_SECTORS, N_STOCKS) # sectors 0 to 9
beta = rng.uniform(0.7, 1.3, N_STOCKS)
cap = np.exp(np.log(10e9) + rng.standard_normal(N_STOCKS)) # median $10bn
payout = rng.uniform(0.0, 0.6, N_STOCKS) # share of earnings paid
# ** raises to a power: below the median size the factor exceeds 1, so smaller firms get
# larger stock-specific shocks
stock_sd = STOCK_SD * (cap / np.median(cap)) ** -0.15
# Book equity is market cap over a price-to-book ratio around 2.5: book-to-market around 0.4
book = cap / np.exp(LOG_PB_MEAN + 0.6 * rng.standard_normal(N_STOCKS))
fiscal_end = rng.choice([3, 6, 9, 12], N_STOCKS) # month of the year-end
# The starting return on equity sets the dividends paid until each firm's first year-end;
# np.maximum(roe, 0) compares stock by stock, so a negative return on equity pays nothing
roe = (ROE_MEAN + PB_TO_ROE * (np.log(cap / book) - LOG_PB_MEAN)
+ ROE_SD * rng.standard_normal(N_STOCKS))
monthly_dividend = payout * np.maximum(roe, 0) * book / 12
# The starting book equity counts as published at the end of December 2003; subtracting n
# from a monthly period gives the month n months earlier
first_reports = pd.DataFrame({"ticker": tickers, "fiscal_end": months[0] - 3,
"book_equity": book.copy(), "published": months[0] - 1})
new_reports = [] # one row per report, added at each fiscal year-end
public = book.copy() # the latest published book equity
due_month = np.full(N_STOCKS, -1) # the month in which each firm's next report is published
due_book = np.zeros(N_STOCKS) # the book equity in that report
returns = np.empty((len(months), N_STOCKS)) # filled month by month below
caps = np.empty((len(months), N_STOCKS))
dividends_paid = np.empty((len(months), N_STOCKS))
opening_cap = cap.copy() # the market caps before January 2004
for m in range(len(months)):
# The expected return of month m increases with the value and momentum percentiles at the
# end of month m-1. rank(pct=True) gives each stock its percentile, nan_to_num gives a
# missing percentile the middle value 0.5, and np.where(condition, a, b) takes a where the
# condition holds and b elsewhere
value_score = pd.Series(np.where(public > 0, public / cap, np.nan)).rank(pct=True)
value_score = np.nan_to_num(value_score.to_numpy(), nan=0.5)
# returns[m - 12:m - 1] holds rows m-12 to m-2, months t-11 to t-1 for t = m-1, and
# np.prod(..., axis=0) multiplies down each stock's column. Before 12 months of returns
# exist, every momentum percentile is 0.5
if m >= 12:
past_growth = np.prod(1 + returns[m - 12:m - 1], axis=0)
momentum_score = pd.Series(past_growth).rank(pct=True).to_numpy()
else:
momentum_score = np.full(N_STOCKS, 0.5)
expected = (BASE_RETURN + VALUE_PREMIUM * (value_score - 0.5)
+ MOMENTUM_PREMIUM * (momentum_score - 0.5))
# Total return: expected return plus a market, a sector and a stock-specific shock;
# sector_shock[sector] gives each stock the shock of its own sector
market = MARKET_SD * rng.standard_normal()
sector_shock = SECTOR_SD * rng.standard_normal(N_SECTORS)
returns[m] = (expected + beta * market + sector_shock[sector]
+ stock_sd * rng.standard_normal(N_STOCKS))
dividends_paid[m] = monthly_dividend
cap = cap * (1 + returns[m]) - monthly_dividend # the dividend paid leaves the firm
caps[m] = cap
# Each firm whose fiscal year ends this month: earnings and dividends change book equity,
# and the report is published 2, 3 or 4 months later. np.flatnonzero lists the positions
# where the condition holds
for i in np.flatnonzero(fiscal_end == months[m].month):
roe[i] = max(ROE_MEAN + PB_TO_ROE * (np.log(cap[i] / book[i]) - LOG_PB_MEAN)
+ ROE_SD * rng.standard_normal(), -0.9) # never below -90%
earnings = roe[i] * book[i]
dividends = payout[i] * max(earnings, 0.0)
book[i] = book[i] + earnings - dividends
monthly_dividend[i] = dividends / 12 # paid over the next twelve months
lag = int(rng.integers(2, 5))
due_month[i] = m + lag
due_book[i] = book[i]
new_reports.append({"ticker": tickers[i], "fiscal_end": months[m],
"book_equity": book[i], "published": months[m] + lag})
# The reports due this month become public
public[due_month == m] = due_book[due_month == m]
# Daily data. The trading days are the weekdays except US exchange holidays; a holiday on a
# weekend moves to the nearest weekday, except New Year's Day, which moves only from Sunday.
# The calendar runs into 2026 so that every report has a publication date
holidays = AbstractHolidayCalendar(rules=[
Holiday("New Year's Day", month=1, day=1, observance=sunday_to_monday),
USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay,
Holiday("Juneteenth", month=6, day=19, start_date="2022-01-01", observance=nearest_workday),
Holiday("Independence Day", month=7, day=4, observance=nearest_workday),
USLaborDay, USThanksgivingDay,
Holiday("Christmas", month=12, day=25, observance=nearest_workday),
]).holidays("2003-12-01", "2026-06-30")
trading_days = pd.bdate_range("2003-12-01", "2026-06-30", freq="C", holidays=holidays)
days = trading_days[(trading_days >= "2004-01-01") & (trading_days <= "2025-12-31")]
month_of_day = months.get_indexer(days.to_period("M")) # 0 for the days of January 2004
days_per_month = np.bincount(month_of_day) # trading days in each month
# A day's log return spreads the month's log return evenly over its trading days and adds a
# draw from which the month's average draw is taken off, so the draws sum to zero and the
# daily returns compound to the monthly return
draws = DAILY_SD * rng.standard_normal((len(days), N_STOCKS))
draws = draws - pd.DataFrame(draws).groupby(month_of_day).transform("mean").to_numpy()
daily_log = np.log1p(returns)[month_of_day] / days_per_month[month_of_day, None] + draws
# Market cap: the month's opening cap grown by the month's daily returns so far, less the
# month's dividend, paid on its last trading day
opening_caps = np.vstack([opening_cap, caps[:-1]])
growth = np.exp(pd.DataFrame(daily_log).groupby(month_of_day).cumsum().to_numpy())
daily_caps = opening_caps[month_of_day] * growth
last_day = np.append(month_of_day[1:] != month_of_day[:-1], True)
daily_caps[last_day] = daily_caps[last_day] - dividends_paid
# Reports with dates: a fiscal year ends on the last day of its month, and a report is
# published on the last trading day of its month
fundamentals = pd.concat([first_reports, pd.DataFrame(new_reports)], ignore_index=True)
last_trading_day = pd.Series(trading_days, index=trading_days.to_period("M"))
last_trading_day = last_trading_day.groupby(level=0).max()
fundamentals["fiscal_year_end"] = fundamentals["fiscal_end"].dt.end_time.dt.normalize()
fundamentals["published"] = last_trading_day.loc[fundamentals["published"]].to_numpy()
fundamentals = fundamentals[["ticker", "fiscal_year_end", "published", "book_equity"]]
# The daily panel: one row per ticker and trading day. ravel() lays the days-by-tickers
# arrays out day by day, the order of repeat and tile
daily = pd.DataFrame({"date": days.repeat(N_STOCKS), "ticker": np.tile(tickers, len(days)),
"return": np.expm1(daily_log).ravel(), "market_cap": daily_caps.ravel(),
"sector": np.tile(sector + 1, len(days))})
# Each day's book equity is the latest one published by that day: merge_asof matches each row
# with the ticker's last report published on or before its date, and needs both tables sorted
# by date
reports = fundamentals[["ticker", "published", "book_equity"]].sort_values("published")
daily = pd.merge_asof(daily, reports, left_on="date", right_on="published", by="ticker")
daily = daily[["date", "ticker", "return", "market_cap", "book_equity", "sector"]]How to do factor investing
Part 3 of 3 in a series on stock characteristics and factors: 1. Portfolio sorts and Fama-MacBeth regressions, 2. Testing a stock characteristic as a factor, 3. How to do factor investing.
Part 2 tested whether a stock characteristic is a factor. Factor investing is when you take stock characteristics that research has found to be factors and use them to construct a portfolio.
I simulate stocks and their characteristics and show how to do factor investing when you have access to stock returns and characteristics over time. I use two characteristics: value and momentum.
This portfolio is long-only. Research papers often use long-short portfolios, such as HML, the Fama and French value factor, but I want to keep the post as short and simple as possible.
The animation follows six of the 500 stocks through one rebalance.
Simulating the data
I simulate the data so you can reproduce it and to ensure value and momentum are truly associated with future returns. A stock at the top of the value ranking has an expected return 0.3 percentage points a month higher than a stock at the bottom, and the same holds for momentum. The data covers 500 stocks from January 2004 to December 2025, with one row per stock and trading day, as a data vendor normally delivers it.
The whole point of the simulation is to make the data realistic. The simulation first runs month by month, so one observation is a stock-month. At the start, each stock gets a sector, one of ten, a beta, \(\beta_i\), between 0.7 and 1.3, a market cap around $10 billion, a payout ratio up to 60%, a book-to-market ratio around 0.4 and a fiscal year-end at the end of a quarter. I picked these numbers from my experience with mid- and large-cap stocks, without any concrete evidence, and a beta can, of course, be smaller or larger than that.
Some of the simulation comes from the literature, such as the return, which is an expected return plus a market, a sector, and a stock-specific shock. Each month’s total return is
\[r_{i,t+1} = \mu_{i,t} + \beta_i f_{t+1} + g_{s(i),t+1} + \varepsilon_{i,t+1},\]
where \(f_{t+1}\) is the market shock, with a standard deviation of 4.5% per month, \(g_{s(i),t+1}\) is the shock to stock \(i\)’s sector \(s(i)\), with 2.5%, and \(\varepsilon_{i,t+1}\) is the stock’s own shock, with 6% for a stock of median starting size, multiplied by the stock’s starting size relative to the median to the power \(-0.15\), so smaller firms have larger shocks. Each shock is a normal draw with a mean of zero. The shocks are independent across months. The market shock is shared by every stock, and a sector shock by the stocks of that sector. The expected return includes the assumed premium:
\[\mu_{i,t} = 0.008 + 0.003\,(p^V_{i,t} - 0.5) + 0.003\,(p^M_{i,t} - 0.5),\]
where \(p^V_{i,t}\) and \(p^M_{i,t}\) are the stock’s value and momentum percentiles at the end of month \(t\), computed as in Steps 1 and 2, with the momentum percentile set to 0.5 before twelve months of returns exist. Market cap follows
\[C_{i,t+1} = C_{i,t}\,(1 + r_{i,t+1}) - D_{i,t+1},\]
where \(D_{i,t+1}\) is the dividend paid during month \(t+1\).
Once a year, at the end of the fiscal year, the firm’s earnings minus its dividends are added to its book equity. Firms priced above 2.5 times book grow their book equity faster on average than firms priced below it. The changes to book equity come out in the annual report two to four months after the fiscal year ends.
\[\text{ROE}_{i,y} = 0.10 + 0.2\,\big(\ln(C_i/B_i) - \ln 2.5\big) + 0.04\,z_{i,y},\]
where \(\text{ROE}_{i,y}\) is the return on equity, earnings divided by book equity, at fiscal year-end \(y\), but never below \(-90\%\), \(C_i/B_i\) is the market cap at the year-end divided by the book equity at the start of the year, \(\ln\) the natural logarithm and \(z_{i,y}\) a normal draw with a mean of 0 and a standard deviation of 1.
I split each monthly return into daily returns over the trading days. Each day gets an equal share of the month’s return plus some random noise with a mean of zero, so the daily returns compound exactly to the monthly return.
\[\ln\left(1 + r^{\text{day}}_{i,s}\right) = \frac{\ln(1 + r_{i,t})}{n_t} + \eta_{i,s} - \bar\eta_{i,t},\]
where \(r^{\text{day}}_{i,s}\) is a stock’s total return on trading day \(s\) of month \(t\), \(n_t\) the number of trading days in month \(t\), \(\eta_{i,s}\) a normal draw with a mean of 0 and a standard deviation of 1.5%, and \(\bar\eta_{i,t}\) its average over the month’s trading days.
A stock’s market cap moves with its daily returns, and the month’s dividend is paid on its last trading day. Each report is published on the last trading day of its month, and on each day, book equity comes from the latest report published by then. The simulation also makes the table of reports, which the timing example in Step 1 uses.
# The first three rows, with market cap and book equity in billions of dollars
COLUMN_NAMES = {"return": "total return", "market_cap": "market cap ($bn)",
"book_equity": "book equity ($bn)"}
first_rows = daily.head(3).copy()
first_rows[["market_cap", "book_equity"]] = first_rows[["market_cap", "book_equity"]] / 1e9
first_rows.round({"return": 4, "market_cap": 1, "book_equity": 1}).rename(columns=COLUMN_NAMES)| date | ticker | total return | market cap ($bn) | book equity ($bn) | sector | |
|---|---|---|---|---|---|---|
| 0 | 2004-01-02 | S001 | 0.0040 | 4.8 | 1.5 | 9 |
| 1 | 2004-01-02 | S002 | 0.0211 | 16.3 | 17.0 | 2 |
| 2 | 2004-01-02 | S003 | -0.0120 | 2.8 | 1.6 | 1 |
Preparing the data
Steps 1 to 4 use monthly data, so I compound each month’s daily returns into a monthly return:
\[r_{i,t} = \prod_{s \in t} \left(1 + r^{\text{day}}_{i,s}\right) - 1,\]
where \(r^{\text{day}}_{i,s}\) is stock \(i\)’s total return on trading day \(s\) of month \(t\). The month-end market cap and book equity are those on the month’s last trading day.
# Each ticker may appear once per date
repeated = daily.duplicated(["date", "ticker"])
if repeated.any():
raise ValueError(f"{repeated.sum()} repeated ticker-date rows, the first on "
f"{daily.loc[repeated, 'date'].iloc[0]:%d %B %Y}")
# A wide table of daily returns: one row per trading day, one column per ticker
daily_returns = daily.pivot(index="date", columns="ticker", values="return")
month = daily_returns.index.to_period("M") # each trading day's calendar month
# A month without any trading data would shorten the 11-month windows, so it stops the code
gaps = pd.period_range(month.min(), month.max(), freq="M").difference(month.unique())
if len(gaps) > 0:
raise ValueError(f"no trading data in {gaps[0]}")
# A month is complete when a ticker has as many daily returns as the best-covered ticker, and
# at least one; counts.gt(0) keeps a month without any return missing
counts = daily_returns.notna().groupby(month).sum()
complete = counts.eq(counts.max(axis=1), axis=0) & counts.gt(0)
# prod() compounds each month's 1 + r, and where() leaves an incomplete month missing
monthly_returns = ((1 + daily_returns).groupby(month).prod() - 1).where(complete)
# Each month is labeled by the market's last trading day
last_days = daily_returns.index.to_series().groupby(month).max()
monthly_returns.index = pd.DatetimeIndex(last_days.to_numpy(), name="date")
# melt turns the wide table back into one row per ticker and month-end
monthly = monthly_returns.reset_index().melt(id_vars="date", var_name="ticker",
value_name="return")
# Market cap, book equity and sector from each month's last trading day; a ticker without data
# that day keeps missing values, never an earlier day's
month_end_rows = daily.loc[daily["date"].isin(last_days),
["date", "ticker", "market_cap", "book_equity", "sector"]]
monthly = monthly.merge(month_end_rows, on=["date", "ticker"], how="left")
monthly = monthly.sort_values(["date", "ticker"], ignore_index=True)first_rows = monthly.head(3).copy()
first_rows[["market_cap", "book_equity"]] = first_rows[["market_cap", "book_equity"]] / 1e9
first_rows.round({"return": 4, "market_cap": 1, "book_equity": 1}).rename(columns=COLUMN_NAMES)| date | ticker | total return | market cap ($bn) | book equity ($bn) | sector | |
|---|---|---|---|---|---|---|
| 0 | 2004-01-30 | S001 | 0.0415 | 5.0 | 1.5 | 9 |
| 1 | 2004-01-30 | S002 | 0.0582 | 16.9 | 17.0 | 2 |
| 2 | 2004-01-30 | S003 | 0.0088 | 2.9 | 1.6 | 1 |
Step 1. Calculating value and momentum
Following Bali, Engle and Murray, value is book equity divided by market cap:
\[V_{i,t} = \frac{B_{i,t}}{C_{i,t}},\]
where \(B_{i,t}\) and \(C_{i,t}\) are stock \(i\)’s book equity and market cap at month-end \(t\), and \(V_{i,t}\) is missing when book equity is negative.
You could also measure value in other ways, such as EBIT divided by enterprise value, but I keep it to one measure to make it simple.
Momentum asks how a stock has performed in the past, looking only at its return over the past year, without the latest month:
\[M_{i,t} = \prod_{j=1}^{11} (1 + r_{i,t-j}) - 1,\]
where \(r_{i,t-j}\) is the stock’s total return \(j\) months before month \(t\). I skip the most recent month to avoid short-term reversals. I do not want a stock’s momentum to be positive only because it rose sharply just before rebalancing.
# Value: book equity over market cap, missing unless both are positive
monthly["value"] = (monthly["book_equity"].where(monthly["book_equity"] > 0)
/ monthly["market_cap"].where(monthly["market_cap"] > 0))
# Momentum in a wide table of monthly returns, one row per month-end and one column per ticker:
# rolling(11, min_periods=11) requires all 11 returns, raw=True passes each window to np.prod as
# a plain array, and shift(1) moves the products down one row, so month t covers t-11 to t-1
wide_returns = monthly.pivot(index="date", columns="ticker", values="return")
momentum = (1 + wide_returns).rolling(11, min_periods=11).apply(np.prod, raw=True).shift(1) - 1
momentum = momentum.reset_index().melt(id_vars="date", var_name="ticker", value_name="momentum")
monthly = monthly.merge(momentum, on=["date", "ticker"], how="left")EXAMPLE_DATE = pd.Timestamp("2015-03-31") # the month-end the examples show
# The report behind S001's book equity on the example date: the latest one published by then
report = fundamentals.loc[(fundamentals["ticker"] == "S001")
& (fundamentals["published"] <= EXAMPLE_DATE)]
report = report.sort_values("published").iloc[-1] # iloc[-1]: the last row
example_month = EXAMPLE_DATE.to_period("M")
window = pd.period_range(example_month - 11, example_month - 1, freq="M")
# {:%d %B %Y} writes a date as day, month name and year, and strftime("%B %Y") a month as its
# name and year
print(f"Timing on {EXAMPLE_DATE:%d %B %Y}, for ticker S001:")
print(f" value uses the book equity reported for the fiscal year ending "
f"{report['fiscal_year_end']:%d %B %Y}, published on {report['published']:%d %B %Y}")
print(f" momentum compounds the {len(window)} monthly returns from "
f"{window[0].strftime('%B %Y')} to {window[-1].strftime('%B %Y')} "
f"and skips {example_month.strftime('%B %Y')}")
wide_returns.iloc[:2, :3].round(4) # the wide table's first rowsTiming on 31 March 2015, for ticker S001:
value uses the book equity reported for the fiscal year ending 31 December 2014, published on 31 March 2015
momentum compounds the 11 monthly returns from April 2014 to February 2015 and skips March 2015
| ticker | S001 | S002 | S003 |
|---|---|---|---|
| date | |||
| 2004-01-30 | 0.0415 | 0.0582 | 0.0088 |
| 2004-02-27 | 0.1558 | 0.0730 | 0.1570 |
Step 2. Scoring the stocks
I rank the stocks by percentile, assigning each stock a score for how well it performs on each of the two characteristics. Value is a ratio and momentum a return, so the percentiles also put both on the same scale, where higher is better:
\[p^V_{i,t} = \frac{\operatorname{rank}(V_{i,t})}{N_t},\]
where \(N_t\) is the number of stocks with both a value and a momentum at month-end \(t\), and the rank counts from 1 for the lowest value. The momentum percentile \(p^M_{i,t}\) works the same way.
There are different ways to combine the two percentiles, but I assume an equal weight on both and take the average, so that I get one score that combines value and momentum:
\[S_{i,t} = 0.5\,p^V_{i,t} + 0.5\,p^M_{i,t}.\]
I rank the stocks by this score and select the 100 best. The table shows six stocks at the end of March 2015.
N_SELECTED = 100
# Eligible on a date: a positive market cap, a value and a momentum from all 11 monthly
# returns. dropna drops a ticker from that date only, so it can qualify again later
scores = monthly.loc[monthly["market_cap"] > 0].dropna(subset=["value", "momentum"]).copy()
# rank(pct=True) gives each ticker's rank as a fraction of the eligible tickers on its date
scores["value_percentile"] = scores.groupby("date")["value"].rank(pct=True)
scores["momentum_percentile"] = scores.groupby("date")["momentum"].rank(pct=True)
scores["score"] = 0.5 * scores["value_percentile"] + 0.5 * scores["momentum_percentile"]
# ascending=False numbers each date's tickers from 1, the highest score; method="first" gives
# tied scores consecutive ranks in table order
scores["rank"] = (scores.groupby("date")["score"]
.rank(ascending=False, method="first").astype(int))
scores["selected"] = scores["rank"] <= N_SELECTED# Six tickers from the full table: the top score, the last selected and the first left out, the
# highest value with weak momentum, the highest momentum with weak value, and S022, whose weight
# Step 3 caps
example_tickers = ["S118", "S222", "S002", "S119", "S102", "S022"]
march = scores.loc[scores["date"] == EXAMPLE_DATE].set_index("ticker")
example = march.loc[example_tickers]
print(f"Scores on {EXAMPLE_DATE:%d %B %Y}; the {N_SELECTED} highest of the {len(march)} "
f"are selected")
pd.DataFrame({
"date": example["date"],
"market cap ($bn)": (example["market_cap"] / 1e9).round(1),
"value V": example["value"].round(2),
"momentum M (%)": (example["momentum"] * 100).round(1),
"value percentile": example["value_percentile"].round(3),
"momentum percentile": example["momentum_percentile"].round(3),
"combined score S": example["score"].round(3),
"rank": example["rank"],
"selected": example["selected"].map({True: "yes", False: "no"}),
})Scores on 31 March 2015; the 100 highest of the 500 are selected
| date | market cap ($bn) | value V | momentum M (%) | value percentile | momentum percentile | combined score S | rank | selected | |
|---|---|---|---|---|---|---|---|---|---|
| ticker | |||||||||
| S118 | 2015-03-31 | 5.9 | 0.93 | 18.5 | 0.948 | 0.810 | 0.879 | 1 | yes |
| S222 | 2015-03-31 | 34.0 | 0.39 | 24.1 | 0.360 | 0.862 | 0.611 | 100 | yes |
| S002 | 2015-03-31 | 47.7 | 0.39 | 24.8 | 0.348 | 0.872 | 0.610 | 101 | no |
| S119 | 2015-03-31 | 0.2 | 2.03 | -46.2 | 1.000 | 0.004 | 0.502 | 252 | no |
| S102 | 2015-03-31 | 3.0 | 0.25 | 80.6 | 0.082 | 1.000 | 0.541 | 185 | no |
| S022 | 2015-03-31 | 154.2 | 0.35 | 53.8 | 0.266 | 0.980 | 0.623 | 87 | yes |
S119 has the highest value but near-lowest momentum, so it ranks only 252nd. S118, strong in both, ranks first.
Step 3. Selecting and weighting the stocks
I use the market cap to weight the stocks in the portfolio, together with their score, so each stock’s base weight is
\[w^0_{i,t} = \frac{C_{i,t}\,S_{i,t}}{\sum_j C_{j,t}\,S_{j,t}},\]
where the sum runs over the 100 selected stocks.
If a stock’s weight exceeds 5%, I cap it at 5% and distribute the remaining weight to the other stocks that have not hit the cap, in proportion to their weights. If a weight is 8%, for example, the remaining 3 percentage points go to the other stocks. So the target weight of a stock that has not hit the cap is
\[w^{\text{target}}_{i,t} = w^0_{i,t}\,\frac{1 - 0.05\,K_t}{\sum_{j\ \text{uncapped}} w^0_{j,t}},\]
where \(K_t\) is the number of stocks that hit the cap.
I am not saying this is identical to what mutual funds or hedge funds do, but they usually have a weight cap.
WEIGHT_CAP = 0.05
def cap_weights(weights):
"""Cut every weight above 5% to 5% and give the excess to the other stocks in proportion to
their weights, until no weight is above 5%."""
weights = weights.copy()
capped = pd.Series(False, index=weights.index)
# 1e-12 allows for rounding in the last digits of a computed weight
while (weights > WEIGHT_CAP + 1e-12).any():
capped = capped | (weights > WEIGHT_CAP) # |: capped earlier or above the cap now
weights[capped] = WEIGHT_CAP
free = ~capped # ~ turns True into False
weights[free] = weights[free] / weights[free].sum() * (1 - WEIGHT_CAP * capped.sum())
return weights
# Base weight: market cap times score for the selected tickers, 0 for the others, as a share of
# the month's total; transform("sum") puts each month's total on every row of that month
cap_times_score = (scores["market_cap"] * scores["score"]).where(scores["selected"], 0.0)
scores["base_weight"] = cap_times_score / cap_times_score.groupby(scores["date"]).transform("sum")
# transform(cap_weights) caps each month's weights separately and keeps the rows in place
scores["target_weight"] = scores.groupby("date")["base_weight"].transform(cap_weights)march = scores.loc[scores["date"] == EXAMPLE_DATE].set_index("ticker")
held = march.loc[march["selected"]] # the 100 selected tickers
shown = march.loc[example_tickers]
shown = shown.loc[shown["selected"]] # the example tickers that are held
# {:.2%} writes a fraction as a percentage with two decimals
print(f"Target weights on {EXAMPLE_DATE:%d %B %Y}: {len(held)} tickers, "
f"{(held['base_weight'] > WEIGHT_CAP).sum()} base weight above 5%; target weights from "
f"{held['target_weight'].min():.2%} to {held['target_weight'].max():.2%}, "
f"summing to {held['target_weight'].sum():.2%}")
# map("{:.2f}".format) writes each number with two decimals, so the columns line up
pd.DataFrame({
"date": shown["date"],
"market cap ($bn)": (shown["market_cap"] / 1e9).map("{:.1f}".format),
"combined score S": shown["score"].map("{:.3f}".format),
"base weight (%)": (shown["base_weight"] * 100).map("{:.2f}".format),
"target weight (%)": (shown["target_weight"] * 100).map("{:.2f}".format),
})Target weights on 31 March 2015: 100 tickers, 1 base weight above 5%; target weights from 0.04% to 5.00%, summing to 100.00%
| date | market cap ($bn) | combined score S | base weight (%) | target weight (%) | |
|---|---|---|---|---|---|
| ticker | |||||
| S118 | 2015-03-31 | 5.9 | 0.879 | 0.40 | 0.41 |
| S222 | 2015-03-31 | 34.0 | 0.611 | 1.60 | 1.64 |
| S022 | 2015-03-31 | 154.2 | 0.623 | 7.40 | 5.00 |
S022’s base weight of 7.40% is cut to 5%, so the other 99 stocks share 95% instead of 92.6%.
Step 4. Rebalancing the portfolio
At the end of each quarter, I rebalance by redoing Steps 1, 2, and 3 with the information known at that date. To keep it simple, I trade at that day’s closing prices, and the weights drift between the rebalances. They drift because of returns. Stocks with higher returns than the portfolio get a higher weight, and the opposite happens for stocks with lower returns. I am not saying daily rebalancing does not work. I am saying that in some code, I have seen people miss that they need to let weights drift when they rebalance less frequently than daily. So, at the end of month \(t\), before I trade, the drifted weight of stock \(i\) is
\[w^{\text{drifted}}_{i,t} = \frac{w_{i,t-1}\,(1 + r_{i,t})}{1 + R_t}, \qquad R_t = \sum_i w_{i,t-1}\,r_{i,t},\]
where \(w_{i,t-1}\) is the stock’s weight after any trade at the end of the previous month, and \(R_t\) is the portfolio’s return before trading costs.
I use turnover to account for the trading cost. Trading costs apply to the stocks I sell out of the portfolio and the new stocks I buy, and also to stocks whose weight has drifted and that I need to adjust back. In an extreme case, a stock capped at 5% may go up so much that it has a weight of 10% by the next rebalancing date. Even if the stock stays in the portfolio, I need to sell 5 percentage points to bring it back to the cap, and that comes with a trading cost. So turnover adds up everything that I buy and sell, as a share of wealth:
\[\tau_t = \sum_i \left| w^{\text{target}}_{i,t} - w^{\text{drifted}}_{i,t} \right|.\]
I assume a trading cost of 10 basis points for each dollar that I trade, so each rebalance multiplies wealth by \(1 - 0.001\,\tau_t\). The trading cost is an assumption and can be higher or lower in practice.
COST_PER_DOLLAR = 0.001 # 10 basis points of every dollar bought or sold
wide_caps = monthly.pivot(index="date", columns="ticker", values="market_cap")
# The target weights in the shape of the wide returns; fillna(0.0) makes a missing target 0,
# no holding, while missing returns stay missing
target_table = (scores.pivot(index="date", columns="ticker", values="target_weight")
.reindex(columns=wide_returns.columns).fillna(0.0))
study_dates = wide_returns.index[WARMUP_MONTHS - 1:] # from December 2005, the first formation
# The quarter-ends from December 2005 to September 2025; December 2025 has no next month
formation_dates = study_dates[:-1][study_dates[:-1].month.isin([3, 6, 9, 12])]
eligible_counts = scores.groupby("date").size() # eligible tickers on each datecurrent_weights = pd.Series(0.0, index=wide_returns.columns) # weights after the last trade
wealth_before, wealth_after = [1.0], [1.0]
rebalances = {} # each rebalance's current weights, targets and trades
turnovers = [] # each rebalance's turnover
# zip pairs each month-end with the next one, whose month's returns the weights earn
for date, next_date in zip(study_dates[:-1], study_dates[1:]):
# 1. Rebalance when due: trade from the drifted weights to the new targets and pay 10 basis
# points of every dollar traded; wealth_after[-1] is the latest wealth
if date in formation_dates:
if eligible_counts.get(date, 0) < N_SELECTED:
raise ValueError(f"{date:%d %B %Y}: {eligible_counts.get(date, 0)} eligible tickers, "
f"fewer than {N_SELECTED}")
target_weights = target_table.loc[date]
trade = target_weights - current_weights
turnover = trade.abs().sum() # bought plus sold, as a share of wealth
wealth_after[-1] = wealth_after[-1] * (1 - COST_PER_DOLLAR * turnover)
rebalances[date] = pd.DataFrame({"current": current_weights, "target": target_weights,
"trade": trade})
turnovers.append(turnover)
current_weights = target_weights
# 2. Apply the next month's returns: a held ticker without a return stops the loop, and a
# ticker not held adds nothing, whatever its return
next_returns = wide_returns.loc[next_date]
missing = current_weights.index[(current_weights > 0) & next_returns.isna()]
if len(missing) > 0:
raise ValueError(f"no return in the month to {next_date:%d %B %Y} for {', '.join(missing)}")
held_returns = next_returns.where(current_weights > 0, 0.0)
portfolio_return = (current_weights * held_returns).sum()
# 3. Update wealth, then let the weights drift with the returns
wealth_before.append(wealth_before[-1] * (1 + portfolio_return))
wealth_after.append(wealth_after[-1] * (1 + portfolio_return))
current_weights = current_weights * (1 + held_returns) / (1 + portfolio_return)# The benchmark holds every ticker with a positive market cap at its share of the month-end
# total, whether or not it is eligible
positive_caps = wide_caps.where(wide_caps > 0)
total_caps = positive_caps.sum(axis=1)
no_caps = total_caps.loc[study_dates[:-1]] <= 0
if no_caps.any():
raise ValueError(f"no positive total market cap on {no_caps.idxmax():%d %B %Y}")
benchmark_weights = positive_caps.div(total_caps, axis=0).fillna(0.0)
# shift(1) moves each month-end's weights down one row, to the month whose returns they earn
held_weights = benchmark_weights.shift(1).loc[study_dates[1:]]
month_returns = wide_returns.loc[study_dates[1:]]
missing = (held_weights > 0) & month_returns.isna()
if missing.any().any():
first = missing.any(axis=1).idxmax() # the first month with a missing return
raise ValueError(f"benchmark: no return in the month to {first:%d %B %Y} for "
f"{', '.join(missing.columns[missing.loc[first]])}")
benchmark = (held_weights * month_returns.where(held_weights > 0, 0.0)).sum(axis=1)
wealth = pd.DataFrame({"before costs": wealth_before, "after costs": wealth_after,
"benchmark": np.append(1.0, (1 + benchmark).cumprod())},
index=study_dates)NEXT_REBALANCE = pd.Timestamp("2015-06-30") # the rebalance after the example month-end
rebalance = rebalances[NEXT_REBALANCE]
kept = rebalance.loc[(rebalance["current"] > 0) & (rebalance["target"] > 0)]
sold = rebalance.loc[(rebalance["current"] > 0) & (rebalance["target"] == 0)]
bought = rebalance.loc[(rebalance["current"] == 0) & (rebalance["target"] > 0)]
turnover = rebalance["trade"].abs().sum()
# clip(lower=0) keeps the purchases and clip(upper=0) the sales
print(f"Rebalancing on {NEXT_REBALANCE:%d %B %Y}: {len(kept)} tickers kept, {len(sold)} sold, "
f"{len(bought)} bought; bought {rebalance['trade'].clip(lower=0).sum():.2%} and sold "
f"{-rebalance['trade'].clip(upper=0).sum():.2%} of wealth, turnover {turnover:.2%}, "
f"cost {COST_PER_DOLLAR * turnover:.3%} of wealth")
# The two largest weights kept, sold and bought
rows = (list(kept.nlargest(2, "current").index) + list(sold.nlargest(2, "current").index)
+ list(bought.nlargest(2, "target").index))
(rebalance.loc[rows] * 100).round(2).rename(columns={"current": "drifted weight (%)",
"target": "new target (%)",
"trade": "trade (%)"})Rebalancing on 30 June 2015: 59 tickers kept, 41 sold, 41 bought; bought 51.44% and sold 51.44% of wealth, turnover 102.89%, cost 0.103% of wealth
| drifted weight (%) | new target (%) | trade (%) | |
|---|---|---|---|
| ticker | |||
| S412 | 3.61 | 3.27 | -0.34 |
| S258 | 3.38 | 2.98 | -0.40 |
| S022 | 4.71 | 0.00 | -4.71 |
| S257 | 4.15 | 0.00 | -4.15 |
| S165 | 0.00 | 4.37 | 4.37 |
| S178 | 0.00 | 4.30 | 4.30 |
At the end of June 2015, S022, capped at 5% in March, had drifted to 4.71%, and it was sold because it no longer ranked among the 100 best.
Evaluating the portfolio
The chart shows, on a log scale, how the portfolio and its benchmark performed. The benchmark holds all 500 stocks weighted by market cap.
import matplotlib.pyplot as plt
TEAL, ORANGE, GREY = "#17868A", "#D2822B", "#888888"
# The site's chart colors: a cream background, dark text, a light grid and a pale frame
BG, INK, GRID, SPINE, AXIS_TEXT = "#FCEFE3", "#1f1f1f", "#EADCCC", "#D5C6B4", "#4a4a4a"
# figsize is the figure's width and height in inches; lw is the line width, ls="--" dashed
fig, axis = plt.subplots(figsize=(7.6, 4.4))
axis.plot(wealth.index, wealth["after costs"], color=TEAL, lw=2.4, label="portfolio after costs")
axis.plot(wealth.index, wealth["before costs"], color=ORANGE, lw=1.4, ls="--",
label="portfolio before costs")
axis.plot(wealth.index, wealth["benchmark"], color=GREY, lw=1.8, label="cap-weighted benchmark")
axis.set_yscale("log") # equal distances are equal percentage changes
axis.set_yticks([1, 2, 5, 10, 20], ["1", "2", "5", "10", "20"])
axis.minorticks_off()
axis.set_ylabel(r"wealth of \$1 invested at the end of 2005", color=AXIS_TEXT) # \$: a dollar
axis.set_title("Simulated wealth, 2006 to 2025", color=INK)
# frameon=False draws no box around the legend, and labelcolor="linecolor" writes each entry
# in the color of its line
axis.legend(frameon=False, loc="upper left", labelcolor="linecolor")
# The site's look: a cream background, a light grid behind the lines, a pale frame on the left
# and bottom only, and no tick marks
fig.patch.set_facecolor(BG)
axis.set_facecolor(BG)
axis.grid(True, axis="y", color=GRID, lw=1.0)
axis.set_axisbelow(True)
axis.spines[["top", "right"]].set_visible(False)
axis.spines[["left", "bottom"]].set_color(SPINE)
axis.tick_params(axis="both", length=0, colors=INK, pad=6)
plt.tight_layout()
# dpi sets the saved image's pixels per inch, and bbox_inches="tight" trims the empty margin
plt.savefig("fi_wealth.png", dpi=140, bbox_inches="tight", facecolor=BG)
plt.show()
years = STUDY_MONTHS / 12
wealth_returns = wealth.pct_change().dropna() # each month's return; dropna() drops row one
annualised_return = (wealth.iloc[-1] ** (1 / years) - 1) * 100 # the yearly growth rate, in %
# The monthly standard deviation times the square root of 12, in %: the variances of
# independent months add up
annualised_volatility = wealth_returns.std() * np.sqrt(12) * 100
# Turnover per year leaves out the first purchase: the other 79 rebalances span 19.75 years
turnover_per_year = sum(turnovers[1:]) / (years - 0.25)
performance = pd.DataFrame({
"portfolio": [annualised_return["before costs"], annualised_return["after costs"],
annualised_volatility["after costs"], turnover_per_year * 100],
"benchmark": [annualised_return["benchmark"], annualised_return["benchmark"],
annualised_volatility["benchmark"],
0.0]}, # the benchmark trades only to reinvest dividends, free in both
index=["annualised return before costs (%)", "annualised return after costs (%)",
"annualised volatility (%)", "turnover per year (% of wealth)"])
# The holdings on each formation date: every ticker with a positive market cap, so missing
# signals cannot shrink the benchmark, with its percentiles (missing when not eligible), its
# portfolio weight (0 when not selected) and its benchmark weight
holdings = monthly.loc[monthly["date"].isin(formation_dates) & (monthly["market_cap"] > 0),
["date", "ticker", "market_cap", "sector"]]
holdings = holdings.merge(
scores[["date", "ticker", "value_percentile", "momentum_percentile", "target_weight"]],
on=["date", "ticker"], how="left")
holdings["portfolio"] = holdings["target_weight"].fillna(0.0)
holdings["benchmark"] = (holdings["market_cap"]
/ holdings.groupby("date")["market_cap"].transform("sum"))
# melt stacks the two weight columns: one row per ticker, date and holder, portfolio or
# benchmark, so each measure below is one groupby for both holders
holdings = holdings.melt(id_vars=["date", "sector", "value_percentile", "momentum_percentile"],
value_vars=["portfolio", "benchmark"], var_name="holder",
value_name="weight")
keys = [holdings["holder"], holdings["date"]]
# sort_values then head(10) keeps each holder's 10 largest weights on each date
top_ten = holdings.sort_values("weight", ascending=False).groupby(["holder", "date"]).head(10)
# A percentile's average weights only the tickers that have that percentile
value_weight = holdings["weight"].where(holdings["value_percentile"].notna())
momentum_weight = holdings["weight"].where(holdings["momentum_percentile"].notna())
sector_weights = holdings.groupby(["holder", "date", "sector"])["weight"].sum()
by_date = pd.DataFrame({
"effective number of stocks": 1 / (holdings["weight"] ** 2).groupby(keys).sum(),
"weight of the 10 largest (%)": top_ten.groupby(["holder", "date"])["weight"].sum() * 100,
"weighted average value percentile": ((value_weight * holdings["value_percentile"])
.groupby(keys).sum() / value_weight.groupby(keys).sum()),
"weighted average momentum percentile": ((momentum_weight * holdings["momentum_percentile"])
.groupby(keys).sum()
/ momentum_weight.groupby(keys).sum()),
"largest sector weight (%)": sector_weights.groupby(["holder", "date"]).max() * 100,
})
# The average over the 80 formation dates, one column per holder
holding_measures = by_date.groupby("holder").mean().T[["portfolio", "benchmark"]]
# pd.concat stacks the two tables, the performance rows above the holdings rows
pd.concat([performance, holding_measures]).round(2)| portfolio | benchmark | |
|---|---|---|
| annualised return before costs (%) | 16.18 | 14.81 |
| annualised return after costs (%) | 15.79 | 14.81 |
| annualised volatility (%) | 16.03 | 15.88 |
| turnover per year (% of wealth) | 332.80 | 0.00 |
| effective number of stocks | 45.02 | 162.93 |
| weight of the 10 largest (%) | 36.77 | 16.44 |
| weighted average value percentile | 0.65 | 0.36 |
| weighted average momentum percentile | 0.75 | 0.56 |
| largest sector weight (%) | 23.82 | 16.36 |
Not surprisingly, the portfolio did better, at 15.79% a year after trading costs versus 14.81% for the benchmark, but only because I built in a premium for value and momentum. Trading costs are 0.39 points per year, and both have about 16% volatility per year.
Limitations
The main limitation is that I built in the premium. In a real market, the premiums of these characteristics may shift over time, and sometimes they may be dead. Value stocks, for example, did poorly against growth stocks from 2007 to 2020, in what some call the value winter, and some blame the low interest rates of that time.
Conclusion
This post illustrates a realistic way to do factor investing. Whether factor investing still works in practice is another question, which I leave to the reader to test with the data they have access to.
Read next
- Trading costs more than halved Swedish momentum’s ending value What trading costs did to a momentum portfolio on Swedish stocks.
- Same data, same trading signal, different answer How ordinary choices in building a portfolio can flip its trading decision.
Disclaimer: a teaching example on simulated data, whose returns follow the assumptions in the code. Not investment advice.
Sources: Turan G. Bali, Robert F. Engle and Scott Murray, Empirical Asset Pricing: The Cross Section of Stock Returns (Wiley, 2016), Chapter 5 for weighted portfolio returns and formation timing, Chapter 10 for book-to-market and when fundamentals are public, and Chapter 11 for momentum over months \(t-11\) to \(t-1\). Kenneth French, momentum factor, with prior returns from months 2 to 12 before the holding month. Shaun Fitzgibbons, Jacques Friedman, Lukasz Pomorski and Laura Serban, Long-Only Style Investing: Don’t Just Mix, Integrate, Journal of Investing (2017). S&P Dow Jones Indices, S&P Quality, Value & Momentum Multi-Factor Indices methodology, for weights proportional to market cap times score. Eugene F. Fama and Kenneth R. French, “Common risk factors in the returns on stocks and bonds”, Journal of Financial Economics (1993).