如何在r中针对“赌博”问题和返还概率编写模拟?

我试图在R中运行仿真,但我不确定从哪里开始。问题如下:

“您有$ 100,并在一场公平游戏中下$ 10的赌注。到您下第100个赌注时,您将输掉所有钱的可能性是多少?”

到目前为止,我已经编写了一个小函数来从“硬币翻转”中生成随机结果,但是据我所知。

win.lose <- function(x){
  sample(0:1,x,rep=TRUE)
}

我担心的是,此功能不考虑从奖金中赚钱。在这里为上面的问题编写更好的功能的一些帮助将不胜感激。谢谢!

zhouyoulie04 回答:如何在r中针对“赌博”问题和返还概率编写模拟?

这是binomial发行版中的一个简单问题。

  • 第一:由于我们有兴趣计算概率“ 到您下注第100个赌注”,因此我们需要计算最大可成功数在第100次试用之前,为了用光钱: 需要解决的方程是:100 + 20*X + (99 - X)*(-10) <= 0,其中X是在99次试验中必须完成的成功数量。这只会导致36成功。
  • 第二:由于我们已经知道我们需要36下注中的99成功,因此我们可以使用pbinom函数来计算概率如果我们赌博了99次,则获得36或更少的成功

    pbinom(36,99,0.5)

这导致0.004316793的可能性。

这是公平游戏的通用框架,可以用于任何数量的初始财富,下注资金和终止审判。

NB:顺便说一下,与任何模拟方法相比,它可以计算出确切的概率!

,

使用矢量化效果最佳。而不是循环,我们应该一次采样所有内容:

sample(c(-1,1),100,replace = TRUE)

我们还知道,如果我们净亏损10,我们将被淘汰。转换为累计金额:

cumsum(sample(c(-1,replace = TRUE))

any(cumsum(sample(c(-1,replace = TRUE)) == -10)

最后,我们可以使用replicate()来重复这个完全相同的模拟:

#specify simulation criteria
n <- 100 
n_sim <- 10

# betting criteria
n_broke <- 10 #if we have 10 net losses,we're broke
bet <- 10 #each bet is $10

# way 1
set.seed(123)
replicate(n_sim,cumsum(sample(c(-1,n,replace = TRUE))) 

#or with actual money totals - note,1st row is the initial money amount
set.seed(123)
replicate(n_sim,cumsum(c(n_broke * bet,bet * sample(c(-1,replace = TRUE))))

#or a summary of it:
set.seed(123)
table(replicate(n_sim,ifelse(any(cumsum(sample(c(-1,replace = TRUE)) == -n_broke),'Out_of_Money','Has_Money')))

#faster way to do it:
set.seed(123)
table(
  ifelse(
    apply(matrix(sample(c(-1,n * n_sim,replace = TRUE),ncol = n_sim),2,function(x) min(cumsum(x)) <= -n_broke),'Has_Money')
)

对于n_sim = 10,000:

   Has_Money Out_of_Money 
        6783         3217 

幕后发生的事情:

set.seed(123)
replicate(n_sim,replace = TRUE))))
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
  [1,]  100  100  100  100  100  100  100  100  100   100
  [2,]   90   90  110  110   90  110  110  110   90   110
  [3,]   80  100  120  120   80  120  120  100   80   120
  [4,]   70  110  130  110   70  130  130   90   90   110
  [5,]   80  100  120  120   60  140  120   80  100   100
  [6,]   70  110  130  110   50  130  110   90   90   110
  [7,]   80  120  140  100   40  120  100   80   80   120
  [8,]   90  110  150  110   50  110   90   70   70   130
  [9,]  100  100  140  100   40  100  100   60   60   120
,

您可以像这样模拟100个投注。

library(dplyr)

current_balance <- 100
bet <- 10
odds <- 0.5

for(i in 1:100) {

  current_balance <- current_balance - bet # place bet

  outcome <- ifelse(runif(1) > 0.5,bet * 2,# win: receive twice the bet ($20)
                    0) # lose and the initial $10 is lost

  current_balance <- current_balance + outcome

  paste("Balance after",i,"bets is:",current_balance) %>% print

  if(current_balance <= 0) { stop() }
}

您可以将整个程序包装在另一个循环中,以多次运行模拟并记录结果,就像这样

ending_balances <- c()

for(s in 1:10) {

current_balance <- 100
bet <- 10
odds <- 0.5

  for(i in 1:100) {

    current_balance <- current_balance - bet # place bet

    outcome <- ifelse(runif(1) > 0.5,# win: receive twice the bet ($20)
                      0) # lose and the initial $10 is lost

    current_balance <- current_balance + outcome

    # paste("Balance after",current_balance) %>% print

    if(current_balance <= 0) { 

      ending_balances[s] <- current_balance
      break() 
      }

    ending_balances[s] <- current_balance

  }

}



> ending_balances
 [1]   0  80 220  60   0 120   0  80   0 200
,

我们可以写一个函数says(Animal,Today到-10(输)-10(赢)和20(赢)100次,如果在任何100投注中的任何时间返回sample,我们都会输光所有钱(100 $)。

TRUE

我们可以使用lost.balance <- function() { total <- cumsum(sample(c(-10,20),replace = TRUE)) any(total <= -100) } replicate次进行模拟,并使用n

计算比率
table
,

因为您有100,所以您使用10,那么您只有90。这里的窍门是,如果您获胜,您将获得什么。如果加倍,则需要IEnumerable<T>

IEnumerable<T>
本文链接:https://www.f2er.com/3130810.html

大家都在问