I want to get the mode and standard deviation of the sum of two random variables. For that, I create the discrete representation of the PDFs, and perform a convolution to get the new PDF, from which I calculate the mode and standard deviation.
I am using scipy.signal.fftconvolve
, but as the operation is performed around 400K times, this is taking a lot of time.
I want to use pyFFTW
library to speed things up, but could not make it work. From what I understand I need to:
- Calculate the FFT on both PDFs.
- Multiply the FFTs.
- Calculate the IFFT of the multiplication of FFTs.
To add insult to injury, neither of the distributions is centered around zero, nor is symetrical.
The code of a sample of two PDFs and current convolution can be seen here: https://gist.github.com/ajossorioarana/047202db9c2990b43cf7ba2e02735faf
The key section is the last one:
# Do the convolution with scipy.signal.fftconvolve
convolution_pdf = scipy.signal.fftconvolve(pdf1, pdf2, 'full')
convolution_pdf = convolution_pdf[0:len(pdf1)]
I tried using replacing the code from above with the following code:
## Do the convovlution with pyfftw.builders
builder1 = pyfftw.builders.fft(pdf1)
builder2 = pyfftw.builders.fft(pdf2)
fft1 = builder1()
fft2 = builder2()
builder3 = pyfftw.builders.ifft(fft1*fft2)
convolution = builder3()