how to plot joint distribtuion of 2 random variable having 1000 data

ⅰ亾dé卋堺 提交于 2019-12-13 04:07:20

问题


here is the code i wrote to generate probability distribtuion of two random variable. now i would like to plot 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)

I am trying hard to get ans..plz any help out there..i have been tryihng over a month Thanks..


回答1:


I understand you don't have close form for your joint pdf, but "only the data". Using Matlab, you can indeed use this tool named 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);

Hope this is of any help.



来源:https://stackoverflow.com/questions/16313949/how-to-plot-joint-distribtuion-of-2-random-variable-having-1000-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!