gpt4 book ai didi

R:使用 Mersenne-Twister 种子将来自 runif 的随机值极端聚集

转载 作者:行者123 更新时间:2023-12-04 19:29:05 27 4
gpt4 key购买 nike

我们在使用 R 的 runif 时,在我们的代码中遇到了一个奇怪的情况。并用 set.seed 设置种子与 kind = NULL选项(除非我弄错了,否则解析为 kind = "default" ;默认值为 "Mersenne-Twister" )。

在调用 runif 之前,我们使用上游系统生成的(8 位)唯一 ID 设置种子。 :

seeds = c(
"86548915", "86551615", "86566163", "86577411", "86584144",
"86584272", "86620568", "86724613", "86756002", "86768593", "86772411",
"86781516", "86794389", "86805854", "86814600", "86835092", "86874179",
"86876466", "86901193", "86987847", "86988080")

random_values = sapply(seeds, function(x) {
set.seed(x)
y = runif(1, 17, 26)
return(y)
})

这给出了 的值极挤在一起。
> summary(random_values)
Min. 1st Qu. Median Mean 3rd Qu. Max.
25.13 25.36 25.66 25.58 25.83 25.94
runif的这种行为当我们使用 kind = "Knuth-TAOCP-2002" 时消失,我们得到的值似乎分布得更均匀。
random_values = sapply(seeds, function(x) {
set.seed(x, kind = "Knuth-TAOCP-2002")
y = runif(1, 17, 26)
return(y)
})

输出省略。

这里最有趣的是,这不会发生在 Windows 上——只发生在 Ubuntu 上。 ( sessionInfo 下面是 Ubuntu 和 Windows 的输出)。

窗口输出:
> seeds = c(
+ "86548915", "86551615", "86566163", "86577411", "86584144",
+ "86584272", "86620568", "86724613", "86756002", "86768593", "86772411",
+ "86781516", "86794389", "86805854", "86814600", "86835092", "86874179",
+ "86876466", "86901193", "86987847", "86988080")
>
> random_values = sapply(seeds, function(x) {
+ set.seed(x)
+ y = runif(1, 17, 26)
+ return(y)
+ })
>
> summary(random_values)
Min. 1st Qu. Median Mean 3rd Qu. Max.
17.32 20.14 23.00 22.17 24.07 25.90

有人可以帮助了解发生了什么吗?

Ubuntu
R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8
[9] LC_ADDRESS=en_US.UTF-8 LC_TELEPHONE=en_US.UTF-8
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base

other attached packages:
[1] RMySQL_0.10.8 DBI_0.6-1
[3] jsonlite_1.4 tidyjson_0.2.2
[5] optiRum_0.37.3 lubridate_1.6.0
[7] httr_1.2.1 gdata_2.18.0
[9] XLConnect_0.2-12 XLConnectJars_0.2-12
[11] data.table_1.10.4 stringr_1.2.0
[13] readxl_1.0.0 xlsx_0.5.7
[15] xlsxjars_0.6.1 rJava_0.9-8
[17] sqldf_0.4-10 RSQLite_1.1-2
[19] gsubfn_0.6-6 proto_1.0.0
[21] dplyr_0.5.0 purrr_0.2.4
[23] readr_1.1.1 tidyr_0.6.3
[25] tibble_1.3.0 tidyverse_1.1.1
[27] rBayesianOptimization_1.1.0 xgboost_0.6-4
[29] MLmetrics_1.1.1 caret_6.0-76
[31] ROCR_1.0-7 gplots_3.0.1
[33] effects_3.1-2 pROC_1.10.0
[35] pscl_1.4.9 lattice_0.20-35
[37] MASS_7.3-47 ggplot2_2.2.1

loaded via a namespace (and not attached):
[1] splines_3.4.0 foreach_1.4.3 AUC_0.3.0 modelr_0.1.0
[5] gtools_3.5.0 assertthat_0.2.0 stats4_3.4.0 cellranger_1.1.0
[9] quantreg_5.33 chron_2.3-50 digest_0.6.10 rvest_0.3.2
[13] minqa_1.2.4 colorspace_1.3-2 Matrix_1.2-10 plyr_1.8.4
[17] psych_1.7.3.21 XML_3.98-1.7 broom_0.4.2 SparseM_1.77
[21] haven_1.0.0 scales_0.4.1 lme4_1.1-13 MatrixModels_0.4-1
[25] mgcv_1.8-17 car_2.1-5 nnet_7.3-12 lazyeval_0.2.0
[29] pbkrtest_0.4-7 mnormt_1.5-5 magrittr_1.5 memoise_1.0.0
[33] nlme_3.1-131 forcats_0.2.0 xml2_1.1.1 foreign_0.8-69
[37] tools_3.4.0 hms_0.3 munsell_0.4.3 compiler_3.4.0
[41] caTools_1.17.1 rlang_0.1.1 grid_3.4.0 nloptr_1.0.4
[45] iterators_1.0.8 bitops_1.0-6 tcltk_3.4.0 gtable_0.2.0
[49] ModelMetrics_1.1.0 codetools_0.2-15 reshape2_1.4.2 R6_2.2.0
[53] knitr_1.15.1 KernSmooth_2.23-15 stringi_1.1.5 Rcpp_0.12.11

