gpt4 book ai didi

r - 生成具有最小距离的随机 x 和 y 坐标

转载 作者:行者123 更新时间:2023-12-04 11:59:49 25 4
gpt4 key购买 nike

R中有没有办法生成它们之间距离最小的随机坐标?

例如。我想避免什么

x <- c(0,3.9,4.1,8)
y <- c(1,4.1,3.9,7)
plot(x~y)

最佳答案

这是随机几何中的经典问题。空间中完全随机的点,其中落入不相交区域的点数彼此独立,对应于齐次泊松点过程(在这种情况下为 R^2,但几乎可以在任何空间中)。

一个重要的特征是点的总数必须是随机的,然后才能独立于不相交区域中的点数。

对于泊松过程,点可以任意靠近。如果您通过对 Poisson 过程进行采样直到没有任何点靠得太近来定义一个过程,那么您就有了所谓的 Gibbs Hardcore 过程。这已经在文献中进行了大量研究,并且有不同的方法来模拟它。 R包spatstat有功能可以做到这一点。 rHardcore是一个完美的采样器,但是如果你想要高强度的点和大的硬核距离,它可能不会在有限的时间内终止......分布可以作为马尔可夫链的极限和rmh.default获得。允许您使用给定的 Gibbs 模型作为其不变分布运行马尔可夫链。这在有限时间内完成,但仅给出了近似分布的实现。

rmh.default您还可以在固定数量的点上模拟条件。请注意,当您在有限框内采样时,对于给定的硬核半径可以拟合的点数当然有上限,并且越接近此限制,从分布中正确采样就越成问题。

例子:

library(spatstat)
beta <- 100; R = 0.1
win <- square(1) # Unit square for simulation
X1 <- rHardcore(beta, R, W = win) # Exact sampling -- beware it may run forever for some par.!
plot(X1, main = paste("Exact sim. of hardcore model; beta =", beta, "and R =", R))



minnndist(X1) # Observed min. nearest neighbour dist.
#> [1] 0.102402

近似模拟

model <- rmhmodel(cif="hardcore", par = list(beta=beta, hc=R), w = win)
X2 <- rmh(model)
#> Checking arguments..determining simulation windows...Starting simulation.
#> Initial state...Ready to simulate. Generating proposal points...Running Metropolis-Hastings.
plot(X2, main = paste("Approx. sim. of hardcore model; beta =", beta, "and R =", R))



minnndist(X2) # Observed min. nearest neighbour dist.
#> [1] 0.1005433

以点数为条件的近似模拟

X3 <- rmh(model, control = rmhcontrol(p=1), start = list(n.start = 42))
#> Checking arguments..determining simulation windows...Starting simulation.
#> Initial state...Ready to simulate. Generating proposal points...Running Metropolis-Hastings.
plot(X3, main = paste("Approx. sim. given n =", 42))



minnndist(X3) # Observed min. nearest neighbour dist.
#> [1] 0.1018068

关于r - 生成具有最小距离的随机 x 和 y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48663440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com