f/tax.py
Greg Pomerantz 576d9fba97 Fix after-tax model: raw prices + reinvestment (was 2.7x over-taxing)
The old model valued holdings in ADJ (total-return index) units but
computed distribution flows as raw per-share dollars — so every
distribution, and its tax, was overstated by the raw/adj ratio
(JLPSX: 30.10/11.25 = 2.7x; the Dec-2020 cap-gain tax showed as 12.1%
of the account instead of the true 4.5%). The wiggle in the after-tax
curve was this bug, not a convention issue.

tax.py now:
- values holdings in RAW share units on close prices (bundle.close);
- receives the per-share distribution on its event date, pays the tax
  (recorded in TaxResult.taxes), and reinvests the after-tax remainder
  at the same day's raw close — the tax's effect lives in the (smaller)
  reinvested units and is NOT also deducted from cash (double-count
  caught and fixed in review);
- recomputes market value after the reinvestment so equity[t] is the
  post-event liquidation value.

With the fix, the 'as-if-liquidated' equity on JLPSX's ex-div day drops
by exactly the true tax cost (4.72% vs 12.1% before); the -22.9% price
drop is offset by the distribution kept.

Also:
- app.py passes bundle.close to the after-tax model (pre-tax
  portfolio_returns still uses adj);
- JLPSX/JLPYX: the 2020-12-11 6.824 capital-gain distribution is moved
  to the true ex-div date 2020-12-14 (remove/add correction ops), so
  the reinvestment prices at the post-drop close;
- tests/test_tax.py: 8 synthetic regression tests (tax magnitude,
  reinvestment MV, no double-count, ex-div equity step, per-component
  rates); run_tests.sh now runs it.
2026-08-31 23:58:15 -04:00

223 lines
9.3 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`
capital gain dist-> taxed at `lt_rate`
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, state rates,
brackets.
"""
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,
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."""
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
# 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)
c_tax = float(cinc.sum() * lt_rate)
tax_rows[t, 0] = d_tax
tax_rows[t, 1] = c_tax
for i in range(len(syms)):
net = (dv[t, i] * (1 - div_rate) + cv[t, i] * (1 - lt_rate)) * 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
if gain >= 0:
tax = gain * (lt_rate if li == 1 else st_rate)
real_rows[t, li - 1] += gain
else:
tax = gain * (lt_rate if li == 1 else st_rate)
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, st_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),
)