使用aprof软件包分析Monty Hall代码 [英] profiling Monty Hall code with aprof package

查看:246
本文介绍了使用aprof软件包分析Monty Hall代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Monty Hall问题编写了R代码.据我所知,该代码有效.但是,我也使用aprof包来尝试降低代码的速度和内存要求.我能够将速度降低50%,但无法使aprof程序包的内存配置功能正常工作.感谢您提供解决此错误的建议或帮助.

I have written R code for the Monty Hall problem. The code works as far as I can tell. However, I also was using the aprof package to try to reduce speed and memory requirements of the code. I was able to reduce speed by 50%, but I cannot get the memory profiling feature of the aprof package to work. Thank you for any advice or assistance in resolving this error.

首先,我描述Monty Hall问题:

First I describe the Monty Hall problem:

向参赛者展示了三扇门.一扇门藏着一个不错的奖品.两扇门掩盖了一个不好的奖项.参赛者不知道每个门的背后是什么.参赛者选择一扇门.主持人打开了参赛者未选择的两个门之一.主人没有打开门掩藏好价钱.接下来,主持人询问参赛者是否希望保留最初选择的门或切换到参赛者最初未选择的其余关门.参赛者该怎么办?答:参赛者应始终切换门.这是因为参赛者最初有33%的机会获胜,而67%的机会会输.切换门将获胜的可能性提高到67%.

Three doors are presented to a contestant. One door hides a nice prize. Two doors hide a bad prize. Contestant does not know what is behind each door. Contestant selects a door. Host opens one of the two doors not selected by the contestant. Host does not open the door hiding the nice price. Next, host asks contestant if contestant wishes to keep door initially selected or switch to the one remaining closed door not initially selected by contestant. What should contestant do? Answer: contestant should ALWAYS switch doors. This is because contestant initially has 33% chance to win and 67% chance to lose. Switching doors increasing probability of winning to 67%.

这是R代码,我认为它是有效的.

Here is the R code, which I believe works.

library(aprof)
set.seed(1234)
foo <- function(N) {

     game.interations  <- 10000
     contestant.action <- rep(NA, game.interations)
     game.result       <- rep('lose', game.interations)

     for(i in 1:game.interations) {

          door <- c(0,0,0)
          door[sample(3, 1)] = 1            # assign nice prize to a door
                                            # door  with '1' has  nice prize
                                            # doors with '0' have bad  prize
          initial.pick <- sample(3, 1)      # initial contestant action
          not.picked   <- c(1:3)[-initial.pick]
          door.opened.by.host <- not.picked[1]
          if(door[initial.pick   ]==1) door.opened.by.host = not.picked[sample(2,1)]
          if(door[  not.picked[1]]==1) door.opened.by.host = not.picked[2]
          contestant.action[i] <- sample(c('k', 's'), 1)
          second.pick <- ifelse(contestant.action[i] == 'k', initial.pick, 
                         not.picked[which(not.picked!=door.opened.by.host)])
          if(door[second.pick]==1) game.result[i] = 'win'
     }

x <- table(contestant.action , game.result)         # examine probability of 
                                                    # winning by action 
prop.table(x)

}

foo(N)

#                      game.result
# contestant.action   lose    win
#          k (keep)   0.3293 0.1613
#          s (switch) 0.1705 0.3389

此处是aprof代码的开始位置.从现在开始,代码取自软件包文档.本节中的代码似乎也可以正常工作,并且可以识别功能foo的每一行所需要的时间.

Here is where the aprof code begins. From this point onward the code is taken from the package documentation. The code in this section also seems to work correctly and identifies time required by each line of the function foo.

## save function to a source file and reload
dump("foo",file="foo.R")
source("foo.R")

## create file to save profiler output
tmp<-tempfile()

## Profile the function
Rprof(tmp,line.profiling=TRUE)
foo(1e4)
Rprof(append=FALSE)

## Create a aprof object
fooaprof<-aprof("foo.R",tmp)

## display basic information, summarize and plot the object
fooaprof
summary(fooaprof)
plot(fooaprof)

# another plot
profileplot(fooaprof)

此处是内存配置文件代码的开始位置.返回错误的行如下所示.

Here is where the memory profiling code begins. The line that returns an error is identified below.

## To continue with memory profiling:
## enable memory.profiling=TRUE
Rprof(tmp,line.profiling=TRUE,memory.profiling=TRUE)
foo(1e4)
Rprof(append=FALSE)

#
# This line returns the error message below
#
## Create a aprof object
fooaprof<-aprof("foo.R",tmp)
#
# Error in `colnames<-`(`*tmp*`, value = c("sm_v_heap", "lrg_v_heap", "mem_in_node" : 
# 'names' attribute [3] must be the same length as the vector [0]
#

## display basic information, and plot memory usage
fooaprof
plot(fooaprof)

这是我认为aprof程序包在返回错误时尝试读取的文件内容,但我不确定.请注意,这是一个隐藏文件:

Here are the contents of the file that I think the aprof package is attempting to read when it returns the error, but I am not sure. Note that this is a hidden file:

memory profiling: line profiling: sample.interval=20000
#File 1: foo.R
:153316:554084:15881544:162:1#22 "foo" 
:150595:494927:15084104:869:1#26 "foo" 
:149818:473956:14839440:908:1#12 "sample" 1#12 "foo" 
:147827:430136:14250768:879:"sample" 1#16 "foo" 
:154551:576315:16254896:864:1#24 "foo" 
:151678:512463:15404032:896:"is.numeric" "sample" 1#12 "foo" 
:150598:488049:15083040:929:"length" "sample" 1#12 "foo" 
:146904:403852:13989752:857:"sample.int" "sample" 1#12 "foo" 
:146035:384446:13729968:919:"sample" 1#24 "foo" 
:156862:629525:16944760:955:"sample.int" "sample" 1#24 "foo" 
:154543:577567:16250584:905:1#24 "foo" 
:150690:595793:15020264:942:

推荐答案

这是由于错误(正则表达式语法不完整)引起的.感谢您的举报.该错误已在0.3.2版中修复.可从 github 获得.它将很快上传到CRAN.

This is due to a bug (incomplete regex syntax). Thanks for reporting this. The bug has been fixed in version 0.3.2. available from github. It will be uploaded to CRAN soon.

这篇关于使用aprof软件包分析Monty Hall代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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