<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[InvestWealth Research]]></title><description><![CDATA[Systematic investing research on 150 years of market data, with the code behind it. Main site: investwealth.com]]></description><link>https://investwealth.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/6b06807f569cabf960e2da1e/7f5601ad-136e-4afe-b933-501a8f720d23.png</url><title>InvestWealth Research</title><link>https://investwealth.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 20:51:12 GMT</lastBuildDate><atom:link href="https://investwealth.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Testing the 4% rule on every retirement since 1872 in 40 lines of Python]]></title><description><![CDATA[Every 30-year US retirement since 1872, tested with the 4% rule in under 40 lines of Python. 121 of 125 survived. Data and code included.]]></description><link>https://investwealth.hashnode.dev/testing-the-4-rule-on-every-retirement-since-1872-in-40-lines-of-python</link><guid isPermaLink="true">https://investwealth.hashnode.dev/testing-the-4-rule-on-every-retirement-since-1872-in-40-lines-of-python</guid><category><![CDATA[Python]]></category><category><![CDATA[Open Source]]></category><category><![CDATA[Data Science]]></category><category><![CDATA[Tutorial]]></category><dc:creator><![CDATA[Harry Hamann]]></dc:creator><pubDate>Wed, 25 Nov 2026 06:44:07 GMT</pubDate><content:encoded><![CDATA[<p>The 4% rule says: in the first year of retirement, take out 4% of your savings. Every year after that, take out the same amount, raised with inflation. Do that, and the money should last 30 years.</p>
<p>It is one of the most quoted numbers in personal finance. It is also easy to test yourself. This post does it in under 40 lines of Python, with no libraries beyond the standard one, on US data that goes back to 1872.</p>
<h2>The data</h2>
<p>One CSV, one row per calendar year from 1872 to 2025, three columns:</p>
<table>
<thead>
<tr>
<th>column</th>
<th>meaning</th>
</tr>
</thead>
<tbody><tr>
<td><code>us_stocks_total_return</code></td>
<td>US stocks (S&amp;P 500 and its predecessors), total return</td>
</tr>
<tr>
<td><code>us_10y_treasury_total_return</code></td>
<td>10-year US Treasury, total return</td>
</tr>
<tr>
<td><code>us_cpi_inflation</code></td>
<td>US consumer price inflation</td>
</tr>
</tbody></table>
<p>Returns are decimals (0.10 means +10%). The series are built from Robert Shiller's long-run data (Yale) and FRED. The file and the full script are on GitHub: <a href="https://github.com/investwealth-com/investwealth-retirement-1872">investwealth-com/investwealth-retirement-1872</a>.</p>
<h2>The test</h2>
<p>The idea is simple. Start one retirement in every year since 1872. For each one:</p>
<ol>
<li>Take the year's withdrawal out at the start of the year.</li>
<li>Let the rest grow with that year's return of a 60/40 mix of stocks and bonds.</li>
<li>Raise the withdrawal with that year's inflation.</li>
<li>Repeat for 30 years. If the balance hits zero, that retirement failed.</li>
</ol>
<p>With data to 2025, the last 30-year retirement starts in 1996. That gives 125 retirements.</p>
<pre><code class="language-python">import csv, io, urllib.request

URL = "https://raw.githubusercontent.com/investwealth-com/investwealth-retirement-1872/main/data/us_annual_returns_1872.csv"
rows = list(csv.DictReader(io.StringIO(urllib.request.urlopen(URL).read().decode())))
years = [int(r["year"]) for r in rows]
stocks = [float(r["us_stocks_total_return"]) for r in rows]
bonds = [float(r["us_10y_treasury_total_return"]) for r in rows]
cpi = [float(r["us_cpi_inflation"]) for r in rows]


