Thread Subject: random sample

Subject: random sample

From: wang wang

Date: 12 Jun, 2008 09:07:02

Message: 1 of 4

Hi,everyone~How to generate a random sample from a
specific density function using matlab?

Subject: random sample

From: ImageAnalyst

Date: 12 Jun, 2008 11:17:20

Message: 2 of 4

On Jun 12, 5:07=A0am, "wang wang" <pli...@gmail.com> wrote:
> Hi,everyone~How to generate a random sample from a
> specific density function using matlab?
-----------------------------------------------------
Short answer: construct the cumulative distribution function of your
desired distribution y=3Dcdf(x). Then "invert" it to get x=3D"cdf-1"(y),
then plug in a y gotten from the rand() function to get an x that
would be as if it were generated from your desired pdf.

Subject: random sample

From: wang wang

Date: 13 Jun, 2008 09:14:02

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in
message <0552cab7-b53e-4473-ad6a-
02413d924362@27g2000hsf.googlegroups.com>...
> On Jun 12, 5:07=A0am, "wang wang" <pli...@gmail.com>
wrote:
> > Hi,everyone~How to generate a random sample from a
> > specific density function using matlab?
> -----------------------------------------------------
> Short answer: construct the cumulative distribution
function of your
> desired distribution y=3Dcdf(x). Then "invert" it to
get x=3D"cdf-1"(y),
> then plug in a y gotten from the rand() function to get
an x that
> would be as if it were generated from your desired pdf.

Thank you imageanalyst.But I really don't understand the
method. I am a beginner,would you mind giving me some
suggestion about the references or papers about this
method?

Subject: random sample

From: us

Date: 13 Jun, 2008 10:01:05

Message: 4 of 4

"wang wang":
<SNIP random sampling evergreen...

> Hi,everyone~How to generate a random sample from a
> specific density function using matlab?

one of the many solutions is outlined below (copy/paste)
in addition, if you own the stats tbx, look at

     help randsample;

% the snippet
% the data
     nr=100000; % <- #samples
     d=[.1,.5,.1,.3]; % <- the distribution: sum==1!
     ds=cumsum([0,d]);
% - show dist
     subplot(2,1,1);
     stairs(ds(2:end),'o-');
     set(gca,...
          'xlim',[.5,numel(d)+.5],...
          'xtick',1:numel(d),...
          'xticklabel',d);
     ylabel('cumulative frequency');
     title('distribution');
% the engine
% - get rands according to dist
     [n,ix]=histc(rand(1,nr),ds);
% - the random numbers according to your dist
     rn=d(ix);
% - prepare the hist
     n=n(1:end-1); % we never get rands == 1
     ns=cumsum(n);
     nsm=max(ns);
     n=n./nsm;
     ns=ns./nsm;
% the result
     subplot(2,1,2);
     bar(n);
     hold on;
     line(1:numel(n),ns,...
         'marker','s',...
         'markerfacecolor',[0,0,0],...
         'color',[0,0,0]);
     set(gca,'xticklabel',d);
     xlabel('probability distribution');
     ylabel('frequency / cumulative frequency');
     title(sprintf('%d samples',nr));

us

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
code us 13 Jun, 2008 06:05:07
evergreen us 13 Jun, 2008 06:05:07
rand us 13 Jun, 2008 06:05:07
randsample us 13 Jun, 2008 06:05:07
cumsum us 13 Jun, 2008 06:05:07
histc us 13 Jun, 2008 06:05:07
subplot us 13 Jun, 2008 06:05:07
plot us 13 Jun, 2008 06:05:07
line us 13 Jun, 2008 06:05:07
rssFeed for this Thread

Contact us at files@mathworks.com