python-3.x - 如何使用 Python 创建摊销表,包括图表?

标签 python-3.x

我正在测试这段代码...

import pandas as pd
from datetime import date
import numpy as np
from collections import OrderedDict
from dateutil.relativedelta import *


def amortize(principal, interest_rate, years, addl_principal=0, annual_payments=12, start_date=date.today()):

    pmt = -round(np.pmt(interest_rate/annual_payments, years*annual_payments, principal), 2)
    # initialize the variables to keep track of the periods and running balances
    p = 1
    beg_balance = principal
    end_balance = principal

    while end_balance > 0:

        # Recalculate the interest based on the current balance
        interest = round(((interest_rate/annual_payments) * beg_balance), 2)

        # Determine payment based on whether or not this period will pay off the loan
        pmt = min(pmt, beg_balance + interest)
        principal = pmt - interest

        # Ensure additional payment gets adjusted if the loan is being paid off
        addl_principal = min(addl_principal, beg_balance - principal)
        end_balance = beg_balance - (principal + addl_principal)

        yield OrderedDict([('Month',start_date),
                           ('Period', p),
                           ('Begin Balance', beg_balance),
                           ('Payment', pmt),
                           ('Principal', principal),
                           ('Interest', interest),
                           ('Additional_Payment', addl_principal),
                           ('End Balance', end_balance)])

        # Increment the counter, balance and date
        p += 1
        start_date += relativedelta(months=1)
        beg_balance = end_balance


schedule = pd.DataFrame(amortize(700000, .04, 30, addl_principal=200, start_date=date(2016, 1,1)))
schedule.head()


schedule.tail()


schedule1, stats1 = amortization_table(100000, .04, 30, addl_principal=50, start_date=date(2016,1,1))
schedule2, stats2 = amortization_table(100000, .05, 30, addl_principal=200, start_date=date(2016,1,1))
schedule3, stats3 = amortization_table(100000, .04, 15, addl_principal=0, start_date=date(2016,1,1))

pd.DataFrame([stats1, stats2, stats3])


additional_payments = [0, 50, 200, 500]
fig, ax = plt.subplots(1, 1)

for pmt in additional_payments:
    result, _ = amortization_table(100000, .04, 30, addl_principal=pmt, start_date=date(2016,1,1))
    ax.plot(result['Month'], result['End Balance'], label='Addl Payment = ${}'.format(str(pmt)))
plt.title("Pay Off Timelines")
plt.ylabel("Balance")
ax.legend();

我从下面的链接中找到了代码:

http://pbpython.com/amortization-model.html

示例代码似乎适用于发布它的人,但对我来说,我看到的只是以下错误...

NameError: name 'amortization_table' is not defined

最佳答案

如果您在 http://pbpython.com/amortization-model.html 中的 amortization_Table 上按了 ctrl+F你可以解决它。在“分析时间”部分下...以下代码块定义了摊销表。另请参阅笔记本 - https://github.com/chris1610/pbpython/blob/master/notebooks/Amortization-Model-Article.ipynb

def amortization_table(interest_rate, years, payments_year, principal, addl_principal=0, start_date=date.today()):
""" Calculate the amortization schedule given the loan details

 Args:
    interest_rate: The annual interest rate for this loan
    years: Number of years for the loan
    payments_year: Number of payments in a year
    principal: Amount borrowed
    addl_principal (optional): Additional payments to be made each period. Assume 0 if nothing provided.
                               must be a value less then 0, the function will convert a positive value to
                               negative
    start_date (optional): Start date. Will start on first of next month if none provided

Returns:
    schedule: Amortization schedule as a pandas dataframe
    summary: Pandas dataframe that summarizes the payoff information
"""
# Ensure the additional payments are negative
if addl_principal > 0:
    addl_principal = -addl_principal

# Create an index of the payment dates
rng = pd.date_range(start_date, periods=years * payments_year, freq='MS')
rng.name = "Payment_Date"

# Build up the Amortization schedule as a DataFrame
df = pd.DataFrame(index=rng,columns=['Payment', 'Principal', 'Interest', 
                                     'Addl_Principal', 'Curr_Balance'], dtype='float')

# Add index by period (start at 1 not 0)
df.reset_index(inplace=True)
df.index += 1
df.index.name = "Period"

# Calculate the payment, principal and interests amounts using built in Numpy functions
per_payment = np.pmt(interest_rate/payments_year, years*payments_year, principal)
df["Payment"] = per_payment
df["Principal"] = np.ppmt(interest_rate/payments_year, df.index, years*payments_year, principal)
df["Interest"] = np.ipmt(interest_rate/payments_year, df.index, years*payments_year, principal)

# Round the values
df = df.round(2) 

# Add in the additional principal payments
df["Addl_Principal"] = addl_principal

# Store the Cumulative Principal Payments and ensure it never gets larger than the original principal
df["Cumulative_Principal"] = (df["Principal"] + df["Addl_Principal"]).cumsum()
df["Cumulative_Principal"] = df["Cumulative_Principal"].clip(lower=-principal)

# Calculate the current balance for each period
df["Curr_Balance"] = principal + df["Cumulative_Principal"]

# Determine the last payment date
try:
    last_payment = df.query("Curr_Balance <= 0")["Curr_Balance"].idxmax(axis=1, skipna=True)
except ValueError:
    last_payment = df.last_valid_index()

last_payment_date = "{:%m-%d-%Y}".format(df.loc[last_payment, "Payment_Date"])

# Truncate the data frame if we have additional principal payments:
if addl_principal != 0:

    # Remove the extra payment periods
    df = df.ix[0:last_payment].copy()

    # Calculate the principal for the last row
    df.ix[last_payment, "Principal"] = -(df.ix[last_payment-1, "Curr_Balance"])

    # Calculate the total payment for the last row
    df.ix[last_payment, "Payment"] = df.ix[last_payment, ["Principal", "Interest"]].sum()

    # Zero out the additional principal
    df.ix[last_payment, "Addl_Principal"] = 0

# Get the payment info into a DataFrame in column order
payment_info = (df[["Payment", "Principal", "Addl_Principal", "Interest"]]
                .sum().to_frame().T)

# Format the Date DataFrame
payment_details = pd.DataFrame.from_items([('payoff_date', [last_payment_date]),
                                           ('Interest Rate', [interest_rate]),
                                           ('Number of years', [years])
                                          ])
# Add a column showing how much we pay each period.
# Combine addl principal with principal for total payment
payment_details["Period_Payment"] = round(per_payment, 2) + addl_principal

payment_summary = pd.concat([payment_details, payment_info], axis=1)
return df, payment_summary

关于python-3.x - 如何使用 Python 创建摊销表,包括图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958612/

相关文章:

mysql - Python3、Pandas 和 MySQL 索引问题

c - 为什么在 TF2 中不推荐使用 convert_variables_to_constants()?

python - 多个字符串与索引附加在一起

Python 字节数组打印

python - 类型错误 : '_AtIndexer' object is not callable in pandas

python - 如何更改由 plt.hist() 描绘的直方图的尺寸,因为 figsize 不是参数

python - 连接字符串输入而不创建换行符

python - 返回非实例化类类型的类型提示

python - 在Python中根据字典制作图表

python - 是否可以创建一个既没有 __slots__ 也没有 __dict__ 的 python 对象?