Python for Finance Automation Project: Make a Mortgage Loan Analysis Class

in this video we're going to be making a 
loan class to handle some of the common   repetitive tasks that are involved in 
loan analysis all right so when we're done   you're going to be able to import and use their 
class in your python applications like this   okay and i'm going to make this code available 
from a link in the video description on github   all right so if we want to just see what 
some of the things it will be able to do   are okay so we're gonna have to pass in a few 
parameters first right the interest rate the term   and the amount of money we're borrowing 
and then yeah we're gonna be able to do   some simple stuff right like get a payment all 
right maybe you want a loan amortization table   all right so it'll be able to do that and it'll 
be able to show you a summary of the loan okay and   you know if you don't like the summary i've 
come up with you can add things to it all   right and uh you can also see what happens 
if you pay a little bit extra every month   all right and here i'm going to just add a 
hundred dollars all right so by doing that   we reduced our initial term from 30 years to 
about 27.

All right so those are the kinds of   things we're going to be able to do and uh 
with that i guess we can go ahead and get   started writing this class okay so the 
first thing i'm going to do is my imports okay and i'll just let you know that you may 
have to install a couple things particularly   date utils may not be installed and also 
we're gonna be using numpy financial   that may not be installed so to install 
them right you could just open the terminal   inside pycharm right and then pip install for 
example date utils all right there's a few uh   similarly named modules out there about date 
utilities alright so just make sure you get   this one all right and then you would do the 
same thing for numpy financial if you need that okay so here's all the extensions i'm going to 
be using and then right we'll just go ahead and   define our class all right and i'll start by 
writing a initializer all right and we're going   to pass in a few variables so the rate term 
and the loan amount all right and i'm going to   add an additional variable here which is the 
start and i'm going to set it as a default for   basically 30 days or the next month 
from today all right so i'll use   date time okay all right and this is going 
to be the default and i'm going to allow   people to or you may want to allow people 
to import their own dates and as long as we   express it as an iso format 
we're going to be able to do that all right this is about the easiest way to to 
get this done all right so if it started in   the past or it's going to start in the future 
somebody may want to input that we're going   to allow that flexibility okay and then we'll 
just start assigning the class variables here okay so that should be enough 
to get started and uh we can   start to take a look at how this is 
shaping up so doesn't do anything here   what i'm going to do is put in a temporary 
sort of placeholder and i'll call it with those values all right we'll say it started 
today and then we'll just sort of print the   payment out that's about all we can do at 
this point okay so there it is uh about 2130.   if we have a loan like that all right i'm going 
to add another variable here because later when   i do that summary i'm going to convert this 
payment into a string so i'll put that in here all right and i'll just make an 
f string of the of the payment all right so there is our initializer all right 
let me make one more change to that later all   right but i need to first define a method before 
i can do that and the method i'm going to define   here is the one that will give us a amortization 
table all right so i'm going to call it the loan   table and i don't need to pass anything in 
because i have already initialized all the   variables that i need inside here right what 
i'm going to do is make a few arrays and then   combine them into a pandas data frame for the 
amortization table okay so i will get my periods   and i just need to enumerate the 
start date out to the end date here okay so this is going to end 
up being the index of our   data frame all right i'm going 
to calculate the interest   for each period to do that i'm going to use this 
interest payment function from numpy financial okay this line of code is getting somewhat long 
i'm going to put the loop part of it down here okay i am going to do the same thing for principal   all right and it's almost exactly the 
same code so i'll just copy and paste it all right and instead of the i payment 
it's going to be the principal payment   from numpy financial so thank you numpy financial 
okay so we'll start off with those three columns   and then i will define a local variable call it 
table and that will become a pandas data frame   and it'll have columns payment all right and 
that's just going to equal the loan payment okay it'll have interest right which will 
be the interest comprehension from above   and it will have principal okay 
likewise from the comprehension above okay i need to add that index so let's let 
me actually space this out a little differently   all right make it a little bit more readable those 
are the columns and then our index is going to   equal the periods comprehension 
converted to a panda's date time okay and then usually these amortization tables 
have sort of a running balance in them so once   we're done with that i will add a balance column 
all right and that's going to also be based on   uh what's happened in the principal 
column all right so i'm going to take   our initial loan amount all right and i'm 
going to subtract away from that for each row all right and i'll get the cumulative sum 
there okay so that's our amortization table   once we're done there we're going to return 
it and i will round it to two decimal places   all right so this should get us within a couple 
of pennies pretty much everywhere at least   otherwise we have a lot more precision than 
we need okay so what i'm going to do here then   is define that as a property so i'm going to add 
here this table and i'm going to call the loan   table method on it okay so we can just kind of 
confirm that i didn't make any mistakes there   and i'll run this again and instead of the 
payment will return the amortization table   okay so so far so good all right and uh we 
can see that the interest actually didn't get   rounded there which is kind of interesting but 
everything else did all right and uh yeah down   at the bottom here yeah we are actually 
paying the entire balance down to zero all right so next we will sort of move on 
and add another method here and it doesn't   really matter what order i put these in but 
maybe the next thing you might want to do   is to plot the uh you know the interest paid 
against the balance over time all right so we'll just define a method called plot balances   and uh it won't take any arguments because we'll 
just use some of these class variables in there   all right so i will define a local 
variable though and i'll set it equal   to that amortization table okay once i do 
that i'm just going to go ahead and plot oops okay we'll give it a label and then i will 
also plot the cumulative sum of the interest put some grid lines in there okay we'll put the legend on there and we will 
show it okay so we can test that out if we want okay and so there's our loan 
all right and so you can   modify that to make it look however you 
want but at least that gets us started   all right and we can see that okay we paid off in 
2052 based on on the inputs we gave it close that   just comment that out i don't need to look at that 
every time i run this moving on to our next method   all right we will define our summary again we 
don't need any arguments here we do need it to be   aligned properly though okay that looks better and 
so here i'm just gonna format a bunch of strings   as as an as a summary and i just chose the you 
know the monthly payment uh the date that you   pay off the loan and then the total interest 
obviously we could include other things in the   summary but that's what i've chosen all right so 
it's just a bunch of formatting of strings here okay so here's where i'm going to use that 
formatted string that i made when i when i   was making the initializer just right align 
that and we're going to need to actually get   a copy of that pandas data frame in here so just get the last value of the index there 
and uh we'll get the date of it okay and then   finally we'll print off the cumulative interest 
that we paid over the life of the loan i didn't   get the last value there obviously ahead of time 
played around a little bit with the formatting   here so hopefully this will get us something 
pretty close and then we'll just repeat this   okay obviously we could uh alternatively you know 
return this as probably uh something like a doc   string if you don't want to use a bunch of print 
statements sorry but this is just the way i did it   and uh so down here right all we need to do then 
is we don't need to print anymore we just call   summary on it okay let's test that out okay so 
pretty good the formatting uh obviously it looks   like it needs to be modified here a little bit 
all right let me get a little more space there so   uh maybe right align that date push that over and 
maybe take a space away from the interest and then   that should be pretty good for a wide variety 
of total interests all right and uh let's see   just push this over a bunch and let's see how 
that's all right so almost right obviously i   don't want the colon there i want it here 
and let's see if that fixes it okay so   pretty good all right so as i said if you want 
more stuff in your summary you are welcome to   add that there all right but i am i'm gonna stop 
there all right in the interest of time and uh   we're gonna move on to our next method which is 
gonna tell us uh how many years it'll take to pay   off if we pay a little bit extra every month all 
right so i'll just call this method extra payment   and uh here i'm gonna have to pass something in 
and that's the extra amount that we want to apply   okay and then this is pretty simple all i need is 
a return here we're going to use our friend numpy   financial and the n per function inside there and 
pass in the required arguments here oh it's the   term we're trying to solve for so i can't do that 
self payment and that extra amount loan amount   okay and if we want it expressed as years we will 
divide that by 12.

