# [R] convolution of the double exponential distribution

Matthias Kohl Matthias.Kohl at stamats.de
Fri Dec 23 16:08:30 CET 2005

```Duncan Murdoch schrieb:

>On 12/22/2005 7:56 PM, Bickel, David wrote:
>
>
>>Is there any R function that computes the convolution of the double
>>exponential distribution?
>>
>>If not, is there a good way to integrate ((q+x)^n)*exp(-2x) over x from
>>0 to Inf for any value of q and for any positive integer n? I need to
>>perform the integration within a function with q and n as arguments. The
>>function integrate() is giving me this message:
>>
>>"evaluation of function gave a result of wrong length"
>>
>>
>
>Under the substitution of y = q+x, that looks like a gamma integral.
>The x = 0 to Inf range translates into y = q to Inf, so you'll need an
>incomplete gamma function, such as pgamma.  Be careful to get the
>constant multiplier right.
>
>Duncan Murdoch
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>
>

Hi,

you can use our package "distr".

require(distr)
## define double exponential distribution
loc <- 0 # location parameter
sca <- 1 # scale parameter

rfun <- function(n){ loc + scale * ifelse(runif(n) > 0.5, 1, -1) * rexp(n) }
body(rfun) <- substitute({ loc + scale * ifelse(runif(n) > 0.5, 1, -1) *
rexp(n) },
list(loc = loc, scale = sca))

dfun <- function(x){ exp(-abs(x-loc)/scale)/(2*scale) }
body(dfun) <- substitute({ exp(-abs(x-loc)/scale)/(2*scale) }, list(loc
= loc, scale = sca))

pfun <- function(x){ 0.5*(1 + sign(x-loc)*(1-exp(-abs(x-loc)/scale))) }
body(pfun) <- substitute({ 0.5*(1 +
sign(x-loc)*(1-exp(-abs(x-loc)/scale))) },
list(loc = loc, scale = sca))

qfun <- function(x){ loc - scale*sign(x-0.5)*log(1 - 2*abs(x-0.5)) }
body(qfun) <- substitute({ loc - scale*sign(x-0.5)*log(1 - 2*abs(x-0.5)) },
list(loc = loc, scale = sca))

D1 <- new("AbscontDistribution", r = rfun, d = dfun, p = pfun, q = qfun)
plot(D1)

D2 <- D1 + D1 # convolution based on FFT
plot(D2)

hth,
Matthias

--
StaMatS - Statistik + Mathematik Service
Dipl.Math.(Univ.) Matthias Kohl
www.stamats.de

```