Serving the Quantitative Finance Community

 
User avatar
ashkar
Topic Author
Posts: 0
Joined: October 17th, 2011, 9:25 am

Strange execution in R

June 10th, 2014, 8:57 pm

Hi guysI'm trying some basic pricing in R. Getting strange behaviour in the following function.In the line "x <- v.term[i-1:i]" in attached code, x should be two element vector but I see strange x values that changes as I restructure the code keeping the logic the same (to check if its some form of lazy evaluation bug). Secondly y should also be two element vector but it turns out be different length from y. Both statements seem to be taking evaluating i differently. If I evaluate v.term[7:8] instead of using i then I get correct result as shown below.Any help will be greatly appreciated.Debug:> curve.discfactor(df.curve,1)Called from: eval(expr, envir, enclos)Browse[1]> ndebug at curve.r#20: x <- v.term[i - 1:i]Browse[2]> i[1] 8Browse[2]> x [1] 0.002777778 0.019444444 0.038888889 0.083333333 0.255555556 0.508333333 0.758333333 1.013888889 2.030555556 3.044444444Browse[2]> v.term[7,8]Error in v.term[7, 8] : incorrect number of dimensionsBrowse[2]> v.term[7:8][1] 0.7583333 1.0138889Browse[2]> ndebug at curve.r#21: y <- v.df[i - 1:i]Browse[2]> i[1] 8Browse[2]> y [1] 0.9999973 0.9999814 0.9999627 0.9999202 0.9997525 0.9994624 0.9991152 0.9986102 0.9918173 0.9754081Browse[2]> v.df[7:8][1] 0.9991152 0.9986102Browse[2]>
 
User avatar
ashkar
Topic Author
Posts: 0
Joined: October 17th, 2011, 9:25 am

Strange execution in R

June 10th, 2014, 10:30 pm

On further looking at all the workspace variables, there turned out to be a x and y variable declared outside of the function which seemed to cause the issue. Seems ok after clearing the workspace.
 
User avatar
ashkar
Topic Author
Posts: 0
Joined: October 17th, 2011, 9:25 am

Strange execution in R

June 10th, 2014, 11:19 pm

globals isn't really the issue here. It seems to be a bug in R. If I replace "v.term[i-1:i]" by "c(v.term[i-1],v.term)" then it works as expected. If someone spots something stupid on my part please let me know. I'll try with a different version etc tomorrow.I should really get to bed now...too much debugging for the evening.
 
User avatar
Hansi
Posts: 41
Joined: January 25th, 2010, 11:47 am

Strange execution in R

June 11th, 2014, 12:06 am

Haven't looked at the code but guess it's index mistakes being silently treated to variable recycling. I assume what you want is v.term[(i-1):i] with i=8, it's 8-1:8, which expands to c(8,8,8,8,8,8,8,8)-c(1,2,3,4,5,6,7,8) -> c(7,6,5,4,3,2,1,0)So (8-1):8 asks for the 7 and 8th indices whereas 8-1:8 asks for the 7th till 1st and then silently throws away 0 since it's treated as an empty integer vector internally (silently again!).I'm sure this is noted in the R Inferno or at least similar. www.burns-stat.com/pages/Tutor/R_inferno.pdf
Last edited by Hansi on June 10th, 2014, 10:00 pm, edited 1 time in total.
 
User avatar
ashkar
Topic Author
Posts: 0
Joined: October 17th, 2011, 9:25 am

Strange execution in R

June 11th, 2014, 6:39 am

Thanks a lot Hansi. Very nice explanation. That link looks very useful too.