*Last Updated: October 3, 2022*

**Traditional Approach Algorithm**

Below is a qualitative description of the algorithm implemented for the traditional approach.

I also put together a higher level diagram of the traditional method:

- Traditional Approach Algorithm
- Time
- Investment Growth
- Expenses
- Dividend Income
- Other Income
- Social Security Income (SSI)
- Selling Taxable Lots
- Pulling from 457(b) account
- Pulling from traditional retirement account – only if penalty-free
- Pulling from Roth IRA – only if penalty-free
- Pulling from Cash Account
- Pulling from Roth IRA – with penalty
- Pulling from traditional retirement account – with penalty
- Out of money!!!!! Gah!!!!
- Computing Taxes
- Iteration
- Investing Excess Cash

- Tax And Penalty Minimization (TPM) Algorithm
- Time, Investment Growth, Expenses, Dividend Income, Other Income
- Specified Income
- Social Security Income (SSI)
- Pulling from 457(b) account
- Pulling from traditional pretax retirement account(s)
- Selling Taxable Lots
- Computing and Paying Taxes
- Pull from Roth
- Pull from Cash Account
- Options that will generate taxes and/or penalties
- Out of money!!!!! Gah!!!!
- Investing Excess Cash

### Time

The simulation starts at “Year 1” and projects forward in time by the specified number of years. All steps described below are executed for each year.

### Investment Growth

First the provided annual investment ROI (e.g. 7%) is applied to all accounts except the cash account. Note that I assume the first year’s expenses, taxes, etc. all happen at the very beginning of the simulation, so I do not apply the annual investment ROI for that first year (since no time has passed).

On top of growing the taxable lots by the ROI, I also increase the capital gains associated with each lot by the same amount.

### Expenses

Next up, we compute expenses for that year, which can be adjusted for any year by the user. Remember that expenses are in current day dollars, so inflation is accounted for.

### Dividend Income

Next we bring in user specified qualified and nonqualified dividends, which count against LT cap gain income and standard income, respectively.

### Other Income

Then any other income the user specifies is brought it, and assumed to count towards standard income.

### Social Security Income (SSI)

The final income step is bringing in social security income (SSI), based on the amount and starting age you specify. Note: you can create an account at the social security administration site to determine a) if you have enough credits to receive SSI and b) how much it predicts you’ll receive at the standard retirement age of 67 (which you can adjust to specify that you won’t earn any more income after a certain year).

### Selling Taxable Lots

At this point, we determine how much more cash we need to cover expenses, after accounting for the above income sources. If we do still need cash, first we turn to the taxable lots.

Before any lots are sold, first we compute what percentage of each lot is capital gains, and we sort them from highest percent to lowest percent. This order is chosen for a couple reasons: a) most people will have FIFO (first in first out) as their cost basis method, since it’s typically the default, which typically means the lots with the highest capital gains are sold first (since the oldest lots typically have the highest percentage capital gains); b) by using lots with the highest percentage cap gains, we maximize capital gain harvesting.

Next we remove from consideration any lots that were purchased the same year the simulation starts (assuming you have enough in older lots to cover expenses the first year) to avoid short term capital gains (taxed at standard income rates).

Now we can loop over all lots to get the remaining cash we need to cover expenses. For each lot, we determine if the lot value will cover the remaining cash we need, and if so we determine what fraction of that lot to sell. If the lot doesn’t cover our entire cash needs, we sell the entire lot and move on to the next lot.

### Pulling from 457(b) account

If after selling all taxable lots you still don’t have enough cash for expenses, or you’ve already sold all your taxable lots, next up is withdrawing money from your 457(b) account, if you have one. These withdrawals count as standard income, and there is no penalty with withdrawing before 59.5.

If your 457(b) account doesn’t cover your remaining cash need, sell any remaining amount and recompute how much cash you need.

### Pulling from traditional retirement account – only if penalty-free

If you still don’t have enough cash, then it’s time to pull from your traditional pretax accounts such as a 401K, 403b, or traditional IRA.

To avoid penalties, I’ve set the algorithm at this step to only consider pulling from your traditional pretax accounts if you’re 60 or older (actually 59.5, but we’re using whole integer ages in this simulation).

Just like for the 457(b) account, 100% of your pretax account withdrawals count as standard income.

### Pulling from Roth IRA – only if penalty-free

If you still don’t have enough cash after selling your pretax account assets or have already sold all your pretax account assets, next up is pulling from your Roth accounts.

If you’re 60 or older, you can pull directly from your Roth accounts without worrying about taxes or penalties.

If you’re not old enough yet, you can still pull any original contribution amounts from your Roth account without penalty (or taxes). This rule is why it’s important to specify how much of your Roth account is original contributions in the user inputs.

### Pulling from Cash Account

If you still don’t have enough cash after selling your penalty-free Roth funds, next up is pulling from your standard cash account (e.g. a checking or savings account).

### Pulling from Roth IRA – with penalty

If you still don’t have enough cash and you’re under 60 (actually 59.5), your next best option is pulling from your Roth account and taking the 10% penalty hit. At least you’re not also paying taxes on top of that penalty.

### Pulling from traditional retirement account – with penalty

If you still don’t have enough cash and you’re under 60 (actually 59.5), your final option is to pull from your traditional retirement account, paying both taxes and a 10% penalty on those withdrawals (though if the total withdrawal is less than the standard deduction, fortunately you won’t pay any taxes).

### Out of money!!!!! Gah!!!!

If you still don’t have enough cash after all the above steps, you’re officially broke – time to go get a job and/or seriously cut your expenses down to your current income levels. I suspect that’s very unlikely to happen to anyone that’s reading this article, but it is good to consider the worst case scenario.

### Computing Taxes

After getting the cash needed for that year, next up is computing taxes. And if you have social security income, you also must compute how much of your social security income is taxable (which then counts as standard income), which is a function of both how much SSI you have and the total amount of other income you have. I plan to focus on how to optimize your income while accounting for SSI taxation rules in a future post.

Taxes are computed using the provided tax brackets, filing status, your total standard income, and your total LT capital gain (and qualified dividend) income.

### Iteration

Here’s where things get slightly tricky.

All the above steps were shooting for a particular amount of cash that would cover your expenses. BUT if you owe taxes, you then have to pay those taxes and you then don’t have enough cash to cover your expenses. Blah!

So we ACTUALLY need to pull a particular amount of money out of our accounts that will cover both our expenses and taxes we will owe.

Confused? Let’s look at an example:

If a married couple needs $36K for expenses, and withdraws $36K from their traditional pretax or 457(b) accounts, that’s about $10K over the standard deduction of $25900. So they will pay 10% taxes (first tax bracket rate) on that $10K, which is $1K. But that means they need another $1K from their investments, which will also be taxed at 10%, which is $100. But that means they need another $100 from their investments, which will also be taxed at 10%, which is $10. But that means they need another $10 from their investments, which will also be taxed at 10%, which is $1. But that means they need another $1 from their investments, which will also be taxed at 10%, which is $0.10. But that means they need another $0.10 from their investments, which will also be taxed at 10%, which is $0.01. Whew!

So the total tax bill is $1000 + $100 + $10 + $1 + $0.10 + $0.01 = $1111.11, and thus they need a total investment withdrawal of $36000 + $1000 + $100 + $10 + $1 + $0.1 = $37111.10.

So how do we compute the actual withdrawals needed? We iterate, using what’s known as a “while loop”.

The first time through the loop, we withdraw enough funds to match the expenses specified for that year. Then we compute taxes on that amount. If taxes are not zero, we add that tax amount to the amount of cash needed overall. Then we go through the entire loop again, starting with the “Dividend Income” step above, pulling enough funds to get the new needed amount of cash. Again we compute taxes on the new amount, and repeat this loop until the amount of cash you get from your accounts equals your expenses plus the taxes you owe (down to the penny).

### Investing Excess Cash

If we reach a point where income exceeds expenses (e.g. social security starts rolling in, your house gets paid off, etc.), then a new taxable lot is purchased with that excess cash.

**Tax And Penalty Minimization (TPM) Algorithm**

Much of the TPM algorithm is the same as the traditional approach algorithm – so I’ll be highlighting the differences. As always, you can review the code to see the exact implementation.

I also put together a higher level diagram of the TPM method:

### Time, Investment Growth, Expenses, Dividend Income, Other Income

These steps are the same as the traditional approach algorithm.

### Specified Income

Unlike the traditional approach algorithm, the TPM method targets an income specified by the user for each year of the simulation. This approach is vital to ensuring taxes are minimized/eliminated (and optimizing ACA subsidies as well).

Currently the specified income only changes if all simulated people are 65 or older and thus you no longer need to worry about income affecting ACA subsidies. Though I know medicare premiums can increase if your income is above a certain level (looks like over $91K in 2022, though fortunately that’s above the top of the 0% LT cap gain tax bracket).

