如何绘制具有1000个数据的2个随机变量的联合分布

时间:2013-05-01 06:50:43

标签: matlab math statistics probability bayesian

这里是我编写的代码,用于生成两个随机变量的概率分布。现在我想绘制JPD。

clear all;
clc;

x1 = randn(1000,1);
x2 = 10*randn(1000,1);

[count_1, b] = hist(x1, 25);   %25 bins
pd1 = count_1 / length(x1) / (b(2) -  b(1));   % probability distribution function of x1

[count_2, bn] = hist(x2, 25);   %25 bins
pd2 = count_2 / length(x2) / (bn(2) -  bn(1));    % probabitlity distribtuion function of x2

%subplot(2,2,1), plot(x,s1)
%subplot(2,2,2),plot(x,s2)
%subplot(2,2,1),plot(b,pd1)
%subplot(2,2,2),plot(bn,pd2)

我正努力让ans..plz得到任何帮助..我已经尝试了一个多月 感谢..

1 个答案:

答案 0 :(得分:0)

我知道您的联合pdf没有密切的表格,但“只有数据”。使用Matlab,您确实可以使用名为hist3

的工具
% Generate random data
nData = 1e5;
data = zeros(2,nData);
m1 = 0; m2 = 1;
s1 = 1; s2 = 2;
for i=1:nData
    d1 = m1+s1*randn;
    d2 = m2+s2*randn;
    data(:,i) = [d1; d2];
end

% hist3 will bin the data
xi = linspace(min(data(1,:)), max(data(1,:)), 50);
yi = linspace(min(data(2,:)), max(data(2,:)), 50);
hst = hist3(data,{xi yi}); %removed extra '

% normalize the histogram data
dx = xi(2)-xi(1);
dy = yi(2)-yi(1);
area = dx*dy;
pdfData = hst/sum(sum(hst))/area;

% plot pdf
figure(2); clf
contour(xi,yi,pdfData);

希望这有任何帮助。