# Monte Carlo Estimator for a quantile

I am trying to understand how to compute a crude Monte Carlo estimator for an $\alpha-$quantile. I have read the algorithm from the book

Monte Carlo Methods and Models in Finance and Insurance, (Korn, Korn and Kroisandt, 2010, CRC Press)

I don´t understand how can I implement this algorithm in R with an simple example, supposing that I don´t know the distribution $F$ or is hard to compute, how to use the remark, i.e solve numerically $F(x)-\alpha=0$.

In this case for instance in R I found that I have the function ecdf() that gives as output the empirical distribution function of a vector $x$, my problem is how to define the $F(x)-\alpha=0$ and solve it numerically?

I am confused also because if I need to compute it numerically I will need the derivatives of $F$ but if I don´t know it explicitly it seems really difficult.

• You would have to know F to simulate the data. Note that the estimate of the quantile is only a function of the empirical distribution. I think the remark means that the direct calculation is hard to compute. The part about "not available" makes no sense. Oct 17, 2017 at 15:32
• For some known distributions the quantile may be hard to compute. You made a good point. Oct 17, 2017 at 15:39
• But how to estimate the quantile using the algoritm? i.e. the inverse of the empirical distribution? Oct 17, 2017 at 15:45
• If n is is your sample size find k such that k/n is less than $\alpha$ and (k+1)/n is greater than $\alpha$. Then take the kth observation to be the quantile estimate or average the kth with the next one. These estimates should be good approximations especially when the sample size n is large. Oct 17, 2017 at 16:01
• @Michael One scarcely needs to know $F$ to simulate data! For instance, you could simulate a nasty distribution via, say, log(besselK(runif(1000, 0, .5), runif(1000))) without having to compute or otherwise know in any way how these data are distributed. Isn't this the entire point of running simulations? Boris: I recommend you read the help page for ecdf. Pay attention to the quantile method documented there.
– whuber
Oct 17, 2017 at 16:35

The remark is rather ill-thought, as it confuses the theoretical quantile that is solution to $F(q_\alpha)=\alpha$ with the empirical quantile that is solution to $\hat{F}_n(\hat{q}_\alpha)=\alpha$. Assuming you have an iid sample $X_1,\ldots,X_n$ from $F$, it is always possible to derive $\hat{F}_n$ by the ecdf function in R:

#Assuming x is the notation for the sample
Fn=ecdf(x)
plot(Fn)
#Taking a particular value of alpha
alpha=0.1017
abline(h=alpha)


This picture tells you where the empirical quantile should be, roughly, without giving you the solution to the equation $\hat{F}_n(\hat{q}_\alpha)=\alpha$ that you can solve by dyadic divide-and-conquer strategies or by calling the quantile function.

If the probability $\alpha$ is arbitrary, there will be not exact solution to this equation$$\hat{F}_n(\hat{q}_\alpha)=\alpha$$since $\hat{F}_n$ only takes $n+1$ possible values. (This is also visible from the above graph.) In that case, the "solution" is found as the smallest observation for which $\hat{F}_n(x)$ is above $\alpha$, mimicking the resolution in the theoretical case where $$F^{-1}(\alpha)=\inf\{x\,|\, F(X)\geq \alpha \}$$

• do you mean that is not necessary to know the distribution of $X_1,....,X_n$ explicitly? Oct 17, 2017 at 23:43
• I have accepted your answer, but I am not clear with some of the functions of the forum, I think that your answer is clear. Now I think that the following step is to have clear which numerical procedure use and work in some examples. Oct 19, 2017 at 10:48
• could you explain me in the Monte Carlo Approach how is considered the infimum? I am not clear with it. Oct 19, 2017 at 11:06
• Well, in the book is explained that the Monte Carlo estimator for the quantile is the root of $\hat{F_N(x)}(x)=\alpha$, I don´t understand the relation between this estimator and the $inf\{x\geq 0 | F(X)\geq \alpha \}$, how to see that in fact that computing the root of $\hat{F_N(x)}=\alpha$ this root is the infimum? Oct 19, 2017 at 11:13
• But If I can't solve exactly $\hat F_n(q)=\alpha$, why the estimator of the quantile is the root of $\hat F_n(x)=\alpha$? Oct 19, 2017 at 11:35

Basically, you need to sort the sample and take $$\alpha\cdot N$$th element. The R code would be

sort(sample)[length(sample)*alpha]


As Xi'an said, it will give $$F^{-1}(\alpha)=\inf\{x\,|\, F(X)\geq \alpha \}$$