<div dir="ltr"><div>Hello, Simon.<br><br></div>I ran into a similar problem before (with<a href="https://stat.ethz.ch/R-manual/R-devel/library/stats/html/uniroot.html"> uniroot</a>), and what I found was that the function in R is actually written in C. I found the original code, but not being a real programmer, I stopped working on what I was doing and put it off until the point where I have enough time to develop the skill to port that into RCpp (maybe 2047?). In your case, <a href="https://stat.ethz.ch/R-manual/R-devel/library/stats/html/optimize.html">optimize() </a>itself is a translation of an algorithm by Brent in FORTRAN which can be found <a href="http://www.netlib.org/fmm/fmin.f">here (textfile)</a>. Perhaps you can translate it into C++ and call it directly?<br><br>Avi<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 23, 2014 at 12:04 AM, Simon Riddell <span dir="ltr"><<a href="mailto:simonruw@gmail.com" target="_blank">simonruw@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>I have been judiciously using RCPP for six months, and have had my numerous questions answered so far from previous threads. I have now run into an issue I cannot get a handle on:</div><div><br></div><div>I have converted a fairly large (recursive) Kalman filter function from R to C++ using RCPP. This function is then called within the R optim() function, subject to constraints, which estimates a set of ~30 parameters. <br><br>My issue occurs within the C++ Kalman Filter function, where, I use the optimize() function to calculate the yield to maturity rate of various bonds. I do this by calling back to the R environment to access the optimize() function. Below is the R Function I create to be used within the Kalman filter, and below this R function is my method for calling it within the C++ code. To complicate matters further, the R Function calls a C++ Function. To clarify: The Kalman Filter C++ code calls an R function, and this R Function calls an additional separate C++ function. (Code included below)<br><br>As I iterate the Kalman filter it runs perfectly for anywhere from 30 minutes to six hours (and produces correct output when matched to the R code), but it inevitably crashes. From past reading I have done, Dirk has before mentioned that calling an R function many times within C++ is usually not a good idea. As a result I suspect this is my issue (the error codes vary, sometimes mentioning numerical errors, other times recursive errors, other times random RCPP error codes -- I can document and provide them if needed)</div><div><br></div><div>My biggest impasse is I cannot figure out a way to complete this without calling the R optimize() function, and cannot find any RCPP optimize functions to use instead. Thank you for reading.</div><div><br></div><div><br></div><div><b>R Function <i>(</i></b><i>IRNPV.CPP is the C++ function, which R optimizes the rate parameter over):</i></div><div><i><br></i>optimcpp<-function(CoupDate=1,coupNo=1,coup=1,price=1,rate=1)<div>{</div><div>m<-optimize(function(CoupDate,coupNo,coup,price,rate) <i>IRNPV.CPP</i>(CoupDate=CoupDate,coupNo=coupNo,coup=coup,<i>rate</i>)-price)^2,c(-0.05,0.2),tol=1e-20,CoupDate=CoupDate,coupNo=coupNo,coup=coup,price=price)</div><div>m$minimum</div><div>}</div><div><br></div><div><b>Accessing the R environment within the C++ code:</b></div><div>CPP.SRC <- '<br></div><div>using namespace arma;<br></div><div><div>Rcpp::Environment global = Rcpp::Environment::global_env();</div><div>Function optimizecpp = global["optimcpp"];</div></div><div>//Various matrix computations to calculate CD, CN, Co, & Pricex[0]<br></div><div>optimvec0 = optimizecpp(CD,CN,Co,pricex[0],Ra);<br></div><div>'</div><div><br></div><div><b>IRNPV.CPP Function </b>(What the R Optimize() function optimizes 'rate' over -- <i>Very</i> <i>Likely Unnecessary for Purposes of this Question)</i></div><div><br></div><div><div>IR.NPV.TIPS.CBF.SRC <- '</div><div>using namespace arma;</div><div><br></div><div>double CD = Rcpp::as<double>(CoupDate);</div><div>double CN = Rcpp::as<double>(coupNo);</div><div>double RN = Rcpp::as<double>(rate);</div><div>double Co = Rcpp::as<double>(coup);</div><div><br></div><div>Rcpp::NumericVector LM;</div><div>mat DiscountFunc;</div><div>double length;</div><div><br></div><div>double price;</div><div><br></div><div>if (CN > 1) {</div><div><br></div><div>length = floor((((CD+0.5*(CN-1))-CD)/0.50))+1;</div><div><br></div><div>for (double i = CD; i <= (CD+0.5*(CN-1))+.05; i += 0.5) {</div><div><span style="white-space:pre-wrap">      </span>LM.insert(LM.end(),i);</div><div>}</div><div><br></div><div>DiscountFunc.set_size(LM.size(), 1);</div><div>DiscountFunc.fill(0);</div><div><br></div><div><br></div><div>double k = 0;</div><div>mat::row_iterator q = DiscountFunc.begin_row(0);  </div><div>mat::row_iterator w = DiscountFunc.end_row((LM.size()-1));   </div><div>for(mat::row_iterator i=q; i!=w; ++i)</div><div>     {</div><div>     (*i) = exp(-RN*LM[k]);</div><div>     k = k + 1;</div><div>     }</div><div><br></div><div>price = CD*Co*DiscountFunc[0];</div><div><br></div><div>for (int i=1; i<(LM.size()); ++i) {</div><div><span style="white-space:pre-wrap">       </span>price = price+0.5*Co*DiscountFunc[i];</div><div><span style="white-space:pre-wrap">    </span>}</div><div><br></div><div><br></div><div>}</div><div>else {</div><div>double DiscountFunc;</div><div>DiscountFunc = exp(-RN*CD);</div><div>price = (1+CD*Co)*DiscountFunc;</div><div>}</div><div>return Rcpp::wrap(price);</div><div><br></div><div>'</div><div><br></div><div><br></div><div>IRNPV.CPP <- cxxfunction(signature(CoupDate="NumericVector", coupNo="NumericVector", coup="NumericVector",rate="NumericVector"),</div><div><span style="white-space:pre-wrap">     </span>IR.NPV.TIPS.CBF.SRC, include=CMATH, plugin="RcppArmadillo")</div><div><br></div><div><br></div><div style="font-style:italic"><br></div></div><div>Thank you,</div><div>Simon</div><span class="HOEnZb"><font color="#888888"><div><br></div><div><br></div>-- <br><div><div dir="ltr"><span style="border-collapse:collapse;font-size:13px"><font face="arial, sans-serif">Simon Alexander Riddell</font><br></span><div><font face="Arial, sans-serif">Economic Research RA<br></font><span style="font-family:Arial,sans-serif">Federal Reserve Bank</span></div></div></div>
</font></span></div></div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>