Skip to content

Instantly share code, notes, and snippets.

@yv84
Last active June 8, 2017 03:33
Show Gist options
  • Save yv84/b715a8dede38f71123f1 to your computer and use it in GitHub Desktop.
Save yv84/b715a8dede38f71123f1 to your computer and use it in GitHub Desktop.
loan
def differ(loan, rate, t, accuracy=2):
list_pay_per_month = []
body_of_loan = loan
month_count = t * 12
body_per_month = loan / month_count
sum = 0
for _ in range(month_count):
interest = body_of_loan * rate / 12
pay_per_month = interest + body_per_month
body_of_loan = body_of_loan - body_per_month
sum += pay_per_month
list_pay_per_month.append(
[round(pay_per_month, accuracy),
round(body_of_loan, accuracy)]
)
sum = round(sum, accuracy)
return sum, list_pay_per_month
###
# x -platej obschu'
# y - platej za telo dolga
# interest = loan *rate_per_time
# rate = rate / month_count # rate per time
# x = interest + y
# x0 = x1 = x2 = ... = xn = x = const
# y0 + y1 + y2 + ... + yn = loan
# x0 = loan * rate + y0
# x1 = (loan - y0) * rate + y1
# x2 = (loan - y0 - y1) * rate + y2
# ---
# (loan - y0) * rate + y1 = (loan - y0 - y1) * rate + y2 =>
# y2 = y1 * (1+rate)
# y1 = y0 * (1+rate)
# y0 + y0*(1+rate) + y0*(1+rate)**2 + ... + y0*(1+rate)**n = loan
# y0 = loan / (1 + (1+rate) + ... + (1+rate)**n)
##---
# xn = (loan - y_n-1 - ... - y1 - y0)*rate + yn
# xn = (yn + (y_n-1 + ... + y1 + y0) - (y_n-1 + ... + y1 + y0))*rate + yn
# x = yn*(1+rate)
###
def annuitent(loan, rate, t, accuracy=2):
list_pay_per_month = []
body_of_loan = loan
month_count = t * 12
sum = 0
u = 0
for i in range(month_count):
u += (1+rate/12)**i
pay_per_month = loan*rate/12 + loan/u
for _ in range(month_count):
interest = body_of_loan * rate/12
sum += pay_per_month
body_of_loan = body_of_loan + interest - pay_per_month
list_pay_per_month.append(
[round(pay_per_month, accuracy),
round(body_of_loan, accuracy)]
)
sum = round(sum, accuracy)
return sum, list_pay_per_month
assert(differ(1000, 0.2, 5) == \
(1508.33, [[33.33, 983.33], [33.06, 966.67], [32.78, 950.0], [32.5, 933.33], [32.22, 916.67], [31.94, 900.0], [31.67, 883.33], [31.39, 866.67], [31.11, 850.0], [30.83, 833.33], [30.56, 816.67], [30.28, 800.0], [30.0, 783.33], [29.72, 766.67], [29.44, 750.0], [29.17, 733.33], [28.89, 716.67], [28.61, 700.0], [28.33, 683.33], [28.06, 666.67], [27.78, 650.0], [27.5, 633.33], [27.22, 616.67], [26.94, 600.0], [26.67, 583.33], [26.39, 566.67], [26.11, 550.0], [25.83, 533.33], [25.56, 516.67], [25.28, 500.0], [25.0, 483.33], [24.72, 466.67], [24.44, 450.0], [24.17, 433.33], [23.89, 416.67], [23.61, 400.0], [23.33, 383.33], [23.06, 366.67], [22.78, 350.0], [22.5, 333.33], [22.22, 316.67], [21.94, 300.0], [21.67, 283.33], [21.39, 266.67], [21.11, 250.0], [20.83, 233.33], [20.56, 216.67], [20.28, 200.0], [20.0, 183.33], [19.72, 166.67], [19.44, 150.0], [19.17, 133.33], [18.89, 116.67], [18.61, 100.0], [18.33, 83.33], [18.06, 66.67], [17.78, 50.0], [17.5, 33.33], [17.22, 16.67], [16.94, 0.0]])
)
assert(annuitent(1000, 0.2, 5) == \
(1589.63, [[26.49, 990.17], [26.49, 980.18], [26.49, 970.02], [26.49, 959.7], [26.49, 949.2], [26.49, 938.52], [26.49, 927.67], [26.49, 916.64], [26.49, 905.42], [26.49, 894.02], [26.49, 882.43], [26.49, 870.64], [26.49, 858.66], [26.49, 846.47], [26.49, 834.09], [26.49, 821.5], [26.49, 808.69], [26.49, 795.68], [26.49, 782.44], [26.49, 768.99], [26.49, 755.31], [26.49, 741.41], [26.49, 727.27], [26.49, 712.9], [26.49, 698.29], [26.49, 683.43], [26.49, 668.33], [26.49, 652.97], [26.49, 637.36], [26.49, 621.49], [26.49, 605.35], [26.49, 588.95], [26.49, 572.27], [26.49, 555.32], [26.49, 538.08], [26.49, 520.55], [26.49, 502.73], [26.49, 484.62], [26.49, 466.2], [26.49, 447.48], [26.49, 428.44], [26.49, 409.09], [26.49, 389.41], [26.49, 369.41], [26.49, 349.07], [26.49, 328.4], [26.49, 307.38], [26.49, 286.0], [26.49, 264.28], [26.49, 242.19], [26.49, 219.73], [26.49, 196.9], [26.49, 173.69], [26.49, 150.09], [26.49, 126.1], [26.49, 101.7], [26.49, 76.9], [26.49, 51.69], [26.49, 26.06], [26.49, 0.0]])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment