- #林共进教授一次讲座提到他喜欢出一道题,题目是让学生交出抛100次硬币的书面记录
- #他的目的是检验那些学生没有认真抛硬币
- #他使用的统计量是最长连续出现正面反面的长度
- #这个统计量没有解析解,他说作为一道编程模拟习题布置给大家
- #今天我突然想起,就把他做出来
- #目的是求出抛100次硬币的统计量是几
- #我们的答案中位数6, max=24,min=3
- #所以抛硬币赌博很容易出现连续的正面或反面
- #这与直觉是相违背的
- CountAndCut = function(x){
- # this funciton want to serch the
- # initial x=1 and cut the vector x
- # then we can solve this problem using recurse method
- # note at least k=1
- # so my function is wright
-
- k = 1
- N = length(x)
- for(i in 1:(N-1)){
- if((x[i] == x[i+1])&(x[i]==1))
- k =k+1
- else
- break
- }
- z1 = k
- z2 = x[(i+1):N]
- return(list(z1,z2))
- }
- SearchContinuousHead = function(x){
- # input head=0 or tail=1 represent 0&1
- # lsit [[1]] is the longest x=1
- num = length(x)
- y1 = CountAndCut(x)[[1]]
- y2 = CountAndCut(x)[[2]]
- num=length(y2)
-
- while(num>=2){
- y1 = max(y1,CountAndCut(y2)[[1]])
- y2 = CountAndCut(y2)[[2]]
- num = length(y2)
- }
- return(list(y1,y2))
-
- }
- # so we want to check the Statistic the longest x=1 with
- # num =100 ,so I will run a loop=1000
- # to check the value of this Statistic
- # as the head and the tail is the same
- # so I did't need to count k=0
- num=100
- Count = rep(NA,1000)
- for(i in 1:1000){
- x = rbinom(n=num, size=1, prob=0.5)
- Count[i]=SearchContinuousHead(x)[[1]]
- }
- median(Count)
- max(Count)
- min(Count)
- # median(Count)
- # [1] 6
- # max(Count)
- # [1] 24 ####非常惊人的数字
- # min(Count)
- # [1] 3
- # now we make a conclusion that the Statistic=6
- # so if you want to cheating your teacher
- # you need to creat a continuous tail or head near to 6


雷达卡




京公网安备 11010802022788号







