sha*_*nuo 5 python sql numpy solver pandas
这是我在Excel中执行的简单计算.我想知道它是否可以用python或任何其他语言完成.
Loan amount 7692
Period : 12 months
Rate of interest 18 Per Annum
The formula in the B2 cell is =A1*18/100/12
The formula in the A2 cells is =A1+B2-C2
Run Code Online (Sandbox Code Playgroud)
C列是借款人每月可能需要偿还的暂定金额.C2旁边的所有其他单元格只指向200的第一部分.使用如下图所示的求解器后,我在C列中得到正确的705.20部分.
我想知道是否可以使用任何脚本语言(如python(或SQL))完成此计算
以下是最终版本的样子......
我试过这样的东西,但它不会退出循环并打印所有组合.
loan_amount= 7692
interest = 18
months =12
for rg in range(700, 710):
for i in range(months):
x = loan_amount * interest / 100 / 12
y = loan_amount + x - rg
if x < 0:
print rg, i
exit
else:
loan_amount = y
Run Code Online (Sandbox Code Playgroud)
好吧,你可以用数值方法解决它(就像Excel那样),你可以通过在某个范围内检查每个量的一些步骤来强力解决它,或者你可以在一张纸上解析它.
使用以下表示法
L - initial loan amount = 7692
R - monthly interest rate = 1 + 0.18/12
m - number of months to repay the loan = 12
P - monthly payment to pay the loan in full after m months = unknown
Run Code Online (Sandbox Code Playgroud)
是n第一个月后的贷款额.
是初始贷款额(7692).
是m月(0)后的贷款额.
n第 - 和(n-1)第 - 月之间的主要关系是:
所以,分析公式结果是:
现在用任何编程语言计算它都应该是相当简单的.
对于给定的初始参数
顺便说一下,如果你正在模拟真实银行的运作方式,那么将其正确计算到最后一分可能会很棘手.
您从上述精确分析公式中得到的答案只是近似值.
实际上,所有月度金额(包括付款和利息)通常都是四分之一.每个月都会出现一些舍入错误,这会导致累积和增长.
除了这些舍入错误之外,不同的月份具有不同的天数,即使每个月的付款相同,通常也会计算每月的每一天的利息,因此每月都会有所不同.然后有额外一天的闰年,这也影响了每月的兴趣.
码:
from __future__ import print_function
"""
Formulas: http://mathforum.org/dr.math/faq/faq.interest.html
"""
def annuity_monthly_payment(P, n, q, i, debug = False):
"""
Calculates fixed monthly annuity payment
P - amount of the Principal
n - Number of years
q - the number of times per year that the interest is compounded
i - yearly rate of interest (for example: 0.04 for 4% interest)
"""
if debug:
print('P = %s\t(amount of the Principal)' %P)
print('n = %s\t\t(# of years)' %n)
print('q = %s\t\t(# of periods per year)' %q)
print('i = %s %%\t(Annual interest)' %(i*100))
return P*i/( q*(1 - pow(1 + i/q, -n*q)) )
### Given :
P = 7692
n = 1
q = 12
i = 18/100
print('M = %s' %annuity_monthly_payment(P=P, n=n, q=q, i=i, debug=True))
Run Code Online (Sandbox Code Playgroud)
输出:
P = 7692 (amount of the Principal)
n = 1 (# of years)
q = 12 (# of periods per year)
i = 18.0 % (Annual interest)
M = 705.2025054347173
Run Code Online (Sandbox Code Playgroud)
我认为这些表格/矢量/矩阵类型分析非常适合numpy和pandas.您经常可以编写更易于阅读的紧凑代码.看看你是否同意.
import numpy as np
import pandas as pd
def mpmt(amt, i, nper):
"""
Calculate the monthly payments on a loan/mortgage
"""
i = i/12 # convert to monthly interest
i1 = i + 1 # used multiple times in formula below
return amt*i1**nper*i/(i1**nper-1)
def ipmt(amt, i, per, nper):
"""
Calculate interest paid in a specific period, per, of a loan/mortgage
"""
i = i/12 # convert to monthly interest
i1 = i + 1 # used multiple times in formula below
return (amt*i*(i1**(nper+1)-i1**per))/(i1*(i1**nper-1))
def amorttable(amt, i, nper):
"""
Create an amortization table for a loan/mortgage
"""
monthlypmt = mpmt(amt, i, nper)
# the following calculations are vectorized
df = pd.DataFrame({'month':np.arange(1, nper+1)})
df['intpaid'] = ipmt(amt, i, df['month'], nper)
df['prinpaid'] = monthlypmt - df['intpaid']
df['balance'] = amt
df['balance'] -= np.cumsum(df['prinpaid'])
return df
print(amorttable(7692, .18, 12).round(2))
Run Code Online (Sandbox Code Playgroud)
这是结果:
month intpaid prinpaid balance
0 1 115.38 589.82 7102.18
1 2 106.53 598.67 6503.51
2 3 97.55 607.65 5895.86
3 4 88.44 616.76 5279.09
4 5 79.19 626.02 4653.08
5 6 69.80 635.41 4017.67
6 7 60.27 644.94 3372.73
7 8 50.59 654.61 2718.12
8 9 40.77 664.43 2053.69
9 10 30.81 674.40 1379.29
10 11 20.69 684.51 694.78
11 12 10.42 694.78 -0.00
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3514 次 |
| 最近记录: |