How to plot a joint pdf of 2 independent continuous variables?

10 vues (au cours des 30 derniers jours)
Kapil Varne
Kapil Varne le 2 Août 2017
Commenté : Himanshu le 31 Oct 2023
Hey guys, I have data series of 2 continuous random variables, both are independent, I want to plot their joint pdf. I tried using the meshgrid and surf commands but I am not able to succeed. I tried the following code:
load('tut1_3_indpXY.txt')
X = corr(tut1_3_indpXY)
Y = tut1_3_indpXY'
A = Y(1,:)
B = Y(2,:)
M1 = mean(A)
M2 = mean(B)
S1 = std(A)
S2 = std(B)
N1 = pdf('Normal',A,M1,S1)
N2 = pdf('Normal',B,M2,S2)
N1T = N1'
MIN1 = min(A)
MAX1 = max(A)
MIN2 = min(B)
MAX2 = max(B)
I1 = linspace(MIN1,MAX1,200)
I2 = linspace(MIN2,MAX2,200)
[P,Q] = meshgrid(I1,I2)
R = N1T*N2
surf(P,Q,R)
  2 commentaires
Kapil Varne
Kapil Varne le 2 Août 2017
Modifié(e) : Kapil Varne le 2 Août 2017
Here is the code:
load('tut1_3_indpXY.txt')
X = corr(tut1_3_indpXY)
Y = tut1_3_indpXY'
A = Y(1,:)
B = Y(2,:)
M1 = mean(A)
M2 = mean(B)
S1 = std(A)
S2 = std(B)
N1 = pdf('Normal',A,M1,S1)
N2 = pdf('Normal',B,M2,S2)
N1T = N1'
MIN1 = min(A)
MAX1 = max(A)
MIN2 = min(B)
MAX2 = max(B)
I1 = linspace(MIN1,MAX1,200)
I2 = linspace(MIN2,MAX2,200)
[P,Q] = meshgrid(I1,I2)
R = N1T*N2
surf(P,Q,R)
the cyclist
the cyclist le 2 Août 2017
Modifié(e) : the cyclist le 2 Août 2017
Can you upload your text file, so we can try your code? Even better would be the MAT file with the variable.
Also, is there a reason you uploaded the code twice? Is there a difference?

Connectez-vous pour commenter.

Réponses (2)

the cyclist
the cyclist le 2 Août 2017
Modifié(e) : the cyclist le 2 Août 2017
Taking an admittedly quick look, shouldn't your joint pdf be
R = N1T.*N2
rather than what you have?
If you have an older version of MATLAB, you'll need to do
R = bsxfun(@mtimes,N1T,N2)

Himanshu
Himanshu le 31 Oct 2023
Modifié(e) : Himanshu le 31 Oct 2023
I tried to do the same thing, by the raw calculation of gaussian distribution,
But I am facing the below error
close all;
clear all;
% sigma of i is more that q, they are independent
sigma_q = 1;
sigma_i = sqrt(2);
x =-4:0.1:4;
y =-4:0.1:4;
[X, Y] = meshgrid(x,y);
pdf_i = (1/sqrt(2*pi)/sigma_i)*exp(-x.^2/(2*sigma_i^2));
pdf_q = (1/sqrt(2*pi)/sigma_q)*exp(-y.^2/(2*sigma_q^2));
joint = pdf_i.*pdf_q;
contour(X,Y,joint);
Error using contour
Z must be at least a 2x2 matrix.
surf(X,Y,joint);
  1 commentaire
Himanshu
Himanshu le 31 Oct 2023
Never Mind!
I was able to get the problem.
After doing the meshgrid, our x, y should the meshed ones, i.e. X, Y.
close all;
clear all;
% sigma of i is more that q, they are independent
sigma_q = 1;
sigma_i = sqrt(2);
x =-4:0.1:4;
y =-4:0.1:4;
[X, Y] = meshgrid(x,y);
pdf_i = (1/sqrt(2*pi)/sigma_i)*exp(-X.^2/(2*sigma_i^2));
pdf_q = (1/sqrt(2*pi)/sigma_q)*exp(-Y.^2/(2*sigma_q^2));
joint = pdf_i.*pdf_q;
%plot(x,joint);
figure();
contour(X,Y,joint);
grid("on");
figure();
surf(X,Y,joint);
grid("on");

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by