【问题标题】:how to plot joint distribtuion of 2 random variable having 1000 data如何绘制具有 1000 个数据的 2 个随机变量的联合分布
【发布时间】:2022-01-21 08:08:10
【问题描述】:

这是我为生成两个随机变量的概率分布而编写的代码。现在我想绘制 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)

我正在努力寻求帮助..请任何帮助..我已经尝试了一个多月 谢谢..

【问题讨论】:

  • 在你的代码中没有叫 s1 和 s2。
  • 计算联合概率分布,然后绘制它?基本上你只是分别计算 x1 的 pd 和 x2 的 pd ......
  • 你是 matlab 的吗?就这么说吧
  • 我回答假设您想要 matlab 并且没有 pdf 的关闭形式

标签: matlab math statistics probability bayesian


【解决方案1】:

我了解您的联合 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);

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 2021-10-07
    • 1970-01-01
    • 2014-01-06
    • 2021-06-23
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多