Jump to content
  • entries
    4
  • comments
    7
  • views
    1,193

On The Computational Complexity of Being Greedy

wpirobotbuilder

697 views

During my lunch break today, I wrote this MATLAB function:

function [rate] = greedy( balance, periods, profit)

% greedy Takes in a numerical balance, an integer number of periods, and

% the desired profit to be made from the loan holder. Returns the optimal

% APR in decimal form (i.e. 3.94% = .0394)

payment = (profit + balance)/periods;

syms x

fun = 0;

for i = 0:periods-1

fun = fun - payment*(1+x/12)^i;

end

fun = fun + balance*(1+x/12)^periods;

solns= double(solve(fun == 0, x));

rate = solns(solns> 0);

end

The coding description is pretty brief; I'll outline it a little more:

P is the desired monthly payment from the loan holder such that, over a given number of periods n, we will obtain the desired profit R, in dollars, from an initial loan balance B, also in dollars. It can be calculated as follows:

P = (B + R)/n

fun(x) is the amount of money left on the balance B after n periods of interest, which compounds at rate x, assuming the loan holder makes a single payment of amount P every period of interest. It is generated as a symbolic expression of the variable x, which evaluates to:

fun(x) = B*(1 + ax)n-1 - P(1 + ax)n-2 - P(1 + ax)n-3 - ... - P(1 + ax)1 - P

Where B is the starting loan balance, P is the required monthly payment, n is the number of periods for interest to compound, and a is the inverse of the rate of interest compounding (1/12).

The goal is to find a zero of the function fun(x), i.e. (x : fun(x) = 0). This will return the optimal APR such that the loan is paid off in full after n periods of interest compounding.

This is not easy to solve. The polynomial order of fun(x) grows linearly with n, which means finding the roots of an (n-1)th order polynomial. If your loan compounds over 5 years (60 months), you are solving for the roots of a polynomial that looks like this:

f(x) = a0x59 + a1x58+ ... + a58x + a59

Yikes. Fortunately, MATLAB has the beautiful function solve, which you see in the script above. This allows us to solve for all the roots of the polynomial. As it turns out, most of them are complex numbers with a nonzero imaginary part, but most of the time there is one real root, which is the one you care about.

For example:

I have a $14162 car loan with a 3.94% APR, to be paid back monthly for 5 years. According to calculator.net, I will give the bank approximately $1463.89 over those 5 years, paying out $260.43 per month.

Running the MATLAB function,

blogentry-653-0-78378900-1395087094_thum

We can see that our function works. And if the bank wanted to make a large sum, say $3200:

blogentry-653-0-64089700-1395087222_thum

That is about the original APR I was offered before I got a cosigner with good credit.

This algorithm doesn't take very long to run on modern computers, making it incredibly easy to do this. Of course, it didn't stop loaners before, because you could work in reverse, trying different APRs and calculating the profit from each APR until you got something close to what you wanted.

It was a good lunch break.

5 Comments

The crap you do in your free time man. XD

Edit: After reading it more thoroughly, I remember this from my Business Calculus class. This is cool. Thanks. 

So you're saying the optimal APR happens when you have no fun? /joke

The sad part is, it's funny because it's true. XD

Link to comment
Link to post

The crap you do in your free time man. XD

Edit: After reading it more thoroughly, I remember this from my Business Calculus class. This is cool. Thanks. 

So you're saying the optimal APR happens when you have no fun? /joke

The sad part is, it's funny because it's true. XD

I love math (to some extent). Also, MATLAB is just fun to play with in general.

 

I did this because I was trying to figure out a discrepancy in the interest that the bank had charged for one period, and the whole thing kind of snowballed from there.

Link to comment
Link to post

That's why you never take a loan out for a car. Its better to buy a used beater till you can actually buy one outright or a used one outright.  Otherwise, its a bad deal because a car never improves in value no matter how special anyone says it is, sure you might get what you paid for it but no one ever takes into account what they spent to keep it up in the calculation, oil, gas, repairs, license fee's, insurance, its a money pit! Then you drive it till it dies, or I do, recycle the parts or sell the carcass  then start again.

Link to comment
Link to post

That's why you never take a loan out for a car. Its better to buy a used beater till you can actually buy one outright or a used one outright.  Otherwise, its a bad deal because a car never improves in value no matter how special anyone says it is, sure you might get what you paid for it but no one ever takes into account what they spent to keep it up in the calculation, oil, gas, repairs, license fee's, insurance, its a money pit! Then you drive it till it dies, or I do, recycle the parts or sell the carcass  then start again.

It's true. Unless you need a car immediately and can get a very low interest rate (which is hard to do). I needed a car to commute to work and school and didn't want to have to worry about breakdowns. Luckily I got a good used car (already lost the depreciation value) and I'll be spending only ~$230 on interest, compared to the $1463 because I'll be paying it off by August (7 months of interest).

Link to comment
Link to post

Hmm. Just came back and saw this. 

... And I plan to buy a Tesla Model S. Outright of course, but still. That'll be a while. 

Interestingly, related (to an extent), I'm always surprised how having cash changes things for these types of transactions. Both in the Car Industry and the Insurance (specifically medical) industry.

My mother was someone who knew this and is who I learned it from. She wanted a used car, and so she went to a lot and found one she liked. It was for $5400. She told the salesman that if he'd accept $3600, she had it in cash right then and there. He went to talk to his manager and they sold it to her for that. The thing is still (miraculously) running.

Similar thing happened with my eye doctor stuff. The hospital was going to charge the insurance company something around $1,100. She said she wasn't going to put it on the insurance if it would cost that much (raises the premiums or wtv), so she paid cash and only had to pay $500-$600. 

I find this funny, because those are the two industries that I constitute as the most overpriced/biggest scams. You can kind of see why (them wanting immediate cash over loans that they can't be sure people will pay off).

Link to comment
Link to post
×