Julia 相当于 R 的 qnorm()?

Julia's equivalent of R's qnorm()?

我正在尝试 "translate" 从 R 到 Julia 的这些行:

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

但是,我在使用 qnorm 函数时遇到了问题。我搜索了 "quantile function" 并找到了 quantile() 函数。但是,R 的版本 returns 是一个长度为 100 的向量,而 Julia 的版本 returns 是一个长度为 5.

的向量

这是我的尝试:

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

在 Julia 1.1 下,您应该像这样广播对 quantile 的调用:

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

尝试

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 使用多重分派 select 给定分布的适当 quantile 方法,与 R 相比,你似乎有前缀。根据 documentation 第一个参数应该是分布,第二个参数是你想要计算逆 cdf 的点。

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