Last active
July 18, 2020 03:50
-
-
Save HYChou0515/8f888da4da94cc7b95f9d6dc656e0831 to your computer and use it in GitHub Desktop.
Calculate loan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def loan2(R, M=[55000], L=8500000, F=3000000, P=12, verbose=0): | |
# R is a list of rate of each year | |
# M is a list of month pay-back of each year | |
# P is period per year | |
R = [1.0 * r for r in R] | |
M = [1.0 * m for m in M] | |
L = L * 1.0 | |
F = F * 1.0 | |
P = P * 1.0 | |
principal = L-F | |
interest = 0.0 | |
paid = 0.0 | |
t = 0 | |
if verbose > 0: | |
print(' period remain paid principal interest') | |
while principal > 1e-5: | |
if int(t/P) < len(R): | |
r = R[int(t/P)]/P | |
else: | |
r = R[-1]/P | |
if int(t/P) < len(M): | |
m = M[int(t/P)] | |
else: | |
m = M[-1] | |
if t > 1e5: | |
raise ValueError('Over maximum iterations') | |
interest = principal * r | |
if principal - m + interest < 0: | |
m = principal + interest | |
if verbose > 0: | |
print('%2d-%02d(%03d) %10.2f %8.2f %11.2f %10.2f' % (int(t/P), int(t)%int(P), int(t), principal-m+interest, m, m-interest, interest)) | |
principal = principal - m + interest | |
paid += m | |
t += 1 | |
principal = F | |
tf = t | |
while principal > 1e-5: | |
r = 0.0 | |
if int(tf/P) < len(M): | |
m = M[int(tf/P)] | |
else: | |
m = M[-1] | |
if tf > 1e5: | |
raise ValueError('Over maximum iterations') | |
interest = principal * r | |
if principal - m + interest < 0: | |
m = principal + interest | |
if verbose > 0: | |
print('%2d-%02d(%03d) %10.2f %8.2f %11.2f %10.2f' % (int(tf/P), int(tf)%int(P), int(tf), principal-m+interest, m, m-interest, interest)) | |
principal = principal - m + interest | |
paid += m | |
tf += 1 | |
return t, t/P, tf/P, paid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment