绘制归一化均匀混合物 [英] Plot normalized uniform mixture

查看:108
本文介绍了绘制归一化均匀混合物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在下面重新生成归一化的密度p(x),但是给出的代码不会生成归一化的PDF.

I need to reproduce the normalized density p(x) below, but the code given does not generate a normalized PDF.

clc, clear
% Create three distribution objects with different parameters
pd1 = makedist('Uniform','lower',2,'upper',6);
pd2 = makedist('Uniform','lower',2,'upper',4);
pd3 = makedist('Uniform','lower',5,'upper',6);
% Compute the pdfs
x = -1:.01:9;
pdf1 = pdf(pd1,x); 
pdf2 = pdf(pd2,x); 
pdf3 = pdf(pd3,x); 
% Sum of uniforms
pdf = (pdf1 + pdf2 + pdf3);
% Plot the pdfs
figure;
stairs(x,pdf,'r','LineWidth',2);

如果我仅通过将它们的总和缩放就可以计算出归一化混合PDF,那么与上面的原始图相比,我具有不同的归一化概率.

If I calculate the normalized mixture PDF by simply scaling them by their total sum, I have different normalized probability comparing with the original figure above.

pdf = pdf/sum(pdf);

推荐答案

混合物

两个随机变量的混合物的可能性为 p 使用分布1,概率为1- p 使用分布2.

Mixture

A mixture of two random variables means with probability p use Distribution 1, and with probability 1-p use Distribution 2.

根据图表,您似乎正在混合分布,而不是添加(卷积)分布.精确结果对混合概率至关重要.例如,我选择了a = 0.25b = 0.35c = 1-a-b.

Based on your graph, it appears you are mixing the distributions rather than adding (convolving) them. The precise results matter very much upon the mixing probabilities. As an example, I've chosen a = 0.25, b = 0.35, and c = 1-a-b.

对于混合物,概率密度函数(PDF)可通过分析获得:
pdfMix =@(x) a.*pdf(pd1,x) + b.*pdf(pd2,x) + c.*pdf(pd3,x).

For a mixture, the probability density function (PDF) is analytically available:
pdfMix =@(x) a.*pdf(pd1,x) + b.*pdf(pd2,x) + c.*pdf(pd3,x).

% MATLAB R2018b
pd1 = makedist('Uniform',2,6);
pd2 = makedist('Uniform',2,4);
pd3 = makedist('Uniform',5,6);
a = 0.25;
b = 0.35;
c = 1 - a - b;    % a + b + c = 1

pdfMix =@(x) a.*pdf(pd1,x) + b.*pdf(pd2,x) + c.*pdf(pd3,x);

Xrng = 0:.01:8;
plot(Xrng,pdfMix(Xrng))
xlabel('X')
ylabel('Probability Density Function')

由于分布是混合的,因此您也可以使用 stairs() 命令:stairs(Xrng,pdfMix(Xrng)).

Since the distributions being mixed are uniform you could also use the stairs() command: stairs(Xrng,pdfMix(Xrng)).

我们可以通过确保总面积为1来验证这是有效的PDF.
integral(pdfMix,0,9)

We can verify this is a valid PDF by ensuring the total area is 1.
integral(pdfMix,0,9)

ans = 1.0000

ans = 1.0000


卷积:添加随机变量

将随机变量加在一起会产生不同的结果.同样,这可以凭经验轻松地完成.从分析上讲,这是可能的.例如,对两个Uniform(0,1)分布进行卷积会产生Triangular(0,1,2)分布.随机变量的卷积只是我们加起来,并且如果您对分析结果感兴趣,可以使用集成的方法来获得结果PDF.


Convolution: Adding Random Variables

Adding the random variables together yields a different result. Again, this can be done empirically easily. It is possible to this analytically. For example, convolving two Uniform(0,1) distributions yields a Triangular(0,1,2) distribution. The convolution of random variables is just a fancy way of saying we add them up and there is a way to obtain the resulting PDF using integration if you're interested in analytical results.

N = 80000;                  % Number of samples
X1 = random(pd1,N,1);       % Generate samples     
X2 = random(pd2,N,1);
X3 = random(pd3,N,1);

X = X1 + X2 + X3;           % Convolution      

请注意x轴(Xrng = 0:.01:16;)的比例更改.

Notice the change of scale for the x-axis (Xrng = 0:.01:16;).

为此,我使用 random() ,然后将它们相加以获得80k个所需卷积的样本.请注意,当我使用 histogram() 我使用了'Normalization', 'pdf'选项.

To obtain this, I generated 80k samples from each distribution with random() then added them up to obtain 80k samples of the desired convolution. Notice when I used histogram() I used the 'Normalization', 'pdf' option.

Xrng = 0:.01:16;
figure, hold on, box on
p(1) = plot(Xrng,pdf(pd1,Xrng),'DisplayName','X1 \sim U(2,6)')
p(2) = plot(Xrng,pdf(pd2,Xrng),'DisplayName','X2 \sim U(2,4)')
p(3) = plot(Xrng,pdf(pd3,Xrng),'DisplayName','X3 \sim U(5,6)')
h = histogram(X,'Normalization','pdf','DisplayName','X = X1 + X2 + X3')

% Cosmetics
legend('show','Location','northeast')
for k = 1:3
    p(k).LineWidth = 2.0;
end
title('X = X1 + X2 + X3 (50k samples)')
xlabel('X')
ylabel('Probability Density Function (PDF)')

您可以使用 fitdist() 获得PDF的估算值和内核分发对象,然后调用

You can obtain an estimate of the PDF using the fitdist() and the Kernel distribution object then calling the pdf() command on the resulting Kernel distribution object.

pd_kernel = fitdist(X,'Kernel')

figure, hold on, box on
h = histogram(X,'Normalization','pdf','DisplayName','X = X1 + X2 + X3')
pk = plot(Xrng,pdf(pd_kernel,Xrng),'b-')           % Notice use of pdf command
legend('Empirical','Kernel Distribution','Location','northwest')

如果执行此操作,则会注意到生成的内核是无界的,但是由于您使用 ksdensity() 函数,尽管采用了概率分布对象方法由于所有功能,对用户更友好您可以直接访问.您应该意识到内核是一个近似值(您可以在内核图中清楚地看到这一点).在这种情况下,对3个均匀分布进行卷积的积分并不算太差,因此,如果需要PDF,则以解析方式找到PDF可能是首选.否则,经验方法(尤其是生成方法)可能就足够了,尽管这取决于您的应用程序.

If you do this, you'll notice the resulting kernel is unbounded but you can correct this since you know the bounds using truncate(). You could also use the ksdensity() function, though the probability distribution object approach is probably more user friendly due to all the functions you have direct access to. You should be aware that the kernel is an approximation (you can see that clearly in the kernel plot). In this case, the integration to convolve 3 uniform distributions isn't too bad, so finding the PDF analytically is probably the preferred choice if the PDF is desired. Otherwise, empirical approaches (especially for generation), are probably sufficient though this depends on your application.

pdt_kernel = truncate(pd_kernel,9,16)

从混合和卷积中生成样本是一个不同的问题(但可管理).

Generating samples from mixtures and convolutions is a different issue (but manageable).

这篇关于绘制归一化均匀混合物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