【问题标题】:Julia's equivalent of R's qnorm()?Julia 相当于 R 的 qnorm()?
【发布时间】:2019-02-05 00:31:42
【问题描述】:

我正在尝试将这些行从 R“翻译”到 Julia:

n <- 100
mean <- 0
sd <- 1
x <- qnorm(seq(1 / n, 1 - 1 / n, length.out = n), mean, sd)

但是,我在使用 qnorm 函数时遇到了问题。我搜索了“分位数函数”并找到了quantile() 函数。然而,R 的版本返回一个长度为 100 的向量,而 Julia 的版本返回一个长度为 5 的向量。

这是我的尝试:

import Distributions
n = 100
x = Distributions.quantile(collect(range(1/n, stop=1-1/n, length=n))) 

【问题讨论】:

    标签: r julia distribution normal-distribution quantile


    【解决方案1】:

    在 Julia 1.1 下,您应该像这样将呼叫广播到 quantile

    quantile.(Normal(0, 1), range(1/n, 1-1/n, length = n))
    

    【讨论】:

      【解决方案2】:

      试试

      using Distributions
      n = 100
      qs = range(1/n, stop=1-1/n, length=n) # no need to collect it
      d = Normal() # default is mean = 0, std = 1
      result = [quantile(d, q) for q in qs]
      

      Julia 使用多重分派来为给定的分布选择适当的quantile 方法,这与您似乎有前缀的 R 形成鲜明对比。根据documentation,第一个参数应该是分布,第二个参数是您要评估逆 cdf 的点。

      奇怪的是,当我尝试执行quantile.(d, qs)(广播分位数呼叫)时出现错误。更新:在这种情况下,请参阅 Bogumil 的回答。在我的基准测试中,两种方法的速度相同。

      【讨论】:

      • 在我的回答中,我将展示如何使用broadcast 处理您的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2021-09-14
      • 2015-05-17
      • 1970-01-01
      相关资源
      最近更新 更多