如何在r中为“a”编写模拟;“赌博”;问题和返回概率?
我试图在R中运行模拟,但我不确定从何处开始。问题如下: “您有100美元,在一场公平的比赛中下注10美元。在您下注100次时,您输掉所有钱的可能性有多大?” 到目前为止,我已经编写了一个小函数来从“投币”中生成随机结果,但这就是我所能得到的如何在r中为“a”编写模拟;“赌博”;问题和返回概率?,r,function,simulation,R,Function,Simulation,我试图在R中运行模拟,但我不确定从何处开始。问题如下: “您有100美元,在一场公平的比赛中下注10美元。在您下注100次时,您输掉所有钱的可能性有多大?” 到目前为止,我已经编写了一个小函数来从“投币”中生成随机结果,但这就是我所能得到的 win.lose <- function(x){ sample(0:1, x, rep=TRUE) } win.lose你可以像这样模拟100次下注 library(dplyr) current_balance <- 100 bet &l
win.lose <- function(x){
sample(0:1, x, rep=TRUE)
}
win.lose你可以像这样模拟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() }
}
库(dplyr)
当前的_余额既然你有100,你就用10,然后你只有90。这里的诀窍是,如果你赢了,你会得到什么。如果是双精度,则需要示例(c(-1,2),100,rep=TRUE)
win.lose我们可以编写一个函数来sample
-10(lost)和20(win)100次,并返回TRUE
如果在100次赌注中任何时候我们输掉了所有的钱(100$)
这是一个关于二项式分布的简单问题
- 首先:由于我们有兴趣计算“到您下注100次时”的概率,因此我们需要计算在第100次试验前我们可以取得的最大成功数,以便耗尽资金:
需要求解的方程是:
100+20*X+(99-X)*(-10)使用矢量化效果最好。我们应该一次对所有对象进行采样,而不是循环:
sample(c(-1, 1), 100, replace = TRUE)
我们也知道,如果我们净损失10,我们就会破产。该值转换为累计总和:
cumsum(sample(c(-1, 1), 100, replace = TRUE))
any(cumsum(sample(c(-1, 1), 100, 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, 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, 1), n, replace = TRUE))))
#or a summary of it:
set.seed(123)
table(replicate(n_sim, ifelse(any(cumsum(sample(c(-1, 1), n, replace = TRUE)) == -n_broke), 'Out_of_Money', 'Has_Money')))
#faster way to do it:
set.seed(123)
table(
ifelse(
apply(matrix(sample(c(-1,1), n * n_sim, replace = TRUE), ncol = n_sim),
2,
function(x) min(cumsum(x)) <= -n_broke),
'Out_of_Money', 'Has_Money')
)
以及幕后发生的事情:
set.seed(123)
replicate(n_sim, cumsum(c(n_broke * bet, bet * sample(c(-1, 1), n, 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
但是模拟要有趣得多+我来解释一下。这太神奇了!非常感谢你的帮助!
sample(c(-1, 1), 100, replace = TRUE)
cumsum(sample(c(-1, 1), 100, replace = TRUE))
any(cumsum(sample(c(-1, 1), 100, replace = TRUE)) == -10)
#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, 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, 1), n, replace = TRUE))))
#or a summary of it:
set.seed(123)
table(replicate(n_sim, ifelse(any(cumsum(sample(c(-1, 1), n, replace = TRUE)) == -n_broke), 'Out_of_Money', 'Has_Money')))
#faster way to do it:
set.seed(123)
table(
ifelse(
apply(matrix(sample(c(-1,1), n * n_sim, replace = TRUE), ncol = n_sim),
2,
function(x) min(cumsum(x)) <= -n_broke),
'Out_of_Money', 'Has_Money')
)
Has_Money Out_of_Money
6783 3217
set.seed(123)
replicate(n_sim, cumsum(c(n_broke * bet, bet * sample(c(-1, 1), n, 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