从截断的正态分布中提取伪随机 [英] Drawing pseudorandoms from a truncated normal distribution

查看:164
本文介绍了从截断的正态分布中提取伪随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matlab具有randn函数,可从正态分布中提取,例如.

Matlab has the function randn to draw from a normal distribution e.g.

x = 0.5 + 0.1*randn() 

从均值0.5和标准差0.1的正态分布中得出伪随机数.

draws a pseudorandom number from a normal distribution of mean 0.5 and standard deviation 0.1.

鉴于此,下面的Matlab代码是否等效于从1处截断为0的正态分布采样?

Given this, is the following Matlab code equivalent to sampling from a normal distribution truncated at 0 at 1?

    while x <=0 || x > 1

    x = 0.5 + 0.1*randn();

    end

推荐答案

使用MATLAB的

Using MATLAB's Probability Distribution Objects makes sampling from truncated distributions very easy.

您可以使用 makedist() random() 函数的对象,该函数允许随机生成从中变化.

You can use the makedist() and truncate() functions to define the object and then modify (truncate it) to prepare the object for the random() function which allows generating random variates from it.

% MATLAB R2017a
pd = makedist('Normal',0.5,0.1)     % Normal(mu,sigma)
pdt = truncate(pd,0,1)              % truncated to interval (0,1)
sample = random(pdt,numRows,numCols)  % Sample from distribution `pdt`

一旦创建了对象(这里是pdt,是pd的截短版本),就可以在各种函数调用中使用它.

Once the object is created (here it is pdt, the truncated version of pd), you can use it in a variety of function calls.

生成样本random(pdt,m,n)生成来自pdt的样本的 m x n 数组.

To generate samples, random(pdt,m,n) produces a m x n array of samples from pdt.

此外,如果您想避免使用工具箱,则来自@Luis Mendo的答案是正确的(如下所示) .

Further, if you want to avoid use of toolboxes, this answer from @Luis Mendo is correct (proof below).

figure, hold on
h = histogram(cr,'Normalization','pdf','DisplayName','@Luis Mendo samples');
X = 0:.01:1;
p = plot(X,pdf(pdt,X),'b-','DisplayName','Theoretical (w/ truncation)');

这篇关于从截断的正态分布中提取伪随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