at the moment I am writing my bachelor thesis and I have to program in R for the first time. I think not the best way to learn R but never mind.
My question is concerning a function that can solve an equation like this:
q <- function(ytc) {
(5 / ((1 + (ytc / 4))^4 * ((1645 * 5 / 1826) - (1640 * 5 / 1826)))) +
(5 / ((1 + (ytc / 4))^4 * ((1736 * 5 / 1826) - (1640 * 5 / 1826)))) +
(5 / ((1 + (ytc / 4))^4 * ((1826 * 5 / 1826) - (1640 * 5 / 1826)))) +
100 / (((1 + (ytc / 4))^4 * ((1826 * 5 / 1826) - (1640 * 5 / 1826)))) - 100
}
My aim is to simply solve for the ytc what should be the yield to call of a bond. But I cannot find any way to figure it out. This should be a simple PV calculation like: PV=c/(1+r/4)^4*t1+c/(1+r/4)^4*t2+.... and hence solve vor r. But don't know how to do that. I tried several functions like uniroot, unroot.all, ... but nothing could figure out the solution. Additionally a real problem is that in the my main equation I am discounting 20 payments and and I was not able to modify it as a linear equation yet due to missing knowledge about what to to with the exponents.
I hope anyone could help me.
Looking forward to hear from anyone.
I do not quite understand why you cannot solve your equation.
Take your function q
:
q <- function(ytc) {
(5 / ((1 + (ytc / 4))^4 * ((1645 * 5 / 1826) - (1640 * 5 / 1826)))) +
(5 / ((1 + (ytc / 4))^4 * ((1736 * 5 / 1826) - (1640 * 5 / 1826)))) +
(5 / ((1 + (ytc / 4))^4 * ((1826 * 5 / 1826) - (1640 * 5 / 1826)))) +
100 / (((1 + (ytc / 4))^4 * ((1826 * 5 / 1826) - (1640 * 5 / 1826)))) - 100
}
and assuming you want to find the value of ytc
for which function q
is zero then you can use uniroot
as follows:
uniroot(q, c(0,10))
and if you want a more accurate solution use
uniroot(q, c(0,10),tol = .Machine$double.eps^0.5)
Seems to work nicely.