How to find a code for the following algorithm

I want to find the distribution of random variable Z. Suppose X1, X2, ..., Xn are n mutually independent random variables and let Z be their sum:
Z=X1+X2+...+Xn
The distribution of Z can be derived recursively, using the results for sums of two random variables from the following function:
function [answer]=Sum_Of_2_Random_Variables(z)
F=@(y) normpdf(z-y).*normpdf(y);
answer= integral(F,-Inf,Inf);
end
So the algorithm would be as follow:
Algorithm A:
  1. first, define: Y1=X1+X2, and compute the distribution of Y1 ;
  2. then, define: Y2=Y1+X3, and compute the distribution of Y2 ;
  3. and so on, until the distribution of Z can be computed from: Z=Yn=Y(n-1)+Xn
Now I do not know how to write the code for algorithm A. Any help would be appreciated. Thank you.
The main problem I have is the following: Let's say I want to find distribution of Y1=X1+X2 using the following code:
F=@(y)Sum_Of_2_Random_Variables(z-y).*normpdf(y);
answer=integral(F,-Inf,Inf);
This wont work since the input to the function Sum_Of_2_Random_Variables is not of type double.
  • I know that sum of normal random variables is normal random variable, the distribution of random variables Xi's can be any distribution, to simplify the question I chose standard normal distribution. (In my problem the distribution is not normal.)

1 Comment

I just want to know how to call a function including integral recursively.

Sign in to comment.

Answers (1)

Matt J
Matt J on 21 Sep 2013
Edited: Matt J on 21 Sep 2013
It seems like an unnecessarily painful and brute force way to go about this. Rather than using n-fold convolutions, it seems better to use characteristic functions. I.e., take the FFTs of all the pdfs (the characteristic functions of all the Xi), multiply them together, and then take the inverse Fourier transform of the result.

5 Comments

thanks, you are right this is a better way to implement it. But I have problem in implementing it, any help would be appreciated.
lets say these are the characteristic functions:
phi_1(t),....,phi_n(t)
In theory PDF of sum of n independent random variable would be:
f(x)=ifft(phi_1(t)*...*phi_n(t));
but I do not know how to implement it in MATLAB.
Another problem is that MATLAB cannot calculate the inverse Fourier transform of the characteristic functions I am using. So I think it cannot calculate the ift of their product too.
Matt J
Matt J on 22 Sep 2013
Edited: Matt J on 22 Sep 2013
pdfs are always absolutely integrable, so I don't know why you think the IFT cannot be computed.
This the characteristic function:
function [res]=Characteristic_function_Product(sigma1,sigma2,mean1,mean2,t)
res=exp(t*(-t*mean1^2*sigma2^2-t*mean2^2*sigma1^2+2*1i*mean1*mean2)/(2*(t^2*sigma1^2*sigma2^2+1)))*(1/sqrt(t^2*sigma1^2*sigma2^2+1));
end
Now I want to calculate its ift:
syms t
B=Characteristic_function_Product(0.1,3,1,2,t);
syms x
answer=ifourier(B, t, x);
the answer would be:
B =
exp(-(t*((226*t)/25 - 4*i))/((9*t^2)/50 + 2))/((9*t^2)/100 + 1)^(1/2)
answer=fourier(exp(- (226*t^2)/(25*((9*t^2)/50 + 2)) + (t*4*i)/((9*t^2)/50 + 2))/((9*t^2)/100 + 1)^(1/2), t, -x)/(2*pi)
Any help would be appreciated thank you.
Matt J
Matt J on 22 Sep 2013
Edited: Matt J on 22 Sep 2013
There's no clear reason why you need to be doing this symbolically as opposed to numerically. You've already been attempting numerical integration anyway, using the integral() command.
Just sample your pdfs (or your characterisitic functions if you know them) and use fft() and ifft() instead of trying to do analytic forward and inverse fourier transforms.

Sign in to comment.

Products

Asked:

may
on 20 Sep 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!