Code covered by the BSD License  

Highlights from
Multivariate Gaussian Distribution

3.94737

3.9 | 19 ratings Rate this file 54 Downloads (last 30 days) File Size: 2.05 KB File ID: #5984

Multivariate Gaussian Distribution

by Timothy Felty

 

05 Oct 2004 (Updated 18 May 2007)

Calculates samples from a multivariate Gaussian distribution.

| Watch this File

File Information
Description

Creates a number of samples from a specified number of dimensions and centers them around a given mean, and within a given covariance range. You might not find it very useful, but hey, I need something to do this so why not.

To use:
You need to generate 1000 samples from a 3 dimensional Gaussian distribution with a mean m = [4,5,6], and with a covariance sigma = [9 0 0;0 9 0;0 0 9].

Command line:
x=mgd(1000,3,m,sigma) or x=mgd(1000,3,m',sigma)
it doesn't matter if the mean is given as a row or column vector

x is a (1000x3) matrix of the where
where each row is the coordinates of that point in 3 space.

Acknowledgements
This submission has inspired the following:
Gaussian Random Samples Generation
MATLAB release MATLAB 6.1 (R12.1)
Other requirements As far as I know it will work as long as you have randn function. Send me an email if it doesn't.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (22)
01 Feb 2005 Bob Sturm

Works good, except when I want only a few realizations. Freezes when I ask for 1x1 result.

02 May 2005 lei fang

very good, very convinient to use. helpful to finish course project

24 Sep 2005 mohamad ayat

very good but I couldn't understand it's algorithm

28 Nov 2005 yc wang

in 1-D normal distribution, when sigma=0, it doesn't work.

08 Dec 2005 Gee Didilly

pa pa ooo mow mow

09 Dec 2005 Seblegit SIrgaga

Please try to write the program with out a for loop. Matlab is a good software for matrix manupulation. FOr loops make programs slower. Just use,
x=x*sqrt(variance)+ones(N,1)*mean(x)

31 Jan 2006 Daniel Mark

Thank you so much for your code!

Daniel

09 Feb 2006 Timma Gupta

thank you..!

15 Mar 2006 hicham laanaya

thanks a lot!

28 Mar 2006 ahmed ased  
24 Apr 2006 Sherman Kuo

Excellent Work !!!

09 Sep 2006 Matteo Zandi

Works even with d=2, thanks

13 Sep 2006 Dan McClanahan

Code works well, except....
Generating 2D gaussian works great when I create a vector of samples. However, let's say I want to generate N samples but rather than generating all N at once I make N successive calls to the mgd function. Here, the function breaks down.

14 Sep 2006 Tim Felty

Dan McClanahan: The problem with mean stems from the fact that the code subtracts off the current sample mean assuming that its going to be two dimensional as well.

The covariance issue seems to be cause by the single sample getting multiplied by the covariance matrix. As to why this is causing problems I'm not to sure. But since that is the case it might be better to generate your samples directly from the distribution.

14 Sep 2006 Tim Felty

Dan McClanahan: Actually upon further testing in the situation you describe the error seems to be cause only by the failure to account for the calculated mean of one sample in the code being a scalar.

26 Sep 2006 hendri hendri  
18 Sep 2007 Tom Brady

A waste of time trying to use this file. Better write it yourself. It generates totally wrong distributions. DO NOT WASTE YOUR TIME.

22 Sep 2007 maria papadopoyloy  
22 Nov 2007 M Taj

DO NOT WASTE YOUR TIME. The code looks wrong

15 Aug 2011 Ahmed Fasih

Use "x=bsxfun(@plus, x, rmean)" instead of repmat.

20 Sep 2011 Xiu

is mgd a built-in matlab function?

14 Oct 2011 Krishna Kumar Kottakki

excellent.....thanks allot.

Please login to add a comment or rating.
Updates
11 Oct 2004

I have updated, I made some small changes to save memory that I was using carelessly, and fixed some spelling. Stuff like that. Still works the same way.

13 Oct 2004

I have updated, I made some small changes to save memory that I was using carelessly, and fixed some spelling. Stuff like that. Still works the same way.

05 Dec 2005

The older version had problems when creating distributions in a large number of dimensions. I rewrote the code to use a different method. The whitening transform used earlier was causing errors. This should be a drop in replacement.

12 Dec 2005

There was a review that says I should get rid of the for loops. So I did the job with repmat. Should just drop in to any existing uses of mgd.m

06 Feb 2006

A user emailed me about a better way of getting the covariance. Thanks Alan. The new method requires the variance matrix to be positive-definite, however if the variance is not, it will use the old method.

21 Mar 2006

Repost: Something went wrong last month
A user emailed me about a better way of getting the covariance. Thanks Alan. The new method requires the variance matrix to be positive-definite, however if the variance is not, it will use the old method.

14 Sep 2006

Due to notes from Matteo and Dan, I edited the code so that it doesn't subtract of the mean initial sample mean. The returned mean will be off as much as randn is for the sample size you are using. Fixes bug brought up by Dan. Thanks Dan and Matteo.

18 May 2007

Added some error checking that was lacking. Updated to gracefully handle the one sample issue. Updated the comments.

Tag Activity for this File
Tag Applied By Date/Time
statistics Timothy Felty 22 Oct 2008 07:32:32
probability Timothy Felty 22 Oct 2008 07:32:32
gaussian distribution Timothy Felty 22 Oct 2008 07:32:32
multivariate Timothy Felty 22 Oct 2008 07:32:32
probability Wes Davis 18 Sep 2009 11:29:50
gaussian distribution ismail 11 Mar 2010 18:41:57
probability ismail 11 Mar 2010 18:42:00
statistics ismail 11 Mar 2010 18:42:02
multivariate Selwin kao 13 Oct 2010 16:18:00
multivariate Juanjo 01 Nov 2010 09:56:22
gaussian distribution Krishna Kumar Kottakki 27 Jan 2012 12:04:58
gaussian distribution Xiaoming 08 Mar 2012 07:43:44

Contact us at files@mathworks.com