Fast Sampling From A Discrete Distribution

版本 1.0.0.0 (3.6 KB) 作者: Peng Liu
This function generates independent random samples according to a prescribed discrete distribution.
242.0 次下载
更新时间 2016/10/14

查看许可证

This function generates independent random samples according to a prescribed discrete distribution. It accepts as inputs the discrete probability mass function, the size of the random matrix to be generated, and an optional input parameter which specifies the data type of the output (default to double). The output is a random matrix.

>> x = randsmpl(p, m, n)
returns an m-by-n matrix x of random samples drawn independently from the input (discrete) distribution specified with pmf p. Suppose that the sample space comprises K samples, then p must be a (row- or column-) vector containing K probability masses summing to 1. The output, x(i,j) = k, k = 1, ..., K, describes that the k-th sample is drawn in the (i,j)-th trial, for i = 1, ..., m and j = 1,...,n. The default output data type of x is 'double'.

>> x = randsmpl(p, m, n, classname)
returns an m-by-n matrix x whose data type is specified by classname. The classname input must be a valid numeric class name which includes the following
'double' (default) | 'single' | 'int64' | 'int32' | 'int16' | 'int8' | 'uint64' | 'uint32' | 'uint16' | 'uint8' |
If classname is not provided, it is default to 'double'.

The implementation of randsmpl is based on the discretize function, which was introduced in R2015a. For backward compatibility, we also provided an interp1 alternative. randsmpl is smart enough that it will automatically determine whether to use discretize or interp1 according to users' MATLAB version.

This function is extremely useful for fast sampling for Monte Carlo simulations. The performance and robustness of the function are highly optimized. Comments and suggestions will be appreciated.

引用格式

Peng Liu (2024). Fast Sampling From A Discrete Distribution (https://www.mathworks.com/matlabcentral/fileexchange/59724-fast-sampling-from-a-discrete-distribution), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2015a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0

more accurate title
Improved documentation
N/A

Better description
N/A