In the following video tutorials, we're going to be making a loan class to handle some of the common, repetitive tasks that are involved in loan analysis and then make an application based on the class. So when we're done, not only will you have a menu driven application, but you will also be able to import and use the class in your Python applications. You can download the code from Github.
The application will handle a number of common loan analysis tasks, very simply with minimal user inputs the interest rate, the term and the amount of money we're borrowing. With just hose three inputs you can call payment, generate a summary and amortization table. You can always add functionality to your analyzer to make it better fit your needs.
I have arbitrarily decided what a loan summary should look like, so if you don't like the summary I have come up with, you can add outputs to it as you see fit. Our class will also allow you see what happens if you pay a little bit extra every month. For example if you add $100 every month, you will be able to see how much faster you can pay a loan off. Or suppose you want to payoff your loan in nine (9) years, you will be able to see how much to add each month.
We start off by importing matplotlib, pandas for the amortization table and NumPy Financial, for the time value of money calculations. NumPy Financial used to be part of NumPy, but is now its own module, so you may have to install it. Assuming you are working from an Anaconda installation you should already have matplotlib and pandas. We are also going to use another third party library that you will also likely have to install: dateutils, to synchronize our loan to the beginning of a month. To install them, you can launch a command line interface and use pip to install.
pip install numpy-financial
pip install dateutils
Once we have our imports, we will go ahead and define our class, and initializer. To instantiate a Loan object we will pass in interest rate, term and amount borrowed as well as a starting date. We are going set our start date to default to the first of the month following the current date, but you will be able to manually enter any iso date if desired as well.
With these basic inputs we will use NumPy Financial to calculate a payment. I am going to give developers access to the float value of the loan payment as well as a string version which will be convenient for some outputs.
Next up, we will write a method to generate an amortization table. Inside the method we will make a few arrays and then combine them into a pandas DataFrame to create our amortization table. First up, we will convert the term into monthly periods, starting at some user specified date. This will be the DataFrame index. We will also enumerate out interest and principal per period, again leveraging NumPy Financial here. With these initial "columns" defined, we can create a preliminary DataFrame.
Then usually these amortization tables have a running balance in them and this is easy to add based on our preliminary columns and a cumulative some of principal paid. We'll take our initial loan amount and just subtract away the cumulative principal. So that's our amortization table, and once we're done there, we're going to return it. I am also going to add this amortization table to the class initializer.
Next we will and add another method to plot the interest paid against the balance over time. This is basically a convenience method so the user doesn't have to waste time completing common tasks. So we'll just define a method called plot balances and it won't take any arguments because we'll just use some of these class variables from the amortization table. Our chart is fairly basic with legend labels and grid lines. You can modify that to make it look however you want.
Moving on to our next method, we will define our summary. Again, we don't need any arguments here. For this we are just going to format some strings and output them as a little table. Again I arbitrarily chose the monthly payment, the date that you pay off the loan and then the total interest to be the summary. Obviously, we could include other things in the summary, but that's what I've chosen.
Now we move on to the more analytical methods. First we are going output how many years it will take to pay off a loan a constant extra every month. We have to pass the monthly extra into our method, but then we once again use NumPy Financial to calculate the number of periods in the loan. We're going to use the nper function and if we want periods expressed as years, we will divide that by 12.
The last method we are going to write will take as an argument the number of years we want to be able to pay the loan off in and then return the amount of the extra payment that we need to add every month.
With that done we have our Loan class and we can start using it in other applications, and the second tuturial below gives you an example of what something you can do looks like.