[R] Convolution confusion:

peter dalgaard pdalgd at gmail.com
Wed May 18 22:50:57 CEST 2011


On May 18, 2011, at 15:47 , Alex Hofmann wrote:

> Hi,
> 
> I'm new to R, and I'm a bit confused with the "convolve()" function.
> If I do:
> x<-c(1, 2, 3)
> convolve(x, rev(x), TRUE, "open")
> = 9 12 10 4 1
> 
> But I expected: 3 8 14 8 3 (like in Octave/MATLAB - conv(x, reverse(x)) )
> 
> 3 2 1 x 1 2 3
> = 3 2 1
>    0 6 4 2
>    0 0 9 6 3
> = 3 8 14 8 3
> 
> The thing is, that "convolve(x, x, TRUE, "open")" works.
> For me it feels very confusing, that convolution does the reverse itself but the help suggest to reverse it again.
> 
> The help file says: "Note that the usual definition of convolution of two sequences x and y is given by convolve(x, rev(y), type = "o")."
> 
> Thanks for your help,

This confuses me every time as well. One way of putting it is that R's "convolve" is really what others call "correlate": the product-sum between x and y shifted by k, for k=(1-n):(n-1) (adding appropriate padding):

> z <- 1:3
> crossprod(z,z)
     [,1]
[1,]   14
> crossprod(c(z,0),c(0,z))
     [,1]
[1,]    8
> crossprod(c(z,0,0),c(0,0,z))
     [,1]
[1,]    3

Notice that this always comes out symmetric if x==y. 

However in convolution you want sum(x_j, y_(k-j)) so y is used in reverse order.

One way of spotting the issue is that if x represents the distribution of a binary random variable X, then the convolution of x with itself should be the distribution of the sum of two independent such variables. 

> x
[1] 0.05 0.95
> convolve(x,x,type="o")
[1] 0.0475 0.9050 0.0475
> convolve(x,rev(x),type="o")
[1] 0.0025 0.0950 0.9025

... and it is pretty obviously not the case that the sum of two highly skewed distributions is symmetric, so the 2nd line is right.
  
> dbinom(0:2,p=.95,size=2)
[1] 0.0025 0.0950 0.9025

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list