<div dir="ltr">Dear all,<div><br></div><div>Thank you for your responses. I will look into the resources and code you have shared and hopefully I can figure something out! I appreciate your time and help.</div><div><br></div><div>Simon</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 2, 2015 at 12:45 PM, Mark Clements <span dir="ltr"><<a href="mailto:mark.clements@ki.se" target="_blank">mark.clements@ki.se</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Alternatively, one could use the code for Brent_fmin() from the stats<br>
library (e.g.<br>
<a href="https://github.com/lgautier/R-3-0-branch-alt/blob/master/src/library/stats/src/optimize.c" target="_blank">https://github.com/lgautier/R-3-0-branch-alt/blob/master/src/library/stats/src/optimize.c</a>).<br>
I have done this for the rstpm2 library<br>
(<a href="https://github.com/mclements/rstpm2/blob/develop/src/c_optim.cpp" target="_blank">https://github.com/mclements/rstpm2/blob/develop/src/c_optim.cpp</a>).<br>
<br>
This does use a C API, so I would not argue that it is "best practice".<br>
However, using templates and function objects, this can be comparatively<br>
general.<br>
<br>
An inline example is below.<br>
<br>
require(Rcpp)<br>
require(inline)<br>
src <- "<br>
#include <Rcpp.h><br>
#include <float.h> /* DBL_EPSILON */<br>
// From the URLs above, insert the definition for:<br>
// double Brent_fmin(double ax, double bx, double (*f)(double, void *),<br>
// void *info, double tol);<br>
//<br>
// An example<br>
struct Model {<br>
double a,b;<br>
double operator()(double x) {<br>
return pow(log(x) - a,2) + b;<br>
}<br>
};<br>
Model model = {1.0,2.0};<br>
// template to use a function object (functor) with Brent_fmin()<br>
template<class T><br>
double Brent_fmin_functor(double x, void * par) {<br>
T * model = (T *) par;<br>
return model->operator()(x);<br>
}<br>
//[[Rcpp::export]]<br>
double test_optimise() {<br>
return Brent_fmin(0.001,10.0,&Brent_fmin_functor<Model>,(void *)<br>
&model,1.0e-10);<br>
}<br>
"<br>
sourceCpp(code=src)<br>
test_optimise()<br>
<br>
Kindly, Mark.<br>
<div class="HOEnZb"><div class="h5"><br>
On 12/23/2014 02:48 PM, Dirk Eddelbuettel wrote:<br>
> On 23 December 2014 at 08:21, Hao Ye wrote:<br>
> | There are also some minima-finding functions in GSL that you may want to look<br>
> | into. The source for RcppGSL might help with a fully c++ version.<br>
><br>
> Yes. And there are a bazillion optimisation packages on CRAN:<br>
> <a href="http://cran.r-project.org/web/views/Optimization.html" target="_blank">http://cran.r-project.org/web/views/Optimization.html</a><br>
> Several of these are already used in a Rcpp context.<br>
><br>
> Also, I once needed something similar to what Avi described here, and just 'ripped<br>
> out' a simple one-dim optimizer (to compute implied vols for a lot of option<br>
> price series quickly, so I took the optmizier from QuantLib) -- and blogged<br>
> about it: <a href="http://dirk.eddelbuettel.com/blog/2012/10/25/" target="_blank">http://dirk.eddelbuettel.com/blog/2012/10/25/</a> This isn't all that<br>
> hard, and we can probably help Simon here.<br>
><br>
> A different (and harder to grok at first) take is in RcppDE where I reworked<br>
> the DEoptim optimization package (and "ported" from C to C++ wit RcppArmadillo)<br>
> and allowed use of user-supplied functions to optimize for -- given as C++<br>
> functions. This is likely to confuse Simon now, but some other people have<br>
> used this scheme.<br>
><br>
> Dirk<br>
><br>
<br>
</div></div><div class="HOEnZb"><div class="h5">_______________________________________________<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>
</div></div></blockquote></div><br><br clear="all"><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><span style="border-collapse:collapse;font-size:13px"><span style="font-family:Arial,sans-serif">London School of Economics</span></span><div><span style="border-collapse:collapse;font-size:13px"><span style="font-family:Arial,sans-serif">MSc in Political Economy<br></span></span><a href="http://uk.linkedin.com/in/simonriddell" target="_blank">linkedin.com/in/simonriddell</a><br></div></div></div>
</div>