Python 现金流计算器

标签 python numpy math linear-algebra finance

我正在尝试制定一个方程,可以使用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.rate。 pmt 变量,即借款人必须支付的每月金额,以便在 self.amortMonths 结束时将 self.oringalPrin 值减少到 0

示例:

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

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

编辑:

所以像这样:

Balance_i = Balance_i-1 - (pmt - Balance_i-1 * 汇率)

最佳答案

下面创建了 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)

计算特定时间的未结余额使用: 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

关于Python 现金流计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44911302/

相关文章:

python - Pandas 到 Excel(合并标题列)

python - 交换 CSV 文件中的列

python - 应用数据帧方法后 numpy 数组末尾的额外数据类型

python - numpy的泊松置信区间

python - 我如何告诉 python 启动用户默认终端?

python - 是否可以让函数参数接受 2 种或更多类型?

python - 如何将 `__slots__` 从 Python 2 移植到 3

python - 从 Pandas 数据框中获取数组的子集

java.lang.NumberFormatException 与 BigDecimal java

math - 从面积和角度计算三角形