Finding r
Finding r¶
To find r we are going to have a much tougher time since there is no easy equation solution. To solve it we need to experiment. Let’s set the parameters below and try out different values of r and see if it will get us to match the values. In this lesson we are going to just try a couple values, but there are more advanced methods that you can use that we won’t introduce here. I made the example easy to work out by guessing very 1% within a limited range.
#Set up the parameters
PV = 768.3479521244554
F = 1000
c = .02
n = 10
We can predict the present value for something like say 2% and see what the difference between the actual and predicted is.
#Let's try to see what our PV would be if r=2%
predicted = bond_value(F,c,n,.02)
print(predicted)
If we check the difference we see it is not very close.
print(predicted-PV)
What we could do is test different rates and see which one gets us closest to the PV we are given
rates = [x/100 for x in range(1,13)]
print(rates)
print()
values = [bond_value(F,c,n,x) for x in rates]
print(values)
If we look for the absolute value in the difference between present value and predicted, this will help us find the closest fit.
#Differences
diff = [abs(x-PV) for x in values]
print(diff)
#Find the the minimum error
print(min(diff))
#Get the index of it
i = diff.index(min(diff))
print(i)
#And find the rate that got us there
print(rates[i])
After doing this, we see that we have the correct rate r to get the present value we saw before!
print(PV)
print(bond_value(FV,cr,n,rates[i]))