# 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

lazyas 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

purchaseunder-weighted assets optimally using only the contributed amount so that you get as close as possible to your target allocationwithout sellinganything. Similarly, if you are withdrawing from your portfolio, use this calculator to help yousellover-weighted assets optimally to yield the desired withdrawal while getting as close as possible to your target allocationwithout purchasinganything.

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?

- 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?
- 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.
- Then we add money to that asset until its error becomes equal to the next worst error.
- Once the errors have become equal we then will treat them as a single, new, combined pseudo-asset with that error.
- 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. - 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*. - Then we use that
*target error*to find out the actual amount of money to allocate to that subset of assets.