[R] Probability of data values form DENSITY function

Meyners, Michael, LAUSANNE, AppliedMathematics Michael.Meyners at rdls.nestle.com
Wed Sep 30 08:39:52 CEST 2009


Lina, 

check ?density (and do so carefully). R uses a kernel estimate, by default "Gaussian" if I remember correctly. Values in a certain grid can be found from the code I sent earlier. I didn't check, but these are most likely just linearly interpolated by plot.density, and as the grid is sufficiently tight, it visually looks smooth. You could mimick this by linearly interpolating from the two closest neighbours for any "new" x values (or, as a rough approximation, choosing the density value of the nearest neighbour). I am not sure whether there are some specificities for time series in general or GARCH models in particular, that's not my area of expertise. Maybe there is some method for GARCH objects in the library you are using (you don't tell me which one). Try 
str(density(x)) 
from your original post to see whether you might have to adapt the proposed way to get these values. I copy the list in case someone can give more details on that.

Two side remarks: 
* You should note that the "density" function in R takes a lot of parameters that might heavily influence the outcome, and even though R will propose some defaults that will work frequently, they might not be optimal or even appropriate for your particular situation, model, and data. 
* From your messages I fear that you have a limited understanding of the notion of density functions, at least for continuous distributions; they do NOT give the probability that a certain value is taken. Try 
plot(x<-seq(-1,1,0.01), dnorm(x, sd=.25), type="l") 
to see that the density can easily exceed 1 and can hence not represent a probability. In fact, the theoretical probability that a specific value is actually taken from a continuous distribution is always equal to 0. 

I would strongly suggest to contact a local statistician to clarify these issues.

HTH, Michael
 

________________________________

	From: Lina Rusyte [mailto:linera27 at yahoo.co.uk] 
	Sent: Dienstag, 29. September 2009 18:03
	To: Meyners,Michael,LAUSANNE,AppliedMathematics
	Subject: RE: [R] Probability of data values form DENSITY function
	
	
	Dear Mr. Michael Meyners, 

	  

	Assume, that I have simulated data from GARCH  process: 

	  

	spec = garchSpec(model = list(omega = 0.01, alpha = 0.13, beta = 0.86, shape = 6), cond.dist = "std") 

	yt<-garchSim(spec, n = 4000) 

	  

	The empirical distribution of this data (yt) is assumed to be not parametrized distribution). This data is my in-sample data. 

	  

	I have another set of the data (yt1), generated from the same process (but it is not the same as yt). I need to find the probabilities of these data (yt1)  points in previously mentioned empiric distribution (of yt). 

	  

	Simply to say, having continuous EMPIRIC density function, I need for some points in x axis (values) to find out the corresponding values of y axis (their probability). 

	  

	R plots empiric density function, so I assume it approximates somehow the empirical density function. 

	  

	Thank a lot for the help in advance. 

	  

	Best regards, 

	Lina 


	--- On Tue, 29/9/09, Meyners,Michael, wrote:
	


		From: Meyners,Michael,LAUSANNE,AppliedMathematics
		Subject: RE: [R] Probability of data values form DENSITY function
		To: "Lina Rusyte" <linera27 at yahoo.co.uk>
		Cc: "R help" <r-help at r-project.org>
		Date: Tuesday, 29 September, 2009, 4:59 PM
		
		
		Lina, check whether something like
		
		data.frame(density(rnorm(10))[1:2]) 
		
		contains the information you want. Otherwise, try to be (much) more specific in what you want so that we do not need to guess (and of course provide minimal, self-contained, reproducible code). That has a higher chance to trigger some responses than posting the same message again. And if you even explain what you want to do with these values, you might get even better responses.
		
		HTH, Michael
		
		
		> -----Original Message-----
		> From: r-help-bounces at r-project.org <http://de.mc256.mail.yahoo.com/mc/compose?to=r-help-bounces@r-project.org>  
		> [mailto:r-help-bounces at r-project.org <http://de.mc256.mail.yahoo.com/mc/compose?to=r-help-bounces@r-project.org> ] On Behalf Of Lina Rusyte
		> Sent: Dienstag, 29. September 2009 16:45
		> To: R help
		> Cc: R help
		> Subject: [R] Probability of data values form DENSITY function
		> 
		> Hello,
		> Â
		> Could someone help me please and to tell how to get the 
		> probability from empirical DENSITY (not parametric) for each 
		> data value (R function). 
		> For example, for normal distribution there is such a function like: 
		> Â
		> “dnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = 
		> FALSE)† I need the same function only for the empirical 
		DENSITY function (which does not correspond to any typical > distribution). 
		> R plots the density function of any data: 
		> Â
		> “plot(density(x))â€
		> Â
		> I need to find out the probability for each data value from 
		> this plot-line. 
		> Â
		> Best regards,
		> Lina
		> 
		> 
		>       
		>     [[alternative HTML version deleted]]
		> 
		> 
		

		




More information about the R-help mailing list