Is this a convolution of two PDFs?

Viewed 445

This is my first question on the site! I want to get the sum of two (or more) random variables, so I did this

from scipy.stats import exponweib
import numpy as np
import matplotlib.pyplot as plt

# Parameters
shape, scale, delta = 1.3, 12, 1e-2

dist = exponweib(a=1, loc=0, c=shape, scale=scale)
grid = np.arange(0, 100, delta)
pmfs = dist.pdf(grid)*delta

# Do convolution (?)
def conv(a,b):
    x = np.array([sum(a[:i+1]*b[i::-1]) for i in range(len(a))])
    return x

# Loop to convolve over multiple RVs
c = {1: pmfs}
for i in range(2, 4):
    c[i] = conv(c[i-1], pmfs)

plt.plot(grid, c[1])
plt.plot(grid, c[2])
plt.plot(grid, c[3]);

So, this gets me this, which looks like what I want, but it is super slow to run.

I want to implement this equation to sum two independent random variables and get their discretized PDF. Other questions suggested to use scipy.signal.fftconvolve

x = fftconvolve(pmfs, pmfs, 'same')
plt.plot(grid, x);

But it got me this. Why is it different?

1 Answers

You have to have zeroes on the left side (and right side) when computing PDF grid, otherwise F-image won't be computed properly

After running the code below

import numpy as np
from scipy import signal
from scipy.stats import exponweib
import matplotlib.pyplot as plt

shape, scale, delta = 1.3, 12.0, 1.0e-1
dist = exponweib(a=1, loc=0, c=shape, scale=scale)

grid = np.arange(-50, 100, delta)
ewei = dist.pdf(grid)*delta

plt.plot(grid, ewei)
plt.show()

# %%
t = signal.fftconvolve(ewei, ewei, 'same')
plt.plot(grid, t)
plt.show()

I got nice picture for convolved distribution

enter image description here

Related