A programmer tries to figure out how capital gains tax ACTUALLY works. by@fpgaminer
26,132 reads

A programmer tries to figure out how capital gains tax ACTUALLY works.

Read on Terminal Reader

Too Long; Didn't Read

Company Mentioned

Mention Thumbnail
featured image - A programmer tries to figure out how capital gains tax ACTUALLY works.
fpgaminer HackerNoon profile picture


react to story with heart

I’ve always been told that (in the U.S.) long term capitals gains are taxed at 15%. When my CPA told me that it can sometimes be 0% I became curious about the details of capital gains taxes. Turns out I had stumbled into a labyrinth of tax code, filled with land mines and torturous mathematical puzzles.

I’m a programmer, not a tax professional, so I hope my naivety can be excused. I hit my friendly neighborhood search engine, searched for capital gains tax rate, and found charts like this:

Turns out the capital gains tax is only usually 15%. If you don’t make very much, it’s actually 0%. Neat! If you make a whole lot, it’s 20%. Not too bad.

But, then I thought, do the capital gains tax brackets work just like the income tax brackets? If you’re not familiar with tax brackets in the U.S., they work more like a staircase that your income walks up. You don’t go “oh, I made $20,000 so I’m taxed at 15% so I pay $3,000”. What that tax chart above actually says is that the money you make between $0 and $18,450 is taxed at 10%. Only the amount over $18,451 is taxed at 15%. So your taxes are $18,450 * 10% + $1,550 * 15% = $2,077.50.

That’s why when people complain “oh man my raise is going to put my into a higher tax bracket, and I’ll actually make less money!” is … weird. It does sometimes happen, because your higher income might result in losing some deductions. But it’s not because you’ve been put into a higher tax bracket. Only your raise is being taxed higher.

Disclaimer: I am not a tax professional. None of this is tax advice. It’s just my bumbling story of ham fisting tax codes.

That’s how income taxes work. But do capital gains tax brackets work the same? It wasn’t clear from any of my search results, so I kept digging. I found an online tax calculator, and it certainly seemed like capital gains work the same way. $80,000 in capital gains should result in $765 in federal taxes. Nice!

“But wait…” I thought, “what if you’re earning both income and capital gains?” Do you tax each separately, doing the bracket calculates for each and then adding the results together? Or is it more complicated than that?

When I plugged income and capital gains into online calculators nothing made any sense. So I pulled up irs.gov, put my gloves on, and dove in.


Eventually I found “Qualified Dividends and Capital Gain Tax Worksheet”. You fill that out after filling out Schedule D to figure your taxes (before deductions). At least, that’s how I understood it. Now, my question was, how do income and capital gains interact together when determining your taxes? I should be able to just take this form, grok it, and find out the hopefully simple rules that determine that.

It wasn’t that simple for me. No matter how many times I read through that form, I could not even begin to understand what it was trying to do. There are only two real inputs, your income and your gains, but they were woven into too many non-linear min functions.

I was never big on the theoretical side of mathematics and software, so I did what I do best: I coded the form up:

That little Python program takes as input income and gains, and calculates the final taxes. It can be probed to figure out the value of each line of the Tax Worksheet, and to observe behavior with differing values of income and gains. It’s not 100% accurate; I only needed a rough approximation. It definitely fails if your total income is really high (income_tax function doesn’t encode all the brackets). And the bracket numbers are slightly wrong, because I plugged in numbers from two different tax years. Close enough!

With the help of my wife, bouncing ideas and trying values, we eventually divined how it all works.

It looks like you calculate taxes on your income first, just using the usual tax bracket rules. Then you calculate capital gains taxes. But when you’re doing the bracket calculation you skip the parts of the bracket already used by your income.

For example, let’s say you had $70,000 in income, and $10,000 in gains. You’d calculate taxes on your income like normal: $18,450 * 10% + $51,550 * 15% = $9,577.50

Now, calculate your capital gains, but start in the middle of the “$18,451 — $74,900” bracket. Your income “ate” the first parts of the capital gains brackets. So your capital gains taxes are: $4,900 * 0% + $5,100 * 15% = $765.

Add them together, and you get $10,342.50. Your income was effectively taxed at 13.7% and your gains at 7.7%.

This is a lot higher than if you had done the bracket calculations the naive way; totally separately. That would be the same income tax, but your gains would be in the 0% bracket. So you’d only pay $9,577.50.

Now, again, this is only a rough approximation of how this tax worksheet functions. There are various edge conditions. But this is how I think it works in most cases.

In the Python code I also included some math to figure out the marginal tax rate empirically (using discrete derivatives). When you punch in your income and gains it spits out not only your current taxes, but the marginal tax rate for income and the marginal tax rate for gains.

What is marginal tax rate? If you ended up earning an extra dollar in income, marginal tax rate is how much that extra dollar is taxed at. It’s nice to know, since usually your effective tax rate (your taxes / income) is much lower since most of your income/gains are in lower brackets. Knowing your marginal tax rate helps you figure out how much that new raise is going to get taxed.

But I was surprised when I plugged in $50,000 in income and $30,000 in gains. The marginal tax rate on income was 30%! Holy cow! Shouldn’t $50,000 in income result in only 15% marginal tax rate? The program said the marginal tax rate on gains was 15%, which made sense. What happened to income?

This is where the convoluted calculations from the tax worksheet above come in. Remember how your income eats away tax brackets from your capital gains? Every dollar more in income you earn not only results in a 15% tax on that dollar of income, but it also bumps a dollar of capital gains out of the 0% tax bracket. So the dollar of income gets taxed at 15%, and bumps one dollar of capital gains into the 15% bracket as well. 15% + 15% = 30%. Bummer.

That means, if you’re in a situation like that, earning extra income is particularly painful. This effect goes away once your income is above $74,900, after which the marginal tax rate on income drops to the normal 25% and gains remains at 15%.

What a whacky system! I can’t say I’d change it though … capital gains is advantageous enough as it is; removing the effect of income on how your gains are taxed would make capital gains even more tax advantaged which probably isn’t a good thing.

For reference, states like California treat capital gains as just ordinary income, so you don’t have to do any funky math there. Phew!


. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa