Multivariate Guassian Distribution
Show older comments
- I want to learn Multivariate Gussian distribution so I written the following code.*I am implementing following formula http://upload.wikimedia.org/math/a/0/a/a0ad9db46854c4a616ce6959095cf21d.png
Iam expecting this type of plot
Where I am doing mistake?
clear all
clc
% Taking two guassian random variables
x=randn(1000,1);
y=randn(1000,1);
X=[x y];
X=X';
d=size(X,1);
% find means of x,y
mx=mean(x);
my=mean(y);
mumat=[mx my]';
mumat=repmat(mumat,1,size(X,2));
Dif_mat=X-mumat;
% The above step (Dif_mat) is (X-mu) in the formula
cov_mat=cov(X'); % covariance matrix
det_cv=det(cov_mat); % det of cov matrix
inv_cov=inv(cov_mat); % inverse of cov matrix
% scale term before exp in forumala
scale=((2*pi)^(d/2))*sqrt((abs(det_cv)));
scale=inv(scale);
% Mahabolis distance in formula
MB=Dif_mat'*cov_mat*Dif_mat;
% find the final probability
p=scale*exp((-1/2)*MB);
surf(x,y,p)
Answer this question please!
Accepted Answer
More Answers (1)
UJJWAL
on 26 Sep 2011
0 votes
Hi RaviTeja,
Unfortunately in my knowledge there is no known single line statement to manually evaluate the multivariate normal distribution. You will have to create a meshgrid and go through the meshgrid to evaluate the function at every pair of x and y and store it correspondingly and then plot it. The problem with the above code are many and as I have told that there is no single line statement, so it is not really important to debug the above code. For Example look at the part where you have defined X. It does not fit in anyway as in the real relation.
In short the straight single statement implementation is not possible. Use a function mvnpdf(). It will find the pdf for you and actually internally it also implements in the same manner.
For more information mail me back or leave a message. Hope this helps
Happy to help
UJJWAL
3 Comments
UJJWAL
on 26 Sep 2011
If you try to understand the reason there is no single line statement, then think like this :-
Look at the equation for the MVN distribution. Now there is the term X-mu. X corresponds to each pair of combination of x and y. However when we evaluate 2-D or 3-D or multivariate functions in MATLAB using meshgrid we basically manipulate the whole meshgrids (Meshgrid basically saves us from the for loops by providing all combinations of x and y). However in this case the situation is slightly different . You need to calculate X-mu and pre-multiply and post-multiply it to the inverst of the covariance matrix.
Now the problem that occours is that when you type X-mu then it considers the whole meshgrid and tends to subtract mu from it and it results in dimension mismatch error. If you put X=[x y] like in your example, you are not basically taking all possible pairs of x and y.so that method does not work. You might think of getting a meshgrid and repmat the mu vector to the size of meshgrid and then proceed but during the evaluation it wont allow you to look at individual entries in meshgrid matrices. It will manipulate the matrices as a whole .So you dont use that approach.
Basically the problem is that during plotting or evaluation MATLAB does not allow you to handle the individual entries in the vectors or matrices which you really need in this case. So you will have to do it by using loops. Or use the mvnpdf which is a much better and elegant way to do it .
Raviteja
on 26 Sep 2011
UJJWAL
on 27 Sep 2011
Belo Andrew has posted a code. He is also essentially doing the same thing as he has to calculate for all the possible pairs of x and y. You have to use the for loop to do that.
I hope that code will be useful. Else mail back.
Categories
Find more on Inverse Gaussian Distribution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!