Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))) 
like image 750
Dominique Makowski Avatar asked Jan 24 '26 20:01

Dominique Makowski


2 Answers

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))
like image 86
Bogumił Kamiński Avatar answered Jan 26 '26 09:01

Bogumił Kamiński


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.

like image 24
carstenbauer Avatar answered Jan 26 '26 10:01

carstenbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!