Matlab には、正規分布から描画する関数 randn があります。
x = 0.5 + 0.1*randn()
平均 0.5、標準偏差 0.1 の正規分布から疑似乱数を引き出します。
これを考えると、次の Matlab コードは、1 で 0 で切り捨てられた正規分布からのサンプリングと同等ですか?
while x <=0 || x > 1
x = 0.5 + 0.1*randn();
end
Matlab には、正規分布から描画する関数 randn があります。
x = 0.5 + 0.1*randn()
平均 0.5、標準偏差 0.1 の正規分布から疑似乱数を引き出します。
これを考えると、次の Matlab コードは、1 で 0 で切り捨てられた正規分布からのサンプリングと同等ですか?
while x <=0 || x > 1
x = 0.5 + 0.1*randn();
end
次の手順が必要です。 1. 一様分布 u からランダムな値を引き出します。2. 正規分布が a と b で打ち切られていると仮定します。得る
u_bar = F(a)*u +F(b) *(1-u)
3. F の逆関数を使用する
epsilon= F^{-1}(u_bar)
epsilon は、打ち切られた正規分布のランダム値です。
ベクトル化しませんか?それはおそらく速くなるでしょう:
N = 1e5; % desired number of samples
m = .5; % desired mean of underlying Gaussian
s = .1; % desired std of underlying Gaussian
lower = 0; % lower value for truncation
upper = 1; % upper value for truncation
remaining = 1:N;
while remaining
result(remaining) = m + s*randn(1,numel(remaining)); % (pre)allocates the first time
remaining = find(result<=lower | result>upper);
end