Python 现金流量计算器

Python CashFlow Calculator

我正在尝试制定一个可以使用 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 结束时将 self.oringalPrin 值降低到 0

示例:

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

所以我实际上需要一些帮助来确定一个方程式,该方程式可以一次性完成,而无需迭代过程。显然,我需要使用线性代数,但我没有数学背景,所以我想知道是否有人有任何想法?

编辑:

所以像这样:

Balance_i = Balance_i-1 - (pmt - Balance_i-1 * Rate).

下面创建了 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 来计算每期的利息和本金。请注意,这些不取决于您的 运行 贷款余额。好的是下面的结果是数组(付款时间表):

months = np.arange(1, n * 12 + 1) # months 1 thru 360
principal = np.ppmt(rate / 12, months, n * 12, prin)
interest = np.ipmt(rate / 12, months, n * 12, prin)

计算特定时间的未结余额使用:

我们可以在下面定义。实施时注意标志。

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

此外,计算 "constant" PMT 值。这是利息加本金,并且在所有期间都是不变的。这是一个标量值,而不是数组。

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

最后把上面的组合成table的形式:

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

这是我的解决方案。

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)

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