Python现金流计算

2024-10-04 01:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python建立一个可以一次性计算outstanding_balance的方程。使用迭代过程非常简单。例如:

for month in range(1, self.amortMonths + 1):

        # Calculate intial and future interest payments

        interest = self.originalPrin * self.rate / 12

        # Calculate intial and future principal payments

        principal_pmt = self.pmt - interest

        # Outstanding balance is reduced by the principal_pmt

        self.originalPrin = self.originalPrin - principal_pmt

因此self.amortMonths基本上是每月必须还清贷款的期限,与self.rate一起,他们将确定self.pmt变量,即借款人必须支付的每月金额,以便在self.amortMonths结束时将{}值减少到0。在

示例:

假设我有一笔1000美元的贷款,利率是10%,那么我第一个月的利息是1000*10%=100美元。为了找到self.pmt金额,我使用了一个numpy.pmt函数,该函数以未结余额prin、rate、amortMonths为参数来生成一个月付款值,该值将在分期付款月结束时将未付金额减少到0。假设self.pmt = $120,然后principal_pmt = 120 - 100 = $20。因此,下个月的未付款项是1000-20=$980。然后这就变成了一个迭代过程。在

所以我需要一些帮助来确定一个方程,它可以在不需要迭代过程的情况下一次性完成。显然,我需要用线性代数,但我不是数学出身,所以我想知道有没有人有什么想法?在

编辑:

所以像这样:

Balance_i = Balance_i-1 - (pmt - Balance_i-1 * Rate)。在


Tags: andselfprincipalrate过程金额方程calculate
2条回答

这是我的解决方案。在

import numpy as np

a = np.array([[1,0,0,0,0], [1.00583333,-1,0,0,-1], [0, 1.005833333, -1, 0, -1], [0,0,1.005833333, -1, -1],[0,0,0,1,0]])
b = np.array([162000,0,0,0,0])
x = np.linalg.solve(a, b)
balance_arr = np.delete(x, -1)
print(balance_arr)

interest_arr = balance_arr * 0.07/12
print(interest_arr)

prin_payment = np.subtract(54631.22, interest_arr)
prin_payment[-1] = 0
print(prin_payment)

np.allclose(np.dot(a,x), b)

我根据手工计算手工创建的数组值。下一步我要弄清楚如何根据期限、原始余额和利率自动生成它们。在

下面创建了一个设置了以下值的this Excel example的Python实现:

import pandas as pd
import numpy as np

prin = 200000 # principal/beginning loan balance
rate = 0.0675 # annual rate; monthly will be 6.75%/12
n = 30 # years; total periods will be 360 w/ monthly pmts

接下来,您可以使用NumPy的Financial functions来计算每个时期的利息和本金。请注意,这些并不取决于您的流动贷款余额。好在下面的结果是数组(付款计划):

^{pr2}$

计算特定时间的未偿余额使用: enter image description here

我们可以在下面定义。执行时要小心标志。在

def balance(pv, r, n, p):
    dfac = (1 + r / 12) ** n
    return pv * dfac - p * (dfac - 1) / (r / 12) 

另外,计算一个“恒定”的PMT值。这是利息加本金,在所有时期都是不变的。它是一个标量值,而不是数组。在

pmt = np.pmt(rate / 12, n * 12, prin)

最后,将上述内容以表格形式汇总:

table = pd.DataFrame({'Beg Balance' : balance(prin, rate, months - 1, -pmt),
                      'Principal' : principal,
                      'Interest' : interest,
                      'End Balance' : balance(prin, rate, months, -pmt)},
                     index=months)

# Check that the loan amortizes down to 0
assert np.allclose(table['End Balance'].tail(1), 0)

print(table.round(2))
     Beg Balance  End Balance  Interest  Principal
1      200000.00    199827.80  -1125.00    -172.20
2      199827.80    199654.64  -1124.03    -173.16
3      199654.64    199480.50  -1123.06    -174.14
4      199480.50    199305.38  -1122.08    -175.12
5      199305.38    199129.28  -1121.09    -176.10
..           ...          ...       ...        ...
356      6377.95      5116.63    -35.88   -1261.32
357      5116.63      3848.22    -28.78   -1268.42
358      3848.22      2572.67    -21.65   -1275.55
359      2572.67      1289.94    -14.47   -1282.72
360      1289.94        -0.00     -7.26   -1289.94

相关问题 更多 >