Scott Stark
2007-Aug-23 22:44 UTC
[R] Clarification: Expedite scalar f(x) evaluation over vectors
Please note clarifications in <<>> below. My apologies for any confusion. Thanks again, Scott ---------- Forwarded message ---------- From: Scott Stark <stark.sc@gmail.com> Date: Aug 23, 2007 1:03 PM Subject: Expedite scalar f(x) evaluation over vectors To: r-help@lists.r-project.org Dear R community, I am trying to code a fairly complex equation for optim(). My current approach is too slow for optim(). I have a function that takes a double integral (hopefully correctly) across two terms, e.g., <<note replacement of example function by hypothetical f(x,y)>> doubleint <- function(c1,c2) {integrate(function(y) { sapply(y, function(y) { integrate(function(x) {f(x,y)}, boundsx[1], boundsx[2])$value }) }, boundsy[1],boundsy[2]) } <<I do not have a closed form for the real function>> I would like to rapidly evaluate this function over vectors c1 and c2 of equal length where the double integral is calculated for each (matching) element c1_i & c2_i. At present I get length mismatch errors. Furthermore, mapply() takes too long. Can I expedite this evaluation or do I need to reformulate my approach? Thanks in advance for your help, Scott C. Stark University of Arizona [[alternative HTML version deleted]]
Daniel Lakeland
2007-Aug-24 01:21 UTC
[R] Clarification: Expedite scalar f(x) evaluation over vectors
On Thu, Aug 23, 2007 at 03:44:36PM -0700, Scott Stark wrote:> Please note clarifications in <<>> below. My apologies for any confusion. > Thanks again, > Scott > > ---------- Forwarded message ---------- > From: Scott Stark <stark.sc at gmail.com> > Date: Aug 23, 2007 1:03 PM > Subject: Expedite scalar f(x) evaluation over vectors > To: r-help at lists.r-project.org > > Dear R community, > > I am trying to code a fairly complex equation for optim(). My current > approach is too slow for optim(). > > I have a function that takes a double integral (hopefully correctly) across > two terms, e.g., > > <<note replacement of example function by hypothetical f(x,y)>> > > doubleint <- function(c1,c2) {integrate(function(y) { > sapply(y, function(y) { > integrate(function(x) {f(x,y)}, boundsx[1], boundsx[2])$value > }) > }, boundsy[1],boundsy[2]) > } > > <<I do not have a closed form for the real function>>I assume you're trying to find the optimal c1 and c2. You could try varying the tolerance to integrate, initially allowing a rough approximation of your integral for the first steps until you get near an optimum, and then re-running the optimize on a much higher tolerance. For example, set your maximum subdivisions to 10, run the optimize, then set to 20 and run the optimize from the endpoint of the last run. When the optimal point stops moving, call it good?? The next approach I'd try is to be more fancy about your mathematics, by finding an approximate closed form for your function that was analytically integrable, perhaps in a computer algebra system like maxima. -- Daniel Lakeland dlakelan at street-artists.org http://www.street-artists.org/~dlakelan
Reasonably Related Threads
- Expedite scalar f(x) evaluation over vectors
- Is LDAP + Kerberos without Active Directory no longer supported?
- lmer coefficient distributions and p values
- Bootstrapping Contrasts for Repeated Measures ANOVA
- Calculating confidence in an estimate including number of trials?