def survives(start, rate, horizon=30, stock_share=0.6):
    """One retirement: withdraw at the start of each year, raise the withdrawal with inflation, rebalance once a year."""
    balance, withdrawal = 1.0, rate
    for i in range(start, start + horizon):
        balance -= withdrawal
        if balance &lt;= 0:
            return False, years[i]
        balance *= 1 + stock_share * stocks[i] + (1 - stock_share) * bonds[i]
        withdrawal *= 1 + cpi[i]
    return True, None


def test(rate, horizon=30, stock_share=0.6):
    starts = range(len(years) - horizon + 1)
    failed = [years[s] for s in starts if not survives(s, rate, horizon, stock_share)[0]]
    return len(starts) - len(failed), len(starts), failed


ok, n, failed = test(0.04)
print(f"4% rule, 60/40, 30 years: {ok} of {n} retirements survived. Failed starts: {failed}")

lo, hi = 0.0, 0.10          # the highest rate that survived every start
for _ in range(30):
    mid = (lo + hi) / 2
    lo, hi = (mid, hi) if not test(mid)[2] else (lo, mid)
print(f"Highest rate that survived every start: {lo:.2%}")
</code></pre>
<h2>The result</h2>
<pre><code class="language-plaintext">4% rule, 60/40, 30 years: 121 of 125 retirements survived. Failed starts: [1965, 1966, 1968, 1969]
Highest rate that survived every start: 3.79%
</code></pre>
<p>Three things stand out.</p>
<p><strong>The 4% rule held in 121 of 125 retirements.</strong> That is 97%. For a rule of thumb, that is a strong record.</p>
<p><strong>All four failures started within five years of each other.</strong> 1965, 1966, 1968 and 1969. Not 1929. The Great Depression was brutal for stocks, but prices fell by almost a quarter from 1929 to 1932, so the withdrawal shrank with them, and bonds gained. The late 1960s were the reverse. From 1966 to 1981 prices almost tripled, so the withdrawal almost tripled too. Over the same years stocks lost 16% after inflation and bonds lost 37%. The retiree was hit from both sides.</p>
<p><strong>The rate that never failed was 3.79%.</strong> Not far below 4%. The last fifth of a point is the price of surviving the worst start in 150 years.</p>
<h2>Change the question</h2>
<p>The functions take the horizon and the stock share, so other questions are one line each:</p>
<pre><code class="language-python">print(test(0.04, horizon=40))            # 40 years instead of 30
print(test(0.04, stock_share=0.2))       # a cautious 20/80 mix
print(test(0.035, horizon=40, stock_share=0.8))
</code></pre>
<p>The full script in the repository prints the table for five mixes and four horizons. A few of its numbers: over 40 years the safe rate for 60/40 drops to about 3.5%, and a cautious 20/80 mix falls to 2.7%. Fewer stocks did not make a long retirement safer. It made it worse, because bonds alone could not keep up with inflation for 40 years.</p>
<h2>What this does not tell you</h2>
<ul>
<li><strong>It is one country.</strong> The United States had one of the best stock markets of the last 150 years. Other countries did worse.</li>
<li><strong>It is before taxes and costs.</strong> A yearly cost of half a percent lowers every number here.</li>
<li><strong>The retirements overlap.</strong> 125 start years are not 125 independent experiments. Retirements that start a year apart share 29 of their 30 years.</li>
<li><strong>The past is not a forecast.</strong> The test shows what happened, not what will.</li>
</ul>
<h2>Try it without code</h2>
<p>If you want the same test with sliders instead of Python, the calculator is free and needs no account: <a href="https://www.investwealth.com/tools/safe-withdrawal-rate">Safe withdrawal rate calculator</a>. It runs on this exact dataset.</p>
<p>Data and code: <a href="https://github.com/investwealth-com/investwealth-retirement-1872">github.com/investwealth-com/investwealth-retirement-1872</a> (code MIT, data free to use with attribution).</p>
<p><em>Backtested results are hypothetical. Educational research, not investment advice.</em></p>
]]></content:encoded></item></channel></rss>