使用准蒙特卡罗方法进行对数正态分布的样本

时间:2016-11-26 16:29:23

标签: matlab montecarlo

我想使用准蒙特卡罗方法(如Halton序列)生成对数正态分布的随机样本。我希望数字在一定范围内。我在matlab中有以下代码,但它给出了超出界限的数字,如何限制在边界内生成样本。

M=1;   
bounds=[2 4];
Ns=20; % number of models 
urnd = haltonset(M,'Skip',1e3,'Leap',1e2);
urnd = scramble(urnd,'RR2');
urnd = qrandstream(urnd);
modelCDF = qrand(urnd,Ns);
models = zeros(Ns,M);
models=logninv(modelCDF,0,3);

1 个答案:

答案 0 :(得分:1)

You do not use the bounds in your attempt. You have to squeeze your samples (modelCDF) between the bounds, this can be done by scaling and shifting the [0,1] uniformly distributed sample.

M=1;   
bounds=[2 4];
Ns=1e3; % number of models 

% calculate the probabilities corresponding to the bounds
P_bounds = logncdf(bounds,0,3);

urnd = haltonset(M,'Skip',1e3,'Leap',1e2);
urnd = scramble(urnd,'RR2');
urnd = qrandstream(urnd);
modelCDF = qrand(urnd,Ns);

% scale and shift to cover P_bounds
modelCDF = P_bounds(1) + modelCDF*diff(P_bounds);

% models = zeros(Ns,M); % no need for this
models=logninv(modelCDF,0,3);

% Plot them for visual comparison
[f,c] = hist(models, 30);
f = f./trapz(c,f)*diff(P_bounds); % normalize for comparison with analytical pdf

bar(c, f)
hold on
x = linspace(bounds(1),bounds(2),1e2);
y = lognpdf(x,0,3);
plot(x,y, 'r', 'LineWidth', 2)

enter image description here