【问题标题】:Julia: Generate normally distributed random number with restricted rangeJulia:生成范围受限的正态分布随机数
【发布时间】:2018-11-21 03:04:00
【问题描述】:

问题:如何从 Julia 的高斯分布中生成区间 [0,1] 内的随机数?

我认为randn 是生成正态分布随机数的方法,但文档中关于如何指定范围的描述非常不透明。

【问题讨论】:

    标签: random range julia normal-distribution


    【解决方案1】:

    使用Distributions 包。如果您还没有:

    using Pkg ; Pkg.add("Distributions")
    

    然后:

    using Distributions
    mu = 0    #The mean of the truncated Normal
    sigma = 1 #The standard deviation of the truncated Normal
    lb = 0    #The truncation lower bound
    ub = 1    #The truncation upper bound
    d = Truncated(Normal(mu, sigma), lb, ub)  #Construct the distribution type
    x = rand(d, 100) #Simulate 100 obs from the truncated Normal
    

    或全部在一行中:

    x = rand(Truncated(Normal(0, 1), 0, 1), 100)
    

    【讨论】:

    • 为什么如果我这样做然后尝试std(rand(Truncated(Normal(0, 1), 0, 1), 100)) 我总是得到小于 1 的答案?就像我理解的那样,我可能不会完全得到 1,但我得到的值在 0.2 范围内,这似乎远低于 1。
    • @newtothis 您将标准法线限制在 [0,1] 范围内,这是对该分布的一个相当大的限制。请注意,[0,1] 上的均匀随机变量的标准差约为 0.29,因此区间 [0,1] 上截断标准正态的结果接近该数字的结果听起来是正确的。通常尝试在区间 [a,b] 上截断标准法线。当ab 彼此非常接近时,标准差将接近0,但它会接近1,因为a 变为-infinity,b 变为无穷大。
    猜你喜欢
    • 2010-11-21
    • 2021-09-01
    • 2014-08-11
    • 2011-02-14
    • 2014-11-06
    • 1970-01-01
    • 2016-09-21
    • 2018-06-22
    • 1970-01-01
    相关资源
    最近更新 更多