Saya melihat bagaimana jarak Euclidean minimum yang diharapkan antara titik-titik seragam acak dan titik asal berubah saat kami meningkatkan kepadatan titik-titik acak ( titik per unit persegi ) di sekitar titik asal. Saya telah berhasil menemukan hubungan antara keduanya yang digambarkan sebagai berikut:
Saya datang dengan ini dengan menjalankan beberapa simulasi Monte Carlo di R dan pas kurva secara manual (kode di bawah).
Pertanyaan saya adalah : dapatkah saya memperoleh hasil ini secara teoritis daripada melalui eksperimen?
#Stack Overflow example
library(magrittr)
library(ggplot2)
#---------
#FUNCTIONS
#---------
#gen random points within a given radius and given density
gen_circle_points <- function(radius, density) {
#round radius up then generate points in square with side length = 2*radius
c_radius <- ceiling(radius)
coords <- data.frame(
x = runif((2 * c_radius) ^ 2 * density, -c_radius, c_radius),
y = runif((2 * c_radius) ^ 2 * density, -c_radius, c_radius)
)
return(coords[sqrt(coords$x ^ 2 + coords$y ^ 2) <= radius, ])#filter in circle
}
#Example plot
plot(gen_circle_points(radius = 1,density = 200)) #200 points around origin
points(0,0, col="red",pch=19) #colour origin
#return euclidean distances of points generated by gen_circle_points()
calculate_distances <- function(circle_points) {
return(sqrt(circle_points$x ^ 2 + circle_points$y ^ 2))
}
#find the smallest distance from output of calculate_distances()
calculate_min_value <- function(distances) {
return(min(distances))
}
#Try a range of values
density_values <- c(1:100)
expected_min_from_density <- sapply(density_values, function(density) {
#simulate each density value 1000 times and take an average as estimate for
#expected minimum distance
sapply(1:1000, function(i) {
gen_circle_points(radius=1, density=density) %>%
calculate_distances() %>%
calculate_min_value()
}) %>% mean()
})
results <- data.frame(density_values, expected_min_from_density)
#fit based off exploration
theoretical_fit <- data.frame(density = density_values,
fit = 1 / (sqrt(density_values) * 2))
#plot monte carlo (black) and fit (red dashed)
ggplot(results, aes(x = density_values, y = expected_min_from_density)) +
geom_line() +
geom_line(
data = theoretical_fit,
aes(x = density, y = fit),
color = "red",
linetype = 2
)