MATLAB: Conditioning bivariate gaussian distribution

bivariateconditional probabilityconditional reductiondistributiongaussian distributionmultivariatenormal distributionpdfprobabilityreductionunivariate distribution

Hi,
I have a bivariate normal distribution as follows f(x):
m = 0;
c = [0.5 0.8; 0.8 2.0];
x1 = -4:0.2:4;
x2 = -4:0.2:4;
[X1, X2] = meshgrid(x1,x2);
X = X1(:)';
Y = X2(:)';
fun = @(X, Y) 1/(2*pi*(det(c))^(0.5))* exp(-(0.5)*sum(([X; Y]-m).*(inv(c)*([X; Y]-m))));
i = @(X)integral(@(Y)fun(X,Y),-inf,inf,'ArrayValued',true);
fplot(i)
And this gives output:
hw2q7.jpg
I want to find the f(x / y = 1.5)
I have tried to find f(x) and f(y) and then filter out y = 1.5 to get the x values from the distribution. but this method is not working and giving errors as follows:
mu_x = 0;
c = [0.5 0.8; 0.8 2.0];
x1 = -4:0.2:4;
x2 = -4:0.2:4;
[X1,X2] = meshgrid(x1,x2);
X = X1(:)';
Y = X2(:)';
fun = @(X, Y) 1/(2*pi*(det(c))^(0.5))* exp(-(0.5)*sum(([X; Y]-mu_x).*(inv(c)*([X; Y]-mu_x))));
px = @(X)integral(@(Y)fun(X,Y),-inf,inf,'ArrayValued',true);
py = @(Y)integral(@(X)fun(X,Y),-inf,inf,'ArrayValued',true);
%vq1 = interp1(-3:0.2:3,px,-3:0.2:3)
px = px([-3:0.2:3])
p = [px([-3:0.2:3]); py([-3:0.2:3])]
fplot(py)
The error :
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in pg2>@(X,Y)1/(2*pi*(det(c))^(0.5))*exp(-(1/2)*sum(([X;Y]-mu_x).*(inv(c)*([X;Y]-mu_x)))) (line 109)
fun = @(X, Y) 1/(2*pi*(det(c))^(0.5))* exp(-(1/2)*sum(([X; Y]-mu_x).*(inv(c)*([X; Y]-mu_x))));
Error in pg2>@(Y)fun(X,Y) (line 110)
px = @(X)integral(@(Y)fun(X,Y),-inf,inf,'ArrayValued',true);
Error in integralCalc/iterateArrayValued (line 156)
fxj = FUN(t(1)).*w(1);
Error in integralCalc/vadapt (line 130)
[q,errbnd] = iterateArrayValued(u,tinterval,pathlen);
Error in integralCalc (line 103)
[q,errbnd] = vadapt(@minusInfToInfInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in pg2>@(X)integral(@(Y)fun(X,Y),-inf,inf,'ArrayValued',true) (line 110)
px = @(X)integral(@(Y)fun(X,Y),-inf,inf,'ArrayValued',true);
Error in pg2>dist (line 113)
px = px([-4:0.2:4])
How do i get the values f(x) and f(y) from px and py in range -4:0.2:4 so that i can find f(x / y = 1.5)?

Best Answer

There a few issues with your code.
First INTEGRAL is integration of a scalar function, you cannot integrate a vector function. So in 1D you need to loop in the parameters (x) or (y).
Second parameters ans variable are no longer have the same length, you can not do vercat them.
Here is a modified code
mu_x = 0;
c = [0.5 0.8; 0.8 2.0];
% this block is irrelevant!!!
% x1 = -4:0.2:4;
% x2 = -4:0.2:4;
% [X1,X2] = meshgrid(x1,x2);
% X = X1(:)';
% Y = X2(:)';
fun = @(X, Y) gfun(X,Y,mu_x,c); % nested defined bellow
px = @(X) arrayfun(@(x) integral(@(y)fun(x,y),-inf,inf,'ArrayValued',true), X);
py = @(Y) arrayfun(@(y) integral(@(x)fun(x,y),-inf,inf,'ArrayValued',true), Y);
Pxy = [px([-3:0.2:3]); py([-3:0.2:3])]
close all
plot(Pxy')
%%
function f = gfun(X,Y,mu,c)
[X,Y] = ndgrid(X(:)',Y(:)'); % expanding the scalar to match the vector, regardless which is which
XY = [X; Y];
f = 1/(2*pi*(det(c))^(0.5))* exp(-(0.5)*sum((XY-mu).*(c\(XY-mu))));
end
gaussian.png