5.0

5.0 | 1 rating Rate this file 158 downloads (last 30 days) File Size: 3.03 KB File ID: #21912

Sampling from a discrete distribution

by Dahua Lin

 

27 Oct 2008 (Updated 28 Oct 2008)

No BSD License  

The function is to draw samples from an arbitrary discrete distribution.

Download Now | Watch this File

File Information
Description

There are a lot of cases that you might need to sample from a discrete distribution in Monte Carlo simulations. Here are some typical examples that this function may help

(1) you want to sample from a discrete distribution over finitely many categories(labels). However, the pmf is not easy to directly sample from. Then you can just compute p(k) for each category, and then use this function to do the sampling.

(2) For a general (possibly non-parametric) continuous distribution defined in a 1D/2D space, you may discretized the sample space into small regions, and compute the probability mass for these regions, and then use this function to do the sampling.

(3) In many models, the probability distribution is expressed as a weighted sum of several "modes". To sample from this kind of distributions, you may need to first choose which mode to generate the sample according to their weights, then this function may fit in.

In sum, this function can be used directly in simple sampling, and may also be used as a building block of complicated simulation procedure.

The use of this function is easy:

x = discretesample(p, n).

You just input the probability mass, and tell the function how many sample you want to sample, then it returns the samples in form of a 1 x n vector.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
28 Oct 2008 Dahua Lin

There's a bug in the initial submission, which has been fixed, and the new version has been uploaded, which may come out in a day.

28 Oct 2008 Jos

if you'd searched before submitting you would have found RANDP on the FEX. What does this function add?

29 Oct 2008 Dahua Lin

Jos,

I have checked with randp. Though they seem offering similar functionalities, however, the efficiency is drastically different, especially in very large scale monte carlo simulation, say you need to draw thousands or millions of samples from a distribution over thousands or even millions of states, which is not unusual in real engineering applications.

With randp, it would be incur obvious latency when you want to draw thousands of samples from thousands of states, or even run out of memory (thus resulting an empty matrix), as the algorithm implemented by randp is of complexity O(k n), where k is the number of states in the sample space. However, even million-state-level sampling can be accomplished by this function within milliseconds, as its complexity is only O(n logk).

Hope my explanation can clarify the differences between these two files.

21 Jun 2009 Carlos Baiz  
05 Nov 2009 Michael Chen

your function seems to complicate the problem a little bit.
The following line is enough to do the jod
[~,x] = histc(rand(1,n),[0;cumsum(p(:))/sum(p)]);

Please login to add a comment or rating.
Updates
28 Oct 2008

Fixing a bug that may lead to incorrect sampling.

Tag Activity for this File
Tag Applied By Date/Time
monte carlo Dahua Lin 28 Oct 2008 09:20:05
simulation Dahua Lin 28 Oct 2008 09:20:05
discrete distribution Dahua Lin 28 Oct 2008 09:20:05
sampling Dahua Lin 28 Oct 2008 09:20:05
monte carlo Yi Zhang 01 Nov 2009 16:51:45
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com