大熊猫复利的资本化

问题描述 投票:0回答:2

我正在写一个大熊猫银行存款账户的模拟。我陷入了复利(这是利息再投资的结果,因此下一期的利息将通过本金和先前累计利息获得。)

到目前为止,我有以下代码:

import pandas as pd
from pandas.tseries.offsets import MonthEnd
from datetime import datetime

# Create a date range
start = '21/11/2017'
now = datetime.now()
date_rng = pd.date_range(start=start, end=now, freq='d')

# Create an example data frame with the timestamp data
df = pd.DataFrame(date_rng, columns=['Date'])

# Add column (EndOfMonth) - shows the last day of the current month
df['LastDayOfMonth'] = pd.to_datetime(df['Date']) + MonthEnd(0)

# Add columns for interest, Sasha, Artem, Total, Description
df['Debit'] = 0
df['Credit'] = 0
df['Total'] = 0
df['Description'] = ''

# Iterate through the DataFrame to set "IsItLastDay" value
for i in df:
    df['IsItLastDay'] = (df['LastDayOfMonth'] == df['Date'])

# Add the transaction of the first deposit
df.loc[df.Date == '2017-11-21', ['Debit', 'Description']] = 10000, "First deposit"

# Calculate the principal sum (It the summ of all deposits minus all withdrows plus all compaund interests)
df['Total'] = (df.Debit - df.Credit).cumsum()

# Calculate interest per day and Cumulative interest
# 11% is the interest rate per year
df['InterestPerDay'] = (df['Total'] * 0.11) / 365
df['InterestCumulative'] = ((df['Total'] * 0.11) / 365).cumsum()

# Change the order of columns
df = df[['Date', 'LastDayOfMonth', 'IsItLastDay', 'InterestPerDay', 'InterestCumulative', 'Debit', 'Credit', 'Total', 'Description']]

df.to_excel("results.xlsx")

输出文件看起来很好,但我需要以下内容:

  1. “InterestCumulative”列添加到每个月的最后一天的“总计”列(使利益复合)
  2. 在每个月的开始,应该清除“利息累积”列(因为利息被添加到本金总额)。

enter image description here

我不知道该怎么做。

你有什么想法吗?提前致谢!

python pandas economics
2个回答
1
投票

您将需要循环,因为您的总更改取决于之前的行,然后会影响后面的行。因此,您当前的利息计算是错误的。

total = 0
cumulative_interest = 0

total_per_day = []
interest_per_day = []
cumulative_per_day = []
for day in df.itertuples():
    total += day.Debit - day.Credit
    interest = total * 0.11 / 365
    cumulative_interest += interest

    if day.IsItLastDay:
        total += cumulative_interest

    total_per_day.append(total)
    interest_per_day.append(interest)
    cumulative_per_day.append(cumulative_interest)

    if day.IsItLastDay:
        cumulative_interest = 0

df.Total = total_per_day
df.InterestPerDay = interest_per_day
df.InterestCumulative = cumulative_per_day

不幸的是,这看起来更令人困惑,但是当值依赖于先前的值时会发生这种情况。根据您的具体要求,可能有很好的方法可以使用数学来简化这一过程,但除此之外,这就是您所拥有的。

我已经将它直接写入stackoverflow,因此它可能并不完美。


1
投票

不太优雅的解决方案。

# Get the values for the first and last days of months respectively
first_days = df.groupby(by=df['Date'].dt.month, as_index=False).first()
last_days = df.groupby(by=df['Date'].dt.month, as_index=False).last()

print(df.loc[df['Date'].isin(first_days['Date']), 'InterestCumulative'])
print(df.loc[df['Date'].isin(last_days['Date']), 'Total'])

# Replace first day interest with 0s
df.loc[df['Date'].isin(first_days['Date']), 'InterestCumulative'] = 0

# Adds last day 'Interestcumulative' to 'Total'
df.loc[df['Date'].isin(last_days['Date']), 'Total'] = (
            last_days['Total']
            + last_days['InterestCumulative']
        ).values

print(df.loc[df['Date'].isin(first_days['Date']), 'InterestCumulative'])
print(df.loc[df['Date'].isin(last_days['Date']), 'Total'])
© www.soinside.com 2019 - 2024. All rights reserved.