MATLAB: Convolution of two probability density functions

convolutionMATLABprobability density functions

I am considering two exponential probability distribution functions with mean equal to 5 and 3.
pdf1=@(x)exppdf(x,5);
pdf2=@(x)exppdf(x,3);
I want to compute the pdf of the sum of these two densities using convolution:
x=0:100;
uh=conv(pdf1(x),pdf2(x));
Since the step-size of x is one, uh should be a density and the area under uh should be one. But
trapz(0:200,uh)
yields 1.26.
But when I choose x=0:0.1:100;
trapz(0:0.1:200,uh)*0.1
yields 1.026.
So, it turns out that the accuracy of 'using 'conv' to the get the density of the sum of two independent random variables' depends heavily upon the support chosen. Am I doing it correct or there is something wrong with my approach. Is there an automatic way to select a reasonable support for the convolution of two densities.

Best Answer

Hello Abhinav
pdfs are continuous functions, so the closer spaced your x points are, the closer you get to the expected answer. In the following code the smaller you make the array spacing delx, the closer you get to the expected answer (although for delx = .0001 it takes awhile).
n1 = 3; n2 = 5;
delx = .001;
xmax = -ceil(log(1e-10)*n1)
x = 0:delx:xmax;
a1 = exp(-x/n1)/n1;
a2 = exp(-x/n2)/n2;
trapz(x,a1) % norm
trapz(x,a2.*x) % mean
trapz(x,a2)
trapz(x,a2.*x)
uh = conv(a1,a2)*delx;
x3 = 0:delx:2*xmax;
plot(x3,uh)
trapz(x3,uh)
trapz(x3,x3.*uh)
I don't have the exppdf function so I used the equivalent.
Trapz, primitive as it is, shows the trend, but it would be much preferable if Mathworks supported something better in basic Matlab like integration by cubic spline.
APPENDED
[1] Convolution of exponential pdfs
In the case of the convolution of n exponential pdfs, all of whose decay constants are unique (no repeats): let 'a' be the vector of decay constants. The kth pdf is
(1/a(k))*exp(-x/a(k)).
The convolution of all n pdfs is the sum over k of
c(k)*(1/a(k))*exp(-x/a(k)), where
c(k) = a(k)^(n-1) / [ (a(k)-a(1))*(a(k)-a(2)) ...(a(k)-a(n)) ]
In the denominator the factor (a(k) - a(k)) = 0 is excluded, so there are n-1 factors in all.
[2] Convolution by fft
For a convolution of n general pdfs, let x be a row array of N equally spaced points with spacing delx, where the range of x is wide enough that all pdfs die down to very small values at the upper and lower ends of the x array. Let M be an (n x N) matrix of the pdfs stacked on top of each other. Then the convolution is
y = real(ifft(prod(fft(M,[],2))))*delx^(n-1);
In other words take the fft of each pdf down the rows, multiply them all together down the columns and transform back. For n = 10 and N = 2e6 points this takes less than 2 seconds on my PC and it is basically linear in N (as long as N has lots of divisors).