From ripley@stats.ox.ac.uk  Fri Jan 12 08:22:42 2001
From: ripley@stats.ox.ac.uk (Prof Brian D Ripley)
Date: Fri, 12 Jan 2001 08:22:42 +0000 (GMT)
Subject: [Rd] segmentation fault in integrate (PR#812)
In-Reply-To: <Pine.LNX.4.21.0101120814190.31983-100000@artemis.imbe.med.uni-erlangen.de>
Message-ID: <Pine.GSO.4.31.0101120821550.3062-100000@auk.stats>

On Fri, 12 Jan 2001, Torsten Hothorn wrote:

>
> > I tried to integrate numerically a function wich is similar to the
> > following:
> >
> > > dummy <- function(x) { exp(-1*x) * dnorm(x) }
> > > dummy(-100)
> > [1] 0
> > > dummy(-1000)
> > [1] NaN
> > > dummy(-10000)
> > [1] NaN
> >
> > If I choose the lower boundary to be too small integrate causes a
> > segmentation fault:
> >
> > > library(integrate)
> > > integrate(dummy, -100, 0)$value
> > [1] 1.387143
> > > integrate(dummy, -1000, 0)$value
> > [1] NaN
> > Warning message:
> > Ifail=1, maxpts was too small. Check the returned relerr! in: adapt(1,
> > lower, upper, minpts, maxpts, functn, eps, ...)
> > > integrate(dummy, -10000, 0)$value
> > Segmentation fault
>
> Running Version 1.2.0 Patched (2001-01-07) and integrate Version  2.2-3
> on SuSE 6.4 gives:
>
> > integrate(dummy, -1000000, 0)$value
> [1] NaN
> Warning message:
> Ifail=1, maxpts was too small. Check the returned relerr! in: adapt(1,
> lower, upper, minpts, maxpts, functn, eps, ...)
>
> I has not able to reproduce the segfault ;-)
>
> BTW: if one uses
>
> > dummy <- function(x) { a <-  exp(-1*x) * dnorm(x); ifelse(is.na(a), 0,
> a) }
>
> this returns
>
> > integrate(dummy, -100, 0)$value
> [1] 1.387143
> > integrate(dummy, -1000, 0, maxpts=1000)$value
> [1] 0.6935715

Better, try

dummy <- function(x) { exp(-1*x + dnorm(x, log=TRUE))}

that is, do your calculations on log scale as far as possible.  This is
more accurate than cop-ing out at Inf*0.

Even better, you can adjust the normal mean to cancel the linear term
exactly: dnorm(x, mu) has linear term -mu*x. So use

dummy <- function(x) exp(0.5)*dnorm(x, -1)

You will still need to adjust maxpts or find a sensible lower end.

(BTW, I don't get a segfault either, and we need you to debug it
on your system, I am afraid.)


-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] segmentation fault in integrate (PR#812)
Prof Brian D Ripley