Re: [R] Fwd: Numerical integration

2010-11-17 Thread Hans W Borchers
Eduardo de Oliveira Horta  gmail.com> writes:

> 
> -- Forwarded message --
> From: Eduardo de Oliveira Horta  gmail.com>
> Date: Wed, Nov 17, 2010 at 3:59 PM
> Subject: Re: [R] Numerical integration
> To: David Winsemius  comcast.net>
> 
> It works, however is not very efficient for the problem I'm working with,
> since it is the same as vectorizing the integrand before applying
> "integrate".
> 
> Any other thoughts? Efficiency would be highly welcome!
> 
> Thanks again,
> 
> Eduardo Horta
> 

'adaptIntegrate' in package "cubature" does not require pre-vectorized 
functions. But I doubt it is more efficient as every integration needs
to perform a lot of function calls.

And with 'adaptIntegrate' you have to do the reduction of an infinite
interval to a finite one yourself. E. g., with a given function f define

g <- function(x) f(1/x) / x^2
adaptIntegrate(g, 0, 1/a)  # instead of integrate(f, a, Inf)

You have not given the slightest indication why your function integration
would be so slow. So it's difficult to give more focussed advice.

Hans Werner

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Fwd: Numerical integration

2010-11-17 Thread Eduardo de Oliveira Horta
-- Forwarded message --
From: Eduardo de Oliveira Horta 
Date: Wed, Nov 17, 2010 at 3:59 PM
Subject: Re: [R] Numerical integration
To: David Winsemius 


It works, however is not very efficient for the problem I'm working with,
since it is the same as vectorizing the integrand before applying
"integrate".

Any other thoughts? Efficiency would be highly welcome!

Thanks again,

Eduardo Horta


On Wed, Nov 17, 2010 at 2:10 PM, Eduardo de Oliveira Horta <
eduardo.oliveiraho...@gmail.com> wrote:

> Thanks! I'll take a look into it and let you know if it works.
>
>
> On Wed, Nov 17, 2010 at 1:57 PM, David Winsemius 
> wrote:
>
>>
>> On Nov 17, 2010, at 6:44 AM, Eduardo de Oliveira Horta wrote:
>>
>>  Hi!
>>>
>>> I was wondering if there are any other functions for numerical
>>> integration,
>>> besides 'integrate' from the stats package, but which wouldn't require
>>> the
>>> integrand to be vectorized. Oh, and must be capable of integrating over
>>> (-inf,+inf).
>>>
>>
>> You could modify integrate to suit you specifications, Just substitute
>> this at the beginning of the integrate:
>>
>> integrateV <- function (f, lower, upper, ..., subdivisions = 100, rel.tol
>> = .Machine$double.eps^0.25,
>>abs.tol = rel.tol, stop.on.error = TRUE, keep.xy = FALSE,
>>aux = NULL)
>> {
>>f <- match.fun(f)
>>### New material
>>options(show.error.messages = FALSE)
>>if( class(try(integrate(f, upper, lower))) == 'try-error') {f <-
>> Vectorize(f)}
>>options(show.error.messages = TRUE)
>>### End new material
>>ff <- function(x) f(x, ...)
>> .
>> .
>> .
>>
>>
>> Passes both your requirements:
>> >  zz<- function(x) 2.0  # the function that fails in the integrate help
>> page
>> > try(integrateV(zz, 0, 1))
>> 2 with absolute error < 2.2e-14
>>
>> >  zz<- function(x) dnorm(x) # test of infinite range capacity
>> > try(integrateV(zz, -Inf, Inf))
>> 1 with absolute error < 9.4e-05
>>
>>
>> --
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.