首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Matlab:拒绝采样

Matlab:拒绝采样
EN

Stack Overflow用户
提问于 2014-03-16 21:05:23
回答 1查看 1.5K关注 0票数 3

当我运行蒙特卡洛模拟时,我只想从正态分布的尾部(-5sigma,-3sigma和3sigma,5sigma)中抽样,因此我想到了拒绝抽样。然而,我正在努力在Matlab中实现这一点。到目前为止,我一直在使用与下面的代码类似的代码(我知道这不是拒绝采样),但是拒绝采样是解决这个问题的更好方法吗?

代码语言:javascript
运行
复制
function [new_E11] = elasticmodulusrng()

new_E11 = normrnd(136e9,9.067e9,[1 1]);

while new_E11>=136e9-3*9.067e9 && new_E11<=136e9+3*9.067e9
        new_E11 = normrnd(136e9,9.067e9,[1 1]);
end

谢谢

编辑:在答案中使用代码

EN

回答 1

Stack Overflow用户

发布于 2014-03-16 22:53:29

代码语言:javascript
运行
复制
mu=0
sigma=1
%get scaling factor
scale=(normcdf(5*sigma+mu,mu,sigma)-normcdf(3*sigma+mu,mu,sigma))*2
%define pdf
cpdf=@(x)((1/scale)*normpdf(x,mu,sigma).*((abs(x-mu)<5.*sigma)&(abs(x-mu.*sigma)>3)))
%get cdf via integral
ccdf=@(x)integral(cpdf,mu-5.*sigma,x)
%allow vector inputs
ccdf=@(x)arrayfun(ccdf,x)

%inverse cdf
icdf=@(y)(fzero(@(x)(ccdf(x)-y),.5));
%allow vector inputs    
icdf=@(x)arrayfun(icdf,x);
%icdf is very slow, thus evaluate some numbers and use the cached and interpolated version:
cachedicdf=nan(n+1,1);
x=0:0.01:1;
y=icdf(x);
icdf=@(uni)interp1(x,y,uni);
%plot some example data
hist(icdf(rand(10000000,1)),1000);

准确性不是我所期望的,但我将把它留在这里。也许有人能够改进代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22437218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档