Python:计算贷款付款的更智能方法
如何计算贷款月费?
给出的是:
- a:贷款金额。
- b:贷款期限(月数)。
- c:利率pa(利息按月计算加算,加1/12的利息。所以如果利息是12%,每月加1%的利息)。
- d:期末后所欠金额。
这个问题与通常的问题有点不同,因为目标不是在贷款期结束后偿还贷款,而是仍然欠所给的金额。如果我想支付全部金额,我已经能够找到一种算法来解决问题,但它当然不适用于这个问题,因为目标是最终欠给定金额而不是不欠任何东西。
我设法通过猜测开始解决这个问题,然后继续改进这个猜测,直到它足够接近。然而我想知道是否有更好的方法来简单地计算这个,而不仅仅是猜测。
编辑:这就是我现在的做法。
def find_payment(start, end, months, interest):
difference = start
guess = int(start / months * interest)
while True:
total = start
for month in range(1, months + 1):
ascribe = total * interest / 12
total = total + ascribe - guess
difference = total - end
# See if the guess was good enough.
if abs(difference) > start * 0.001:
if difference < 0:
if abs(difference) < guess:
print "payment is %s" % guess
return evolution(start, guess, interest, months)
else:
mod = int(abs(difference) / start * guess)
if mod == 0:
mod = 1
guess -= mod
else:
mod = int(difference / start * guess)
if mod == 0:
mod = 1
guess += mod
else:
print "payment is %s" % guess
return evolution(start, guess, interest, months)
进化只是一个函数,它显示贷款的付款方式和利息的利息,汇总支付的利息总额等。
一个例子如果我想找出一笔从 10 万美元开始到 5 万美元结束、利息为 8%、期限为 70 个月的贷款每月还款额,调用
>>> find_payment(100000, 50000, 70, 0.08)
payment is 1363
在上述情况下,我最终会欠 49935,并且我循环了5次。通过循环所需的次数取决于我想要达到的次数有多接近,并且它会略有不同。
How to calculate the monthly fee on a loan?
Given is:
- a: an amount to loan.
- b: the loan period (number of months).
- c: the interest rate p.a. (interests is calculated and added every month, 1/12 of the interest is added. So if the interest is on 12%, 1% interest is added every month).
- d: the amount of money owed after the end of the period.
This problem is a bit different than the usual since, the goal is not to have the loan payed after the loan period has ended, but to still owe an amount that is given. I have been able to find an algorithm so solve the problem if I wanted to pay the entire amount, but it will of course not work for this problem where the goal is to end up owing a given amount rather than not owing anything.
I managed to make a solution to this problem by starting with an guess and then keep on improving that guess until it was close enough. I wondered however, if there is a better way to simply calculate this, rather than just guessing.
Edit: Here's how I'm doing it now.
def find_payment(start, end, months, interest):
difference = start
guess = int(start / months * interest)
while True:
total = start
for month in range(1, months + 1):
ascribe = total * interest / 12
total = total + ascribe - guess
difference = total - end
# See if the guess was good enough.
if abs(difference) > start * 0.001:
if difference < 0:
if abs(difference) < guess:
print "payment is %s" % guess
return evolution(start, guess, interest, months)
else:
mod = int(abs(difference) / start * guess)
if mod == 0:
mod = 1
guess -= mod
else:
mod = int(difference / start * guess)
if mod == 0:
mod = 1
guess += mod
else:
print "payment is %s" % guess
return evolution(start, guess, interest, months)
evolution is just a function that displays how the loan would look like payment for payment and interest for interest, summing up total amount of interest paid etc.
An example would be if I wanted to find out the monthly payments for a loan starting with $100k and ending at $50k with an interest of 8% and a duration of 70 months, calling
>>> find_payment(100000, 50000, 70, 0.08)
payment is 1363
In the above case I would end up owing 49935, and I went through the loop 5 times. The amount of times needed to go through the loop depends on how close I want to get to the amount and it varies a bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这基本上是一个抵押贷款还款计算。
假设开始大于结束,并且利息在 0 到 1 之间(即 10% 利息为 0.1)
首先考虑您想要还清的付款部分。
每月还款额由下式给出:
然后您需要考虑额外的利息。这正好等于剩余本金乘以每月利息
所以总还款额是
在您给出的示例中
当我在 Excel 中测试这些值时,在 70 次还款后,剩余贷款为 50,000。这是假设您在每月付款之前支付名义利息。
This is a basically a mortgage repayment calculation.
Assuming that start is greater than end, and that interest is between 0 and 1 (i.e. 0.1 for 10% interest)
First consider the part of the payment you want to pay off.
The monthly payment is given by:
You then need to consider the extra interest. Which is just equal to the remaining principal times the monthly interest
So the total payment is
On the example you gave of
When I tested these values in Excel, after 70 payments the remaing loan was 50,000. This is assuming you pay the interest on the notional before the payment is made each month.
也许考虑这个问题的最简单的方法是将贷款分成两部分,一部分需要全额偿还,另一部分则不需要偿还任何东西。您已经计算了第一部分的月费。
Perhaps the easiest way to think about this is to split the loan in two parts, one part which is to be repaid in full and another part where you don't pay off anything. You have already computed the monthly fee for the first part.
您可以继续支付每个月的利息;那么,你将永远欠同样的金额。
You can keep paying the interest of every month; then, you will alway owe the same amont.
计算 emi 的 python 代码
了解更多信息,请访问:
python code to calculate emi
for more info visit : https://emilgeorgejames.wordpress.com/2015/07/29/python-emi-equated-monthly-installment-calculator/
这是一个相当详细的方式,但也会给出全部付款
This rather a detailed way but will give the whole payment as well
这个怎么样?
How about this?
这是使用 numpy 函数的代码片段。这会显示每月的还款额、本金、利息、分期付款和总金额。运行它并查看输出。您还可以检查 Excel“IPMT()”和“PPMT()”函数的语法,以获取参数的更多说明。
https://docs。 scipy.org/doc/numpy-1.13.0/reference/ generated/numpy.pmt.html#numpy.pmt
Here is a code snippet using numpy functions. This shows you the payment, principal, interest, instalment and total_amount each month. Run it and see the output. You can also check the syntax for Excel "IPMT()" and "PPMT()" functions for more explanation of the arguments.
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.pmt.html#numpy.pmt