Let me get rid of some of the   decimals as well in case there's extra there 
we'll round this okay and then let's try that   so we'll print that off and uh we'll just give an 
extra hundred dollars a month there okay it's not   going to impact the summary so we'd have to add 
some functionality if we wanted that to happen   but i'll leave it up to you to do that all right 
so there it is and again it didn't round it i'm   not sure why it's not doing that i guess i can put 
the whole thing i can force it a little bit here   we'll round it here all right see if that works 
better okay so there it is all right cutting off   three and a third years by adding an extra hundred 
bucks a month to your payment okay so then the   last method i'm going to write is rather than 
you know checking a whole bunch of extra payment   amounts to get to a payoff period that we want 
i'm going to write a function that asks us to   input how many years we want to be able to pay 
the loan off in and then it returns the amount   of the extra payment that we need to add every 
month okay so i'll just call this one retire debt   and uh right well pass in uh years to pay off and 
if you want you could you could pass in uh you   know set a default value here something maybe like 
10 years right i'm going to leave it and require   you to actually type that in okay and then we're 
going to start an extra payment here all right i'm   going to set it at a dollar all right so this 
is going to get us how much do we have to pay   extra every month within a dollar to pay it off in 
some specified number of years okay and then all i   need to do is loop through the numpy financial 
and per until i get to that years to pay off okay this is going to happen pretty fast 
but you know if it's too precise you can   obviously you know change that to 5 or something 
like that and increment it by fives all right and   then you'd be within five dollars all right once 
we get there all we need to do is sort of return   right and you can return whatever 
you want here you could maybe return   a tuple of values all right i'm just going to 
return the extra payment all right but all right   maybe we can return the the loan payment with 
that added in so nobody has to do that math   all right let's try that out so that one's going 
to be retire and obviously we don't want it to   to be a hundred years let's try 10.

okay run 
it okay so in order to pay it off in 10 years   we have to add 1845 each month right for a total 
of 39 we'll call it 75 okay so i'm going to make   the code available like i said and hopefully 
that helps you make a loan analyzing class.

test attribution text

Add Comment