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

发布于 2014-03-16 22:53:29
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);准确性不是我所期望的,但我将把它留在这里。也许有人能够改进代码。
https://stackoverflow.com/questions/22437218
复制相似问题