简体   繁体   English

从R中的核密度估计中获取值

[英]Getting values from kernel density estimation in R

I am trying to get density estimates for the log of stock prices in R. I know I can plot it using plot(density(x)) . 我试图获得R中股票价格对数的密度估计。我知道我可以使用plot(density(x))绘制它。 However, I actually want values for the function. 但是,我实际上想要函数的值。

I'm trying to implement the kernel density estimation formula. 我正在尝试实现核密度估计公式。 Here's what I have so far: 这是我到目前为止所拥有的:

a <- read.csv("boi_new.csv", header=FALSE)
S = a[,3] # takes column of increments in stock prices
dS=S[!is.na(S)] # omits first empty field

N = length(dS)                  # Sample size
rseed = 0                       # Random seed
x = rep(c(1:5),N/5)             # Inputted data

set.seed(rseed)   # Sets random seed for reproducibility

QL <- function(dS){
    h = density(dS)$bandwidth
    r = log(dS^2)
    f = 0*x
    for(i in 1:N){
        f[i] = 1/(N*h) * sum(dnorm((x-r[i])/h))
    }
    return(f)
}

QL(dS)

Any help would be much appreciated. 任何帮助将非常感激。 Been at this for days! 已经好几天了!

You can pull the values directly from the density function: 您可以直接从density函数中提取值:

x = rnorm(100)
d = density(x, from=-5, to = 5, n = 1000)
d$x
d$y

Alternatively, if you really want to write your own kernel density function, here's some code to get you started: 或者,如果您真的想编写自己的内核密度函数,可以使用以下代码来启动:

  1. Set the points z and x range: 设置点zx范围:

     z = c(-2, -1, 2) x = seq(-5, 5, 0.01) 
  2. Now we'll add the points to a graph 现在我们将点添加到图表中

     plot(0, 0, xlim=c(-5, 5), ylim=c(-0.02, 0.8), pch=NA, ylab="", xlab="z") for(i in 1:length(z)) { points(z[i], 0, pch="X", col=2) } abline(h=0) 
  3. Put Normal density's around each point: 将正常密度放在每个点周围:

     ## Now we combine the kernels, x_total = numeric(length(x)) for(i in 1:length(x_total)) { for(j in 1:length(z)) { x_total[i] = x_total[i] + dnorm(x[i], z[j], sd=1) } } 

    and add the curves to the plot: 并将曲线添加到图中:

     lines(x, x_total, col=4, lty=2) 
  4. Finally, calculate the complete estimate: 最后,计算完整的估算:

     ## Just as a histogram is the sum of the boxes, ## the kernel density estimate is just the sum of the bumps. ## All that's left to do, is ensure that the estimate has the ## correct area, ie in this case we divide by $n=3$: plot(x, x_total/3, xlim=c(-5, 5), ylim=c(-0.02, 0.8), ylab="", xlab="z", type="l") abline(h=0) 

    This corresponds to 这对应于

     density(z, adjust=1, bw=1) 

The plots above give: 上面的图给出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM