[Python] Loan Calculator Program

Edmund Lee
5 min readJan 28, 2024

--

Photo by Edmund Lee (Author)

In the previous article, ‘Credit: Understanding the Differences Between Flat Interest Rate and Effective Interest Rate’ we explored the nuances of these interest rate types. Now, we will delve into the practical aspect as we demonstrate the conversion of flat interest rates to their more precise counterpart, EIR (Effective Interest Rate), using Python.

If you haven’t had the chance to read the initial article, I recommend giving it a quick read with the link to the article below.

Import libraries

The first step is to import the necessary libraries in Python. These libraries, including NumPy for numerical operations and Matplotlib for plotting charts. The use of matplotlib.ticker.StrMethodFormatter will help in formatting the x-axis or y-axis values for better representation of datapoints in the plots.

Input loan parameters

First, gather the essential loan parameters from the user. The following code prompts the user to input the total loan amount, the loan tenure in years, and the annual interest rate. Upon execution, the output will reflect the entered loan parameters and display the print statement.

The code provides an interactive interface for users to input the necessary loan details. Throughout the demonstration, we will be using the following input values as an illustration:

total_loan = 600000
tenure_yr = 30
interest_rate = 3.00

Calculate flat rate details

Based on the input of credit loan details, we could further perform the calculation of key details for a flat interest rate loan. The code snippet below performs these computations, providing insights into the financial aspects of a flat interest rate loan.

Breaking down the calculations:

  • tenure_mth: The total tenure is converted from years to months to align with the general monthly recurring of loan interest.
  • total_interest: This represents the total interest accrued over the loan tenure, calculated by multiplying the total loan amount with the interest rate and tenure in years.
  • total_repayment: The sum of the principal loan amount and the total interest, providing the total amount to be repaid at the end of the loan period.
  • yearly_interest: The annual interest amount.
  • monthly_interest: The monthly interest amount.
  • monthly_installment: The fixed monthly payment that includes both the principal and interest.

The code then presents calculated values concisely, offering a detailed overview of the flat interest rate loan. For instance, for a $600,000 loan, the total interest would be $540,000.00, resulting in a total repayment of $1,140,000.00 over 30 years.

Visualize Flat Interest Rate Details

Next, let’s visualize how the outstanding debt and yearly interest amount evolve over the loan tenure via plotting charts.

In the visualization section, two charts are created:

  1. Outstanding Debt Over Years (Principal + Interest): The first chart illustrates the evolution of outstanding debt over the loan tenure, considering both principal and interest components. The bar heights represent the remaining debt at each year, offering a visual representation of the decreasing debt as payments are made.
  2. Yearly Interest Amount: The second chart depicts the distribution of yearly interest amounts across the loan tenure. Each bar represents the constant yearly interest, allowing you to observe how interest payments remain consistent over the years.

The code calculates outstanding debt by subtracting cumulative monthly installments made up to each year from the total repayment. The resulting values are then used to plot the first chart. Additionally, the code generates the constant yearly interest amount for the second chart, providing insights into the distribution of interest payments over the loan tenure.

Find EIR of the flat rate

Upon completing the calculation and visualization of flat rate loan details, let’s delve into the process of finding the EIR for flat interest rates using an iterative method. This method involves the utilization of two functions: calculate_eir_guess_balance and find_effective_interest_rate.

The Conversion Process:

The conversion commences with the flat rate serving as the initial guessed EIR then slowly adjust the guessed EIR value until found the correct one. Here’s a step-by-step guide to the iterative process:

  1. Initial Estimation: The process starts by adopting the flat rate as the initial estimate for the EIR.
  2. Iterative Calculation: Monthly interest amounts are computed based on the guessed EIR. Subsequently, the monthly payment amount from the flat rate is subtracted to determine the balance for each month. This iterative process continues until the end of the loan tenure.
  3. Adjustment Phase: If the balance at the end of the iteration is negative, it indicates that the guessed rate is too conservative, necessitating an increase. Conversely, if the balance is positive, the guessed rate is adjusted downwards.
  4. Refinement Iterations: The loop iteratively refines the guessed EIR, moving towards the correct EIR value for the flat rate.

In the adjustment of the guessed EIR, an increment adjustment is introduced with an increment factor. This factor ensures that as the adjustment approaches the correct EIR, the increment diminishes. This precaution prevents overshooting and guarantees a more precise convergence towards the correct EIR value.

Applying the conversion function

Continuing the iterative conversion process, the subsequent step involves applying the conversion function find_effective_interest_rate to determine the EIR. The function either returns a calculated EIR value or signals if the calculation encounters any issues.

The output communicates the outcome of the EIR calculation. In this example, the result displayed a calculated EIR value of 4.85% for the flat interest rate of 3%.

Visualize EIR Details

Similarly to the flat interest rate scenario, this section involves plotting charts to illustrate the outstanding debt and yearly interest amounts over the tenure period, considering the calculated EIR value.

Based on the calculated EIR, it is transformed into monthly rates and proceeds to calculate the monthly interest and outstanding amounts in an iterative process. Additionally, it recalculates yearly interest and outstanding debt. The subsequent charts represent the outstanding debt and yearly interest amounts throughout the loan tenure,

From the chart, particularly the yearly interest amount, it’s evident that interest is highest at the beginning of the loan tenure, gradually decreasing over time as the outstanding debt diminishes. This stands in contrast to the flat interest rate scenario where the interest amount remains constant. Thus, the EIR provides a more realistic representation of how interest accrual evolves over time, providing a clearer understanding of the interest dynamics throughout the loan duration.

This summarize the coding process:

  1. Finding the flat interest rate loan details.
  2. Visualize the flat interest rate details.
  3. Perform the conversion of flat interest rate to EIR.
  4. Visualize the EIR details.

Lastly, you may visit https://loan-calculator-1123.uw.r.appspot.com/ for the webpage version!

Thanks for reading!

--

--