视窗
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_India.1252 LC_CTYPE=English_India.1252 LC_MONETARY=English_India.1252
[4] LC_NUMERIC=C LC_TIME=English_India.1252

attached base packages:
[1] graphics grDevices utils datasets grid stats methods base

other attached packages:
[1] bindrcpp_0.2 h2o_3.14.0.3 ggrepel_0.6.5 eulerr_1.1.0 VennDiagram_1.6.17
[6] futile.logger_1.4.3 scales_0.4.1 FinCal_0.6.3 xml2_1.0.0 httr_1.3.0
[11] wesanderson_0.3.2 wordcloud_2.5 RColorBrewer_1.1-2 htmltools_0.3.6 urltools_1.6.0
[16] timevis_0.4 dtplyr_0.0.1 magrittr_1.5 shiny_1.0.5 RODBC_1.3-14
[21] zoo_1.8-0 sqldf_0.4-10 RSQLite_1.1-2 gsubfn_0.6-6 proto_1.0.0
[26] gdata_2.17.0 stringr_1.2.0 XLConnect_0.2-12 XLConnectJars_0.2-12 data.table_1.10.4
[31] xlsx_0.5.7 xlsxjars_0.6.1 rJava_0.9-8 readxl_0.1.1 googlesheets_0.2.1
[36] jsonlite_1.5 tidyjson_0.2.1 RMySQL_0.10.9 RPostgreSQL_0.4-1 DBI_0.5-1
[41] dplyr_0.7.2 purrr_0.2.3 readr_1.1.1 tidyr_0.7.0 tibble_1.3.3
[46] ggplot2_2.2.0 tidyverse_1.0.0 lubridate_1.6.0

loaded via a namespace (and not attached):
[1] gtools_3.5.0 assertthat_0.2.0 triebeard_0.3.0 cellranger_1.1.0 yaml_2.1.14
[6] slam_0.1-40 lattice_0.20-34 glue_1.1.1 chron_2.3-48 digest_0.6.12.1
[11] colorspace_1.3-1 httpuv_1.3.5 plyr_1.8.4 pkgconfig_2.0.1 xtable_1.8-2
[16] lazyeval_0.2.0 mime_0.5 memoise_1.0.0 tools_3.3.2 hms_0.3
[21] munsell_0.4.3 lambda.r_1.1.9 rlang_0.1.1 RCurl_1.95-4.8 labeling_0.3
[26] bitops_1.0-6 tcltk_3.3.2 gtable_0.2.0 reshape2_1.4.2 R6_2.2.0
[31] bindr_0.1 futile.options_1.0.0 stringi_1.1.2 Rcpp_0.12.12.1

最佳答案

注意:这个答案总结了在 R-devel 邮件列表上发生的关于这个问题的讨论的元素。我只是试图捕捉和总结最初在那里表达的想法。

尽管您保证这些数字不是特殊构造的边缘情况,但它们的外观看起来就是这样。这是原始序列加代码,用于检查产生的值的分布:

seeds = c(
86548915, 86551615, 86566163, 86577411, 86584144, 86584272,
86620568, 86724613, 86756002, 86768593, 86772411, 86781516,
86794389, 86805854, 86814600, 86835092, 86874179, 86876466,
86901193, 86987847, 86988080)
checkit <- function(seeds) {
sapply(seeds, function(x) {
set.seed(x)
y = runif(1, 17, 26)
return(y)
})}

正如所指出的,原始序列的变化令人惊讶地很小:
  summary(checkit(seeds+0))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
##25.13 25.36 25.66 25.58 25.83 25.94

原始序列似乎确实有一些特别之处,因为对它的最小修改不会产生同样令人惊讶的结果:
summary(checkit(seeds+1))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 17.18 19.65 22.75 22.02 24.37 25.79

summary(checkit(seeds-1))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
##17.15 18.44 19.92 20.77 22.97 25.95

在原始序列跨越的范围内的所有种子中,预期数量产生观察范围内的值:
possible.seeds <- min(seeds):max(seeds)

s25 <- Filter(function(s){
set.seed(s)
x <- runif(1,17,26)
x > 25.12 & x < 25.95},
possible.seeds)

length(s25)/length(possible.seeds)
##[1] 0.09175801

然而,原始序列中的所有值都在这个子集中(当然我们已经知道了......)。
table(seeds %in% s25)

##TRUE
## 21

所有这些都表明原始序列实际上可能是(可能是无意的)特殊构造的边缘情况。

关于R:使用 Mersenne-Twister 种子将来自 runif 的随机值极端聚集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47079702/

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