A more general approach that allows for any user specified income at any year could easily be implemented in the future – let me know if you’re interested.

### Social Security Income (SSI)

The SSI step is more complex in the TPM method.

Once you reach the age when SSI starts, the SSI amount is counted fully towards your cash need for the year. That’s the easy part.

Here’s the challenge: the amount of SSI that is considered taxable is a formula dictated by the IRS, which is a function of both SSI and all other taxable income. So if we’re targeting a specific standard income and total income (standard income plus LT cap gains), we have to set both non-SSI standard income and LT capital gains to get a particular taxable SSI which fits into these equations:

- taxable SSI + non-SSI standard income = specified standard income
- taxable SSI + non-SSI standard income + LT capital gains = specified total income

Tricky, eh?

Given the trickiness, I wrote an entire post that provides the details of how this is done. In a nutshell, I use a numerical method known as the Secant method with the highly optimized scipy.optimize.newton routine to solve for the appropriate non-SSI standard income and LT capital gains.

In the end, you have a value for the taxable SSI that is counted towards standard income.

The method may also modify the maximum LT cap gains amount to ensure standard income (including taxable SSI) does not exceed the standard income ceiling (e.g. standard deduction), which changes the targeted specified total income.

### Pulling from 457(b) account

After all the above income sources have been counted, we determine how much more standard income we need to get to the specified standard income (e.g. standard deduction).

If we still need more standard income, withdraw that amount from your 457(b). If your 457(b) account doesn’t cover your remaining needed standard income, sell any remaining amount and recompute how much standard income you need.

Any funds withdrawn from your 457(b) can be used immediately for expenses.

### Pulling from traditional pretax retirement account(s)

If you still don’t have enough standard income, then it’s time to pull from your traditional pretax accounts such as a 401K, 403b, or traditional IRA. Just like for the 457(b) account, 100% of your pretax account withdrawals count as standard income.

If your age is not yet 60, the amount you need for income is rolled over to your Roth account. That means you achieve the specified standard income (since the rollover counts as income), but you do not have penalty-free access to that money for 5 years (i.e. you’re performing a Roth conversion ladder). Thus the method tracks how much was rolled over and the age you rolled it over, for latter withdrawal as needed.

If you’re 60 or older, it’s much simpler: you just cash out the amount you need for income and you can immediately use that money for expenses.

And as with the 457(b), if there are not sufficient funds remaining in your traditional pretax account(s), the remainder is withdrawn.

### Selling Taxable Lots

At this point, we determine how much more TOTAL income we need to hit the specified total income – i.e. standard income plus capital gains.

Our goal is to sell taxable lots to generate enough capital gains to reach this total income value. If the previous steps did not reach the desired standard income, we can generate more capital gains to make up for it – so we’re still taking full advantage of the 0% LT cap gains tax bracket, even if we’re not taking full advantage of the standard deduction.

Before any lots are sold, first we compute what percentage of each lot is capital gains.

If we have not generated enough cash to cover expenses at this point, we sort the lots from lowest percent to highest percent capital gains. That order will maximize the chance there is sufficient cash for expenses after selling each subsequent lot.

If we HAVE generated enough cash to cover expenses at this point, we sort the lots from highest percent to lowest percent capital gains. That order will minimize the excess cash generated, since we already have enough.

This does mean in general you want to use “specific identification of shares” for your cost basis method in your brokerage account, so you can select the fund with the highest percentage or lowest percentage cap gains accordingly.

Next we remove from consideration any lots that were purchased the same year the simulation starts (assuming you have enough in older lots to cover expenses the first year) to avoid short term capital gains (taxed at standard income rates).

Now we can loop over all lots to generate the capital gains needed to achieve the specified total income. For each lot, we determine if the cap gains will cover the remaining income we need, and if so we determine what fraction of that lot to sell. If the lot doesn’t cover the entire remaining needed income, we sell the entire lot and move on to the next lot.

### Computing and Paying Taxes

After collecting income in the previous steps (which hopefully equals the specified standard income and specified total income), next up is computing taxes.

Taxes are computed using the provided tax brackets, filing status, your total standard income, and your total LT capital gain (and qualified dividend) income.

The total tax amount is then subtracted from the cash generated from the above steps.

### Pull from Roth

If after paying taxes you do not have sufficient cash to cover all your expenses, it’s time to pull funds from your Roth account.

If you’re 60 or older, you can pull directly from your Roth accounts without worrying about taxes or penalties. Very nice!

