Thread Subject: Generating random numbers (multivariate normal distribution)

Subject: Generating random numbers (multivariate normal distribution)

From: Tomaz

Date: 10 Nov, 2009 10:07:01

Message: 1 of 2

I allready tried to get an answer to this number generating question regarding multivariate normal distribution, but did not get any final answer. I believe I was unlcear so I will try to ask all you guys about this in more direct way.

Let's say I had 'build' multivariate normal distribution from my dataset and is characterized like this:

mu = [0 0];
Sigma = [.25 .3; .3 1];
x1 = -3:.2:3; x2 = -3:.2:3;
[X1,X2] = meshgrid(x1,x2);
F = mvnpdf([X1(:) X2(:)],mu,Sigma);
F = reshape(F,length(x2),length(x1));
surf(x1,x2,F);
caxis([min(F(:))-.5*range(F(:)),max(F(:))]);
axis([-3 3 -3 3 0 .4])
xlabel('x1'); ylabel('x2'); zlabel('Probability Density');

Now I have to generate new record for my dataset and I have x1 given (let's say it's x1=0.5). How can I get the corresponding distribution for x2 (given that x1=0.5) and then generate (sample) new values for it? Thanks for your help, this is really important for me.
 

Subject: Generating random numbers (multivariate normal distribution)

From: Tom Lane

Date: 10 Nov, 2009 19:07:28

Message: 2 of 2

>I allready tried to get an answer to this number generating question
>regarding multivariate normal distribution, but did not get any final
>answer. I believe I was unlcear so I will try to ask all you guys about
>this in more direct way.
>
> Let's say I had 'build' multivariate normal distribution from my dataset
> and is characterized like this:
...
> Now I have to generate new record for my dataset and I have x1 given
> (let's say it's x1=0.5). How can I get the corresponding distribution for
> x2 (given that x1=0.5) and then generate (sample) new values for it?
> Thanks for your help, this is really important for me.

This problem can be worked out analytically. Doing it that way doesn't
really have anything to do with MATLAB.

However, if you want to play around with MATLAB, here are some things you
could do.

1. You could evaluate a slice of the multivariate pdf at x1=0.5:

   f = @(x2) mvnpdf([repmat(.5,size(x2(:))),x2(:)],mu,Sigma);
   xx = linspace(-3,3);
   plot(xx,f(xx))

2. You could note that this isn't really a probability density, because it
doesn't integrate to 1. But you could make a new function that is
approximately a density.

   quad(f,-5,5)
   g = @(x) f(x)/.4839;
   line(xx,g(xx),'color','r')

3. Forgetting for the moment that this function could be worked out
analytically, you could use the slicesample function to generate random
numbers from it. It turns that the f function that doesn't integrate to 1
would be adquate for this purpose. You can see that the numbers look
reasonably like a sample from the distribution with density g.

   newx = slicesample(0,2000,'pdf',f);
   [yks,xks] = ksdensity(newx);
   line(xks,yks,'color','g')

-- Tom

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
random number g... Tomaz 10 Nov, 2009 05:09:02
multivariate Tomaz 10 Nov, 2009 05:09:02
conditional dis... Tomaz 10 Nov, 2009 05:09:02
rssFeed for this Thread

Contact us at files@mathworks.com