如何排除runif()的特定结果? [英] How to exclude specific result of runif()?

查看:127
本文介绍了如何排除runif()的特定结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想在不包含特定值(例如0.5)的情况下在特定间隔内生成带有runif()的随机数时,我可以编写执行该功能的函数ex.runif(),但是它慢了数百倍正常的runif().谁能给我指出一个更好的解决方案?

When I want to generate a random number with runif() within a specific interval under exclusion of a particular value (e.g. 0.5) I can write this function ex.runif() who does the job, but it is hundreds of times slower than the normal runif(). Could anyone point me to a better solution?

ex.runif <- function(n, excl, min, max) {
  # ex.runif() excludes the specific value 'excl'
  q <- excl
  while (q == excl) {
    q <- runif(n, min = min, max = max)
  } 
  return(q)
}

set.seed(42)
ex.runif(1, .5, .25, .75)  # exclude .5, interval [.25, .75]
# [1] 0.707403

library(microbenchmark)
microbenchmark(ex.runif(1, .5, .25, .75), runif(1, min = .25, max = .75))
# Unit: microseconds
# expr         min      lq      mean   median       uq     max neval cld
# ex.runif 692.439 704.685 721.51135 715.2735 722.9275 962.373   100   b
# runif      2.041   2.551   3.49044   2.8070   3.3170  21.176   100  a 

推荐答案

如果要排除的值的集合是有限的,则在大多数情况下,不需要像这样的函数.原因是均匀分布是连续的,并且任何有限数量的值都以零概率获得.也就是说,根据概率论,q == excl在概率为零时为真.

If the set of values that you want to exclude is finite, then, in most cases, there is no need for a function like that. The reason is that the uniform distribution is continuous and any finite number of values are taken with probability zero. That is, q == excl is, in terms of probability theory, true with probability zero.

例如,

set.seed(42)
ex.runif(5, .5, .25, .75)
# [1] 0.7074030 0.7185377 0.3930698 0.6652238 0.5708728
set.seed(42)
runif(5, 0.25, 0.75)
# [1] 0.7074030 0.7185377 0.3930698 0.6652238 0.5708728

在其他任何种子下也很可能发生同样的情况.因此,您可能只是继续使用runif.

The same is most likely going to happen under any other seed as well. Thus, you may just keep using runif.

@duckmayr很好地说明了数字精度.实际上,随着间隔[min, max]的变窄,q == excl以真实的可能性越来越高,在某些应用中,它甚至可能变得有意义.

@duckmayr makes a good point about numeric precision. In fact, as the interval [min, max] is getting narrower, q == excl becomes true with increasingly high probability and, in some applications, it may even become relevant.

但是,如果理论上您确实只需要排除单个值0.5,那么执行q == excl之类的检查甚至可能会排除不必要的抽签而造成伤害.

However, if in theory you indeed need to exclude only a single value 0.5, then performing a check like q == excl might even do harm by excluding unnecessary draws.

例如,在我的情况下,.Machine$double.eps是2.220446e-16.那么当[min,max][0.5 - 10^(-k), 0.5 + 10^(-k)]时从[0.5 - .Machine$double.eps / 4, 0.5 + .Machine$double.eps / 4]获得平局并做​​出错误结论的概率为2 *(2.220446e-16/4)/(2 * 10 ^(-k))或大约0.55 * 10 ^(k-16).

For instance, in my case .Machine$double.eps is 2.220446e-16. Then the probability of getting a draw from [0.5 - .Machine$double.eps / 4, 0.5 + .Machine$double.eps / 4] when [min,max] is [0.5 - 10^(-k), 0.5 + 10^(-k)] and making a false conclusion is 2 * (2.220446e-16 / 4) / (2 * 10^(-k)) or around 0.55 * 10^(k-16).

这篇关于如何排除runif()的特定结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