【问题标题】:Modelling Loan Payments - Calculate IRR贷款支付建模 - 计算内部收益率
【发布时间】:2019-06-18 23:04:42
【问题描述】:

处理贷款数据。 我有一个包含列的数据框:

df_irr = df1[['id', 'funded_amnt_t', 'Expect_NoPayments','installment']]

贷款ID |资助金额 |预期付款次数 |固定分期年金。

我已经通过回归分析估计了付款次数。 贷款期限为 36 或 60 个月。

现在我正在尝试计算预期的 irr(内部收益率)。

但我被卡住了

我打算使用numpy.irr 但是,我从来没有机会使用它——因为我的日期格式不正确?

我已经尝试过 pandas 的 pivot 和 reshape 函数。没有运气。

现金流的时间序列: - 列:月 0 , ...., 60 - 行:每笔贷款的 ID - 第 0 个月的值 = - funded_amount - 0-60 月的值:如果 expected_number_of_payments > 月,则分期付款

我的旧 Stata 代码是:

keep id installment funded_amnt expectednumberofpayments
sort id
expand 61, generate(expand)
bysort id : gen month = _n      
gen cf = 0
replace cf = installment if (expectednumberofpayments+1)>=month
replace cf = funded_amnt*-1 if month==1

enter image description here

【问题讨论】:

  • 你有什么问题?
  • :-) 需要用于重塑/构建数据框的代码。
  • 我现在尝试的东西:df_irr2 = pd.DataFrame(data=df_irr, index= 'id', columns= 'funded_amnt_t' 'Expect_NoPayments''installment') df_irr3 = df_irr.groupby(by = df_irr ['id']) df_irrT.pivot(index='id', columns='Expect_NoPayments', values='installment')

标签: python pandas pivot reshape irr


【解决方案1】:

numpy.irr 是错误的公式。该公式适用于不定期付款(例如,第 1 个月为 100 美元,第 2 个月为 0 美元,第 3 个月为 400 美元)。相反,您想使用numpy.rate。我正在对此解决方案的数据做出一些假设:

 import numpy as np
 df_irr['rate'] = np.rate(nper=df_irr['Expect_NoPayments'],
                          pmt=df_irr['installment'],
                          pv=df_irr['funded_amnt_t'])

更多信息可以在这里找到numpy documentation

【讨论】:

  • 不,不是解决方案。而且我仍然需要重塑我的数据集。
  • 为什么这不是解决方案? “重塑数据集”是什么意思?如果你算一下,IRRrate 会给你同样的结果。您能否更具体地回答您的问题?
  • 如果您为问题添加了所需的输出,这可能会很有用。
  • 亲爱的@Polkaguy6000 我尽我所能写下一个excel文件来解释预期的结果。
  • 不,我已经为范围内的 i 尝试过这个(0, 60):如果 i == 0: df_irrT['i'] = df_irr[['funded_amnt_t']] if i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
相关资源
最近更新 更多