Back
Close

Computing with Data

elgeish
170K views
Previous: Kernel Functions Next: Smoothing Histograms Using qplot

Smoothing Histograms Using Gaussian Kernels

The R code below graphs the smoothed histogram of the data {-1, 0, 0.5, 1, 2, 5, 5.5, 6} using the Gaussian kernel:

data = c(-1, 0, 0.5, 1, 2, 5, 5.5, 6)
data_size = length(data)
x_grid = seq(-3, data_size, length.out = 100)
kernel_values = x_grid %o% rep(1, data_size)
f = x_grid * 0
for (i in 1:data_size) {
kernel_values[, i] = dnorm(x_grid, data[i], 1/6) / data_size
f = f + kernel_values[, i]
}
plot(x_grid, f, xlab = "$x$", ylab = "$f_h(x)$", type = "l")
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants