{"word_count": 105, "character_count": 1064}
The following simple code can be used to find roots of functions (based on the secant algorithm),secant=function(fun, x0, x1, tolerence=1e-07, niter=500){for ( i in 1:niter ) {x2 &#60;- x1-fun(x1)*(x1-x0)/(fun(x1)-fun(x0))if (abs(fun(x2)) &#60; tolerence)return(x2)x0 &#60;- x1x1 &#60;- x2}}It can be interesting in actuarial science, e.g. to find the actuarial rate so that to present values are equal. For instance, consider the following capital, given only if the insured is still alive (this example was initially considered here). We would like to find the rate so that the probable discounted value is 600,&#62; Lx=read.table("https://perso.univ-rennes1.fr/arthur.charpentier/TV8890.csv",+ header=TRUE,sep=";")&#62; capital=c(100,100,125,125,150,150)&#62; n=length(capital)&#62; x=0.035&#62; X=45&#62; f=function(x){+ capital.act=capital*(1/(1+x))^(1:n)+ PROBA=Lx[((Lx[,1]&#62;X)*(Lx[,1]&#60;=(X+n)))==1,2]/Lx[(Lx[,1]==X)==1,2]+ return(sum(capital.act*PROBA))}&#62;&#62; f1=function(x){f(x)-600}&#62; secant(f1,0,0.1)[1] 0.06022313&#62; f(0.06022313)[1] 600