MATLAB中两个从属分布的卷积

时间:2019-05-15 10:35:09

标签: matlab convolution

假设我有两个离散的随机变量 X Y

X = {1,3,3,5,7,7,7,9,9,9,9,9}

Y = {5,5,9,9,10,12,13}

其经验CDF表示为:

F_x(1) = 0.0833, F_x(3) = 0.25, F_x(5) = 0.33, F_x(7) = 0.5833 and F_x(9) = 1

F_y(5) = 0.2857, F_y(9) = 0.5714, F_y(10) = 0.7143, F_y(12) = 0.8571 and F_y(13) = 1 

假设他们的联合分布是

H(x,y) = F_x(x) * F_y(y)

实际上是X和Y的“假设”是独立的。

如何在MATLAB中计算 Z = X + Y F(z)

注意:为简单起见,我给出了 H(x,y)作为简单乘积函数,但实际上它可以是对X和Y之间的依赖关系建模的任何东西。

1 个答案:

答案 0 :(得分:1)

给出连续概率密度函数 F X F Y ,以及联合概率密度函数 F X,Y ,我们可以将 F X + Y 计算为the integral of FX,Y over the line z=x+y。如果概率密度函数是离散的,则上面的积分应写为 z <= x + 给定的平面部分上的积分的导数y

这在MATLAB中非常简单。让我们从OP的数据开始:

F_x = [0.0833,0.0833,0.25,0.25,0.33,0.33,0.5833,0.5833,1]; % CDF
F_x = diff([0,F_x]); % PDF
F_y = [0,0,0,0,0.2857,0.2857,0.2857,0.2857,0.5714,0.7143,0.7143,0.8571,1]; % CDF
F_y = diff([0,F_y]); % PDF
H = F_x.' .* F_y; % example joint PDF

现在,我们对所有值F_cum(z) = sum(H(x,y))求和z<=x+y,然后取导数F = diff([0,F_cum])

[m,n] = size(H);
F_cum = zeros(1,m+n-1);
for z = 1:numel(F_cum)
   s = 0;
   for x = 1:numel(F_x)
      y = z-x+1;
      y = max(min(y,n),1); % avoid out of bounds indexing
      s = s + sum(H(x,1:y));
   end
   F_cum(z) = s;
end
F = diff([0,F_cum]);

请注意,我们定义了y=z-x+1,即z=y+x-1。因此F(1)对应于z=2。这是可以从两个分布的总和中得出的最低可能值,我们将其定义为从1开始。

可以通过将H填充为零并将每一行移动一个附加元素来简化上述操作。这使矩阵的列上的 z = x + y 线对齐,从而使我们可以使用简单的sum投影:

H = [H,zeros(m)];
for ii=2:m
   H(ii,:) = circshift(H(ii,:),ii-1);
end
F_cum = cumsum(sum(H,1));
F_cum = F_cum(1:end-1); % last element we don't need
F2 = diff([0,F_cum]);

但是由于diff([0,cumsum(F)]) == F(不超过数值精度),我们可以跳过这两个操作:

F3 = sum(H,1);
F3 = F3(1:end-1); % last element we don't need

({all(abs(F-F2)<1e-15)all(abs(F-F3)<1e-16)