I'd like to compute probability density functions for the sum of independent variables, which is the convolution of the density functions of the variables. Toward that end, I'll make a discrete approximation of each density function and compute the convolution of those approximations. I'll come back to that in a future blog post; for now I'll just work on the convolution.
Discrete convolution can be implemented via the fast Fourier transform, since FT(conv(x, y)) = FT(x)*FT(y), where the multiplication is taken element by element. So just take FT(x), FT(y), multiply them together, and take the inverse transform. Voila, there's your convolution.
In the following implementation of conv, I've taken some details into account. Maxima's fft can handle only inputs whose length is a power of 2. The exact length of the convolution is length(x) + length(y) - 1, so we'll just chop off any extra elements. Maxima's fft has a scaling convention which needs to be adjusted in order to get the correct result. Also, I'll call realpart to chop off any imaginary parts (these can appear due to round-off error).
Discrete convolution via fast Fourier transform
Let's try it out. I'll make a couple of random inputs.
⇒ |
⇒ |
Well, that's plausible. I'll check it more carefully later, but first, is conv symmetric, at least?
⇒ |
Discrete convolution via summation
I'll also define the discrete convolution as a summation; this is slow compared to the fast Fourier transform. There are at least a couple of ways to do that; in naive_conv_1, I'll be careful to define the summation range, and in naive_conv_2, I'll let the summation index go where it will, but I'll protect the inputs against out-of-bounds indices. These two are equivalent -- which you think is more comprehensible is a matter of preference.
Let's try these functions.
⇒ |
That's comforting. If I handled it right, these two functions are equivalent.
⇒ |
Comparing naive and fast convolution implementations
So far, so good. Is the naive implementation the same as the fast convolution function?
⇒ |
Terrific. I'll let that be all for now.
Good stuff.......
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete