# How to get the quantiles by simulations?

I would like to find the quantiles of a random variable $X$ by simulations. I plan to do it in the following way, however, I do not know which one should be more correct though the results look very very close. Let's say p=0.99

A)1.Simulate 100 random numbers of $X$, order them, and pick up the 99th one. 2.Do 1 for 1e4 times and take the average.

B)Simulate 1e6(100*1e4) random numbers of $X$, and pick up the .99*1e6-th one from the ordered sequence.

I guess A may look may "correct", and the standard error can be computed. But B is much faster. However, I do not know how to compute the standard error with B. The reuslts are close indeed.

Thanks a lot for you helps

Your second method is more accurate for extracting the quantile. You're confused because you're not considering SE correctly.

Perhaps if you get the SE's for both values the same way you'll have a better understanding. For A it's the SD of your sampling across your 1e4 simulations and for B it's the same if you again do a bunch of samples.

What you should find, with a little experimentation, is that if the initial sample (your simulation) is large enough then the SE tends toward 0. Therefore, method B is better. In such examples considerations of SE are often irrelevant. SE is very important when you can't get enormous sample sizes. But if you can, like when your computer pumps them out instantaneously, SE becomes equivalent to 0. Try it out.

mX  <- 100
sdX <- 15

Y <- replicate(1e4, sort(rnorm(100, mX, sdX))[99])
sd(Y)
mean(Y)

Y <- replicate(1e4, sort(rnorm(1e4, mX, sdX))[.99*1e4])
sd(Y)
mean(Y)


It will take a little while to run the second one. I shortened to it 1e4 so you get the idea. Make the n in rnorm big enough and you can ignore SE.

• @John.Thanks for ur comments. Can I just understand you like: for B, there is no SE? Since I was asked to present, if so, I have to take plan A. However, B is indeed faster and more accurate I guess. – Jingjings May 8 '13 at 18:53
• Although doubtless the thoughts behind this answer are correct, the way it is expressed might be misinterpreted. In particular, one does not need to "do a bunch of samples" to obtain an SE and although the SE will tend to $0$ with large $N$, it is so slow (as $N^{-1/2}$) that in many practical applications it will not get "equivalent to 0." Any sample quantile (for continuous variates) has a Beta distribution as transformed by the probability integral transformation and estimating it is a matter of using the empirical distribution, which does not require multiple samples to obtain. – whuber May 8 '13 at 19:06
• OK, so I was in a hurry, I fixed it a little. You're absolutely right in practical applications. And I originally had this written suggesting the values Jingjings wants to know don't require this bootstrapping. But then I thought perhaps the bootstrapping was an exercise of some sort and thought I'd expand the exercise. – John May 8 '13 at 20:00
• Jingjings, B has an SE. You can see it by taking multiple samples. You could estimate it easily from the smaller simulations (multiply the observed sd by sqrt(n) and divide by sqrt(1e6)) or just run it up to 1e6. Or, you could do as Whuber suggested and get an analytic estimate, unless your presentation was pedagogical and required sampling. – John May 8 '13 at 20:07