# [R] Computing a CDF or many quantiles

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Wed Sep 10 00:28:53 CEST 2003

```On 09-Sep-03 Kevin S. Van Horn wrote:
> Given f, a pdf over a finite interval, is there any existing R function
> that
> can efficiently tabulate the cumulative distribution function for f, or
> produce all N+1 quantiles of the form i/N?  "Efficiently" here means
> better than doing repeated integrations for each point.

If that's all you want to do, then a very straightfoward approach should
be OK. I illustrate with a truncated normal distribution on [-1,1]:

x <- (-1)+(0.001*(0:2000));pdf<-dnorm(x); pdf<-pdf/(sum(pdf)*0.001)
CDF<-cumsum(pdf)*0.001
plot(x,pdf,ylim=c(0,1),type="l");lines(x,CDF)

Quantiles:
N=10;e<-CDF;
for(i in (0:10)){
j<-max(which(CDF<=i/N+e));print(c(x[j],CDF[j]))
}
 -1.0000000000  0.0003543119
 -0.75000000    0.09992753
 -0.5390000     0.1999932
 -0.3500000     0.2999169
 -0.1720000     0.4003052
  0.0000000     0.5002921
  0.1720000     0.6002703
  0.3490000     0.7000831
  0.5380000     0.8000068
  0.7490000     0.9000725
  1             1
(a bit approximate here owing to not adopting a slightly more subtle
approach to the first step).

Tabulation should be extremely straightforward.

Is this what you mean?

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 09-Sep-03                                       Time: 23:28:53
------------------------------ XFMail ------------------------------

```