# 直感的にはわかりやすいけど、非効率な例
bet.example<-function(n.trial,p.win,max.bet) {
rew.history=rep(0,n.trial)
bet.history=rep(0,n.trial)
WL.history=rep("W",n.trial)
loss.counter=0
i_trial=0;
WL="L"
loss.counter.reset="F"
while (i_trial < n.trial | WL=="L" ) {
i_trial=i_trial+1
bet=2^loss.counter
if (bet > max.bet) {
bet = max.bet
loss.counter.reset = "T"
}
bet.history[i_trial]=bet
WL=sample(c("W","L"),1,prob=c(p.win, 1-p.win))
WL.history[i_trial]=WL
if (WL=="L") {
loss.counter=loss.counter+1
reward=-bet
} else {
loss.counter=0
reward=bet
}
if (loss.counter.reset=="T"){loss.counter=0}
rew.history[i_trial]=reward
}
return(list(reward=sum(rew.history),money.required=max(bet.history)))
}
# より効率的な例 - max.betは未導入
bet.example02<-function(n.trial,p.win) {
WL=sample(c("W","L"),n.trial,replace=T,prob=c(p.win, 1-p.win))
if (WL[n.trial]!="W") {
WL.extra=sample(c("W","L"),1,prob=c(p.win,1-p.win))
while (tail(WL.extra,1)=="L") {
WL.extra=c(WL.extra,sample(c("W","L"),1,prob=c(p.win,1-p.win)))
}
WL=c(WL,WL.extra)
}
reward=sum(WL=="W")
result=rle(WL)
loss.index=result$values=="L"
money.required=2^max(result$lengths[loss.index])
return(list(reward,money.required))
}
Related