Recursion to compute convolution

Hello,
consider the sum of i.i.d. random variables with :
Is it possible to implement this recursively?
If it is, can anybody tell me how I can do that or give at least a hint?

4 个评论

If you want convolution, simply use the built-inconv() function. Don't do it manually - the hard way.
Thank you. But how do I use it here? conv() takes only to input arguments?
I don't know what that formula is, especially the index for P which is S(n-1)==(k - j), but I'm just going by what you say - that you want convolution. There is a built in function for that, that takes several arguments. First one is the main signal. Second argument is the filter weights of the scanning/sliding filter window. Third argument is whether you want the full convolution, only the valid elements, or an output that is the same size as the input signal. Look up the documentation and online tutorials about what convolution is. Find one that has diagrams that show the window as it slides along -- I'm sure there are lots of convolution tutorials out there.
I am not sure we are talking about the same thing. If I want to compute the probability of a random variable and know its distribution, I can easy compute it. The distribution of sum of random variable is computed using convolution. For example take . Therefore:
The second factor would be . How do I use conv() here?

请先登录,再进行评论。

 采纳的回答

Bruno Luong
Bruno Luong 2020-11-15
编辑:Bruno Luong 2020-11-15
Here is a hint, you must replace XXX and YYY with something (after all it's your homework)
function P = Psum(n, k, alpha)
if k < n
error('not valid parameter');
end
P1 = @(j) 1/j^alpha - 1/(j+1)^alpha;
if n == 1
P = XXX;
else
P = 0;
for j=1:k-n+1
P = P + YYY *P1(j);
end
end
end

20 个评论

Thank you for answer. So in the case n==1, XXX=PSum(k-j)^P1(j).
In the other case I get: YYY=PSum(k-j). Is that a possible way which works in Matlab?
Wrong in both.
You should get (I think)
>> Psum(7,10,2)
ans =
0.1250
Why do you set P=0 in the else part? I has to be YYY=PSum(n-1,k-j,alpha)
XXX=P1(k)
You are now good!
Thank youi very much. Can you explain me why you resest P=0 in the else part?
To make a P = P + ... in the for-loop returns the right sum at the end.
Last question. Why do you put in P1(k) and not j? j would not work. But what is the logic behind this?
For n = 1, the code must return P(S1 = k) that is P(X = k). You don't sum on j (or you sum on single j=k if you insist)
Thank you very much:) Have a nice day.
If I use this code to compute different convolutions in an other program, I get the following error:
Out of memory. The likely cause is an infinite recursion within the program.
Why do I get an infinte recursion?
Do you call with n<=0? or not integer?
Yes it happens. Should I add: if(n<=0) return;?
function P = Psum(n, k, alpha)
if k < n
error('not valid parameter k');
end
P1 = @(j) 1/j^alpha - 1/(j+1)^alpha;
if n == 0
% just do it, I have no desire to explain it
P = double(k == 0);
else
P = 0;
for j=1:k-n+1
P = P + Psum(n-1, k-j, alpha)*P1(j);
end
end
end
Sorry this leads to a negative proability
Sorry I get the same.
I give here a non-recursive solution using CONV suggested by Paul
%
function P = Psum_nonrecurse(n, k, alpha)
% n scalar, k can be vector
if (n < 1) || any(k < n)
error('not valid parameter');
end
P1 = @(j) 1./j.^alpha - 1./(j+1).^alpha;
M = P1(1:max(k)-n+1);
P = M;
for i=1:n-1
P = conv(P, M);
end
P = P(k-n+1);
end
Thank you very much for your help. Can you explain to me what the for loop does?
When you have two independent integer-discrete random variables X and Y, with repectively PDF A and B, the pdf of random variable X+Y is the convolution of A and B:
conv(A,B)
With that in mind, the for-loop computes then sequentially the pdf of
S(i+1) = S(i) + X(i+1).
If you only need to calculate P(S(n)=k) for a scalar k, this methods computes a lot of things and throw away a big part, as oppose to the recursive method. However if you need for a bunch of k, use CONV is a better approach. This function can be called with a vector k (but same n), not the recursive method that can only deal with scalar k.
Sorry for the late answer. I think I get your point. Thank you very much. I appreciate your help:)

请先登录,再进行评论。

更多回答(2 个)

If dist is your distribution, then the distribution of the sum would be
distSum = conv(dist, dist, 'full');
No recursion needed.

3 个评论

What should I fill in for dist?
That is your probability distribution function for your random variable. Just one, not the distribution of the sum of the two but just one alone. For example for a uniform distribution it would be all ones, like
n = 500; % Whatever.
dist = ones(n, 1) / n;
subplot(2, 1, 1);
plot(dist, 'b-', 'LineWidth', 2);
title('PDF (Histogram) of One Variable', 'FontSize', 15);
ylim([0, 0.0025]);
grid on;
distSum = conv(dist, dist, 'full');
subplot(2, 1, 2);
plot(distSum, 'b-', 'LineWidth', 2);
grid on;
title('PDF (Histogram) of Sum of Two Variables', 'FontSize', 15);
where n is the resolution you want to digitize your continuous PDF at.
Looks like you've accepted Bruno's answer, so I guess you got it all solved now.

请先登录,再进行评论。

Paul
Paul 2020-11-15
编辑:Paul 2020-11-15
Let the vector M be the distribution of integer valued X. For example, if X takes on values 1-6 uniformly, then
M = ones(1,6)/6;
Now you can compute the distribution of the sum of the random sample of X as:
Mn = M;
for ii = 2:n
Mn = conv(Mn,M);
end
This isn't recursive, so I'm not sure if that's the implementation you want. Also, it may not be efficient depending on the values of numel(M) and n because Mn is growing each time through the loop. But I think it gives you the result you seek. You'll have to be careful the indexing of Mn if X can take on values less than 1.

1 个评论

Paul
Paul 2020-11-15
编辑:Paul 2020-11-18
Assuming X can only take on values j, j >= 1, then M(j) = P(X == j). If j can take on values < 1, then you'll have to do some reindexing because Matlab uses only 1-based indexing.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by