Dominique Makowski
Dominique Makowski

Reputation: 1673

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

I am trying to "translate" these lines from R to Julia:

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

However, I have trouble with the qnorm function. I've searched for "quantile function" and found the quantile() function. However, the R's version returns a vector of length 100, while the Julia's version returns a vector of length 5.

Here's my attempt:

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

Upvotes: 2

Views: 1074

Answers (2)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69949

Under Julia 1.1 you should broadcast the call to quantile like this:

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

Upvotes: 6

carstenbauer
carstenbauer

Reputation: 10147

Try

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 uses multiple dispatch to select the appropriate quantile method for a given distribution, in constrast to R where you seem to have prefixes. According to the documentation the first argument should be the distribution, the second argument the point where you want to evaluate the inverse cdf.

Strangely I get an error when I try to do quantile.(d, qs) (broadcast the quantile call). UPDATE: See Bogumil's answer in this case. In my benchmarks, both approaches have the same speed.

Upvotes: 2

Related Questions