If you’re not 60 yet, it’s a bit trickier but you have options. You can still pull any original contribution amounts from your Roth account without penalty (or taxes). If you don’t have enough original contributions to get the remaining cash needed, next look to see if any Roth rollovers are at least 5 years old. If so, then you can pull the original rollover balance from your account, penalty (and tax) free.

### Pull from Cash Account

If you still don’t have enough cash after selling your penalty-free Roth funds, next up is pulling from your standard cash account (e.g. a checking or savings account).

### Options that will generate taxes and/or penalties

If you still don’t have enough cash, we now dive into options that will generate taxes and/or penalties. The broad goal is to start with the lowest tax/penalty options and work our way down to the highest tax/penalty options.

Note: I’ve not yet implemented these steps in the code as of publication, as I still haven’t needed to resort to these options in the simulations I’ve run. But I plan to do stress testing in a future post, at which time I’ll implement these steps.

Another note: if you no longer have any tax and penalty free money to pay for taxes and penalties, then you’ll have to use withdrawals that generate taxes/penalties to pay those taxes/penalties. Guess what that means? Iteration! Just as I describe above in the Traditional Approach section. And just like all the below steps, I’ve not yet implemented this iteration.

The sequence of next steps depends on whether you’re under or over 60 years old (actually 59.5, but for this analysis we use 60), due to penalties imposed for withdrawing from tax-advantaged accounts (with the notable exception of the 457(b)). Note that the pretax accounts other than the 457(b) are referenced as “pretax”.

Yet another note: for all steps that involve selling taxable lots, sort the lots from lowest to highest percentage LT cap gain and sell them in that order to minimize taxes.

If you’re **under** 60 and you still need more cash, proceed down this list until you either have enough cash or you deplete all accounts:

- Sell remaining taxable lots that have LT cap gain percentage less than 66%:
**less than 10%**effective tax - Pull from Roth growth or rollovers less than 5 years old:
**10%**penalty - Pull from 457(b) in
**10%**tax bracket (could also do this before pulling from Roth growth, same effective tax/penalty rate of 10%) - Sell remaining taxable lots that have LT cap gain percentage less than 80%:
**10% to 12%**effective tax - Pull from 457(b) in
**12%**tax bracket - Sell remaining taxable lots:
**12% to 20%**effective tax, given max 20% cap gains tax - Pull from pretax in 10% tax bracket: 10% taxes + 10% penalty =
**20%**effective tax/penalty rate - Pull from 457(b) in
**22%**tax bracket - Pull from pretax in 12% tax bracket: 12% taxes + 10% penalty =
**22%**effective tax/penalty rate - Pull from 457(b) in
**24%**tax bracket - Pull from 457(b) in
**32%**tax bracket - Pull from pretax in 22% tax bracket: 22% taxes + 10% penalty =
**32%**effective tax/penalty rate - Pull from pretax in 24% tax bracket: 24% taxes + 10% penalty =
**34%**effective tax/penalty rate - Pull from 457(b) in
**35%**tax bracket - Pull from 457(b) in
**37%**tax bracket (all remaining funds) - Pull from pretax in 32% tax bracket: 32% taxes + 10% penalty =
**42%**effective tax/penalty rate - Pull from pretax in 35% tax bracket: 35% taxes + 10% penalty =
**45%**effective tax/penalty rate - Pull from pretax in 37% tax bracket: 37% taxes + 10% penalty =
**47%**effective tax/penalty rate (all remaining funds)

If you’re **over** 60 and you still need more cash, proceed down this list until you either have enough cash or you deplete all accounts:

- Sell remaining taxable lots that have LT cap gain percentage less than 66%:
**less than 10%**effective tax - Pull from 457(b) or pretax in
**10%**tax bracket - Sell remaining taxable lots that have LT cap gain percentage less than 80%:
**10% to 12%**effective tax - Pull from 457(b) or pretax in
**12%**tax bracket - Sell remaining taxable lots:
**12% to 20%**effective tax, given max 20% cap gains tax - Pull remaining funds from 457(b) or pretax (all remaining tax brackets, starting at
**22%**)

Note that the “over 60” list does not have any Roth withdrawals, as that account should be fully depleted if you’ve reached this section and are over 60.

### Out of money!!!!! Gah!!!!

If you still don’t have enough cash after all the above steps, you’re officially broke – time to go get a job and/or seriously cut your expenses down to your current income levels.

### Investing Excess Cash

If cash generated exceeds expenses, which can easily happen if you’re selling taxable lots to generate a particular amount of capital gains or if your income exceeds expenses any year, a new taxable lot is purchased with that excess cash.