分别对loan和bond的现金流进行分析:
n=120
i1=0.08/12
2000*i1
i2=0.09/12
10000*i2
bondcashflow=function(r,f,C,n){
A=r*f
bcf=1:n
for (m in 1:n){
bcf[m]=A
}
bcf[n]=bcf[n]+C
return(bcf)
}
k1=c(2000,bondcashflow(i1,-2000,-2000,120))
k2=c(-10000,bondcashflow(i2,10000,10000,120))
M=data.frame("loan"=k1,"bond"=k2)
把二者现金流进行合并:
合并之后,可把整个现金流变动看作一个债券,对该债券进行收益率计算:
k=k1+k2
M["cashflow"]=k
v=function(i,n){
v=(1+i)**(-n)
return(v)
}
an=function(i,n){
an=(1-v(i,n))/i
return(an)
}
division=function(a,b,M,n){
while(TRUE){
t=(a+b)/2
if(abs(f(t)-M)<=10**(-n)){
m=t
break
}
if((f(t)-M)/abs(f(t)-M)==
(f(a)-M)/abs(f(a)-M)){
a=t
}else{
b=t
}
}
return(m)
}
f=function(i){
r=-k[2]/k[1]
f=-k[1]
C=f
A=r*f
P=A*an(i,n)+C*v(i,n)
return(P)
}
i12=division(0.0001,0.9999,-k[1],10)
i=v(i12,-12)-1