Credits calculations

Start using basic Python to simplify your everyday life. Such as calculating a mortgage payment. What’s the point of building own functions for such purposes, anyway? Let’s find out.

Examples of formulas for calculating loan installments are available on banks websites with it product offers, theme search engines and credit calculators published on populist dailies.

The most popular formula to calculate the monthly equal loan installment:

where:
R is the monthly installment,
P is the loan amount,
r is the monthly interest rate (annual interest rate divided by 12),
n is the number of installments (loan period in months).

Let’s try at first with simple variables and basic code in Python.

Example for:
Loan amount: 100 000
Annual interest rate: 5%,
Loan period: 30 years.
Formula: = P*r*(1+r)**n/(1+r)**n -1

Annual_interest_rate = 0.05 #Annual interest rate 5%
Years = 30#Loan period

r = Annual_interest_rate/12
n = Years*12
P = 100000
print(P,round(r,4),n) #output: 100000 0.0042 360
R = P*r*(1+r)**n/(1+r)**n-1
print(round(R,2)) #output:415.67

Looks way too perfect, I asked AI to to the same, and it was the answer:

source:chatgpt.com/c/1ec3a627-f305-4663-9b48-f3b1bc5574a2

So, I tried to spread the values in 2 separeted mini-formulas:

V1 = r*(1+r)**n
V2 = (1+r)**n -1

R = P*(V1/V2)
print(round(R,2)) #output 536.82

Now, it is the same result as from ChatGPT source.

Now as define function:

def loan_payment(loan_amount, annual_interest_rate, 
                 loan_period_years):
    #basics calcs:
    P = loan_amount
    r = annual_interest_rate / 12
    n = loan_period_years * 12

    # formula
    R = P * (r * (1 + r) ** n) / ((1 + r) ** n - 1)

    return round(R,2)

How to use:

option1 – call function knowing it variables:

monthly_installment = loan_payment(100000, 0.05, 30)
print(monthly_installment) #output 536.82

option2 – using input method:

loan_input = int(input("loan_amount: "))
rate_input = float(input("annual_interest_rate: "))
years_input = int(input("loan_period_years: "))

print(loan_payment(loan_input, rate_input, years_input))

Let’s try with decreasing payments.

The formula for a decreasing installment:

Where:
C is the fixed capital part:P/n
P is the loan amount,
t is the month number (from 1 to n),
r is the monthly interest rate,
n is the number of installments (loan period in months)

def dec_loan_payment(loan_amount, annual_interest_rate, 
                     loan_period_years):
    #basics calcs:
    P = loan_amount
    r = annual_interest_rate / 12
    n = loan_period_years * 12
    C = P/n

    # formula: R=C+(P−(t−1)⋅C)⋅r
    R = []
    for t in range(1, n + 1):
        R_t = C + (P - (t - 1) * C) * r
        R.append(R_t)

    return R
monthly_installment = dec_loan_payment(100000, 0.05, 30)
first_six_payments = monthly_installment[:6]
print(first_six_payments)

for x in first_six_payments:
    print(round(x,2))

AI show me also a different example for decreasing loan calculations, but something went wrong here:

def decreasing_installments(capital,
                            interest_rate,
                            period_months):
    installment = []
    for month in range(1, period_months + 1):
        monthly_installment = \
            (capital * (1 + interest_rate)**(month / 12) * 
             interest_rate) / ((1 + interest_rate)**(month/12)-1)
        installment.append(monthly_installment)
    return installment

loan_principal = 100000 # Loan amount in PLN
interest_rate = 0.05 # Annual interest rate (5%)
loan_period = 30 * 12 # Loan period in months (30years* 12months)

decreasing_loan_installments = \
    decreasing_installments(loan_principal,
                            interest_rate, loan_period)

for month, installment in enumerate(decreasing_loan_installments,
                                    start=1):
    print(f"Month {month}: {installment:.2f} PLN")

There is an error in this expression:

monthly_installment = (capital * (1 + interest_rate)(month / 12) * interest_rate) / ((1 + interest_rate)(month / 12) – 1)

Lack of fixed capital part value, therefore the result is as follows:

After modifications it can work properly:

def decreasing_installments(capital,
                            annual_interest_rate,
                            period_months):
    fixed_capital_part = capital / period_months
    interest_rate = annual_interest_rate/12

    installment = []
    for month in range(1, period_months + 1):
        monthly_installment = fixed_capital_part + \
                              (capital - (month - 1) * 
                               fixed_capital_part) *\
                              interest_rate
        installment.append(monthly_installment)
    return installment


loan_principal = 100000  
annual_interest_rate = 0.05 
loan_period = 30 * 12 

decreasing_loan_installments = \
    decreasing_installments(loan_principal, 
                            annual_interest_rate, loan_period)

for month, installment in enumerate(decreasing_loan_installments,
                                    start=1):
    print(f"Month {month}: {installment:.2f} PLN")

but I am still more convinced of the first version I present with define variables: C,P,n,r