[R] cumulative density question

Spencer Graves spencer.graves at pdf.com
Fri Mar 19 15:46:10 CET 2004


    1.  I assume you are looking at the cumulative DISTRIBUTION function 
(cdf), F(x), which is the probability that a random variable X is less 
than or equal to x;  the cdf is the integral of the density function. 

    2.  The maximum likelihood nonparametric estimate of the cdf is the 
"empirical cdf" (ecdf) in library(stepfun).  However, this is NOT a 
function appropriate for "integrate".  The following looks like it gives 
me the correct answer: 


pecdf <- function(x, y=1:3, lower.tail=TRUE){
   nx <- length(x)
   F. <- rep(NA, nx)
   for(i in 1:nx){
     F.[i] <- sum(y<=x[i])
   }
   F. <- F./length(y)
   if(lower.tail) F. else (1-F.)
}

pecdf(0:4)
pecdf(0:4, lower.tail=FALSE)

Omega <-
function(r){
  numerator <- integrate(pecdf, 1, r)
  denominator <- integrate(pecdf, r, 3, lower.tail=FALSE)
  numerator$value/denominator$value
}

Omega(2)

      3.  For "data" y = 1:3, I get the following expressions for the 
numerator and denominator: 

      numerator(r) = ((ifelse(r<=2, (5-2*r), (3-r))/3)

      denominator(r) = ((ifelse(r<=2, (r-1), (2*r-3))/3)

One could probably develop a more general form of this for arbitrary "y". 

      hope this helps.  spencer graves

s viswanath wrote:

>Hi,
>
>I am interested in looking at cumulative density functions. If F(x) is a cumulative density of monthly fund returns over the interval of a to b, and I am interested in returns above and below a specified point r, then I would like to find the number that is made up of
> 
>1.(integral from r to b)(1-F(x))dx  
>2. (integral from a to r)(F(x)dx)
>
>3. the ratio of #1/#2 above
>
>
>In financial literature this ratio has been called the Omega function.
>
>My first guess in obtaining this equation using R
>is to use the integrate function but I am have two problems: 
>
>I. can I use a nonparametric density in the integrate function(how?), 
>II. how can i get the ratio of #3 above as the integrate function gives the number plus the absolute error
>  
>
>>integrate(dnorm,-4,1.96)
>>    
>>
>0.9749704 with absolute error < 2.1e-07
>so using a ratio  in #3 above i get the following error:
> : non-numeric argument to binary operator
>
>
>
>Thank you in advance.
>Sri Viswanath
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  
>




More information about the R-help mailing list