
Optimizing debt vs assets
I have a motorcycle that I financed. I also have an investment account. While studying optimization problems in calculus I began to wonder if there was an optimal amount to put towards my debt and my assets. Should I completely stop putting money towards investments to make extra payments towards my debt or just continue making my regular payments. This is one of the things that inspired me to make my debt vs investment calculator. Which you can see here:
https://kingjofr.github.io/DebtvsInvestmentCalculator/
If you want to play with the code:
Another thing that inspired me was hearing that really high networth people take out loans at really low interest rates instead of paying for things in cash because supposedly the rate of growth of their investments is way higher and something something taxes I don’t know.
So I first did some googling and found that there were already saving vs debt calculators but I didn’t feel satisfied using them. They were oversimplified. You put in your principal, rate, and monthly contribution, and it spits out a single number–how much you save in interest, without breaking everything down. I like details. Also they seemed to try to make debt payments more attractive. Compared to dedicated investment calculators the interest you were supposed to earn on your savings was 2 to 3 times lower.
So I decided this would be a good programming exercise.
But first, what is an optimization problem? When doing optimization problems you want to think about two things. You have x and y that add to a number, say 30. This number 30 is a constraint. And they multiply to another number, the product. You want this second number to be the highest possible. A concrete example from my calculus book is that if you have a certain length of fence how can you arrange it in a way to give you the maximum amount of enclosed area? One way to solve this is to adjust those 2 variables, x and y, until they give you the highest possible number. That would play out like this: 1 x 29 = 29, 2 x 28 = 46, 3 x 27 = 81… You keep doing that until the answer stops rising and starts declining. And you could put those values into a table for easy comparison. That can take a long time. A better way is to use Calculus.
x+y = 30 , x*y= max
X = 30 -y, y(30 – y) = 0 -> -y^2+30y = 0
the derivative is -2y+30 which we set to equal to 0 because that’s the point at which it stops rising(the peak), -2y +30 = 0 -> -2y = -30 -> y=15
X + 15 = 30 -> x=15
So 15*15 will give you the maximum which is 225.
When talking about paying debt vs investing we are trying to optimize for Net worth. Something I quickly realized is that this problem isn’t as simple as the book examples. For one the formula for compound interest has a lot of variables, not just 2. I googled optimization problems with more than 2 variables. The first result is a lesson from Calculus 3, which is way past my math level right now, but I don’t give up.
I wondered if there is a way to solve this problem without using that level of math, or is there already a formula that I can just plug my variables into. I asked ChatGPT how to optimize debt vs investment using Calculus. It just said to take the equation for debt and equations for investment and compare the results. So maybe there isn’t a clean optimization equation like the example from the book.
I reached out to r/askmath on reddit. A user said that you only have to compare the interest rates to figure this out. If debt has a higher interest rate than investing it’s better to focus on paying off the debt first. And if investing has a higher interest rate it’s better to do that and just make the minimum payments on the debt. That makes sense, still I decided to continue with my idea but incorporate these things I learned.
What raises networth the fastest.
Going back to my motorcycle If my loan is $4200. And lets say the rate is 10% which is an average loan rate. And I contribute $150 a month to my investment account, and the average rate of return is 10%.
So what we want to find out about is the lowering of how much interest you pay vs an increase in how much interest you earn. Is there an equation to describe that? There doesn’t seem to be a definitive equation for this. My calculator just subtracts the debts from the assets.
Going back to finding the optimum value, how we created a list of equations to find the optimum value we can sort of do the same with a button that increments the amount going towards debt or investments.
So here are my key takeaways from this:
- Programming is hard
- Turning math into a useful program is hard
- Naming variables is hard. Especially when they do similar things.
Thoughts on future improvement. I think one way I could improve this, at least when it comes to readability, would be to use objects or maybe even classes to organize the data and functions. I would often have hard time tracking what called what when and where. One thing that this calculator is missing is the option to take into account any fees on your loan or taxes on your return / dividends.
