f/tax.py
Greg Pomerantz 9f657dc9f4 Tax engine: apply NIIT + state/local to the equity path (not just recorded taxes)
The full rate stack (federal + NIIT + state) was used for the recorded
tax columns and for realized gains at rebalances, but the equity path
itself deducted only the FEDERAL rate in two places:

1. distribution reinvestment kept (1 - div_rate)/(1 - lt_rate) instead of
   (1 - div_rate - niit - sl_rate);
2. the per-date liquidation tax ('sell everything today') passed bare
   (lt_rate, st_rate) to _liquidation_tax.

Symptom: FLCSX 10Y showed a 1.4pt after-tax drag instead of the true
~2.8pt. Fix: d_keep/c_keep factors and stacked liquidation rates.

Regression tests pin the equity path: reinvested net with NIIT+state,
ST and LT liquidation tax at the full stack (all fail on the old code).

Corrected 2015-2026 NYC after-tax: SPY 13.82->11.23, IVV 13.81->11.22,
JLPSX 13.68->9.18 (previously reported 12.51/12.50/11.31, superseded).
2026-09-01 17:22:04 -04:00

237 lines
10 KiB
Python

"""After-tax portfolio value: "what do I keep if I sell everything today?"
Model (simplified DAS)
----------------------
The account starts at 1.0 (growth-ratio units; no fixed capital).
- Holdings are valued on RAW (close) prices, in raw share units.
Distributions (per-share dollars from the event files) are taxed on
their event date and the after-tax remainder is REINVESTED at the same
day's raw close (the ex-div price): a fund paying d drops by d, the
investor keeps d(1-tau) and buys it back cheaper. The tax's effect is
carried by the (smaller) reinvested units; it is recorded in `taxes`
but not separately deducted from cash (that would double-count it).
dividend income -> taxed at (div_rate + niit + sl_rate)
capital gain dist-> taxed at (lt_rate + niit + sl_rate)
`niit` = federal 3.8% Net Investment Income Tax (0 if MAGI is under
the threshold). `sl_rate` = STATE+LOCAL marginal rate (e.g. NY+NYC)
applied at ORDINARY rates to every flow: state and local have no
preferential capital-gains rate, so a NYC resident pays city + state
on top of the federal rate for the same dollar. The after-tax
remainder also increases each lot's cost basis proportionally
(standard after-tax-IRR convention, so a liquidation doesn't re-tax
the distribution).
- On rebalance, sells are FIFO. A lot is long-term if held more than
365 days at sale, else short-term; realized gains/losses are taxed at
`lt_rate` / `st_rate`.
- THE headline number, `equity[t]`, is the after-tax value if you
LIQUIDATE the entire account on day t:
equity[t] = market value + cash - tax_on_unrealized_gains(t)
where tax_on_unrealized_gains marks every remaining lot to market,
classifies it LT/ST by age, nets losses against gains, and applies the
rates. This is what you would actually have in your pocket after
selling everything and filing your taxes.
NOTE: distribution event dates must be EX-DIV dates for the reinvestment
pricing to be right (the raw close on that day already reflects the
drop). Yahoo usually dates events on the ex-div date; the record-date
misalignments found by scripts/scan_adj_misalign.py are corrected by
remove/add ops in overrides/corrections/ where a symbol is analyzed.
Deliberately NOT modeled: loss carryover, wash sales, progressive
brackets (flat marginal rates), SALT-deduction interaction.
"""
from __future__ import annotations
from dataclasses import dataclass
import numpy as np
import pandas as pd
ST_WINDOW_DAYS = 365
@dataclass
class _Lot:
units: float
cost: float
date: pd.Timestamp
@dataclass
class TaxResult:
equity: pd.Series # after-tax value if you sell everything today (ratio)
liq_tax: pd.Series # tax that a full liquidation today would owe (unrealized)
taxes: pd.DataFrame # daily taxes actually PAID: div_tax, capg_tax, realized_tax, total
realized: pd.DataFrame # daily realized (at rebalances): lt_gain, st_gain, lt_loss, st_loss
lots_outstanding: int # final lot count (sanity check)
def _liquidation_tax(lt_gain: float, lt_loss: float,
st_gain: float, st_loss: float,
lt_rate: float, st_rate: float) -> float:
"""Tax on selling all lots now; losses offset gains (same/other type)."""
lt_net = lt_gain - lt_loss
st_net = st_gain - st_loss
if lt_net >= 0 and st_net >= 0:
return lt_net * lt_rate + st_net * st_rate
if lt_net < 0 and st_net < 0:
return 0.0
if st_net < 0: # ST losses offset LT gains
return max(lt_net + st_net, 0.0) * lt_rate
return max(st_net + lt_net, 0.0) * st_rate
def after_tax_portfolio(close: pd.DataFrame, div: pd.DataFrame, capg: pd.DataFrame,
weights: dict[str, float],
rebalance: str | None = None, cost_bps: float = 0.0,
lt_rate: float = 0.20, st_rate: float = 0.15,
div_rate: float = 0.15, niit: float = 0.0,
sl_rate: float = 0.0,
start: str | None = None, end: str | None = None
) -> TaxResult:
"""`close` must be RAW close prices (bundle.close), not adj: the
per-share distribution dollars are raw, so units must be raw
shares. All rates are decimals; realized gains are taxed at
(st/lt_rate + niit + sl_rate)."""
syms = [s for s in weights if s in close.columns]
p = close[syms]
d = div[[s for s in syms if s in div.columns]].reindex(index=p.index, columns=syms).fillna(0.0)
c = capg[[s for s in syms if s in capg.columns]].reindex(index=p.index, columns=syms).fillna(0.0)
if start or end:
p = p.loc[start:end]
p = p.dropna()
d, c = d.reindex(p.index).fillna(0.0), c.reindex(p.index).fillna(0.0)
if len(p) < 2:
raise ValueError("no overlapping data for the given symbols/period")
idx = p.index
pv = p.values
dv = d.values
cv = c.values
n = len(p)
w0 = np.array([weights[s] for s in syms])
w0 = w0 / w0.sum()
cost = cost_bps / 1e4
# full NYC-style stacks: distributions keep (1 - federal - NIIT - state);
# a sale (liquidation) gains are taxed at (federal + NIIT + state)
d_keep = max(0.0, 1.0 - div_rate - niit - sl_rate)
c_keep = max(0.0, 1.0 - lt_rate - niit - sl_rate)
# state (account = 1.0 at start)
units = w0 / pv[0]
lots: list[list[_Lot]] = [[_Lot(units[i], float(units[i] * pv[0][i]), idx[0])]
if units[i] > 0 else [] for i in range(len(syms))]
cash = -float(w0.sum() * cost) # initial purchase cost
tax_cols = ["div_tax", "capg_tax", "realized_tax", "total"]
tax_rows = np.zeros((n, len(tax_cols)))
real_cols = ["lt_gain", "st_gain", "lt_loss", "st_loss"]
real_rows = np.zeros((n, len(real_cols)))
equity = np.empty(n)
liq_tax = np.zeros(n)
# rebalance schedule (skip first day)
from portfolio import _rebalance_dates
rebal = (_rebalance_dates(idx, rebalance) - {idx[0]}) if rebalance else set()
for t in range(n):
prices = pv[t]
# --- distributions: pay tax, reinvest the after-tax remainder at
# today's close. The tax is recorded in tax_rows but NOT also
# deducted from cash: it is already reflected in the (smaller)
# reinvested units, so a second deduction would double-count it.
dinc = units * dv[t]
cinc = units * cv[t]
d_tax = float(dinc.sum() * (div_rate + niit + sl_rate))
c_tax = float(cinc.sum() * (lt_rate + niit + sl_rate))
tax_rows[t, 0] = d_tax
tax_rows[t, 1] = c_tax
for i in range(len(syms)):
net = (dv[t, i] * d_keep + cv[t, i] * c_keep) * units[i]
if net <= 0:
continue
px_i = float(prices[i])
if px_i > 0:
units[i] += net / px_i # reinvest the after-tax cash
if lots[i]: # grow cost basis
tot = sum(l.units for l in lots[i])
for l in lots[i]:
l.cost += net * (l.units / tot)
market_value = float(np.dot(units, prices)) # post-reinvestment
# --- rebalance to target weights --------------------------------
if idx[t] in rebal:
value = market_value
target_val = w0 * value
for i in range(len(syms)):
cur_val = units[i] * prices[i]
trade = target_val[i] - cur_val # + buy, - sell
if abs(trade) < 1e-9:
continue
c_cost = cost * abs(trade)
cash -= c_cost
if trade < 0: # sell |trade| at price, FIFO
to_sell = -trade / prices[i]
for l in lots[i]:
if to_sell <= 1e-12:
break
u = min(l.units, to_sell)
gain = u * prices[i] - (l.cost * u / l.units)
held = (idx[t] - l.date).days
li = 1 if held > ST_WINDOW_DAYS else 2
r_eff = (lt_rate if li == 1 else st_rate) + niit + sl_rate
if gain >= 0:
tax = gain * r_eff
real_rows[t, li - 1] += gain
else:
tax = gain * r_eff
real_rows[t, 3 if li == 1 else 2] += -gain
cash -= tax
tax_rows[t, 2] += tax
l.units -= u
l.cost *= (l.units / (l.units + u)) if l.units > 0 else 0.0
to_sell -= u
lots[i] = [l for l in lots[i] if l.units > 1e-12]
units[i] = cur_val / prices[i] + trade / prices[i]
else: # buy
spend = trade + c_cost
u = trade / prices[i]
lots[i].append(_Lot(u, spend, idx[t]))
units[i] += u
# --- tax owed if we liquidate everything today ------------------
lt_gain = lt_loss = st_gain = st_loss = 0.0
for i in range(len(syms)):
px = prices[i]
for l in lots[i]:
gain = l.units * px - l.cost
if (idx[t] - l.date).days > ST_WINDOW_DAYS:
if gain >= 0:
lt_gain += gain
else:
lt_loss += -gain
else:
if gain >= 0:
st_gain += gain
else:
st_loss += -gain
liq_tax[t] = _liquidation_tax(lt_gain, lt_loss, st_gain, st_loss,
lt_rate + niit + sl_rate,
st_rate + niit + sl_rate)
equity[t] = market_value + cash - liq_tax[t]
tax_rows[t, 3] = tax_rows[t, 0] + tax_rows[t, 1] + tax_rows[t, 2]
return TaxResult(
equity=pd.Series(equity, index=idx, name="after_tax_liquidation"),
liq_tax=pd.Series(liq_tax, index=idx, name="liquidation_tax"),
taxes=pd.DataFrame(tax_rows, index=idx, columns=tax_cols),
realized=pd.DataFrame(real_rows, index=idx, columns=real_cols),
lots_outstanding=sum(len(l) for l in lots),
)