<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 class="" style="white-space:pre">     </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 class="" style="white-space:pre"> </span>price = price+0.5*Co*DiscountFunc[i];</div><div><span class="" style="white-space:pre">      </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 class="" style="white-space:pre">       </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><div><br></div><div><br></div>-- <br><div class="gmail_signature"><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>
</div></div>