# Optimal lazy rebalancing

“Optimal lazy rebalancing” is a technique that Albert H. Mao wrote an online calculator for a number of years ago.

Avoid these issues by being as lazy as possible: only rebalance as you contribute to or withdraw from your portfolio, avoid selling assets when you contribute, and avoid buying assets when you withdraw.

When contributing, use this calculator to help you purchase under-weighted assets optimally using only the contributed amount so that you get as close as possible to your target allocation without selling anything. Similarly, if you are withdrawing from your portfolio, use this calculator to help you sell over-weighted assets optimally to yield the desired withdrawal while getting as close as possible to your target allocation without purchasing anything.

You can go to his website and try it out:

http://optimalrebalancing.tk/index.html

Admittedly, this is a mathematical optimization almost totally useless in real life, since we can just eye-ball things and get close enough. But let’s not let that stop us now!

Mao provides a (terse) explanation of what he’s doing, along with the source code in JavaScript under a GPL 3 license. Unfortunately the JavaScript is minified. Between that and the terse explanation of what it’s actually doing, I found it a bit opaque.

After spending (too much) time yesterday digesting what it is actually doing, here are two new versions that are a bit easier to use in one’s own personal finance workflow.

A python version on github.

And a Google Sheet version.

# How it works.

So what does the algorithm actually do?

1. First it computes the “error” for each asset. This is “error” in the statistical sense: how far away from the target value is our actual value?
2. Then it sorts all of the assets, with the asset with the biggest negative error first. Intuitively, this is the asset that most needs new money added to it.
3. Then we add money to that asset until its error becomes equal to the next worst error.
4. Once the errors have become equal we then will treat them as a single, new, combined pseudo-asset with that error.
5. Figure out how much money it takes for that that new, combined pseudo-asset to reach the error of the next asset. Keep looping like this, agglomerating assets as you have enough money to reach their error.
6. At some point you won’t have enough money to completely reach the next asset’s error. Given the money you have left, how close can you get for our big, combined pseudo-asset? What’s the final error for the big, combined pseudo-asset? Now we have our target error and the set of assets in our big, combined pseudo-asset is our set of target assets.
7. Then we use that target error to find out the actual amount of money to allocate to that subset of assets.

Written by