2016-11-26 42 views
0

我想用諸如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

您不要在您的嘗試中使用bounds。您必須在邊界之間擠壓樣本(modelCDF),這可以通過縮放和移動均勻分佈的樣本來完成。

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