从MATLAB中的经验累积分布函数生成随机数(Generate random numbers from empirical cumulative distribution function in MATLAB)
我在MATLAB中通过生成经验累积分布函数
x = [1, 1.2, ..., 1] [myECDF, xi] = ecdf(x);现在我想用这个ECDF作为绘制一组随机数的基础。 例如,我想做这样的事情:
y = random(myECDF, 10); // drawing 10 random numbers from ECDF这在MATLAB中可行吗?
I generated an empirical cumulative distribution function in MATLAB via
x = [1, 1.2, ..., 1] [myECDF, xi] = ecdf(x);Now I would like to use this ECDF as a basis to draw a set of random numbers from. For example, I would like to do something like this:
y = random(myECDF, 10); // drawing 10 random numbers from ECDFIs this possible in MATLAB?
最满意答案
您可以使用反向CDF方法。 生成0到1之间的均匀分布的随机变量,并将它们视为CDF输出。 然后使用您的经验分布来查找相应的值。
function my_x = my_random(myECDF, xi, N) % Generate N uniformly distributed samples between 0 and 1. u = rand(N,1); % Map these to the points on the empirical CDF. my_x = interp1(myECDF, xi, u, 'linear'); endYou can use the inverse CDF method. Generate uniformly distributed random variables between 0 and 1 and treat them as CDF outputs. Then use your empirical distribution to find the corresponding values.
function my_x = my_random(myECDF, xi, N) % Generate N uniformly distributed samples between 0 and 1. u = rand(N,1); % Map these to the points on the empirical CDF. my_x = interp1(myECDF, xi, u, 'linear'); end更多推荐
发布评论