【问题标题】:Drawing pseudorandoms from a truncated normal distribution从截断的正态分布中绘制伪随机数
【发布时间】:2013-08-08 13:58:53
【问题描述】:

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

x = 0.5 + 0.1*randn() 

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

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

    while x <=0 || x > 1

    x = 0.5 + 0.1*randn();

    end

【问题讨论】:

  • 如何声明和计算变量 (cr,c crmean)?
  • cr 是一个值数组。 cr(c) 是第 c 个元素。 crMean 是一个常数,例如0.5。 c 是一个循环迭代器。
  • 如果您有统计工具箱,请查看here。但是,根据this paper,您的做法是合理的

标签: matlab normal-distribution truncated


【解决方案1】:

使用 MATLAB 的 Probability Distribution Objects 可以非常轻松地从截断分布中采样。

您可以使用makedist() 和truncate() 函数来定义对象,然后修改(截断它)为random() 函数准备对象,该函数允许从中生成随机变量。

% 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 的截断版本),您就可以在各种函数调用中使用它。

为了生成样本,random(pdt,m,n) 会从 pdt 生成一个 m x n 样本数组。


此外,如果您想避免使用工具箱,this answer from @Luis Mendo 是正确的(证明如下)。

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)');

【讨论】:

    【解决方案2】:

    您需要以下步骤 1. 从均匀分布中抽取一个随机值,u。 2. 假设正态分布在 a 和 b 处被截断。得到

    u_bar = F(a)*u +F(b) *(1-u)
    

    3。使用 F 的倒数

    epsilon= F^{-1}(u_bar)
    

    epsilon 是截断正态分布的随机值。

    【讨论】:

    • 公式从何而来?请帮忙提供参考!
    【解决方案3】:

    你为什么不矢量化?它可能会更快:

    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
    

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 1970-01-01
      • 2019-12-20
      • 2012-12-11
      • 1970-01-01
      • 2020-08-07
      • 2021-09-01
      • 2016-02-09
      • 2021-09-03
      相关资源
      最近更新 更多