我正在尝试构建一个Polya Urn模型。两种颜色很好,有三种颜色然而我遇到了麻烦。
<?php
if(mysqli_query($con,"DESCRIBE users ")){
echo "exists";
}else{
echo "dont exists";
}
mysqli_close($con);
exit;
?>
我的问题在于翻译这行代码:
ndraws<-1000; nexps<-2000; Distribution.yellow<-matrix(0,ndraws,1); for (k in 1:nexps){
red<- 1;
yellow<- 1;
blue<-1 ;
for (n in 1:ndraws){
drawn<-sample(0:2,size=1,prob=c(red,yellow,blue)/(red+yellow +blue))
red<-?? ;
blue<-?? ;
yellow<-?? ;
}
Distribution.yellow[k]<-yellow/(red+yellow+blue) }
添加到骨灰盒的各个额外球。 (因此问号)。
我用两种颜色做了如下:
drawn<-sample(0:2,size=1,prob=c(red,yellow,blue)/(red+yellow +blue))
但是当这两种颜色超过两种颜色时,这显然不起作用。我应该如何处理三种或更多颜色?
答案 0 :(得分:1)
根据Wikipedia,Pólya骨灰盒过程的规则是:
从瓮中随机抽取一个球并观察其颜色;然后将其返回到骨灰盒中,并将另一个相同颜色的球添加到骨灰盒中,并重复选择过程。
换句话说,画一个球会使该colo(u)r的球数增加一个。
所以我们可以设置一个if
语句,如果drawn==0
添加一个红球,drawn==1
增加一个黄球,否则添加一个蓝球......
ndraws <- 1000
nexps <- 500
set.seed(101)
yellow_final <- numeric(nexps)
for (k in 1:nexps) {
red <- 1; yellow <- 1; blue<-1
for (n in 1:ndraws) {
drawn <- sample(0:2,size=1,prob=c(red,yellow,blue)/(red+yellow+blue))
if (drawn==0) {
red <- red+1
} else if (drawn==1) {
yellow <- yellow+1
} else blue <- blue+1
}
yellow_final[k]<-yellow/(red+yellow+blue)
}
图片:
par(las=1,bty="l")
hist(yellow_final,col="gray",freq=FALSE,
xlab="Prop. yellow after 1000 draws")
答案 1 :(得分:0)
一般化解决方案:
ndraws<-1000
nexps<-2000
colors <- c('red', 'blue', 'yellow') # add balls with other colors
initial.num.balls <- c(1,1,1) # can have different numbers of balls to start with
ball.to.observe <- 'yellow'
distribution.ball.to.observe <- replicate(nexps, {
urn <- rep(colors, initial.num.balls) # polya's urn
count.balls <- as.list(initial.num.balls)
names(count.balls) <- colors
for (i in 1:ndraws) {
drawn <- sample(urn, 1)
count.balls[[drawn]] <- count.balls[[drawn]] + 1
urn <- c(urn, drawn)
}
count.balls[[ball.to.observe]] / sum(as.numeric(count.balls))
})
library(ggplot2)
ggplot() + stat_density(aes(distribution.ball.to.observe), bw=0.01)