Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

python method - cash flow calculation


May 30, 2021 Article blog


Table of contents


The calculation of common cash flow

1. Fixed cash flow present value calculation function expression:

PresentVal-pv (Rate, NumPeriods, Payment, ExtraPayment, Due) Rate: Discount Rate NumPeriods: Discount Cycle Payment Cycle Cash Flow ExtraPayment: Last Non-Cyclical Cash Flow, function defaults to 0Due: Cash Flow Interest Payment Method (0 is end-of-week interest payment, 1 for the initial interest payments of the cycle) PresentVal: Cash Flow Present Value Code is as follows:
import numpy as np
Facevalue=1000
# 债券付息(面值*利率),假设每年付息一次
Payment=0.05*Facevalue
# 市场利率
Rate=0.06
# 到期还本,ExtraPayment额外现金流为本金
ExtraPayment=Facevalue
# 债券期现为10年
NumPeriods=10
# 每年年末付息
Due=0
PresentVal=abs(np.pv(Rate,NumPeriods,Payment,ExtraPayment,Due))
print(PresentVal)  #该债券价格低于926.4,则被低估,高于926.4,则被高估

The result was: 926.3991294858529

2. Fixed cash flow final value calculation function

FutureVal=fv(Rate,NumPeriods,Payment,PresentVal,Due)
Rate: Discount rate
Num Periods: Discount cycle
Payment cycle cash flow
Due: Cash Flow Interest (0 is interest paid at the end of the week and 1 is interest paid at the beginning of the cycle)
PresentVal: Cash Flow Present Value : FutureVal: Cash Flow End Value
The code is as follows:

import numpy as np
Facevalue=1000
# 债券付息(面值*利率),假设每年付息一次
Payment=0.05*Facevalue
# 市场利率
Rate=0.06
# 到期还本,ExtraPayment额外现金流为本金
ExtraPayment=Facevalue
# 债券期现为10年
NumPeriods=10
# 每年年末付息
Due=0
FutureVal=abs(np.fv(Rate,NumPeriods,Payment,PresentVal,Due))
print(FutureVal)

The results are as follows: 2318.079494238091

3. Change cash flow calculation

The NPV method (NPV) discounts cash flow to the NPV value with the necessary rate of return, which is feasible if NPV >0, otherwise it is not feasible for the IR method to assume NPV=0, calculate the necessary discount rate, and if the IRR is greater than the necessary rate of return, it is feasible to calculate the NPV calculation function Val-npv (Rate, CashFlow) Rate: necessary rate of return Cash Flow: Cash Flow Sequence Vector PresentVal: Cash Flow Present Value Code is as follows:

import numpy as np
# 现金流
CashFlow=[-8000,2500.1500,3000,1000,2000]
# 利率
Rate=0.08
PresentVal1=-8000+2500/1.08+1500/1.08**2+3000/1.08**3+1000/1.08**4+2000/1.08**5
PresentVal1=np.npv(Rate,CashFlow)
print(PresentVal1)

The results are as follows: -849.137888777871

4. IRR calculation function

Return-irr (CashFlow) Return: The IRR code is as follows:

import numpy as np
CashFlow=[-8000,2500,1500,3000,1000,2000]
Return=np.round(np.irr(CashFlow),4) #round() 方法返回浮点数x的四舍五入值。如round(80.23456, 2) :  80.23
print(Return)

The results are as follows: 0.0839

5. Annual cash flow calculation

If you invest 1 million to buy a house, repayment period of 20 years, 6000 yuan per month, then what is the loan interest rate? I f the monthly repayment of 8000, the loan interest rate remains unchanged, then the repayment period is how long? A nnuity interest rate function rate (Num Periods, Pay, PresentValue, FutureValue, Due) NumPeriods: Cash Flow Cycle Payment: Cash Flow Expense FutureValue: Cash Flow End Value, function defaults to 0Due: Cash Flow Interest Payment Method (0 is interest paid at the end of the week, 1 is interest paid at the beginning of the cycle) PresentVal: Cash Flow Present Value Rate: Interest Rate (Discount Rate)

The code is as follows:

import numpy as np
# 贷款现值
PresentValue=1000000.0
# 每次还款金额
Payment=-6000.0
# 还款次数
NumPeriods=20*12
# 现金流终值为0
FutureValue=0
# 每周期还款一次,0为周期末付息
Due=0
# 调用rate函数
Rate=np.around(np.rate(NumPeriods,Payment,PresentValue,FutureValue),4)
print(Rate)

The result is: 0.0032

Continue to solve,

Annuity cycle function nper NumPeriods snper (Rate, Pay, PresentValue, fv,) Rate: Discount Rate Payment: Cash Flow Income (Expenses) PresentValue: Cash Flow PresentValue: Cash Flow Present Value fv: Cash Flow End Value, Default Value 0Due: Cash Flow Interest Method NPerumiods: Cash Flow Cycle

import numpy as np
# 贷款现值
PresentValue=1000000.0
# 每次还款金额
Payment=-8000.0
# 现金流终值为0
FutureValue=0
# 每周期还款一次,0为周期末付息
Due=0
# 月利率,银行的现行计息方式
Rate=0.0389/12
# 调用nper计算还款周期
NumPeriods=np.nper(Rate,Payment,PresentValue,fv=0,when='end')
print(NumPeriods/12)

The results are as follows: 13.377524805437297